-
Notifications
You must be signed in to change notification settings - Fork 0
/
gridworklet.js
69 lines (63 loc) · 1.76 KB
/
gridworklet.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
registerPaint('grid', class {
static get inputProperties() {
return [
'--rows',
'--columns',
'--grid'
]
}
drawGrid (ctx, width, height, rows, columns) {
// draw borders
ctx.beginPath()
ctx.moveTo(0, 0)
ctx.lineTo(width, 0)
ctx.stroke()
ctx.beginPath()
ctx.moveTo(0, 0)
ctx.lineTo(0, height)
ctx.stroke()
ctx.beginPath()
ctx.moveTo(0, height)
ctx.lineTo(width, height)
ctx.stroke()
ctx.beginPath()
ctx.moveTo(width, 0)
ctx.lineTo(width, height)
ctx.stroke()
// draw horizontal lines
for (var i = 1; i < rows; i++) {
ctx.beginPath()
ctx.moveTo(0, i * (height / rows))
ctx.lineTo(width, i * (height / rows))
ctx.stroke()
}
for (var i = 1; i < columns; i++) {
ctx.beginPath()
ctx.moveTo(i * (width / columns), 0)
ctx.lineTo(i * (width / columns), height)
ctx.stroke()
}
}
drawPixels (ctx, rows, columns, points, width, height, colors) {
var size = [width / columns, height / rows]
for (var x = 0; x < rows; x++) {
for (var y = 0; y < columns; y++) {
var point = points[y * columns + x] || 0
ctx.beginPath()
ctx.fillStyle = colors[point]
ctx.rect(x * size[0], y * size[1], size[0], size[1])
ctx.fill()
}
}
}
paint (ctx, geom, properties) {
var { height, width } = geom
var rows = properties.get('--rows').toString()
var columns = properties.get('--columns').toString()
// draw pixels
var points = properties.get('--grid').toString().split(' ')
var colors = ['#CCCCCC', '#777777']
this.drawPixels(ctx, rows, columns, points, width, height, colors)
this.drawGrid(ctx, width, height, rows, columns)
}
})