-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
111 lines (97 loc) · 2.83 KB
/
index.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
// https://medium.com/dev-compendium/creating-a-bouncing-ball-animation-using-javascript-and-canvas-1076a09482e0
;(function () {
// global variables that will be loaded/initialized later
let canvas, ctx, gravity, ball, friction
// runs once at the beginning
// loads any data and kickstarts the loop
function init () {
// *load data here*
// our canvas variables
canvas = document.getElementById('index-screen')
ctx = canvas.getContext('2d')
// set the canvas size
canvas.width = 1200
canvas.height = 600
// world/scene settings
gravity = 0.25
friction = 0.98
// starting objects
ball = {
bounce: 0.995, // energy lost on bounce (25%)
radius: 10,
x: canvas.width / 2,
y: canvas.height / 2,
velX: (Math.random() * 15 + 5) * (Math.floor(Math.random() * 2) || -1),
velY: (Math.random() * 15 + 5) * (Math.floor(Math.random() * 2) || -1)
}
// begin update loop
window.requestAnimationFrame(update)
}
iter = 0;
// draws stuff to the screen
// allows us to separate calculations and drawing
function draw () {
// clear the canvas and redraw everything
//ctx.clearRect(0, 0, canvas.width, canvas.height)
// draw the ball (only object in this scene)
ctx.beginPath()
//ctx.fillStyle = 'blue'
R = Math.floor(iter) % 255;
G = Math.floor(ball.x) % 255;
B = Math.floor(ball.y) % 255;
ctx.fillStyle = 'rgb(' + R +', '+ G + ', ' + B + ')';
ctx.arc(
ball.x, ball.y,
ball.radius,
0, Math.PI * 2
)
ctx.fill()
}
// the main piece of the loop
// runs everything
function update () {
// queue the next update
if(iter < 7200)window.requestAnimationFrame(update)
// logic goes here
iter++;
// bottom bound / floor
if (ball.y + ball.radius >= canvas.height) {
ball.velY *= -ball.bounce
ball.y = canvas.height - ball.radius
ball.velX *= friction
}
// top bound / ceiling
if (ball.y - ball.radius <= 0) {
ball.velY *= -ball.bounce
ball.y = ball.radius
ball.velX *= friction
}
// left bound
if (ball.x - ball.radius <= 0) {
ball.velX *= -ball.bounce
ball.x = ball.radius
}
// right bound
if (ball.x + ball.radius >= canvas.width) {
ball.velX *= -ball.bounce
ball.x = canvas.width - ball.radius
}
// reset insignificant amounts to 0
if (ball.velX < 0.01 && ball.velX > -0.01) {
ball.velX = 0
}
if (ball.velY < 0.01 && ball.velY > -0.01) {
ball.velY = 0
}
// add gravity
ball.velY += gravity
// update ball position
ball.x += ball.velX
ball.y += ball.velY
//console.log(ball.x, ball.y);
// draw after logic/calculations
draw()
}
// start our code once the page has loaded
document.addEventListener('DOMContentLoaded', init)
})()