forked from ECS-251-W2020/garnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
47 lines (36 loc) · 1.05 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
const socket = io.connect('http://localhost:3000')
const url = 'https://duckduckgo.com/?t=ffab&q=operating+system&ia=web'
init()
async function init() {
const CanvasKit = await CanvasKitInit({
locateFile: file => '/node_modules/canvaskit-wasm/bin/' + file
}).ready()
const surface = CanvasKit.MakeCanvasSurface('main')
if (!surface) {
throw new Error('Could not make surface')
}
// Send URL to server
socket.emit('ready', url)
// When a set of draw commands are ready
socket.on('draw', drawCommands)
// When all draw commands have completed
socket.on('done', () => skCanvas.flush())
const skCanvas = surface.getCanvas()
const skFont = new CanvasKit.SkFont(null, 14)
const skPaint = new CanvasKit.SkPaint()
// Draw loading message
skPaint.setStyle(CanvasKit.PaintStyle.Fill)
skPaint.setAntiAlias(true)
skCanvas.drawText(
'Waiting for draw commands from server...',
60,
60,
skPaint,
skFont
)
skCanvas.flush()
function drawCommands(commands) {
console.log(commands)
eval(commands)
}
}