온라인 튜토리얼에 기반한 캔버스 요소를 뒤범벅하고 다음 페이지 인 here을 생성했습니다. 여기 Canvas 'fps가 의도하지 않게 속도가 빨라 졌습니까?
내 마크 업입니다 :<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Canvas Game</title>
<link rel="stylesheet" href="style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script src="script.js"></script>
</head>
<body>
<header>
<h1>Cool Canvas Bouncing Effect!</h1>
<p>Which would you like to see bounce around?</p>
<input id="beachBallButton" type="button" value="Beach Ball" />
<input id="avatarButton" type="button" value="Avatar" />
</header>
<br />
<canvas id="myCanvas" width="600" height="400">
Your browser does not support canvas!
</canvas>
</body>
</html>
그리고 여기 내 자바 스크립트입니다 :
$(document).ready(function() {
const FPS;
var x = 0;
var y = 0;
var xDirection = 1;
var yDirection = 1;
var image = new Image();
image.src = null;
var canvas = document.getElementById('myCanvas');
var ctx = canvas.getContext('2d');
$('#avatarButton').click(function() {
x = 0;
y = 0;
FPS = 30;
image.src = 'avatar.png';
setInterval(draw, 1000/FPS);
function draw() {
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.fillRect(0,0,canvas.width,canvas.height);
ctx.drawImage(image, x, y);
x += 1 * xDirection;
y += 1 * yDirection;
if (x >= 500)
{
x = 500;
xDirection = -1;
}
else if (x <= 0)
{
x = 0;
xDirection = 1;
}
if (y >= 300)
{
y = 300;
yDirection = -1;
}
else if (y <= 0)
{
y = 0;
yDirection = 1;
}
}
});
$('#beachBallButton').click(function() {
x = 0;
y = 0;
FPS = 60;
image.src = 'beachball.png';
setInterval(draw, 1000/FPS);
function draw() {
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.fillRect(0,0,canvas.width,canvas.height);
ctx.drawImage(image, x, y);
x += 5 * xDirection;
y += 5 * yDirection;
if (x >= 450)
{
x = 450;
xDirection = -1;
}
else if (x <= 0)
{
x = 0;
xDirection = 1;
}
if (y >= 250)
{
y = 250;
yDirection = -1;
}
else if (y <= 0)
{
y = 0;
yDirection = 1;
}
}
});
});
이제, 당신은 Avatar
버튼을 클릭 한 다음 나중에 Beach Ball
클릭 말의 FPS입니다 속도가 빨라 졌어. 그러나 두 함수의 click
함수 내에서 FPS 변수를 재설정했습니다. 또한 x
및 y
개의 변수를 재설정했습니다.
또한 동일한 버튼을 계속 클릭하면 속도가 크게 향상됩니다.
왜 이런 일이 발생했는지 설명해 줄 수 있습니까?
감사합니다.
와우, (취소 아무것도 없다 처음 참고). 튜토리얼 온라인에서이 문제를 수정하여 이전에는 문제가되지 않았습니다. xD Thanks! – Qcom