简体   繁体   English

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

[英]Canvas multiple bouncing squares - Clearing Canvas

I've created a little bouncing boxes demo in javascript using the canvas. 我已经使用画布在javascript中创建了一个小弹跳框演示。 Each box (for want of a better word) class handles it's own updates and rendering. 每个框(为了更好的单词)类处理它自己的更新和渲染。 However, my problem lies in clearing the canvas before drawing the next frame. 但是,我的问题在于在绘制下一帧之前清除画布。 It clears fine when the boxes are moving but leaves artifacts behind after it hits a wall. 当盒子移动时它会清除,但是当它撞到墙壁后会留下伪影。 Here's a fiddle - https://jsfiddle.net/dzuvL7ph/1/ 这是一个小提琴 - https://jsfiddle.net/dzuvL7ph/1/

Here's my code; 这是我的代码;

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

I assume it's to do with this.context.clearRect(this.x - this.velocityX, this.y - this.velocityY, this.width, this.height); 我假设它与this.context.clearRect(this.x - this.velocityX, this.y - this.velocityY, this.width, this.height); but I can't figure out what I'm doing wrong. 但我无法弄清楚我做错了什么。

It's a little weird you're doing a cleanup for each box. 你为每个盒子做清理工作有点奇怪。 Normally, you'd wipe the whole canvas on each frame: https://jsfiddle.net/yb58fd47/ 通常,你会在每一帧上擦除整个canvashttps//jsfiddle.net/yb58fd47/

WHY it happens 为什么会这样

Your update function checks for collisions, which can in turn reverse the speed component(s). 您的update功能会检查冲突,从而可以反转速度组件。 But, your render function relies on that speed component to accurately represent the last speed. 但是, render功能依赖于该速度组件来准确表示最后的速度。 If you don't want to clear the whole canvas, you can keep track of the previous actual speed the box had (I added velocityXLast and velocityYLast ): 如果你不想清除整个画布,你可以跟踪盒子之前的实际速度(我添加了velocityXLastvelocityYLast ):

 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(); 
 canvas {border: 1px solid black} 
 <canvas id="canvas" width="400" height="150"></canvas> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM