• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import {Graph} from "./Graph.js";
17import {Rect} from "./Rect.js";
18import {ns2s} from "../TimerShaftElement.js";
19
20export class TimeRuler extends Graph {
21    totalNS: number
22
23    private stepSmall: number;
24    private step: number;
25    private stepNS: number;
26
27    constructor(canvas: HTMLCanvasElement | undefined | null, c: CanvasRenderingContext2D, frame: Rect, totalNS: number = 10_000_000_000) {
28        super(canvas, c, frame)
29        this.totalNS = totalNS;
30        this.step = this.frame.width / 10;
31        this.stepSmall = this.frame.width / 100;
32        this.stepNS = this.totalNS / 10;
33    }
34
35    draw() {
36        this.step = this.frame.width / 10;
37        this.stepSmall = this.frame.width / 100;
38        this.stepNS = this.totalNS / 10;
39        this.c.clearRect(this.frame.x, this.frame.y, this.frame.width, this.frame.height)
40        this.c.beginPath();
41        this.c.strokeStyle = "#999"
42        this.c.lineWidth = 1;
43        for (let i = 0; i <= 10; i++) {
44            let x = Math.floor(i * this.step) + this.frame.x;
45            this.c.moveTo(x, 0);
46            this.c.lineTo(x, this.frame.height);
47            if (i == 10) break;
48            for (let j = 1; j < 10; j++) {
49                this.c.moveTo(x + Math.floor(j * this.stepSmall), 0);
50                this.c.lineTo(x + Math.floor(j * this.stepSmall), this.frame.height / 4);
51            }
52            this.c.fillStyle = '#999'
53            this.c.fillText(`${ns2s(i * this.stepNS)}`, x + 5, this.frame.height - 1)
54        }
55        this.c.stroke();
56        this.c.closePath();
57    }
58}
59