-
Notifications
You must be signed in to change notification settings - Fork 497
/
Copy pathtool_distances.ts
125 lines (111 loc) · 3.48 KB
/
tool_distances.ts
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
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import {Overlay} from './common.js';
interface Style {
'background-color': string;
'background-image': string;
'border-left-width': string;
'border-left-style': string;
'border-left-color': string;
'border-right-width': string;
'border-right-style': string;
'border-right-color': string;
'border-top-width': string;
'border-top-style': string;
'border-top-color': string;
'border-bottom-width': string;
'border-bottom-style': string;
'border-bottom-color': string;
'outline-width': string;
'outline-style': string;
'outline-color': string;
'box-shadow': string;
}
type StyleKey = keyof Style;
type Quad = number[];
interface DistanceInfo {
style: Style;
border: Quad;
padding: Quad;
content: Quad;
boxes: number[][];
}
export class DistancesOverlay extends Overlay {
drawDistances({distanceInfo}: {distanceInfo: DistanceInfo}) {
if (!distanceInfo) {
return;
}
const rect = quadToRect(getVisualQuad(distanceInfo));
this.context.save();
this.context.strokeStyle = '#ccc';
for (const box of distanceInfo.boxes) {
this.context.strokeRect(box[0], box[1], box[2], box[3]);
}
this.context.strokeStyle = '#f00';
this.context.lineWidth = 1;
this.context.rect(rect.x - 0.5, rect.y - 0.5, rect.w + 1, rect.h + 1);
this.context.stroke();
this.context.restore();
}
override install() {
this.document.body.classList.add('fill');
const canvas = this.document.createElement('canvas');
canvas.id = 'canvas';
canvas.classList.add('fill');
this.document.body.append(canvas);
this.setCanvas(canvas);
super.install();
}
override uninstall() {
this.document.body.classList.remove('fill');
this.document.body.innerHTML = '';
super.uninstall();
}
}
function getVisualQuad(data: DistanceInfo): Quad {
const style = data['style'];
if (shouldUseVisualBorder(style)) {
return data['border'];
}
if (shouldUseVisualPadding(style)) {
return data['padding'];
}
return data['content'];
function shouldUseVisualBorder(style: Style): boolean {
const sides = ['top', 'right', 'bottom', 'left'];
for (const side of sides) {
const borderWidth = style[`border-${side}-width` as StyleKey];
const borderStyle = style[`border-${side}-style` as StyleKey];
const borderColor = style[`border-${side}-color` as StyleKey];
if (borderWidth !== '0px' && borderStyle !== 'none' && !borderColor.endsWith('00')) {
return true;
}
}
const outlineWidth = style['outline-width'];
const outlineStyle = style['outline-style'];
const outlineColor = style['outline-color'];
if (outlineWidth !== '0px' && outlineStyle !== 'none' && !outlineColor.endsWith('00')) {
return true;
}
const boxShadow = style['box-shadow'];
if (boxShadow !== 'none') {
return true;
}
return false;
}
function shouldUseVisualPadding(style: Style): boolean {
const bgColor = style['background-color'];
const bgImage = style['background-image'];
if (!bgColor.startsWith('#FFFFFF') && !bgColor.endsWith('00')) {
return true;
}
if (bgImage !== 'none') {
return true;
}
return false;
}
}
function quadToRect(quad: Quad): {x: number, y: number, w: number, h: number} {
return {x: quad[0], y: quad[1], w: quad[4] - quad[0], h: quad[5] - quad[1]};
}