-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.html
367 lines (282 loc) · 11.4 KB
/
index.html
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<!--
Map route renderer.
This will take a set of geojson routes and animate them progressively into an MP4 video.
“Documentation”: https://twitter.com/mwichary/status/1427007100845060096
Necessary libraries:
- d3.v7.js: Download from https://d3js.org/
- FileSaver.js: Download from https://github.com/eligrey/FileSaver.js/
- ffmpeg-worker-mp4.js: Download from https://gist.github.com/ilblog/5fa2914e0ad666bbb85745dbf4b3f106
(note: there are many other ffmpeg JS worker wrappers and they might work as well)
Necessary files:
- data/1.geojson, data/2.geojson, etc. for the routes (converted from GPX using “togeojson” tool)
- map.jpg: Background image with the map, prepare however you see fit
You might have to run this via a server rather than opening it as a file in the browser.
As hilarious as it sounds, running “php -S localhost:4444” (on a Mac) in a given directory
works best for me.
(I know all this is probably archaic, but I like simple JS without complex build systems.)
--
Copyright © 2021 Marcin Wichary
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
-->
<title>Map video renderer</title>
<meta charset='utf-8'>
<script src='d3.v7.js'></script>
<script src='FileSaver.js'></script>
<style>
#content {
outline: 1px solid black;
width: 800px;
height: 800px;
position: relative;
}
#content canvas {
width: 800px;
height: 800px;
}
</style>
<body>
<div id="content">
<canvas width="1600" height="1600"></canvas>
</div>
<script>
// CONSTANTS FOR YOU TO PLAY WITH
// ------------------------------------------------------------
// Frames per second
const FRAMERATE = 60
// How many routes to draw
const ROUTE_COUNT = 20
// Which frames to render. Rendering all the frames of the video will likely exhaust
// the memory. Here you can render just a bit and then splice all the MP4s together
// at the end using ffmpeg
const FIRST_FRAME_TO_RENDER = 100
const LAST_FRAME_TO_RENDER = 150
// HELPER FUNCTIONS
// ------------------------------------------------------------
// Background map image
let bkImage
// Loaded geodata
let geodataToBeLoadedCount = 0
let geodata = []
// Saved pixel data
let imagePixelData = []
// HELPER FUNCTIONS
// ------------------------------------------------------------
function convertDataURIToBinary(dataURI) {
var base64 = dataURI.replace(/^data[^,]+,/, '')
var raw = window.atob(base64)
var rawLength = raw.length
var array = new Uint8Array(new ArrayBuffer(rawLength))
for (i = 0; i < rawLength; i++) {
array[i] = raw.charCodeAt(i)
}
return array
}
// MAIN FUNCTIONS
// ------------------------------------------------------------
function main() {
console.warn('Starting…')
console.warn('Step 1: Loading background image…')
bkImage = new Image()
bkImage.onload = loadGeojsonData
bkImage.src = 'map.jpg'
}
function onGeodataLoaded(routeNo, req) {
if (req.readyState != 4) {
return
}
const data = JSON.parse(req.response)
// This reduces the complexity of a route 15-fold so it’s faster to draw
const skip = 15
let newData = []
for (var i = 0; i < Math.floor(data.features[0].geometry.coordinates.length / skip); i++) {
newData.push(data.features[0].geometry.coordinates[i * skip])
}
geodata[routeNo] = data
geodata[routeNo].features[0].geometry.coordinates = newData
geodataToBeLoadedCount--
if (geodataToBeLoadedCount == 0) {
renderFrames()
}
}
function loadGeojsonData() {
console.warn('Step 2: Loading geojson data…')
for (let i = 1; i <= ROUTE_COUNT; i++) {
geodataToBeLoadedCount++
let req = new XMLHttpRequest()
req.addEventListener('load', () => { onGeodataLoaded(i, req) } )
req.open('GET', `data/${i}.geojson`)
req.send()
}
}
// Percentage is expressed from 0.0 (start drawing a route)
// to 1.0 (the entire route is drawn in a resting state)
function drawRoute(context, geoGenerator, routeNo, percentage) {
if (percentage < 0) {
return
}
let routeLength = geodata[routeNo].features[0].geometry.coordinates.length
// Draw the route in 10 chunks.
// Chunk 9 is the “head”, the thick pulse that leads the route
// Chunk 0 is the tail, the thing that is left behind as the route finished running.
for (var chunk = 0; chunk <= 9; chunk++) {
// This is the length of each chunk. Change this to vary the intensity of the “pulse.”
// Note that the last (tail) chunk will be longer and always and at the beginning of
// a route
let chunkLength = routeLength * .01
// This calculates where each chunk lies on the entire path
let offset = percentage * (routeLength + chunkLength * 11) - chunkLength * 10
// This calculates the beginning and end of each chunk
let start = Math.floor(chunkLength * (chunk - 1) + offset - 1) // minus one guarantees overlap
let end = Math.floor(chunkLength * chunk + offset)
// Always extend the tail to the beginning of a route
if ((chunk == 0) && (start > 0)) {
start = 0
}
if (start < 0) {
start = 0
}
// Don’t continue if there is nothing to draw
if (end <= 0) {
continue
}
// Vary opacity so it starts with 100% for the pulse and ends with 50% for resting state
context.globalAlpha = .5 + (chunk / 9) * .5
// Vary line width so it starts with 8 for the pulse and ends with 1 for resting state
context.lineWidth = 1 + (chunk / 9) * 7
// Draw the chunk
context.beginPath()
let data = JSON.parse(JSON.stringify(geodata[routeNo])) // duplicate geodata and only pick the necessary bits
data.features[0].geometry.coordinates = data.features[0].geometry.coordinates.slice(start, end)
geoGenerator({type: 'FeatureCollection', features: data.features})
context.stroke()
}
}
function renderFrames() {
console.warn('Step 3: Rendering frames…')
// Prepares D3 and drawing
// ------------------------------------------------
let context = d3.select('#content canvas')
.node()
.getContext('2d');
let projection = d3.geoMercator()
.center([-122.4425153, 37.7545106]) // Middle of San Francisco
.translate([800, 800]) // Matches the canvas width
.scale([600000])
let geoGenerator = d3.geoPath()
.projection(projection)
.context(context)
context.strokeStyle = 'black'
context.lineJoin = 'round'
// Figure out with routes to draw. This is useful for testing, where you can just do
// const routes = [1, 2] instead.
// ------------------------------------------------
let routes = []
for (var i = 1; i <= ROUTE_COUNT; i++) { routes.push(i) }
// Precalculate all the timing data: when to draw each route and for
// how long.
// Note that progressively with each route the route duration gets shorter,
// and there is more and more overlap between routes drawn.
// ------------------------------------------------
let routeTimingData = []
// Starting duration of route drawing in finished video (in secs)
let routeDuration = 2.0
// Pause between routes. Note that this will go negative, which means
// routes will start overlapping towards the end of the video
let routePause = 0.0
for (var i = 0; i < routes.length; i++) {
if (i == 0) {
startInSecs = 0
} else {
startInSecs = routeTimingData[i - 1].endInSecs + routePause
}
routeTimingData[i] = {
startInSecs: startInSecs,
endInSecs: startInSecs + routeDuration,
}
// Decrease the duration by a little, but won’t allow it to get smaller by 400ms
routeDuration -= .03
if (routeDuration < .4) {
routeDuration = .4
}
// Decrease the overlap by a little, but won’t allow it to get tighter than 100ms
routePause -= .01
if (routePause < -.1) {
routePause = -.1
}
}
const globalEndInSecs = routeTimingData[routeTimingData.length - 1].endInSecs
// Outputs total time of video in seconds, and in frames
console.warn(`Total time: ${globalEndInSecs} secs + ${Math.ceil(FRAMERATE * globalEndInSecs)} frames`)
// Render all the frames
// ------------------------------------------------
let frameNo = 0
for (var frame = FIRST_FRAME_TO_RENDER; frame <= LAST_FRAME_TO_RENDER; frame++) {
console.log(`…rendering frame ${frame} (out of ${FIRST_FRAME_TO_RENDER}-${LAST_FRAME_TO_RENDER})`)
// Clear the canvas by drawing the background image that contains a map
context.globalAlpha = 1.0
context.drawImage(bkImage, 0, 0, 1600, 1600)
// Draw all the necessary routes in the frame
for (var i in routeTimingData) {
const datum = routeTimingData[i]
const startInFrames = datum.startInSecs * FRAMERATE
if (frame >= startInFrames) {
drawRoute(context, geoGenerator, routes[i], (frame - startInFrames) / ((datum.endInSecs - datum.startInSecs) * FRAMERATE))
}
}
// Save all the pixels
const imgString = document.querySelector('canvas').toDataURL('image/jpeg', 1.0)
imagePixelData.push({
name: `img${(frameNo.toString()).padStart(5, '0')}.jpeg`,
data: convertDataURIToBinary(imgString)
})
frameNo++
}
compileVideo()
}
function compileVideo() {
console.warn('Step 4: Compiling into video…')
let worker = new Worker('/ffmpeg-worker-mp4.js')
worker.onmessage = function(e) {
var msg = e.data
switch (msg.type) {
case 'stdout':
case 'stderr':
console.log(msg.data)
break
case 'exit':
console.warn(`Process exited with code ${msg.data}.`)
break
case 'done':
saveVideo(new Blob([msg.data.MEMFS[0].data], { type: 'video/mp4' }))
break
}
}
worker.postMessage({
type: 'run',
TOTAL_MEMORY: 2068435456,
arguments: `-r ${FRAMERATE} -i img%05d.jpeg -vf scale=1600:1600 -pix_fmt yuv420p -vb 30M out.mp4`.split(/ /),
MEMFS: imagePixelData
})
}
function saveVideo(output) {
console.warn('Step 5: Saving…')
const url = webkitURL.createObjectURL(output)
saveAs(url, `${FIRST_FRAME_TO_RENDER}-${LAST_FRAME_TO_RENDER}.mp4`)
console.warn('All done!')
}
main()
</script>