-
Notifications
You must be signed in to change notification settings - Fork 45.6k
/
SnapshotsView.js
222 lines (186 loc) · 5.39 KB
/
SnapshotsView.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
222
/**
* Copyright (c) Facebook, Inc. and its 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 {Snapshot, ReactProfilerData} from '../types';
import type {
Interaction,
Point,
Rect,
Size,
Surface,
ViewRefs,
} from '../view-base';
import {positioningScaleFactor, timestampToPosition} from './utils/positioning';
import {
intersectionOfRects,
rectContainsPoint,
rectEqualToRect,
View,
} from '../view-base';
import {BORDER_SIZE, COLORS, SNAPSHOT_HEIGHT} from './constants';
type OnHover = (node: Snapshot | null) => void;
export class SnapshotsView extends View {
_intrinsicSize: Size;
_profilerData: ReactProfilerData;
onHover: OnHover | null = null;
constructor(surface: Surface, frame: Rect, profilerData: ReactProfilerData) {
super(surface, frame);
this._intrinsicSize = {
width: profilerData.duration,
height: SNAPSHOT_HEIGHT,
};
this._profilerData = profilerData;
}
desiredSize() {
return this._intrinsicSize;
}
draw(context: CanvasRenderingContext2D) {
const {visibleArea} = this;
context.fillStyle = COLORS.BACKGROUND;
context.fillRect(
visibleArea.origin.x,
visibleArea.origin.y,
visibleArea.size.width,
visibleArea.size.height,
);
const y = visibleArea.origin.y;
let x = visibleArea.origin.x;
// Rather than drawing each snapshot where it occurred,
// draw them at fixed intervals and just show the nearest one.
while (x < visibleArea.origin.x + visibleArea.size.width) {
const snapshot = this._findClosestSnapshot(x);
if (snapshot === null) {
// This shold never happen.
break;
}
const scaledHeight = SNAPSHOT_HEIGHT;
const scaledWidth = (snapshot.width * SNAPSHOT_HEIGHT) / snapshot.height;
const imageRect: Rect = {
origin: {
x,
y,
},
size: {width: scaledWidth, height: scaledHeight},
};
// Lazily create and cache Image objects as we render a snapsho for the first time.
if (snapshot.image === null) {
const img = (snapshot.image = new Image());
img.onload = () => {
this._drawSnapshotImage(context, snapshot, imageRect);
};
img.src = snapshot.imageSource;
} else {
this._drawSnapshotImage(context, snapshot, imageRect);
}
x += scaledWidth + BORDER_SIZE;
}
}
handleInteraction(interaction: Interaction, viewRefs: ViewRefs) {
switch (interaction.type) {
case 'mousemove':
case 'wheel-control':
case 'wheel-meta':
case 'wheel-plain':
case 'wheel-shift':
this._updateHover(interaction.payload.location, viewRefs);
break;
}
}
_drawSnapshotImage(
context: CanvasRenderingContext2D,
snapshot: Snapshot,
imageRect: Rect,
) {
const visibleArea = this.visibleArea;
// Prevent snapshot from visibly overflowing its container when clipped.
// View clips by default, but since this view may draw async (on Image load) we re-clip.
const shouldClip = !rectEqualToRect(imageRect, visibleArea);
if (shouldClip) {
const clippedRect = intersectionOfRects(imageRect, visibleArea);
context.save();
context.beginPath();
context.rect(
clippedRect.origin.x,
clippedRect.origin.y,
clippedRect.size.width,
clippedRect.size.height,
);
context.closePath();
context.clip();
}
context.fillStyle = COLORS.REACT_RESIZE_BAR_BORDER;
context.fillRect(
imageRect.origin.x,
imageRect.origin.y,
imageRect.size.width,
imageRect.size.height,
);
// $FlowFixMe Flow doesn't know about the 9 argument variant of drawImage()
context.drawImage(
snapshot.image,
// Image coordinates
0,
0,
// Native image size
snapshot.width,
snapshot.height,
// Canvas coordinates
imageRect.origin.x + BORDER_SIZE,
imageRect.origin.y + BORDER_SIZE,
// Scaled image size
imageRect.size.width - BORDER_SIZE * 2,
imageRect.size.height - BORDER_SIZE * 2,
);
if (shouldClip) {
context.restore();
}
}
_findClosestSnapshot(x: number): Snapshot | null {
const frame = this.frame;
const scaleFactor = positioningScaleFactor(
this._intrinsicSize.width,
frame,
);
const snapshots = this._profilerData.snapshots;
let startIndex = 0;
let stopIndex = snapshots.length - 1;
while (startIndex <= stopIndex) {
const currentIndex = Math.floor((startIndex + stopIndex) / 2);
const snapshot = snapshots[currentIndex];
const {timestamp} = snapshot;
const snapshotX = Math.floor(
timestampToPosition(timestamp, scaleFactor, frame),
);
if (x < snapshotX) {
stopIndex = currentIndex - 1;
} else {
startIndex = currentIndex + 1;
}
}
return snapshots[stopIndex] || null;
}
/**
* @private
*/
_updateHover(location: Point, viewRefs: ViewRefs) {
const {onHover, visibleArea} = this;
if (!onHover) {
return;
}
if (!rectContainsPoint(location, visibleArea)) {
onHover(null);
return;
}
const snapshot = this._findClosestSnapshot(location.x);
if (snapshot !== null) {
onHover(snapshot);
} else {
onHover(null);
}
}
}