-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
165 lines (123 loc) · 4.08 KB
/
index.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
class LineAnimation {
constructor (id, options) {
const {
playing = false, autoStart = true, loop = false,
dashLength = 1,
duration = 1000,
headColor = `rgba(0,0,0,1)`, tailColor =`rgba(0,0,0,1)`, backgroundColor = `rgba(0,0,0,0)`,
headWidth = 1, tailWidth = 1,
geojson = {
"type": "FeatureCollection",
"features": []
}
} = options;
Object.assign(this, {
id,
geojson,
playing, autoStart, loop,
startTime: 0,
duration,
progressDelta: 0,
dashLength,
headWidth, tailWidth,
headColor, tailColor, backgroundColor
})
return this
}
addTo(map) {
this.map = map;
const {id, geojson, autoStart, backgroundColor} = this;
map.addLayer({
id,
type: 'line',
source: {
data: geojson,
type: 'geojson',
lineMetrics: true
},
paint: {
'line-width':10,
'line-color': backgroundColor,
'line-width-transition': {
duration:0
}
}
})
if (autoStart) this.play()
return this
}
play() {
if (!this.playing) {
this.playing = true;
this.startTime = document.timeline.currentTime;
requestAnimationFrame(this.tick.bind(this))
}
}
pause() {
this.playing = false;
}
setProgress(dashProgress) {
const {id, dashLength, headColor, tailColor, backgroundColor, headWidth, tailWidth, map} = this;
const justABit = 0.0000000001;
let dash;
// at start, only background is visible
if (dashProgress===0) {
dash = [1, backgroundColor]
}
// from start until head touches the end,
else {
const trailingGapLength = Math.max(0, dashProgress-dashLength);
const [trailingGap, tail, head, leadingGap] = [
[trailingGapLength, backgroundColor],
[trailingGapLength + justABit, tailColor],
[dashProgress, headColor],
[dashProgress+justABit, backgroundColor],
];
if (dashProgress<=1) {
// if tail is in view, prepend gradient with a section of background color
// with the length of the starting gap
if (trailingGapLength) dash = [...trailingGap, ...tail, ...head, ...leadingGap];
else dash = [0, tailColor, ...head, ...leadingGap];
}
// if dash head is past the end
else {
dash = [
...trailingGap,
...tail,
...head, // head
];
}
}
map.setPaintProperty(
id,
'line-gradient',
[
'interpolate',
['linear'],
['line-progress'],
...dash
]
)
// interpolate width
if (headWidth-tailWidth) {
let width = Math.sin(Math.PI*dashProgress/(dashLength+1))*(headWidth-tailWidth)
map.setPaintProperty(id, 'line-width', tailWidth+width)
}
return this
}
calculateProgress(now) {
const {duration, dashLength, startTime, progressDelta } = this;
this.currentProgress = (progressDelta + (1 + dashLength)*(now%duration) / duration);
// console.log(this.currentProgress)
return this.currentProgress
}
tick(now) {
const {playing, loop} = this;
if (!playing) return
let dashProgress = this.calculateProgress(now);
if (!loop) dashProgress = Math.min(dashProgress,1)
this.setProgress(dashProgress)
if (loop || dashProgress<1) requestAnimationFrame(this.tick.bind(this))
return this
}
}