-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathmondrian.js
More file actions
46 lines (42 loc) · 1.24 KB
/
mondrian.js
File metadata and controls
46 lines (42 loc) · 1.24 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
const require = (mame, es = '../esm/index.js') => import(
/^(?:localhost|[0-9.]+)$/.test(location.hostname) ?
es : `https://unpkg.com/${mame}?module`
);
Promise.all([
import('https://unpkg.com/wicked-elements?module'),
require('uhtml')
]).then(([{define}, {render, html}]) => {
define('#app .mondrian', {
connected() { this.render(); },
render(blocks = this.generateBlocks()) {
render(this.element, html`${blocks.map(
({colSpan, rowSpan, colorIndex}) => html`
<div class="mondrian__block"
data-col-span=${colSpan}
data-row-span=${rowSpan}
data-color-index=${colorIndex} />`
)}`);
},
generateBlocks() {
const blocks = [];
for (let i = 0; i < 10; i++) {
blocks.push({
colSpan: Math.floor(Math.random() * 3 + 1),
rowSpan: Math.floor(Math.random() * 3 + 1),
colorIndex: Math.floor(Math.random() * 6 + 1),
});
}
return blocks;
},
onGenerate() {
this.render([]);
setTimeout(() => this.render());
}
});
define('#app .generate-button', {
onclick() {
const mondrian = document.querySelector('.mondrian');
mondrian.dispatchEvent(new Event('generate'));
}
});
});