-
Notifications
You must be signed in to change notification settings - Fork 47.2k
/
canvas.js
221 lines (187 loc) · 5.42 KB
/
canvas.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import type {Data} from './index';
import type {Rect} from '../utils';
import type {HostInstance} from '../../types';
import type Agent from '../../agent';
import {isReactNativeEnvironment} from 'react-devtools-shared/src/backend/utils';
// Note these colors are in sync with DevTools Profiler chart colors.
const COLORS = [
'#37afa9',
'#63b19e',
'#80b393',
'#97b488',
'#abb67d',
'#beb771',
'#cfb965',
'#dfba57',
'#efbb49',
'#febc38',
];
let canvas: HTMLCanvasElement | null = null;
function drawNative(nodeToData: Map<HostInstance, Data>, agent: Agent) {
const nodesToDraw = [];
iterateNodes(nodeToData, ({color, node}) => {
nodesToDraw.push({node, color});
});
agent.emit('drawTraceUpdates', nodesToDraw);
const mergedNodes = groupAndSortNodes(nodeToData);
agent.emit('drawGroupedTraceUpdatesWithNames', mergedNodes);
}
function drawWeb(nodeToData: Map<HostInstance, Data>) {
if (canvas === null) {
initialize();
}
const dpr = window.devicePixelRatio || 1;
const canvasFlow: HTMLCanvasElement = ((canvas: any): HTMLCanvasElement);
canvasFlow.width = window.innerWidth * dpr;
canvasFlow.height = window.innerHeight * dpr;
canvasFlow.style.width = `${window.innerWidth}px`;
canvasFlow.style.height = `${window.innerHeight}px`;
const context = canvasFlow.getContext('2d');
context.scale(dpr, dpr);
context.clearRect(0, 0, canvasFlow.width / dpr, canvasFlow.height / dpr);
const mergedNodes = groupAndSortNodes(nodeToData);
mergedNodes.forEach(group => {
drawGroupBorders(context, group);
drawGroupLabel(context, group);
});
}
type GroupItem = {
rect: Rect,
color: string,
displayName: string | null,
count: number,
};
export type {GroupItem};
export function groupAndSortNodes(
nodeToData: Map<HostInstance, Data>,
): Array<Array<GroupItem>> {
const positionGroups: Map<string, Array<GroupItem>> = new Map();
iterateNodes(nodeToData, ({rect, color, displayName, count}) => {
if (!rect) return;
const key = `${rect.left},${rect.top}`;
if (!positionGroups.has(key)) positionGroups.set(key, []);
positionGroups.get(key)?.push({rect, color, displayName, count});
});
return Array.from(positionGroups.values()).sort((groupA, groupB) => {
const maxCountA = Math.max(...groupA.map(item => item.count));
const maxCountB = Math.max(...groupB.map(item => item.count));
return maxCountA - maxCountB;
});
}
function drawGroupBorders(
context: CanvasRenderingContext2D,
group: Array<GroupItem>,
) {
group.forEach(({color, rect}) => {
context.beginPath();
context.strokeStyle = color;
context.rect(rect.left, rect.top, rect.width - 1, rect.height - 1);
context.stroke();
});
}
function drawGroupLabel(
context: CanvasRenderingContext2D,
group: Array<GroupItem>,
) {
const mergedName = group
.map(({displayName, count}) =>
displayName ? `${displayName}${count > 1 ? ` x${count}` : ''}` : '',
)
.filter(Boolean)
.join(', ');
if (mergedName) {
drawLabel(context, group[0].rect, mergedName, group[0].color);
}
}
export function draw(nodeToData: Map<HostInstance, Data>, agent: Agent): void {
return isReactNativeEnvironment()
? drawNative(nodeToData, agent)
: drawWeb(nodeToData);
}
type DataWithColorAndNode = {
...Data,
color: string,
node: HostInstance,
};
function iterateNodes(
nodeToData: Map<HostInstance, Data>,
execute: (data: DataWithColorAndNode) => void,
) {
nodeToData.forEach((data, node) => {
const colorIndex = Math.min(COLORS.length - 1, data.count - 1);
const color = COLORS[colorIndex];
execute({
color,
node,
count: data.count,
displayName: data.displayName,
expirationTime: data.expirationTime,
lastMeasuredAt: data.lastMeasuredAt,
rect: data.rect,
});
});
}
function drawLabel(
context: CanvasRenderingContext2D,
rect: Rect,
text: string,
color: string,
): void {
const {left, top} = rect;
context.font = '10px monospace';
context.textBaseline = 'middle';
context.textAlign = 'center';
const padding = 2;
const textHeight = 14;
const metrics = context.measureText(text);
const backgroundWidth = metrics.width + padding * 2;
const backgroundHeight = textHeight;
const labelX = left;
const labelY = top - backgroundHeight;
context.fillStyle = color;
context.fillRect(labelX, labelY, backgroundWidth, backgroundHeight);
context.fillStyle = '#000000';
context.fillText(
text,
labelX + backgroundWidth / 2,
labelY + backgroundHeight / 2,
);
}
function destroyNative(agent: Agent) {
agent.emit('disableTraceUpdates');
}
function destroyWeb() {
if (canvas !== null) {
if (canvas.parentNode != null) {
canvas.parentNode.removeChild(canvas);
}
canvas = null;
}
}
export function destroy(agent: Agent): void {
return isReactNativeEnvironment() ? destroyNative(agent) : destroyWeb();
}
function initialize(): void {
canvas = window.document.createElement('canvas');
canvas.style.cssText = `
xx-background-color: red;
xx-opacity: 0.5;
bottom: 0;
left: 0;
pointer-events: none;
position: fixed;
right: 0;
top: 0;
z-index: 1000000000;
`;
const root = window.document.documentElement;
root.insertBefore(canvas, root.firstChild);
}