jquery - Multiple image animation in javascript -
i learning , tackling hardest piece of javascript have tried date. want create multiple animated sliding images, 1 sliding left right , straight after sliding right left.
i have achieved 1 sliding image left right can not add image sliding right left.
<canvas id="canvas" width="1600" height="300"></canvas> <script> window.requestanimframe = (function(){ return window.requestanimationframe || window.webkitrequestanimationframe || window.mozrequestanimationframe || window.orequestanimationframe || window.msrequestanimationframe || function( callback ){ window.settimeout(callback, 1000 / 60); }; })(); var canvas = document.getelementbyid("canvas"), cx = canvas.getcontext("2d"); function card(x,y){ this.x = x || -300; this.y = y || -300; this.width = 500; this.height = 0; this.img=new image(); this.init=function(){ // makes mycard available in img.onload function // otherwise "this" inside img.onload refers img var self=this; this.img.onload = function() { // getting width , height of image self.width = this.width; self.height = this.height; self.draw(); loop(); } this.img.src = "f15ourbase.png"; } this.draw = function(){ cx.drawimage(this.img, this.x, this.y); } } var mycard = new card(50,50); mycard.init(); function loop(){ if((mycard.x + mycard.width) < canvas.width){ requestanimframe(loop); } else { // starting again after 5 seconds settimeout(function() { // resetting card old state mycard.x = 50; mycard.y = 50; // call loop again loop(); }, 20000); // 5000ms = 5 seconds, 20 seconds, use 20000 } cx.clearrect(0, 0, canvas.width, canvas.height); mycard.x = mycard.x + 15; mycard.draw(); } </script>
Comments
Post a Comment