canvas图片水平镜像翻转动画实例页面

回到相关文章 »

效果:

点击图片查看动画效果

代码:

HTML代码:
<canvas width="256" height="192"></canvas>
                
JS代码:
<!-- 源自项目:https://github.com/zhangxinxu/tween -->
<script src="animation.js"></script>
<script>
// 截取自https://github.com/zhangxinxu/Tween/blob/master/tween.js
var Tween = {
    Quad: {
        easeInOut: function(t, b, c, d) {
            if ((t /= d / 2) < 1) return c / 2 * t * t + b;
            return -c / 2 * ((--t) * (t-2) - 1) + b;
        }
    }
};

// canvas 2d上下文获取
var canvas = document.querySelector('canvas');
var context = canvas.getContext('2d');

// 下面是一开始图片呈现的处理方法
var strImgUrl = 'mm1.jpg';
var eleImg = document.createElement('img');
eleImg.origin = '';
eleImg.onload = function () {
    context.drawImage(this, 0, 0);
};
eleImg.src = strImgUrl;

// 水平翻转动画
var loop = 1;
var flipX = function () {
    // 缓动执行API
    // Math.animation(form, to, duration, easing, callback);
    Math.animation(1 * loop, -1 * loop, 600, 'Quad.easeInOut', function (value, isEnding) {
        context.clearRect(0, 0, canvas.width, canvas.height);
        context.translate(canvas.width / 2 - canvas.width * value / 2, 0);
        context.scale(value, 1);
        context.drawImage(eleImg, 0, 0);
        // 坐标参考还原
        context.setTransform(1, 0, 0, 1, 0, 0);
        if (isEnding) {
            loop = -1 * loop;    
        }
    });
};

// 点击画布水平动画翻转
canvas.addEventListener('click', flipX);
</script>