-
Notifications
You must be signed in to change notification settings - Fork 1
/
grid.js
48 lines (41 loc) · 1.13 KB
/
grid.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
const html = require('choo/html')
const widget = require('cache-element/widget')
const pixels = require('pixel-grid')
module.exports = widget(function createEl (update) {
let data = null
let grid = null
update((newData) => {
data = newData
if (grid) grid.update(gridifyBlocks(data))
})
return html`
<div class="grid-wrapper">
<div class="grid" onload=${onload}></div>
</div>
`
function onload (el) {
const size = data.length > 200 ? 8 : 12
const pad = 1
let cols = Math.floor(1106 / (size + pad))
const rows = Math.floor(data.length / cols) + 1
if (rows === 1) cols = data.length
grid = pixels(gridifyBlocks(data), {
size: size,
padding: pad,
columns: cols,
rows: rows,
background: [.91, .92, .93],
formatted: true
})
el.appendChild(grid.canvas)
}
function gridifyBlocks (blocks) {
const gridData = []
// const end = blocks.length > 1000 ? 1000 : blocks.length
for (let i = 0; i < blocks.length; i++) {
if (blocks[i]) gridData.push([.207, .705, .310])
else gridData.push([.91, .92, .93])
}
return gridData
}
})