-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
element-screenshot-renderer.js
287 lines (252 loc) · 10.6 KB
/
element-screenshot-renderer.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/**
* @license
* Copyright 2020 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
/**
* @fileoverview These functions define {Rect}s and {Size}s using two different coordinate spaces:
* 1. Screenshot coords (SC suffix): where 0,0 is the top left of the screenshot image
* 2. Display coords (DC suffix): that match the CSS pixel coordinate space of the LH report's page.
*/
import {Globals} from './report-globals.js';
/** @typedef {import('./dom.js').DOM} DOM */
/** @typedef {LH.Audit.Details.Rect} Rect */
/** @typedef {{width: number, height: number}} Size */
/**
* @typedef InstallOverlayFeatureParams
* @property {DOM} dom
* @property {Element} rootEl
* @property {Element} overlayContainerEl
* @property {LH.Result.FullPageScreenshot} fullPageScreenshot
*/
/**
* @param {LH.Result.FullPageScreenshot['screenshot']} screenshot
* @param {LH.Audit.Details.Rect} rect
* @return {boolean}
*/
function screenshotOverlapsRect(screenshot, rect) {
return rect.left <= screenshot.width &&
0 <= rect.right &&
rect.top <= screenshot.height &&
0 <= rect.bottom;
}
/**
* @param {number} value
* @param {number} min
* @param {number} max
*/
function clamp(value, min, max) {
if (value < min) return min;
if (value > max) return max;
return value;
}
/**
* @param {Rect} rect
*/
function getElementRectCenterPoint(rect) {
return {
x: rect.left + rect.width / 2,
y: rect.top + rect.height / 2,
};
}
export class ElementScreenshotRenderer {
/**
* Given the location of an element and the sizes of the preview and screenshot,
* compute the absolute positions (in screenshot coordinate scale) of the screenshot content
* and the highlighted rect around the element.
* @param {Rect} elementRectSC
* @param {Size} elementPreviewSizeSC
* @param {Size} screenshotSize
*/
static getScreenshotPositions(elementRectSC, elementPreviewSizeSC, screenshotSize) {
const elementRectCenter = getElementRectCenterPoint(elementRectSC);
// Try to center clipped region.
const screenshotLeftVisibleEdge = clamp(
elementRectCenter.x - elementPreviewSizeSC.width / 2,
0, screenshotSize.width - elementPreviewSizeSC.width
);
const screenshotTopVisisbleEdge = clamp(
elementRectCenter.y - elementPreviewSizeSC.height / 2,
0, screenshotSize.height - elementPreviewSizeSC.height
);
return {
screenshot: {
left: screenshotLeftVisibleEdge,
top: screenshotTopVisisbleEdge,
},
clip: {
left: elementRectSC.left - screenshotLeftVisibleEdge,
top: elementRectSC.top - screenshotTopVisisbleEdge,
},
};
}
/**
* Render a clipPath SVG element to assist marking the element's rect.
* The elementRect and previewSize are in screenshot coordinate scale.
* @param {DOM} dom
* @param {HTMLElement} maskEl
* @param {{left: number, top: number}} positionClip
* @param {Rect} elementRect
* @param {Size} elementPreviewSize
*/
static renderClipPathInScreenshot(dom, maskEl, positionClip, elementRect, elementPreviewSize) {
const clipPathEl = dom.find('clipPath', maskEl);
const clipId = `clip-${Globals.getUniqueSuffix()}`;
clipPathEl.id = clipId;
maskEl.style.clipPath = `url(#${clipId})`;
// Normalize values between 0-1.
const top = positionClip.top / elementPreviewSize.height;
const bottom = top + elementRect.height / elementPreviewSize.height;
const left = positionClip.left / elementPreviewSize.width;
const right = left + elementRect.width / elementPreviewSize.width;
const polygonsPoints = [
`0,0 1,0 1,${top} 0,${top}`,
`0,${bottom} 1,${bottom} 1,1 0,1`,
`0,${top} ${left},${top} ${left},${bottom} 0,${bottom}`,
`${right},${top} 1,${top} 1,${bottom} ${right},${bottom}`,
];
for (const points of polygonsPoints) {
const pointEl = dom.createElementNS('http://www.w3.org/2000/svg', 'polygon');
pointEl.setAttribute('points', points);
clipPathEl.append(pointEl);
}
}
/**
* Called by report renderer. Defines a css variable used by any element screenshots
* in the provided report element.
* Allows for multiple Lighthouse reports to be rendered on the page, each with their
* own full page screenshot.
* @param {HTMLElement} el
* @param {LH.Result.FullPageScreenshot['screenshot']} screenshot
*/
static installFullPageScreenshot(el, screenshot) {
el.style.setProperty('--element-screenshot-url', `url('${screenshot.data}')`);
}
/**
* Installs the lightbox elements and wires up click listeners to all .lh-element-screenshot elements.
* @param {InstallOverlayFeatureParams} opts
*/
static installOverlayFeature(opts) {
const {dom, rootEl, overlayContainerEl, fullPageScreenshot} = opts;
const screenshotOverlayClass = 'lh-screenshot-overlay--enabled';
// Don't install the feature more than once.
if (rootEl.classList.contains(screenshotOverlayClass)) return;
rootEl.classList.add(screenshotOverlayClass);
// Add a single listener to the provided element to handle all clicks within (event delegation).
rootEl.addEventListener('click', e => {
const target = /** @type {?HTMLElement} */ (e.target);
if (!target) return;
// Only activate the overlay for clicks on the screenshot *preview* of an element, not the full-size too.
const el = /** @type {?HTMLElement} */ (target.closest('.lh-node > .lh-element-screenshot'));
if (!el) return;
const overlay = dom.createElement('div', 'lh-element-screenshot__overlay');
overlayContainerEl.append(overlay);
// The newly-added overlay has the dimensions we need.
const maxLightboxSize = {
width: overlay.clientWidth * 0.95,
height: overlay.clientHeight * 0.80,
};
const elementRectSC = {
width: Number(el.dataset['rectWidth']),
height: Number(el.dataset['rectHeight']),
left: Number(el.dataset['rectLeft']),
right: Number(el.dataset['rectLeft']) + Number(el.dataset['rectWidth']),
top: Number(el.dataset['rectTop']),
bottom: Number(el.dataset['rectTop']) + Number(el.dataset['rectHeight']),
};
const screenshotElement = ElementScreenshotRenderer.render(
dom,
fullPageScreenshot.screenshot,
elementRectSC,
maxLightboxSize
);
// This would be unexpected here.
// When `screenshotElement` is `null`, there is also no thumbnail element for the user to have clicked to make it this far.
if (!screenshotElement) {
overlay.remove();
return;
}
overlay.append(screenshotElement);
overlay.addEventListener('click', () => overlay.remove());
});
}
/**
* Given the size of the element in the screenshot and the total available size of our preview container,
* compute the factor by which we need to zoom out to view the entire element with context.
* @param {Rect} elementRectSC
* @param {Size} renderContainerSizeDC
* @return {number}
*/
static _computeZoomFactor(elementRectSC, renderContainerSizeDC) {
const targetClipToViewportRatio = 0.75;
const zoomRatioXY = {
x: renderContainerSizeDC.width / elementRectSC.width,
y: renderContainerSizeDC.height / elementRectSC.height,
};
const zoomFactor = targetClipToViewportRatio * Math.min(zoomRatioXY.x, zoomRatioXY.y);
return Math.min(1, zoomFactor);
}
/**
* Renders an element with surrounding context from the full page screenshot.
* Used to render both the thumbnail preview in details tables and the full-page screenshot in the lightbox.
* Returns null if element rect is outside screenshot bounds.
* @param {DOM} dom
* @param {LH.Result.FullPageScreenshot['screenshot']} screenshot
* @param {Rect} elementRectSC Region of screenshot to highlight.
* @param {Size} maxRenderSizeDC e.g. maxThumbnailSize or maxLightboxSize.
* @return {Element|null}
*/
static render(dom, screenshot, elementRectSC, maxRenderSizeDC) {
if (!screenshotOverlapsRect(screenshot, elementRectSC)) {
return null;
}
const tmpl = dom.createComponent('elementScreenshot');
const containerEl = dom.find('div.lh-element-screenshot', tmpl);
containerEl.dataset['rectWidth'] = elementRectSC.width.toString();
containerEl.dataset['rectHeight'] = elementRectSC.height.toString();
containerEl.dataset['rectLeft'] = elementRectSC.left.toString();
containerEl.dataset['rectTop'] = elementRectSC.top.toString();
// Zoom out when highlighted region takes up most of the viewport.
// This provides more context for where on the page this element is.
const zoomFactor = this._computeZoomFactor(elementRectSC, maxRenderSizeDC);
const elementPreviewSizeSC = {
width: maxRenderSizeDC.width / zoomFactor,
height: maxRenderSizeDC.height / zoomFactor,
};
elementPreviewSizeSC.width = Math.min(screenshot.width, elementPreviewSizeSC.width);
elementPreviewSizeSC.height = Math.min(screenshot.height, elementPreviewSizeSC.height);
/* This preview size is either the size of the thumbnail or size of the Lightbox */
const elementPreviewSizeDC = {
width: elementPreviewSizeSC.width * zoomFactor,
height: elementPreviewSizeSC.height * zoomFactor,
};
const positions = ElementScreenshotRenderer.getScreenshotPositions(
elementRectSC,
elementPreviewSizeSC,
{width: screenshot.width, height: screenshot.height}
);
const imageEl = dom.find('div.lh-element-screenshot__image', containerEl);
imageEl.style.width = elementPreviewSizeDC.width + 'px';
imageEl.style.height = elementPreviewSizeDC.height + 'px';
imageEl.style.backgroundPositionY = -(positions.screenshot.top * zoomFactor) + 'px';
imageEl.style.backgroundPositionX = -(positions.screenshot.left * zoomFactor) + 'px';
imageEl.style.backgroundSize =
`${screenshot.width * zoomFactor}px ${screenshot.height * zoomFactor}px`;
const markerEl = dom.find('div.lh-element-screenshot__element-marker', containerEl);
markerEl.style.width = elementRectSC.width * zoomFactor + 'px';
markerEl.style.height = elementRectSC.height * zoomFactor + 'px';
markerEl.style.left = positions.clip.left * zoomFactor + 'px';
markerEl.style.top = positions.clip.top * zoomFactor + 'px';
const maskEl = dom.find('div.lh-element-screenshot__mask', containerEl);
maskEl.style.width = elementPreviewSizeDC.width + 'px';
maskEl.style.height = elementPreviewSizeDC.height + 'px';
ElementScreenshotRenderer.renderClipPathInScreenshot(
dom,
maskEl,
positions.clip,
elementRectSC,
elementPreviewSizeSC
);
return containerEl;
}
}