1. 程式人生 > 其它 >Canvas 圖片繞邊旋轉的小動畫

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