자바 스크립트와 캔버스 태그를 사용하여 한 줄짜리 애니메이션을 만들고 싶습니다. 나는 어떤 문제도없이 그것을 할 수 있어요 :JS/캔버스 싱글 라인 애니메이션
만약 당신이 직선을하려고한다면 잘 작동합니다 - 저는 150px (x)에서 150px/20px (Y) ~ 150px (X)/200px (Y) - 모든 것이 멋지게 보입니다. 때문에 1 픽셀마다 추가하여 35px에 150 픽셀 (X)/20 픽셀 (Y)에서 예 (X)/200 픽셀 (Y) 여기 내 애니메이션 실패
을 -
문제점 라인이 오른쪽으로 이동 또는 왼쪽으로 인 X와 Y 모두 10ms로 설정하면 라인이 왼쪽 (35px)으로 먼저 이동 한 다음 거기에서 내 종점 Y로 이동합니다.
내 코드는입니다 (Firefox 또는 Opera가 필요합니다). 라인 히트가 곧 왼쪽으로 치고 그게 내 문제 야. :(
<html>
<script type="text/javascript" src="http://www.prototypejs.org/assets/2008/9/29/prototype-1.6.0.3.js"></script>
<style>
body {background: #fff; color: #ccc;}
</style>
<script type="text/javascript">
var startpointx = 150;
var startpointy = 25;
var curposx = 150;
var curposy = 25;
var myinterval;
function init() {
myinterval = setInterval(' animate(35, 250) ', 10);
}
function random (n)
{
return (Math.floor (Math.random() * n));
}
function animate(endpointx, endpointy) {
if (curposx == endpointx && curposy == endpointy){
clearInterval(myinterval);
drawShape(endpointx, endpointy);
return false;
} else {
if(curposx != endpointx){
if(endpointx > curposx) {
curposx = curposx + 1;
} else {
curposx = curposx - 1;
}
}
if(curposy <= endpointy){
curposy = curposy + 1;
}
}
drawShape(curposx, curposy, "#000");
}
function drawShape(tendpointx, tendpointy, clor){
var canvas = document.getElementById('cnvs');
var ctx = canvas.getContext('2d');
ctx.clearRect(0,0,310,400);
ctx.strokeStyle = clor;
ctx.beginPath();
ctx.moveTo(startpointx ,startpointy);
ctx.lineTo(tendpointx,tendpointy);
ctx.stroke();
}
//
init();
</script>
<body>
<canvas id="cnvs" width="310" height="400" style="border: 1px solid #ccc;"></canvas>
</body>
</html>
매력처럼 작동했습니다. 감사합니다. :) –