HTML5 Canvas在绘图上闪烁

时间:2013-06-23 13:13:21

标签: javascript html5 canvas refresh blink

我从一个等距游戏开始,当绘制地面的所有部分时,我的画布闪烁(不在IE中)。当我将fps设置为20或更低时,闪烁停止。我怎么解决这个问题?有什么想法吗?

var camerax = 300, cameray = 100;
var fps = 60;

function draw() {
    clearCanvas();
    drawGround();
}

function drawGround() {
    var img = new Image();
    img.onload = function() {
    var width = img.width;
    var height = img.height;
    for (var x = 0; x < 3; x++) {
        for (var y = 3; y >= 0; y--) {
                mx = (x-y)*height + camerax;
                my = (x+y)*height/2 + cameray; 
                ctx.drawImage(img, mx, my);
             }
        }
    }
    img.src = "ground.png";
}

var loop = setInterval(function() {
    update();
    draw();
}, 1000/fps);

2 个答案:

答案 0 :(得分:5)

现在你正在每帧重新加载图像,除非onload回调在帧的16ms内触发,否则你将看到一个空白的画布。

您只需拨打new Imageimg.onload序列一次即可预先加载图片。 onload回调将启动您的第一帧,并且绘制调用可以在内存中自由使用该图像。

类似的东西:

var camerax = 300, cameray = 100;
var fps = 60;
var img;
var loop;

function init() {
    img = new Image();
    img.onload = function() {
        loop = setInterval(function() {
            update();
            draw();
        }, 1000/fps);
    };
    img.src = "ground.png";
}

function draw() {
    clearCanvas();
    drawGround();
}

function drawGround() {
    var width = img.width;
    var height = img.height;
    for (var x = 0; x < 3; x++) {
        for (var y = 3; y >= 0; y--) {
                mx = (x-y)*height + camerax;
                my = (x+y)*height/2 + cameray; 
                ctx.drawImage(img, mx, my);
             }
        }
    }
}

当然,一旦等待多个图像预加载,它会变得更加复杂,因为只有在完成所有图像后才需要启动循环。

答案 1 :(得分:1)

很好的提示,免费!谢谢!我的屏幕现在没有闪烁,代码结果是:

        var canvas = document.getElementById("game");
        var ctx = canvas.getContext("2d");

        var camerax = 300, cameray = 100;
        var fps = 60;
        var img;
        var loop;

        function update() {
        }

        function draw() {
            clearCanvas();
            drawGround();
        }

        function clearCanvas() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
        }

        function drawGround() {
            var width = img.width;
            var height = img.height;
            for (var x = 0; x < 3; x++) {
                for (var y = 3; y >= 0; y--) {
                    mx = (x-y)*height + camerax;
                    my = (x+y)*height/2 + cameray; 
                    ctx.drawImage(img, mx, my);
                }
            }
        }

        function init() {
            img = new Image();
            img.onload = function() {
                drawGround();
            };
            img.src = "ground.png";
        }

        function keyListener(e){
           e = e || window.event

           if(e.keyCode==37){
              camerax--;
           }
           else if(e.keyCode==39){
              camerax++;
           }
           else if(e.keyCode==38){
              cameray--;
           }
           else if(e.keyCode==40){
              cameray++;
           }                           
        }

        window.onkeypress = function(e) {
            keyListener(e);
        }

        init();
        var loop = setInterval(function() {
            update();
            draw();
        }, 1000/fps);