• 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, TimerShaftElement} from "../TimerShaftElement.js";
19
20export class TimeRuler extends Graph {
21    totalNS: number
22    private stepSmall: number;
23    private step: number;
24    private stepNS: number;
25
26    constructor(timerShaftEL: TimerShaftElement, frame: Rect, totalNS: number = 10_000_000_000) {
27        super(timerShaftEL.canvas, timerShaftEL.ctx!, frame)
28        this.totalNS = totalNS;
29        this.step = this.frame.width / 10;
30        this.stepSmall = this.frame.width / 100;
31        this.stepNS = this.totalNS / 10;
32    }
33
34    draw() {
35        this.step = this.frame.width / 10;
36        this.stepSmall = this.frame.width / 100;
37        this.stepNS = this.totalNS / 10;
38        this.c.clearRect(this.frame.x, this.frame.y, this.frame.width, this.frame.height)
39        this.c.beginPath();
40        this.c.strokeStyle = "#999"
41        this.c.lineWidth = 1;
42        for (let i = 0; i <= 10; i++) {
43            let x = Math.floor(i * this.step) + this.frame.x;
44            this.c.moveTo(x, 0);
45            this.c.lineTo(x, this.frame.height);
46            if (i == 10) break;
47            for (let j = 1; j < 10; j++) {
48                this.c.moveTo(x + Math.floor(j * this.stepSmall), 0);
49                this.c.lineTo(x + Math.floor(j * this.stepSmall), this.frame.height / 4);
50            }
51            this.c.fillStyle = '#999'
52            this.c.fillText(`${ns2s(i * this.stepNS)}`, x + 5, this.frame.height - 1)
53        }
54        this.c.stroke();
55        this.c.closePath();
56    }
57}
58
59
60
61