javascript - Creating a bouncing box animation on canvas -
javascript - Creating a bouncing box animation on canvas -
i need create animation of dropping box, supposed bounce 10 times when reaches y point on canvas, each time twice lower previous. have animation of dropping box, can't create bounce work. here functions wrote:
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 = 0; < 10; i++){ if(direction=="up"){ //going 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 downwards 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/
why isn't working , how can create work?
to avoid getting headache, you've improve handle animation within single function called setinterval. , maintain animation-related info in 1 object. code below not want, should started :
http://jsfiddle.net/n2derqgw/4/
setup :
var canvas_overlay, ctx_overlay, docwidth, docheight; var img_box = new image(); img_box.src = 'http://corkeynet.com/test/images/gift_box_small.png'; var mustbereadycount = 2; // must load image , window img_box.onload = launchwhenready; window.onload = launchwhenready; var animationstep = ''; var boxanimationdata = { animationstep: '', y: 0, maxy: 0, bouncecount: 6, direction: -1, bounceheight: 0 }; function launchwhenready() { mustbereadycount--; if (mustbereadycount) return; docwidth = window.innerwidth; docheight = window.innerheight; canvas_overlay = document.getelementbyid('canvas_overlay'); ctx_overlay = canvas_overlay.getcontext('2d'); resizecanvas(docwidth, docheight); boxanimationdata.animationstep = 'falling'; boxanimationdata.bounceheight = docheight / 2 - img_box.height; setinterval(animatebox, 30); };
more interesting code here :
function animatebox() { if (boxanimationdata.animationstep == 'falling') dropbox(); else if (boxanimationdata.animationstep == 'bouncing') bouncebox(); } function dropbox() { ctx_overlay.clearrect(0, 0, docwidth, docheight); boxanimationdata.y += 3; if (boxanimationdata.y + img_box.height > docheight) { boxanimationdata.animationstep = 'bouncing'; } ctx_overlay.drawimage(img_box, (docwidth / 2) - (img_box.width / 2), boxanimationdata.y); } function bouncebox() { ctx_overlay.clearrect(0, 0, docwidth, docheight); boxanimationdata.y += boxanimationdata.direction * 3; if (boxanimationdata.y + img_box.height > docheight) { // reached floor ? swap direction boxanimationdata.direction *= -1; // , cut down jump height boxanimationdata.bounceheight *= 3 / 2; boxanimationdata.bouncecount--; if (!boxanimationdata.bouncecount) boxanimationdata.animationstep = ''; } else if (boxanimationdata.y < boxanimationdata.bounceheight) { boxanimationdata.direction *= -1; } ctx_overlay.drawimage(img_box, (docwidth / 2) - (img_box.width / 2), boxanimationdata.y); }
javascript animation canvas html5-canvas
Comments
Post a Comment