-
Notifications
You must be signed in to change notification settings - Fork 498
/
Copy pathhighlight_scroll_snap.ts
166 lines (151 loc) · 5.49 KB
/
highlight_scroll_snap.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
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
// Copyright 2021 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 type {Bounds, PathCommands, Position} from './common.js';
import {drawPath, emptyBounds, type LineStyle, type PathBounds} from './highlight_common.js';
type SnapAlignment = 'none'|'start'|'end'|'center';
export interface ScrollSnapHighlight {
snapport: PathCommands;
paddingBox: PathCommands;
snapAreas: Array<{
path: PathCommands,
borderBox: PathCommands,
alignBlock?: SnapAlignment,
alignInline?: SnapAlignment,
}>;
snapportBorder: LineStyle;
snapAreaBorder: LineStyle;
scrollMarginColor: string;
scrollPaddingColor: string;
}
function getSnapAlignBlockPoint(bounds: Bounds, align: SnapAlignment): Position|undefined {
if (align === 'start') {
return {
x: (bounds.minX + bounds.maxX) / 2,
y: bounds.minY,
};
}
if (align === 'center') {
return {
x: (bounds.minX + bounds.maxX) / 2,
y: (bounds.minY + bounds.maxY) / 2,
};
}
if (align === 'end') {
return {
x: (bounds.minX + bounds.maxX) / 2,
y: bounds.maxY,
};
}
return;
}
function getSnapAlignInlinePoint(bounds: Bounds, align: SnapAlignment): Position|undefined {
if (align === 'start') {
return {
x: bounds.minX,
y: (bounds.minY + bounds.maxY) / 2,
};
}
if (align === 'center') {
return {
x: (bounds.minX + bounds.maxX) / 2,
y: (bounds.minY + bounds.maxY) / 2,
};
}
if (align === 'end') {
return {
x: bounds.maxX,
y: (bounds.minY + bounds.maxY) / 2,
};
}
return;
}
const ALIGNMENT_POINT_STROKE_WIDTH = 5;
const ALIGNMENT_POINT_STROKE_COLOR = 'white';
const ALIGNMENT_POINT_OUTER_RADIUS = 6;
const ALIGNMENT_POINT_FILL_COLOR = '#4585f6';
const ALIGNMENT_POINT_INNER_RADIUS = 4;
function drawAlignment(context: CanvasRenderingContext2D, point: Position, bounds: Bounds): void {
let startAngle = 0;
let renderFullCircle = true;
if (point.x === bounds.minX) {
startAngle = -0.5 * Math.PI;
renderFullCircle = false;
} else if (point.x === bounds.maxX) {
startAngle = 0.5 * Math.PI;
renderFullCircle = false;
} else if (point.y === bounds.minY) {
startAngle = 0;
renderFullCircle = false;
} else if (point.y === bounds.maxY) {
startAngle = Math.PI;
renderFullCircle = false;
}
const endAngle = startAngle + (renderFullCircle ? 2 * Math.PI : Math.PI);
context.save();
context.beginPath();
context.lineWidth = ALIGNMENT_POINT_STROKE_WIDTH;
context.strokeStyle = ALIGNMENT_POINT_STROKE_COLOR;
context.arc(point.x, point.y, ALIGNMENT_POINT_OUTER_RADIUS, startAngle, endAngle);
context.stroke();
context.fillStyle = ALIGNMENT_POINT_FILL_COLOR;
context.arc(point.x, point.y, ALIGNMENT_POINT_INNER_RADIUS, startAngle, endAngle);
context.fill();
context.restore();
}
function drawScrollPadding(
highlight: ScrollSnapHighlight, context: CanvasRenderingContext2D, emulationScaleFactor: number) {
drawPath(
context, highlight.paddingBox, highlight.scrollPaddingColor, undefined, undefined, emptyBounds(),
emulationScaleFactor);
// Clear the area so that previously rendered paddings remain.
context.save();
context.globalCompositeOperation = 'destination-out';
drawPath(context, highlight.snapport, 'white', undefined, undefined, emptyBounds(), emulationScaleFactor);
context.restore();
}
function drawSnapAreas(
highlight: ScrollSnapHighlight, context: CanvasRenderingContext2D, emulationScaleFactor: number): PathBounds[] {
const bounds = [];
for (const area of highlight.snapAreas) {
const areaBounds = emptyBounds();
drawPath(
context, area.path, highlight.scrollMarginColor, highlight.snapAreaBorder.color,
highlight.snapAreaBorder.pattern, areaBounds, emulationScaleFactor);
// Clear the area so that previously rendered margins remain.
context.save();
context.globalCompositeOperation = 'destination-out';
drawPath(context, area.borderBox, 'white', undefined, undefined, emptyBounds(), emulationScaleFactor);
context.restore();
bounds.push(areaBounds);
}
return bounds;
}
function drawAlignmentPoints(
areaBounds: PathBounds[], highlight: ScrollSnapHighlight, context: CanvasRenderingContext2D) {
for (let i = 0; i < highlight.snapAreas.length; i++) {
const area = highlight.snapAreas[i];
const inlinePoint = area.alignInline ? getSnapAlignInlinePoint(areaBounds[i], area.alignInline) : null;
const blockPoint = area.alignBlock ? getSnapAlignBlockPoint(areaBounds[i], area.alignBlock) : null;
if (inlinePoint) {
drawAlignment(context, inlinePoint, areaBounds[i]);
}
if (blockPoint) {
drawAlignment(context, blockPoint, areaBounds[i]);
}
}
}
function drawSnapportBorder(
highlight: ScrollSnapHighlight, context: CanvasRenderingContext2D, emulationScaleFactor: number) {
drawPath(
context, highlight.snapport, undefined, highlight.snapportBorder.color, undefined, emptyBounds(),
emulationScaleFactor);
}
export function drawScrollSnapHighlight(
highlight: ScrollSnapHighlight, context: CanvasRenderingContext2D, emulationScaleFactor: number) {
// The order of the following draw calls is important, change it carefully.
drawScrollPadding(highlight, context, emulationScaleFactor);
const areaBounds = drawSnapAreas(highlight, context, emulationScaleFactor);
drawSnapportBorder(highlight, context, emulationScaleFactor);
drawAlignmentPoints(areaBounds, highlight, context);
}