-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw.js
67 lines (62 loc) · 1.62 KB
/
draw.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
kontra.init('canvas')
var sprites = []
// Constants
const COLOR_GREEN = '#33ff33'
var sketch = {
x: 0,
y: 0,
width: kontra.canvas.width,
height:kontra.canvas.height,
lines: [],
color: COLOR_GREEN,
thickness: 5,
onDown: function () {
this.drawing = true;
// Add a new line
this.lines.push([{x: kontra.pointer.x, y: kontra.pointer.y}])
},
onOver: function () {
if (this.drawing) {
let line = this.lines[this.lines.length-1]
line.push({x: kontra.pointer.x, y: kontra.pointer.y})
}
},
onUp: function () {
this.drawing = false;
},
render: function (dt) {
let context = kontra.context
context.strokeStyle = this.color
context.lineWidth = this.thickness,
this.lines.forEach((line) => {
context.beginPath();
context.moveTo(line[0].x, line[0].y)
line.forEach((point) => {
context.lineTo(point.x, point.y)
})
// context.closePath()
context.stroke()
})
}
}
// Set up the GUI
let reset = function() {
sprites.forEach(s=>s.ttl=-1)
// Drawing
let s = kontra.sprite(sketch)
kontra.pointer.track(s)
sprites.push(s)
}
// Boilerplate gameloop
var loop = kontra.gameLoop({
update(dt) {
sprites.forEach(sprite => sprite.update(dt))
sprites = sprites.filter(sprite => sprite.isAlive());
},
render() {
sprites.forEach(sprite => sprite.render())
}
});
this.loop = loop
reset()
loop.start();