-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
77 lines (59 loc) · 1.75 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
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
'use strict'
const choo = require('choo')
const css = require('sheetify')
const styles = require('./catalogs/styles')
const lastColor = require('./catalogs/colors').length - 1
css('css-wipe')
css('tachyons')
css('./styles.css')
const app = choo()
app.use(require('choo-service-worker')())
if (process.env.NODE_ENV !== 'production') {
app.use(require('choo-log')())
}
app.use(patchwork)
app.route('/', require('./views/main'))
app.route('/*', require('./views/404'))
if (!module.parent) app.mount('body')
else module.exports = app
function patchwork (state, emitter) {
state.grid = makeGrid(8, 8)
state.colorPicker = { selectionID: 0 }
state.canvasColorPicker = { selectionID: lastColor }
emitter.on('changeStyle', function ({ m, n, colorID }) {
const stylesLength = Object.keys(styles).length
const nextID = state.grid[m][n].styleID + 1
if (nextID === stylesLength) { // Cycle
state.grid[m][n].styleID = 0
} else {
state.grid[m][n].styleID += 1
}
state.grid[m][n].colorID = state.colorPicker.selectionID
state.grid[m][n].canvasColorID = state.canvasColorPicker.selectionID
emitter.emit('render')
})
emitter.on('changeColor', function ({ selectionID }) {
state.colorPicker.selectionID = selectionID
emitter.emit('render')
})
emitter.on('changeCanvasColor', function ({ selectionID }) {
state.canvasColorPicker.selectionID = selectionID
emitter.emit('render')
})
}
function makeGrid (width, height) {
const grid = []
for (let m = 0; m < height; m += 1) {
const column = []
for (let n = 0; n < width; n += 1) {
column.push({
element: '',
styleID: 0,
colorID: 0,
canvasColorID: lastColor
})
}
grid.push(column)
}
return grid
}