Приведенный ниже фрагмент кода показывает один функциональный объект с именем «Круг», который рисуется на элемент холста.Я знаю, как убрать визуальный аспект круга с экрана.Я могу просто изменить его непрозрачность с течением времени с помощью c.globalAlpha=0.0;
на основе event.listener
'или' столкновения объектов ', однако, если я визуально undraw сказал круг;он все еще существует и вычисляется, он все еще занимает ресурсы браузера, так как он незаметно подпрыгивает на моем элементе canvas.
Итак, мой вопрос: Каков наилучший способ удаления / удаления функционального объекта после его создания / отрисовки на холсте? => (чтобы он действительно удалялся и не был незаметно подпрыгивать вбраузер)
let canvas = document.getElementById('myCanvas');
let c = canvas.getContext('2d');
function Circle(x, y, arc, dx, dy, radius){
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
this.arc = arc;
this.cCnt = 0;
this.radius = radius;
this.draw = function() {
c.beginPath();
//context.arc(x,y,r,sAngle,eAngle,counterclockwise);
c.arc(this.x, this.y, this.radius, this.arc, Math.PI * 2, false); //
c.globalAlpha=1;
c.strokeStyle = 'pink';
c.stroke();
}
this.update = function() {
if (this.x + this.radius > canvas.width || this.x - this.radius < 0){
this.dx = -this.dx;
}
if (this.y + this.radius > canvas.height || this.y - this.radius < 0){
this.dy = -this.dy;
}
this.x += this.dx;
this.y += this.dy;
this.draw();
}
}
var circle = new Circle(2, 2, 0, 1, 1, 2); // x, y, arc, xVel, yVel, radius
function animate() {
requestAnimationFrame(animate);
c.clearRect(0, 0, canvas.width, canvas.height)
circle.update();
}
animate();
body {
background-color: black;
margin: 0;
}
<canvas id="myCanvas" width="200" height="60" style="background-color: white">