Canvas 图片绕边旋转的小动画

/**
 * 图片绕边旋转的小动画
 */
function initDemo10() {
    var canvas = document.getElementById("demo10");
    if (!canvas) {
        return;
    }
    var context = canvas.getContext("2d");
    var img = new Image();
    img.src ="images/timg3.jpg";
    img.onload = function () {
        var i = 0;
        var i_symbol = 1;
        var j = 0;
        var j_symbol = 1;
        setInterval(function () {
            context.clearRect(0, 0, canvas.width, canvas.height);
            if (i <= 0 && j <= 0){
                i_symbol = 1;
                j_symbol = 0;
            }else if (i >= (canvas.width - img.width) && j <= 0){
                i_symbol = 0;
                j_symbol = 1;
            }else if (i >= (canvas.width - img.width) && j >= (canvas.height - img.height)){
                i_symbol = -1;
                j_symbol = 0;
            }else if (i <= 0 && j >= (canvas.height - img.height)){
                i_symbol = 0;
                j_symbol = -1;
            }
            i += i_symbol;
            j += j_symbol;
            context.drawImage(img, i, j);
        }, 10);
    }
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区