<!DOCTYPE html>
<html>
<style>
div.a {
font-size: 25px;
}
</style>
<head>
</head>
<body>
<canvas id="myCanvas" width="400" height="400" style="border:1px solid
#d3d3d3;">
</canvas>
<br>
<br>
<br>
<div id="circulation" class="a">
Круг циркуляции 1
</div>
<script>
let c = document.getElementById("myCanvas");
let ctx = c.getContext("2d");
ctx.clearRect(0,0,400,400);
ctx.beginPath();
for (let y = 0; y <= 400; y += 50) {
for (let x = 0; x <= 400; x += 50) {
ctx.moveTo(0, y);
ctx.lineTo(400, y);
ctx.moveTo(x, 0);
ctx.lineTo(x, 400);
}
ctx.stroke();
}
let X = 0;
let Y = 0;
let direction_1 = 1;
let direction_2 = 2;
let direction_3 = 3;
let direction_4 = 4;
let current_direction = 1;
let rectsize = 50;
let counter = 1;
let cr = 'rgb('+
Math.floor(Math.random()*256)+','+
Math.floor(Math.random()*256)+','+
Math.floor(Math.random()*256)+')';
setInterval(function() {
cr = 'rgb('+
Math.floor(Math.random()*256)+','+
Math.floor(Math.random()*256)+','+
Math.floor(Math.random()*256)+')';
if (current_direction == direction_1){
X = X + rectsize;
}
if (current_direction == direction_2){
Y = Y + rectsize;
}
if (current_direction == direction_3){
X = X - rectsize;
}
if (current_direction == direction_4){
Y = Y - rectsize;
}
if (current_direction == direction_1 && X >= 350){
X = 350;
current_direction = direction_2;
}
if (current_direction == direction_2 && Y >= 350){
Y = 350;
current_direction = direction_3;
}
if (current_direction == direction_3 && X <= 0){
X = 0;
current_direction = direction_4;
}
if (current_direction == direction_4 && Y <= 0){
Y = 0;
current_direction = direction_1;
counter = counter + 1;
let circulation = document.getElementById("circulation");
circulation.innerHTML = "Круг циркуляции "+ counter;
}
ctx.fillStyle = cr;
ctx.fillRect(X,Y,49,49);
}, 50);
</script>
</body>
</html>
|