-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
232 lines (200 loc) · 7.48 KB
/
Copy pathindex.js
File metadata and controls
232 lines (200 loc) · 7.48 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import PIXI from "pixi.js";
import tween from "gsap";
import collide from "line-circle-collision";
import vec from "vectors";
import Tuna from "tunajs";
var renderer = new PIXI.WebGLRenderer(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.view);
const context = new AudioContext();
const tuna = new Tuna(context);
const compressor = new tuna.Compressor({delayTimeLeft: 100, delayTimeRight: 60});
const pingpong = new tuna.PingPongDelay();
const ballRadius = 20;
const gravity = .07;
const bounce = -1.2;
const wallBounce = 0.9;
const blue = 0x00ddff;
const pink = 0xff00ff;
const darkGray = 0x333333;
const green = 0x44ff44;
const gold = 0xffdd00;
let opacity = 0.3;
let previousTargetPoint = {x: 200, y: 200};
let targetPoint = {x: 300, y: 300};
let linePoints = [];
let collision = false;
compressor.connect(pingpong.input);
pingpong.connect(context.destination);
var stage = new PIXI.Container();
stage.width = renderer.width;
stage.height = renderer.height;
let bg = new PIXI.Graphics();
bg.interactive = true;
bg.on("mousedown", e => {
tween.killTweensOf(targetPoint);
previousTargetPoint.x = e.data.global.x;
previousTargetPoint.y = e.data.global.y;
targetPoint.x = e.data.global.x;
targetPoint.y = e.data.global.y;
let point;
if (linePoints.length < 2) {
point = new PIXI.Graphics();
linePoints.push(point);
stage.addChild(point);
} else {
point = linePoints.shift();
linePoints.push(point);
}
point.click = {x: targetPoint.x, y: targetPoint.y};
});
bg.on("mousemove", e => {
targetPoint.x = e.data.global.x;
targetPoint.y = e.data.global.y;
})
bg.beginFill(darkGray, opacity);
bg.drawRect(0, 0, renderer.width, renderer.height);
bg.endFill();
stage.addChild(bg);
let ball = new PIXI.Graphics();
stage.addChild(ball);
ball.direction = {x: 1, y: 3};
ball.currentPosition = {x: 100, y: 100};
let wall = new PIXI.Graphics();
stage.addChild(wall);
let view = new PIXI.Container();
view.addChild(stage);
animate();
function detectCollision() {
let circle = [ball.currentPosition.x, ball.currentPosition.y];
let radius = ballRadius;
let a = [linePoints[0].click.x, linePoints[0].click.y];
let b = [linePoints[1].click.x, linePoints[1].click.y];
return collide(a, b, circle, radius);
}
function detectWallCollision() {
let collider = false;
if (ball.currentPosition.x + ballRadius >= renderer.width) {
collider = "right";
} else if (ball.currentPosition.x - ballRadius <= 0) {
collider = "left";
} else if (ball.currentPosition.y + ballRadius >= renderer.height) {
collider = "bottom";
} else if (ball.currentPosition.y - ballRadius <= 0) {
collider = "top";
}
return collider;
}
function animate() {
// start the timer for the next animation loop
requestAnimationFrame(animate);
linePoints.forEach((point, i) => {
point.clear();
if (i === 2) return;
point.beginFill(blue, opacity);
point.drawCircle(point.click.x, point.click.y, 30);
point.endFill();
});
wall.clear();
wall.lineStyle(4, pink, opacity);
linePoints.forEach((point, i, arr) => {
if (i === 1) {
collision = detectCollision(point, arr[i - 1]);
if (collision) {
opacity = 1;
wall.lineTo(point.click.x, point.click.y);
wall.lineStyle(4, green, opacity);
wall.lineTo(targetPoint.x, targetPoint.y);
let lineAngle;
if (linePoints[0].click.x > linePoints[1].click.x) {
lineAngle = vec.heading(2)(
[linePoints[0].click.x, linePoints[0].click.y],
[linePoints[1].click.x, linePoints[1].click.y]
);
} else {
lineAngle = vec.heading(2)(
[linePoints[1].click.x, linePoints[1].click.y],
[linePoints[0].click.x, linePoints[0].click.y]
);
}
let cos = Math.cos(lineAngle);
let sin = Math.sin(lineAngle);
let x1 = ball.currentPosition.x - linePoints[0].click.x;
let y1 = ball.currentPosition.y - linePoints[0].click.y;
let x2 = cos * x1 + sin * y1;
let y2 = cos * y1 - sin * x1;
let vx1 = cos * ball.direction.x + sin * ball.direction.y;
let vy1 = cos * ball.direction.y - sin * ball.direction.x;
y2 = -ballRadius;
vy1 *= bounce;
x1 = cos * x2 - sin * y2;
y1 = cos * y2 + sin * x2;
ball.direction.x = cos * vx1 - sin * vy1;
ball.direction.y = cos * vy1 + sin * vx1;
ball.currentPosition.x = linePoints[0].click.x + x1;
ball.currentPosition.y = linePoints[0].click.y + y1;
collision = false;
//play sound
let osc = context.createOscillator();
let gain = context.createGain();
osc.frequency.value = 440;
osc.connect(gain);
gain.connect(compressor.input);
gain.gain.linearRampToValueAtTime(1, context.currentTime + 0.001);
gain.gain.linearRampToValueAtTime(0, context.currentTime + 0.04);
osc.start(context.currentTime);
osc.stop(context.currentTime + 0.05);
} else {
wall.lineTo(point.click.x, point.click.y);
wall.lineStyle(4, green, opacity);
wall.lineTo(targetPoint.x, targetPoint.y);
}
} else if (i === 0) {
wall.moveTo(point.click.x, point.click.y);
}
});
ball.clear();
ball.direction.y += gravity;
ball.currentPosition.x += ball.direction.x;
ball.currentPosition.y += ball.direction.y;
ball.beginFill(gold, opacity);
ball.drawCircle(ball.currentPosition.x, ball.currentPosition.y, ballRadius);
ball.endFill();
let wallCollision = detectWallCollision();
if(wallCollision) {
opacity = 1;
//play sound
let osc = context.createOscillator();
let gain = context.createGain();
osc.frequency.value = 660;
osc.connect(gain);
gain.connect(compressor.input);
gain.gain.linearRampToValueAtTime(1, context.currentTime + 0.001);
gain.gain.linearRampToValueAtTime(0, context.currentTime + 0.04);
osc.start(context.currentTime);
osc.stop(context.currentTime + 0.05);
switch(wallCollision) {
case "right":
ball.currentPosition.x = renderer.width - ballRadius;
ball.direction.x = -ball.direction.x * wallBounce;
break;
case "left":
ball.currentPosition.x = ballRadius;
ball.direction.x = -1 * ball.direction.x * wallBounce;
break;
case "top":
ball.currentPosition.y = ballRadius;
ball.direction.y = -1 * ball.direction.y * wallBounce;
break;
case "bottom":
ball.currentPosition.y = renderer.height - ballRadius;
ball.direction.y = -ball.direction.y * wallBounce;
break;
}
}
opacity = Math.max(0.3, opacity * 0.9);
bg.clear();
bg.beginFill(darkGray, opacity * 0.2);
bg.drawRect(0, 0, renderer.width, renderer.height);
bg.endFill();
renderer.render(view);
}