-
Notifications
You must be signed in to change notification settings - Fork 2
/
scrollbars.ts
220 lines (177 loc) · 6.18 KB
/
scrollbars.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
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
import {InfiniteGrid} from './grid';
interface Rect {
startX: number,
startY: number,
endX: number,
endY: number
}
class Scrollbars {
private draggingX: Boolean = false
private draggingY: Boolean = false
scrollbarColor = 'hsl(0, 0%, 61%)'
scrollbarBackground = 'hsl(0, 0%, 86%)'
scrollButtColor = 'hsl(0, 0%, 76%)'
scrollButtSize = 30
scrollHandleSize = 12
scrollHandleMinLength = 90
constructor(
private ctx: CanvasRenderingContext2D,
private grid: InfiniteGrid
) {
this.scrollButtSize = this.scrollButtSize * window.devicePixelRatio;
this.scrollHandleSize = this.scrollHandleSize * window.devicePixelRatio;
this.scrollHandleMinLength = this.scrollHandleMinLength * window.devicePixelRatio;
}
private getXBarRect():Rect {
let ret = {
startX: 0,
startY: this.grid.dimensions.height - this.scrollButtSize,
};
(ret as Rect).endX = (ret.startX + this.grid.dimensions.width -
this.scrollButtSize);
(ret as Rect).endY = (ret.startY + this.grid.dimensions.height -
this.scrollButtSize);
return (ret as Rect);
}
private isDragging(): Boolean {
return this.draggingX || this.draggingY;
}
private getYBarRect():Rect {
let ret = {
startX: this.grid.dimensions.width - this.scrollButtSize,
startY: 0
};
(ret as Rect).endX = (ret.startX + this.scrollButtSize);
(ret as Rect).endY = (ret.startY + this.grid.dimensions.height -
this.scrollButtSize);
return (ret as Rect)
}
private drawX(maxBounds:{x: number, y: number}) {
let bounds = this.getXBarRect();
this.ctx.fillStyle = this.scrollbarBackground;
this.ctx.fillRect(
bounds.startX,
bounds.startY,
bounds.endX - bounds.startX,
bounds.endY - bounds.startY);
let width = Math.max(
this.scrollHandleMinLength,
(this.grid.dimensions.width / maxBounds.x) * this.grid.dimensions.width);
let xScrollPercent = (this.grid.viewportOffset.x /
(maxBounds.x - this.grid.dimensions.width));
this.ctx.fillStyle = this.scrollbarColor;
this.ctx.fillRect(
xScrollPercent * (this.grid.dimensions.width - width),
(this.grid.dimensions.height -
this.scrollButtSize + this.scrollButtSize / 2 -
this.scrollHandleSize / 2),
width - this.scrollButtSize,
this.scrollHandleSize);
}
private drawY(maxBounds:{x: number, y: number}) {
let bounds = this.getYBarRect();
this.ctx.fillStyle = this.scrollbarBackground;
this.ctx.fillRect(
bounds.startX,
bounds.startY,
bounds.endX - bounds.startX,
bounds.endY - bounds.startY);
let height = Math.max(
this.scrollHandleMinLength,
(this.grid.dimensions.height
/ maxBounds.y) * this.grid.dimensions.height);
let yScrollPercent = (this.grid.viewportOffset.y /
(maxBounds.y - this.grid.dimensions.height));
this.ctx.fillStyle = this.scrollbarColor;
this.ctx.fillRect(
(this.grid.dimensions.width -
this.scrollButtSize + this.scrollButtSize / 2 -
this.scrollHandleSize / 2),
yScrollPercent * (this.grid.dimensions.height - height),
this.scrollHandleSize,
height - this.scrollButtSize);
}
private drawScrollButt() {
this.ctx.fillStyle = this.scrollButtColor;
this.ctx.fillRect(
this.grid.dimensions.width - this.scrollButtSize,
this.grid.dimensions.height - this.scrollButtSize,
this.scrollButtSize,
this.scrollButtSize);
}
private isPointInRect(x:number, y:number, rect:Rect):Boolean {
return (x >= rect.startX && x <= rect.endX) &&
(y >= rect.startY && y <= rect.endY);
}
private handleXClick(x: number) {
let maxBounds = this.grid.getMaxBounds();
let nextXOffset = (maxBounds.x - this.grid.dimensions.width) * (
this.removeXViewportOffset(x) / (this.grid.dimensions.width - this.scrollButtSize));
this.grid.scrollByPixels(nextXOffset - this.grid.viewportOffset.x, 0);
}
private handleYClick(y: number) {
let maxBounds = this.grid.getMaxBounds();
let nextYOffset = (maxBounds.y - this.grid.dimensions.height) * (
this.removeYViewportOffset(y) / (this.grid.dimensions.height - this.scrollButtSize));
this.grid.scrollByPixels(0, nextYOffset - this.grid.viewportOffset.y);
}
draw() {
var maxBounds = this.grid.getMaxBounds();
if (this.isXBarVisible(maxBounds)) {
this.drawX(maxBounds);
}
if (this.isYBarVisible(maxBounds)) {
this.drawY(maxBounds);
}
if (this.isXBarVisible(maxBounds) || this.isYBarVisible(maxBounds)) {
this.drawScrollButt();
}
}
private removeXViewportOffset(x: number): number {
return x - this.grid.viewportOffset.x;
}
private removeYViewportOffset(y: number): number {
return y - this.grid.viewportOffset.y;
}
private isOverX(x: number, y: number): Boolean {
let maxBounds = this.grid.getMaxBounds();
return this.isXBarVisible(maxBounds) &&
this.isPointInRect(this.removeXViewportOffset(x), this.removeYViewportOffset(y), this.getXBarRect());
}
private isOverY(x: number, y: number): Boolean {
let maxBounds = this.grid.getMaxBounds();
return this.isYBarVisible(maxBounds) &&
this.isPointInRect(this.removeXViewportOffset(x), this.removeYViewportOffset(y), this.getYBarRect());
}
isOver(x:number, y:number): Boolean {
return this.isOverX(x, y) || this.isOverY(x, y);
}
resetDragging() {
this.draggingX = false;
this.draggingY = false;
}
handleClick(x:number, y:number) {
let maxBounds = this.grid.getMaxBounds();
if (this.draggingY || this.isOverY(x, y)) {
this.resetDragging();
this.draggingY = true;
this.handleYClick(y);
return
}
if (this.draggingX || this.isOverX(x, y)) {
this.resetDragging();
this.draggingX = true;
this.handleXClick(x);
return
}
}
isXBarVisible(maxBounds): Boolean {
return this.grid.viewportOffset.x > 0 ||
maxBounds.x > this.grid.dimensions.width;
}
isYBarVisible(maxBounds): Boolean {
return this.grid.viewportOffset.y > 0 ||
maxBounds.y > this.grid.dimensions.height;
}
};
export {Scrollbars}