JSFiddle - React, Tailwind, and code Playground
by Spsoi
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>
</body>
</html>
CSS
canvas {
border: 1px solid #000;
}
JavaScript
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;
});
function animate() {
requestAnimationFrame(animate);
c.fillStyle = '#1A1A23';
c.fillRect(0, 0, canvas.width, canvas.height);
if (mouse.x + 100 >= canvas.width / 2 - 50) { // !* обнаружение пересечение слева
console.log('Чпоньк');
}
// red
c.fillStyle = '#E86262';
c.fillRect(mouse.x, mouse.y, 100, 100);
// blue
c.fillStyle = '#92ABEA';
c.fillRect(canvas.width / 2 - 50, canvas.height / 2 - 50, 100, 100);
}
animate();