-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvas.js
More file actions
171 lines (138 loc) · 3.39 KB
/
Copy pathcanvas.js
File metadata and controls
171 lines (138 loc) · 3.39 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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
var canvas = document.getElementById("canvas")
var ctx = canvas.getContext("2d")
var width = 0
var height = 0
var iBubbles = 10
var aBubblesX = []
var aBubblesY = []
var aBubblesSize = []
var aBubblesSpeed = []
var aZeroOrOne = []
var random = (min, max) => {
if(typeof max === 'undefined') {
return Math.random() * (min[1] - min[0]) + min[0]
} else {
return Math.random() * (max - min) + min
}
}
class NumberElementConfig {
constructor(values, xMinMax, yMinMax, sizeMinMax, speedMinMax) {
this.values = values
this.x = xMinMax
this.y = yMinMax
this.size = sizeMinMax
this.speed = speedMinMax
}
}
class NumberElement {
constructor(numberElementConfig) {
this.config = numberElementConfig
this.spawn()
}
spawn() {
this.value = this.config.values[Math.floor(random(0, 2))]
this.size = random(this.config.size)
this.speed = random(this.config.speed)
this.x = random(this.config.x)
this.y = random(this.config.y)
this.viewportOffset = this.config.size[1] + 20
if(Math.abs(this.speed) < 1) {
this.speed += (this.speed < 0 ? -1 : 1)
}
this.setSpawnPosition()
}
respawn() {
this.spawn()
}
setSpawnPosition() {
if(this.speed < 0) {
let key = ['x', 'y'][Math.floor(random(0, 2))]
let value = key === 'x' ? innerWidth : innerHeight
this[key] = value + this.size
} else {
let key = ['x', 'y'][Math.floor(random(0, 2))]
this[key] = 0 - this.size
}
}
isInViewport() {
if(
this.x < 0 - this.viewportOffset ||
this.y < 0 - this.viewportOffset ||
this.x > innerWidth + this.viewportOffset ||
this.y > innerHeight + this.viewportOffset
) {
return false
} else {
return true
}
}
move() {
this.x += this.speed
this.y += this.speed
}
}
class NumberElementCollector {
constructor(count = 100, spawnTimeout = 20) {
this.numberElements = []
let generator = () => {
if(count != this.numberElements.length) {
this.numberElements.push(
this.generateNumberElement()
)
setTimeout(generator, spawnTimeout)
}
}
generator()
}
get() {
return this.numberElements
}
moveAll() {
for (let i = 0; i < this.numberElements.length; ++i) {
let element = this.numberElements[i]
element.move()
if(!element.isInViewport()) {
element.respawn()
}
}
}
generateNumberElement() {
return new NumberElement(
new NumberElementConfig(
['0', '1'],
[0, innerWidth], // x
[0, innerHeight], // y
[15, 50], // size
[-12, 12] // speed
)
)
}
}
var numberElementCollector = new NumberElementCollector()
var draw = () => {
ctx.beginPath()
ctx.fillStyle = "rgba(0, 0, 0, 0.1)"
for (let numberElement of numberElementCollector.get()) {
ctx.font = numberElement.size / 1.5 + 'px arial black'
ctx.textBaseline = 'bottom'
ctx.textAlign = 'left'
ctx.fillText(
numberElement.value,
numberElement.x,
numberElement.y
)
}
ctx.fillStyle = "rgba(0, 0, 0, 0.07)"
ctx.fill()
ctx.lineWidth = 1
ctx.strokeStyle = "rgba(0, 0, 0, 0.08)"
ctx.stroke()
}
var process = () => {
width = canvas.width = innerWidth
height = canvas.height = innerHeight
numberElementCollector.moveAll()
draw()
requestAnimationFrame(process)
}
process()