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);
    }
}

 

posted @ 2017-04-11 16:30  就只是小茗  阅读(588)  评论(0编辑  收藏  举报