画布多个弹跳正方形 - 清除画布

时间:2015-12-14 11:08:18

标签: javascript html canvas

我已经使用画布在javascript中创建了一个小小的弹跳框演示。每个框(为了缺少更好的单词)类处理它自己的更新和渲染。但是,我的问题在于在绘制下一帧之前清除画布。当盒子移动时它会清除,但是当它撞到墙壁后会留下伪影。这是一个小提琴 - https://jsfiddle.net/dzuvL7ph/1/

这是我的代码;

function init(){
    window.box1 = new Box(50, 'red', 0, 0);
    window.box2 = new Box(50, 'blue', 400, 20);
    requestAnimationFrame(update);
}

function update() {
    window.box1.update();
    window.box2.update();

    requestAnimationFrame(update);
    requestAnimationFrame(render);
}

function render() {
    window.box1.render();
    window.box2.render();
}

// -------------------------------------------------------------------------------------------------------
// --
// -------------------------------------------------------------------------------------------------------
var Box = function(dimensions, color, x, y){
    this.width = dimensions;
    this.height = dimensions;
    this.x = x;
    this.y = y;
    this.velocityX = 10;
    this.velocityY = 10;
    this.color = color;
    this.context = document.getElementById('canvas').getContext('2d');

    this.update = function(){
        this.x += this.velocityX;
        this.y += this.velocityY;
        this.collisionCheck();
    };
    this.render = function(){
        this.context.fillStyle = this.color;
        this.context.clearRect(this.x  - this.velocityX, this.y - this.velocityY, this.width, this.height);
        this.context.fillRect(this.x, this.y, this.width, this.height);
    };
    this.collisionCheck = function(){
        if(this.y > 600 - this.height || this.y < 0)
            this.velocityY *= -1;
        if(this.x > 800 - this.width || this.x < 0)
            this.velocityX *= -1;
    };
};

我认为这与this.context.clearRect(this.x - this.velocityX, this.y - this.velocityY, this.width, this.height);有关,但我无法弄清楚我做错了什么。

1 个答案:

答案 0 :(得分:4)

你为每个盒子做清理工作有点奇怪。通常情况下,您可以在每个框架上擦除整个canvashttps://jsfiddle.net/yb58fd47/

为什么会发生

您的update函数会检查是否存在冲突,从而可以反转速度组件。但是,您的render函数依赖于该速度组件来准确表示最后的速度。如果您不想清除整个画布,可以跟踪该框的先前实际速度(我添加了velocityXLastvelocityYLast):< / p>

&#13;
&#13;
function init(){
    window.box1 = new Box(50, 'red', 0, 0);
    window.box2 = new Box(50, 'blue', 120, 20);
    requestAnimationFrame(update);
}

function update() {
    window.box1.update();
    window.box2.update();
		
    requestAnimationFrame(update);
    requestAnimationFrame(render);
}

function render() {
    window.box1.render();
    window.box2.render();
}

// -------------------------------------------------------------------------------------------------------
// --
// -------------------------------------------------------------------------------------------------------
var Box = function(dimensions, color, x, y){
    this.width = dimensions;
    this.height = dimensions;
    this.x = x;
    this.y = y;
    this.velocityX = 10;
    this.velocityY = 10;
    this.color = color;
    this.context = document.getElementById('canvas').getContext('2d');

    this.update = function(){
        this.x += this.velocityX;
        this.y += this.velocityY;
        this.velocityXLast = this.velocityX;
        this.velocityYLast = this.velocityY;
        this.collisionCheck();
    };
    this.render = function(){
        this.context.fillStyle = this.color;
        this.context.clearRect(this.x  - this.velocityXLast, this.y - this.velocityYLast, this.width, this.height);
        this.context.fillRect(this.x, this.y, this.width, this.height);
    };
    this.collisionCheck = function(){
        if(this.y > 150 - this.height || this.y < 0)
            this.velocityY *= -1;
        if(this.x > 400 - this.width || this.x < 0)
            this.velocityX *= -1;
    };
};

init();
&#13;
canvas {border: 1px solid black}
&#13;
<canvas id="canvas" width="400" height="150"></canvas>
&#13;
&#13;
&#13;