Skip to content

Commit e40250f

Browse files
committed
simplify rendering
1 parent cc3cd8c commit e40250f

File tree

4 files changed

+91
-134
lines changed

4 files changed

+91
-134
lines changed

packages/vscode-js-profile-flame/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@
4141
{
4242
"type": "webview",
4343
"id": "vscode-js-profile-flame.realtime",
44-
"name": "Realtime Performance"
44+
"name": "Realtime Performance",
45+
"when": "debugConfigurationType =~ /^pwa-/"
4546
}
4647
]
4748
},

packages/vscode-js-profile-flame/src/realtime/chart.ts

+2-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import { Metric } from './baseMetric';
77
import styles from './chart.css';
88
import { Configurator } from './configurator';
99
import { FrameCanvas, Sizing } from './frameCanvas';
10-
import { GraphCanvas } from './graphCanvas';
1110
import { Settings } from './settings';
1211

1312
const naturalAspectRatio = 16 / 9;
@@ -23,13 +22,8 @@ export class Chart {
2322
private configHadManualToggle = false;
2423

2524
private readonly frameCanvas = new FrameCanvas(this.width, this.height, this.settings);
26-
private readonly graphCanvas = new GraphCanvas(this.width, this.height, this.settings);
2725
private readonly elements = this.createElements();
28-
private readonly settingListener = this.settings.onChange(() => {
29-
this.applySettings();
30-
this.graphCanvas.updateMetrics();
31-
this.frameCanvas.updateMetrics(this.graphCanvas, false);
32-
});
26+
private readonly settingListener = this.settings.onChange(() => this.applySettings());
3327
private readonly configurator = new Configurator(this.settings);
3428

3529
public get elem() {
@@ -44,7 +38,6 @@ export class Chart {
4438
public dispose() {
4539
this.settingListener();
4640
this.frameCanvas.dispose();
47-
this.graphCanvas.dispose();
4841
}
4942

5043
/**
@@ -77,16 +70,13 @@ export class Chart {
7770
}
7871

7972
this.frameCanvas.updateSize(graphWidth, graphHeight);
80-
this.graphCanvas.updateSize(graphWidth, graphHeight);
81-
this.frameCanvas.updateMetrics(this.graphCanvas, false);
8273
}
8374

8475
/**
8576
* Update the chart metrics
8677
*/
8778
public updateMetrics() {
88-
this.graphCanvas.updateMetrics();
89-
this.frameCanvas.updateMetrics(this.graphCanvas);
79+
this.frameCanvas.updateMetrics();
9080

9181
if (this.configOpen) {
9282
this.configurator.updateMetrics();

packages/vscode-js-profile-flame/src/realtime/frameCanvas.ts

+87-20
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,48 @@
22
* Copyright (C) Microsoft Corporation. All rights reserved.
33
*--------------------------------------------------------*/
44

5+
import { Metric } from './baseMetric';
56
import { Canvas } from './canvas';
6-
import { GraphCanvas } from './graphCanvas';
77

88
export const enum Sizing {
99
LabelHeight = 18,
1010
RulerWidth = 1,
1111
Easing = 200,
1212
}
1313

14+
const rulers = 4;
15+
1416
export class FrameCanvas extends Canvas {
17+
private rulers = this.createRulers();
18+
private paths: [Path2D, string][] = [];
1519
private ease?: { raf: number; dx: number };
1620
private rmSettingListener = this.settings.onChange(() => this.redraw());
1721

1822
/**
1923
* Redraws the max values
2024
*/
21-
public updateMetrics(graphCanvas: GraphCanvas, shouldEase = this.settings.value.easing) {
22-
const { width: w, height: h } = this;
23-
24-
let easeLength = graphCanvas.stepWidth;
25+
public updateMetrics(shouldEase = this.settings.value.easing) {
26+
let easeLength = this.width / this.settings.steps;
2527
if (this.ease) {
2628
cancelAnimationFrame(this.ease.raf);
2729
easeLength += this.ease.dx;
2830
}
2931

32+
this.paths = this.settings.enabledMetrics.map(metric => [
33+
this.createMetricPath(metric),
34+
this.settings.metricColor(metric),
35+
]);
36+
3037
if (!shouldEase) {
31-
this.drawGraph(graphCanvas, w, h);
38+
this.drawGraph();
3239
return;
3340
}
3441

3542
let start: number;
3643
const draw = (now: number) => {
3744
const progress = Math.min(1, (now - start) / Sizing.Easing);
3845
const dx = easeLength * (1 - progress);
39-
this.drawGraph(graphCanvas, w, h, dx);
46+
this.drawGraph(dx);
4047

4148
if (progress === 1) {
4249
this.ease = undefined;
@@ -65,18 +72,78 @@ export class FrameCanvas extends Canvas {
6572
}
6673
}
6774

68-
protected drawGraph(graphCanvas: GraphCanvas, w: number, h: number, dx = 0) {
69-
this.ctx.clearRect(0, 0, w, h);
70-
this.ctx.drawImage(
71-
graphCanvas.ctx.canvas,
72-
graphCanvas.width - w - dx,
73-
0,
74-
w * this.scale,
75-
h * this.scale,
76-
0,
77-
0,
78-
w,
79-
h,
80-
);
75+
protected redraw() {
76+
this.rulers = this.createRulers();
77+
this.updateMetrics(false);
78+
}
79+
80+
protected drawGraph(dx = 0) {
81+
this.ctx.clearRect(0, 0, this.width, this.height);
82+
83+
// draw the background rulers first
84+
this.ctx.globalAlpha = 1;
85+
this.ctx.strokeStyle = this.settings.colors.border;
86+
this.ctx.stroke(this.rulers);
87+
88+
this.ctx.save();
89+
this.ctx.translate(dx, 0);
90+
91+
// then the chart fill first (so lines will always be in the foreground)
92+
this.ctx.globalAlpha = 0.1;
93+
for (const [path, color] of this.paths) {
94+
this.ctx.fillStyle = color;
95+
this.ctx.fill(path);
96+
}
97+
98+
// then stroke the lines
99+
this.ctx.globalAlpha = 1;
100+
for (const [path, color] of this.paths) {
101+
this.ctx.strokeStyle = color;
102+
this.ctx.stroke(path);
103+
}
104+
105+
this.ctx.restore();
106+
}
107+
108+
private createRulers() {
109+
const path = new Path2D();
110+
const { width, height } = this;
111+
const step = (height - Sizing.RulerWidth) / rulers;
112+
113+
let y = step;
114+
for (let i = 0; i < rulers; i++) {
115+
const targetY = Math.floor(y) + Sizing.RulerWidth / 2;
116+
path.moveTo(0, targetY);
117+
path.lineTo(width, targetY);
118+
y += step;
119+
}
120+
121+
return path;
122+
}
123+
124+
private createMetricPath({ max, metrics }: Metric) {
125+
const { width, height } = this;
126+
const stepSize = width / this.settings.steps;
127+
const path = new Path2D();
128+
129+
if (metrics.length === 0) {
130+
path.moveTo(0, height - 1);
131+
path.lineTo(width, height - 1);
132+
return path;
133+
}
134+
135+
let x = width;
136+
path.moveTo(x, height * (1 - metrics[metrics.length - 1] / max));
137+
138+
for (let i = metrics.length - 2; i >= 0; i--) {
139+
x -= stepSize;
140+
path.lineTo(x, height * (1 - metrics[i] / max));
141+
}
142+
143+
path.lineTo(x - stepSize, height - 1);
144+
path.lineTo(-stepSize, height - 1);
145+
path.lineTo(-stepSize, height + 2);
146+
path.lineTo(width, height + 2);
147+
return path;
81148
}
82149
}

packages/vscode-js-profile-flame/src/realtime/graphCanvas.ts

-101
This file was deleted.

0 commit comments

Comments
 (0)