캔버스 위의 특정 Y 지점에 도달 할 때마다 바운스되는 것으로 가정되는 드롭 상자 상자의 애니메이션을 만들 필요가 있습니다. 그래서 나는 상자를 떨어 뜨리는 애니메이션을 가지고 있지만 바운스 작업을 할 수는 없습니다. 여기에 내가 쓴 기능은 다음과 같습니다캔버스에 바운싱 박스 애니메이션 만들기
function dropBox(y, width, height) {
var img_box = new Image();
img_box.src = 'images/gift_box_small.png';
var box_y_pos = y;
if(y==0)
box_y_pos = y-img_box.naturalHeight;
img_box.onload = function(){
ctx_overlay.save();
ctx_overlay.clearRect(0,0,width,height);
ctx_overlay.drawImage(img_box, (width/2)-(img_box.naturalWidth/2), box_y_pos);
ctx_overlay.restore();
}
box_y_pos += 3;
var box_bottom_position = box_y_pos - img_box.naturalHeight;
if(box_y_pos+img_box.naturalHeight<height-25)
var loopTimer = setTimeout(function() {dropBox(box_y_pos, width, height)},24);
else
bounceBox(img_box, box_y_pos, box_y_pos, (height/2)-(img_box.naturalHeight/2), "up");
}
function bounceBox(img, img_final_pos, y, midway_pos, direction){
var midway = midway_pos;
var direction = direction;
var img_y_pos = y;
img.onload = function(){
ctx_overlay.save();
ctx_overlay.clearRect(0,0,docWidth,docHeight);
ctx_overlay.drawImage(img, (docWidth/2)-(img.naturalWidth/2), img_y_pos);
ctx_overlay.restore();
}
for(var i = 0; i < 10; i++){
if(direction=="up"){
//going up
if(img_y_pos>midway_){
img_y_pos -= 3;
var loopTimer = setTimeout(function() {bounceBox(img, img_final_pos, img_y_pos, midway_pos, "up")},24);
} else {
img_y_pos += 3;
midway = Math.floor(midway /= 2);
if(midway%2>0)
midway += 1;
var loopTimer = setTimeout(function() {bounceBox(img, img_final_pos, img_y_pos, midway_pos, "down")},24);
}
} else {
//going down
if(img_y_pos < img_final_pos){
img_y_pos += 3;
var loopTimer = setTimeout(function() {bounceBox(img, img_final_pos, img_y_pos, midway_pos, "down")},24);
}
}
}
}
JSFiddle : http://jsfiddle.net/n2derqgw/3/
왜 작동하지 않으며 나는 그것이 작동하는 방법을 만들 수 있습니까?
다음이야 올바른 의사 소통을위한 의사 코드. 'If (Box_Y + Box_VY> = canvas_height) Box_VY * = - 1;'기본적으로 어떻게 할 것인가 (변수를 사용하지 않는 것)는 상자의 위치가 캔버스의 높이보다 큰 경우, 상자의 방향. 나는 애니메이션을 할 때 draw 함수 내에서 (청결 함을 위해서) 별도의 함수 호출로 단일 그리기 함수를 호출하는 단일'setInterval() '을 가지고있다. 모양을 그릴 때 나는 보통 속도 변수와 위치 변수를 가지고 있습니다. 예 :'Box_VY'. Box_VY가 Y보다 낮을 경우 – Brendan