This content originally appeared on DEV Community and was authored by Spsoi
Читать снизу вверх
Статья будет дополнятся
//!* новые строки
Отрисуем второй шарик прям.
let circle1 ;
let circle2 ; //!*
function init() { // точка входа
//1
circle1 = new Circle(300, 300, 100, 'black');
circle2 = new Circle(undefined, undefined, 30, 'red'); //!*
}
function animate() {
requestAnimationFrame(animate); // каждую секунду отрисовываем
c.clearRect(0,0, canvas.width, canvas.height); // удаляем предыдуший отрисованный кадр
circle1.update();
circle2.x = mouse.x; //!*
circle2.y = mouse.y; //!*
circle2.update(); //!*
}
Отрисуем шар в центре канваса
canvas = document.querySelector('#canvas');
let c = canvas.getContext('2d');
canvas.width = innerWidth;
canvas.height = innerHeight;
let mouse = {
x: innerWidth / 2,
y: innerHeight / 2
}
let colors = [
'#2185c5',
'#7ECEFD',
'#FFF6E5',
'#FF7F66'
];
document.addEventListener("mousemove", function(event){
mouse.x = event.clientX;
mouse.y = event.clientY;
});
addEventListener("resize", function(){ // при изменении окна растягиваем окно canvas
canvas.width = innerWidth;
canvas.height = innerHeight;
// init();
});
function randomIntFromRange(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function randomColor (color){
return color[Math.floor(Math.random() * color.length)];
}
addEventListener("click", function () { // клик по экрану сбрасывает анимацию
init();
})
function Circle (x, y, radius, color) { // шарик
// 2
this.x =x;
this.y = y;
this.radius = radius;
this.color = color;
this.update = function() {
this.draw();
}
this.draw = function () {
c.beginPath();
c.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
c.fillStyle = this.color;
c.fill();
c.stroke();
c.closePath();
}
}
let ballArray;
let circle1 ;
function init() { // точка входа
//1
circle1 = new Circle(300, 300, 100, 'black');
}
function animate() {
requestAnimationFrame(animate); // каждую секунду отрисовываем
c.clearRect(0,0, canvas.width, canvas.height); // удаляем предыдуший отрисованный кадр
circle1.update();
}
init();
animate();
style.css
canvas {
border: 1px solid #000;
}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<canvas id="canvas" width="300" height="300"></canvas>
<script src="canvas.js" ></script>
</body>
</html>
This content originally appeared on DEV Community and was authored by Spsoi
Print
Share
Comment
Cite
Upload
Translate
Updates
There are no updates yet.
Click the Upload button above to add an update.
APA
MLA
Spsoi | Sciencx (2022-02-10T07:47:08+00:00) HTML5 Javascript Canvas Collision. Retrieved from https://www.scien.cx/2022/02/10/html5-javascript-canvas-collision/
" » HTML5 Javascript Canvas Collision." Spsoi | Sciencx - Thursday February 10, 2022, https://www.scien.cx/2022/02/10/html5-javascript-canvas-collision/
HARVARDSpsoi | Sciencx Thursday February 10, 2022 » HTML5 Javascript Canvas Collision., viewed ,<https://www.scien.cx/2022/02/10/html5-javascript-canvas-collision/>
VANCOUVERSpsoi | Sciencx - » HTML5 Javascript Canvas Collision. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/10/html5-javascript-canvas-collision/
CHICAGO" » HTML5 Javascript Canvas Collision." Spsoi | Sciencx - Accessed . https://www.scien.cx/2022/02/10/html5-javascript-canvas-collision/
IEEE" » HTML5 Javascript Canvas Collision." Spsoi | Sciencx [Online]. Available: https://www.scien.cx/2022/02/10/html5-javascript-canvas-collision/. [Accessed: ]
rf:citation » HTML5 Javascript Canvas Collision | Spsoi | Sciencx | https://www.scien.cx/2022/02/10/html5-javascript-canvas-collision/ |
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.