forked from AustinCodingAcademy/101-canvas-practice-pt2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
84 lines (56 loc) · 2.23 KB
/
main.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
// define the canvas
let canvas = document.getElementById("my-canvas")
// console.log(canvas)
// adjust the canvas
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// capture the drawing context in a variable
const c = canvas.getContext('2d')
// TODO follow the instructions in the Canvas Pt. 2 Pre-Lesson to see how to build this project
let x = 500;
let y = 500;
let xVelocity = 8;
let yVelocity = 8;
let radius = 100;
const animate = () => {
const colors = [null, "#8C0C3C", "#1B2968", "#4B9C2B", "#A4C89C", "#F8605F", "#F8B493", "#32B9B2", "#F85532", "#C2C8E4", "#357153", "#A061D4", "#404462"]
const randomIndex = Math.floor(Math.random() * (13 - 1)) + 1
// use this special Window method to refresh the Window and call `animate` again, and again, and again...
requestAnimationFrame(animate)
// c.clearRect(0, 0, innerWidth, innerHeight)
c.beginPath();
c.strokeStyle = colors[randomIndex];
c.arc(x, y, radius, 0, 2 * Math.PI);
c.stroke();
// Conditional, if x is greater than innerWidth
if(x + radius > innerWidth || x - radius < 0) {
(xVelocity = -xVelocity)
}
if(y + radius > innerHeight || y - radius < 0) {
(yVelocity = -yVelocity)
}
// // Conditional, if x is greater than innerWidth
// if(y + radius > innerWidth || y - radius < 0) {
// yVelocity = -yVelocity
// }
y += yVelocity
x += xVelocity
}
animate()
// Code to draw new circles on a click of the window
// *************************************************
// window.onclick = () => {
// for(let i=0; i < 50; i++ ) {
// const x = Math.random() * (window.innerWidth - 100)
// const y = Math.random() * (window.innerHeight - 100)
// // the first value will be null to accommodate for no 0 number being drawn
// const colors = [null, "#8C0C3C", "#1B2968", "#4B9C2B", "#A4C89C", "#F8605F", "#F8B493", "#32B9B2", "#F85532", "#C2C8E4", "#357153", "#A061D4", "#404462"]
// const randomIndex = Math.floor(Math.random() * (13 - 1)) + 1;
// // Draw Circle
// c.beginPath();
// // replace "black" with the random color selected from the list
// c.strokeStyle = colors[randomIndex];
// c.arc(x, y, 50, 0, 2 * Math.PI);
// c.stroke();
// }
// }