-
Notifications
You must be signed in to change notification settings - Fork 0
/
Style.js
161 lines (116 loc) · 2.61 KB
/
Style.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
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
window.onload = () =>{
let started = false
const canvas = document.querySelector('canvas')
const c = canvas.getContext('2d')
canvas.width = innerWidth
canvas.height = innerHeight
const mouse = {
x: 50 ,
y: 100
}
const colors = ['red', 'yellow', "blue", "green"]
// Event Listeners
canvas.addEventListener('touchmove', (event) => {
mouse.x = event.touches[0].clientX
mouse.y = event.touches[0].clientY
// console.log(mouse.x , mouse.y)
})
canvas.addEventListener('touchstart', (event) => {
mouse.x = event.touches[0].clientX
mouse.y = event.touches[0].clientY
started = true
// console.log(mouse.x , mouse.y)
})
canvas.addEventListener('touchend', (event) => {
mouse.x = undefined
mouse.y = undefined
// console.log(mouse.x , mouse.y)
})
addEventListener('resize', () => {
canvas.width = innerWidth
canvas.height = innerHeight
init()
})
// Objects
class Object {
constructor(x, y, radius, color) {
this.x = x
this.y = y
this.radius = radius
this.color = color
this.width = 0
this.height = 0
this.img = b1
}
draw() {
c.beginPath()
c.drawImage(this.img, this.x, this.y, this.width, this.height)
c.closePath()
}
update() {
this.draw()
let dy = this.x - mouse.x
let dx = this.y - mouse.y;
let dist = Math.hypot(dx,dy)
if(dist < 100 && this.height < 60){
this.height++;
this.width++;
}
if(dist > 100 && this.height > 0 ){
this.height--;
this.width--;
}
}
}
// Implementation
let objects = []
function init() {
objects = []
for (let i = 0; i < 300; i++) {
let x = Math.random() * (canvas.width - 60);
let y = Math.random() * (canvas.height - 60);
let color = colors[Math.floor(Math.random() * colors.length )]
let r = 1;
objects.push(new Object (x, y, r, color))
}
}
init()
// Animation Loop
function animate() {
requestAnimationFrame(animate)
c.clearRect(0, 0, canvas.width, canvas.height)
if(!started){
cross();
}
objects.forEach(object => {
object.update()
if(mouse.x == undefined && object.height > 0){
object.height--;
object.width--;
}
})
}
let x = 0;
let y = 0;
function cross(){
if(mouse.x == 50 && mouse.y == 100){
x = 5;
y = 0
}
if(mouse.x == 375 && mouse.y == 100){
x = -5;
y = 5;
}
if(mouse.x == 50 && mouse.y == 425){
y = 0
x = 5
}
if(mouse.x == 375 && mouse.y == 425){
y = -5;
x = -5;
}
mouse.x += x;
mouse.y += y
}
animate()
}