javascript - Canvas Oscillating an image -
i'm trying image oscillate. i'm having issues. i'm using tutorial http://www.html5canvastutorials.com/advanced/html5-canvas-oscillation-animation/
and tried changing line 55-61 of tutorial load image src. it's not displaying anything.
any advice?
<canvas id="mycanvas" width="578" height="200"></canvas> <script> window.requestanimframe = (function(callback) { return window.requestanimationframe || window.webkitrequestanimationframe || window.mozrequestanimationframe || window.orequestanimationframe || window.msrequestanimationframe || function(callback) { window.settimeout(callback, 1000 / 60); }; })(); function drawrectangle(myrectangle, context) { context.beginpath(); context.rect(myrectangle.x, myrectangle.y, myrectangle.width, myrectangle.height); context.fillstyle = '#8ed6ff'; context.fill(); context.linewidth = myrectangle.borderwidth; context.strokestyle = 'black'; context.stroke(); } function animate(myrectangle, canvas, context, starttime) { // update var time = (new date()).gettime() - starttime; var amplitude = 150; // in ms var period = 2000; var centerx = canvas.width / 2 - myrectangle.width / 2; var nextx = amplitude * math.sin(time * 2 * math.pi / period) + centerx; myrectangle.x = nextx; // clear context.clearrect(0, 0, canvas.width, canvas.height); // draw drawrectangle(myrectangle, context); // request new frame requestanimframe(function() { animate(myrectangle, canvas, context, starttime); }); } var canvas = document.getelementbyid('mycanvas'); var context = canvas.getcontext('2d'); var myrectangle = new image(); myrectangle.src = "http://www.skilledsoldiers.com/e107_plugins/aacgc_gamelist/icons/dota2_icon.png"; myrectangle.onload = function() }; drawrectangle(myrectangle, context); // wait 1 second before starting animation settimeout(function() { var starttime = (new date()).gettime(); animate(myrectangle, canvas, context, starttime); }, 1000); </script>
you need use context.drawimage
instead of trying draw rectangle image.
function drawimage(myrectangle, context) { context.drawimage(myrectangle.img, myrectangle.x, myrectangle.y, myrectangle.width, myrectangle.height); }
see fiddle: http://jsfiddle.net/fb4vs/
Comments
Post a Comment