-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdojo2.js
More file actions
107 lines (87 loc) · 2.32 KB
/
dojo2.js
File metadata and controls
107 lines (87 loc) · 2.32 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
/*!
* Created by Canvas Dojo <https://github.com/znxkznxk1030/canvas-dojo>
*
* canvas-boilerplate by <https://github.com/christopher4lis/canvas-boilerplate>
* Learn more https://chriscourses.com/
*/
const canvas = document.querySelector("canvas");
const c = canvas.getContext("2d");
canvas.width = innerWidth;
canvas.height = innerHeight;
const mouse = {
x: innerWidth / 2,
y: innerHeight / 2,
};
const colors = ["#2185C5", "#7ECEFD", "#FFF6E5", "#FF7F66"];
// Event Listeners
addEventListener("mousemove", (event) => {
mouse.x = event.clientX;
mouse.y = event.clientY;
});
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.lineWidth = lineWidth;
this.color = color;
}
draw() {
c.beginPath();
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
c.fillStyle = this.color;
c.fill();
c.closePath();
}
update() {
this.draw();
}
}
// Implementation
let objects;
function init() {
objects = [];
// var cir = Object(100, 100, 100, "#000000");
// objects.push(cir);
// for (let i = 0; i < 40; i++) {
// x = randomIntFromRange(0, innerWidth);
// y = randomIntFromRange(0, innerHeight);
// }
// color = randomColor(colors);
// var o = Object(100, 100, 25, "#2185C5");
// objects.push(o);
}
// Animation Loop
function animate() {
requestAnimationFrame(animate);
c.clearRect(0, 0, canvas.width, 5, canvas.height);
c.fillText("HTML CANVAS BOILERPLATE", mouse.x, mouse.y);
// objects.forEach(object => {
// object.update()
// })
}
init();
animate();
/**
* utils.js - <https://github.com/christopher4lis/canvas-boilerplate/blob/master/src/js/utils.js>
* @function randomIntFromRange Picks a random integer within a range
* @function randomColor Picks a random color
* @function dispatch Find the distance between two points
**/
function randomIntFromRange(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function randomColor(colors) {
return colors[Math.floor(Math.random() * colors.length)];
}
function distance(x1, y1, x2, y2) {
const xDist = x2 - x1;
const yDist = y2 - y1;
return Math.sqrt(Math.pow(xDist, 2) + Math.pow(yDist, 2));
}