Я пытаюсь инициировать объект, падающий вниз по нажатию кнопки, но вместо того, чтобы плавно падать вниз, он падает немного при каждом нажатии кнопки.Чтобы нажать кнопку, просто щелкните там, где написано «перетаскивание», я не включил таблицу стилей, поэтому она не будет выглядеть как кнопка, но она все еще работает.Вопрос заключается в функции this.update
.Я хочу, чтобы блок выпал полностью одним нажатием кнопки.
let canvas = document.getElementById("canvas");
let dist = document.getElementById("Dist");
let m = document.getElementById("Mass");
let meter = 1;
let start = document.getElementById("start");
canvas.height = 800;
canvas.width = 900;
canvas.style.backgroundColor = "rgb(65, 163, 193)";
canvas.style.marginLeft = "500px";
canvas.style.marginTop = "75px";
let c = canvas.getContext('2d');
let x = 400;
let y = 200;
let h = 40;
let w = 20;
let xSpeed = 0;
let ySpeed = meter;
function Thing(x, y, xSpeed, ySpeed, h, w){
this.x = x;
this.y = y;
this.xSpeed = xSpeed;
this.ySpeed = ySpeed;
this.h = h;
this.w = w;
this.draw = function(){
c.beginPath();
c.rect(this.x, this.y, this.w, this.h);
c.fillStyle = "black";
c.fill();
c.stroke();
c.closePath();
};
this.update = function(){
start.addEventListener("click", () => {
if (this.y + this.h > canvas.height){
this.y -= this.ySpeed;
};
this.y += this.ySpeed;
this.x += this.xSpeed;
this.draw();
});
this.draw()
};
};
let obj;
function init(){
obj = new Thing(x, y, xSpeed, ySpeed, h, w,);
anim();
};
function anim(){
requestAnimationFrame(anim);
c.clearRect(0, 0, canvas.width, canvas.height);
obj.update();
};
init();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Physics Drop</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<canvas id="canvas"></canvas>
<div id="X/Y title"></div>
<table>
<tc>
<tr>
<input id="Mass" placeholder="Mass">
</tr>
</tc>
<tc>
<tr>
<input id="Dist" placeholder="Height of drop">
</tr>
</tc>
</table>
<div id="start">
<div id="startT">Drop</div>
</div>
<script src="app.js"></script>
</body>
</html>