-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJS_Demo.html
157 lines (128 loc) · 4.84 KB
/
JS_Demo.html
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
---
---
<!DOCTYPE html>
<html lang="en-us">
{% include head.html %}
<body>
{% include sidebar.html %}
<div class="content" style="position: absolute;">
<b>Settings:</b>
<br>
<input type="range" min="5" max="20" value="10" step="any" id="sizeInput">
<label for="sizeInput">Size</label>
<br>
<input type="range" min="1" max="6" value="3" step="any" id="speedInput">
<label for="speedInput">Speed</label>
<br>
<input type="color" value="#00AAFF" id="colourInput">
<label for="colourInput">Colour</label>
<br>
<button type="button" onclick="addBall()" name="addBallButton" class="ballButton">Add ball</button>
<br>
<button type="button" onclick="addTenBall()" name="addBallButton" class="ballButton">Add 10 balls</button>
<br>
<button type="button" onclick="resetBall()" name="resetButton" class="ballButton">Reset</button>
<br>
<span id="ballCount"></span>
</div>
<canvas id="myCanvas" width="10" height="10" style="top: 0px; left:0px;"></canvas>
<script>
// INITIALISATION
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var ballCount = document.getElementById('ballCount');
ballCount.innerHTML = "Number of balls: 0";
var drawFreq = 5; // milliseconds
var ballSize = parseFloat(sizeInput.value);
var ballColour = colourInput.value;
var ballSpeed = parseFloat(speedInput.value);
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
var ballArray = [];
class Ball {
constructor() {
this.x = Math.random() * canvas.width;
this.y = Math.random() * canvas.height;
let angle = Math.random() * 2 * Math.PI;
this.xAngle = Math.cos(angle);
this.yAngle = Math.sin(angle);
}
drawBall() {
let dx = ballSpeed * this.xAngle;
let dy = ballSpeed * this.yAngle;
if (this.x + dx + ballSize >= canvas.width) {
this.xAngle = -this.xAngle;
this.x = canvas.width - ballSize - 2;
}
if (this.x + dx - ballSize <= 0) {
this.xAngle = -this.xAngle;
this.x = ballSize + 2;
}
if (this.y + dy + ballSize >= canvas.height) {
this.yAngle = - this.yAngle;
this.y = canvas.height - ballSize - 2;
}
if (this.y + dy - ballSize <= 0) {
this.yAngle = -this.yAngle;
this.y = ballSize + 2;
}
dx = ballSpeed * this.xAngle;
dy = ballSpeed * this.yAngle;
this.x += dx;
this.y += dy;
for (let i = 2; i < 6; i++) {
ctx.beginPath();
let xoffset = dx * ballSize / 6;
let yoffset = dy * ballSize / 6;
ctx.arc(this.x - (4 * i * dx) - xoffset, this.y - (4 * i * dy) - yoffset, ballSize / (1.5 * i), 0, Math.PI * 2);
ctx.fillStyle = "#cccccc";
ctx.fill();
ctx.closePath();
}
// Draw ball
ctx.beginPath();
ctx.arc(this.x, this.y, ballSize, 0, Math.PI * 2);
ctx.fillStyle = ballColour;
ctx.fill();
ctx.closePath();
}
}
// USER INPUTS
window.onresize = function () {
canvas.height = window.innerHeight;
canvas.width = window.innerWidth;
}
sizeInput.oninput = function () {
ballSize = parseFloat(sizeInput.value);
}
colourInput.oninput = function () {
ballColour = colourInput.value;
}
speedInput.oninput = function () {
ballSpeed = parseFloat(speedInput.value);
}
// ADD BALL
function addBall() {
ballArray.push(new Ball());
ballCount.innerHTML = "Number of balls: " + String(ballArray.length);
// document.getElementById('ballCount').innerHTML = String(ballArray.length);
// String(ballArray.length)
}
function addTenBall() {
for (let i = 0; i < 10; i++) {
addBall();
}
}
// RELOAD PAGE
function resetBall() {
location.reload()
}
// MAIN LOOP
function main() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
ballArray.forEach(item => item.drawBall());
}
setInterval(main, drawFreq)
</script>
</body>
</html>