Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
93 lines (73 loc) · 2.44 KB
/
Copy pathmain.js
File metadata and controls
93 lines (73 loc) · 2.44 KB
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
import * as d3 from "d3";
import './style.css'
// import init from './emscripten/program.wasm?init'
// TODO
// [ ] add frametimes/fps counter
const FPS = 120
let counter = 0
let width = 0
let height = 0
let numLeds = 0
let leds1d = []
let leds = []
const svg = d3.select('#led-svg').attr('viewBox', '0 0 1200 900').style('background-color', 'black');
const ledContainer = svg.select('#led-container').attr('transform', 'translate(37, 37)'); // .style('filter', 'url(#blur)');
function updateSvg(ints) {
const leds1d = []
for (var i = 0; i < numLeds; i++) {
const start = i * 3;
const led = ints.slice(start, start + 3);
leds1d[i] = led
}
for (var y = 0; y < height; y++) {
const start = y * width
leds[y] = leds1d.slice(start, start + width)
}
const rowGroups = ledContainer.selectAll('g').data(leds)
rowGroups.enter().append('g').attr('transform', (d, i) => {return `translate(0, ${i * 100})`})
const squares = rowGroups.merge(rowGroups).selectAll('rect').data((d) => { return d;})
squares.enter().append('rect').attr('width', 25).attr('height', 25).attr('x', (d, i) => {return i * 100;})
squares.merge(squares).attr('fill', (d) => {
return `rgb(${d[0]}, ${d[1]}, ${d[2]})`
})
}
function updateCounter() {
counter += 1;
document.getElementById('counter').innerHTML = counter;
}
async function runWebassembly() {
// const instance = await init()
// console.log(instance.exports);
// const {setup, loop, getWidth, getHeight} = instance.exports
width = Module.ccall('getWidth', 'number', [])
height = Module.ccall('getHeight', 'number', [])
numLeds = width * height
const ledsPointer = Module.ccall('setup', 'number', [])
const loop = Module.cwrap('loop', null, [])
// const ints = new Uint8Array(instance.exports.memory.buffer, ledsPointer);
const ints = new Uint8Array(Module.HEAPU8.buffer, ledsPointer);
// while(true) {
// loop();
// updateSvg(ints);
// updateCounter()
// }
loop();
const runAndScheduleNextFrame = () => {
updateSvg(ints);
const previousMillis = Date.now();
loop();
updateCounter()
const millisToNextFrame = 8 - (Date.now() - previousMillis);
window.setTimeout(runAndScheduleNextFrame, millisToNextFrame)
}
runAndScheduleNextFrame();
// window.setInterval(() => {
// updateSvg(ints);
// updateCounter()
// loop();
// }, 1)
}
// wait for Emscripten to load
Module.onRuntimeInitialized = function() {
runWebassembly();
};