-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdot.js
68 lines (61 loc) · 1.45 KB
/
dot.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
'use strict'
function Dot() {
this.r = randomInt(5, 15)
this.halfR = this.r / 2
this.speed = randomInt(1, 3)
this.x = randomInt(this.halfR, width - this.halfR)
this.y = randomInt(this.halfR, height - this.halfR)
this.direction = random(['ne', 'nw', 'se', 'sw'])
}
Dot.prototype.update = function () {
switch (this.direction) {
case 'ne':
this.x += this.speed
this.y -= this.speed
break
case 'nw':
this.x -= this.speed
this.y -= this.speed
break
case 'se':
this.x += this.speed
this.y += this.speed
break
case 'sw':
this.x -= this.speed
this.y += this.speed
break
}
if (this.y + this.halfR >= height) {
if (this.direction === 'se') {
this.direction = 'ne'
} else if (this.direction === 'sw') {
this.direction = 'nw'
}
}
if (this.x + this.halfR >= width) {
if (this.direction === 'ne') {
this.direction = 'nw'
} else if (this.direction === 'se') {
this.direction = 'sw'
}
}
if (this.y - this.halfR <= 0) {
if (this.direction === 'nw') {
this.direction = 'sw'
} else if (this.direction === 'ne') {
this.direction = 'se'
}
}
if (this.x - this.halfR <= 0) {
if (this.direction === 'nw') {
this.direction = 'ne'
} else if (this.direction === 'sw') {
this.direction = 'se'
}
}
}
Dot.prototype.draw = function () {
fill(fillColor)
circle(this.x, this.y, this.r)
}