简体   繁体   中英

skew and animate image on canvas

I want to skew and animate image on a canvas. I can draw Image on canvas but how to skew and animate it after drawing? How to get reference of the image which is drawn on canvas?

I can draw image on canvas like this

var ctx = document.getElementById('canvas').getContext('2d');
ctx.drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight);

Here is a very rough sketch to get you started:

$(function () {
  var
    loaded = false,
    ctx = $('canvas')[0].getContext('2d'),
    img;

  img = $('<img>', {
    src: 'http://www.gravatar.com/avatar/e25f40f6711403073e7da6c33be21eb8?s=128&d=identicon&r=PG'
  }).on('load', function () {
    loaded = true;
  }).get(0);


  setInterval(function () {
    var f = 0;

    return function () {
      if (loaded) {
        ctx.clearRect(0, 0, 500, 500);

        ctx.save();
        ctx.setTransform (1, f, 0, 1, 0, 0);
        ctx.drawImage(img, 50, 50);
        ctx.restore();

        f += 0.01;

        if (f > 1) {
          f = 0;
        }
      }
    };
  }(), 16);
});

demo: http://jsfiddle.net/UHSKL/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM