javascript - Canvas 多个弹跳方 block - Clearing Canvas

标签 javascript html canvas

我使用 Canvas 在 javascript 中创建了一个小弹跳框演示。每个框(需要一个更好的词)类处理它自己的更新和渲染。但是,我的问题在于在绘制下一帧之前清除 Canvas 。当盒子移动时它可以很好地清除,但在它撞到墙上后会留下人工制品。这是一把 fiddle - 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); 有关,但我可以不知道我做错了什么。

最佳答案

您对每个盒子进行清理有点奇怪。通常,您会在每一帧上删除整个 canvas:https://jsfiddle.net/yb58fd47/

为什么会这样

您的 update 函数检查碰撞,这反过来又可以反转速度分量。但是,您的 render 函数依赖于该速度组件来准确表示最后的速度。如果你不想清除整个 Canvas ,你可以跟踪盒子之前的实际速度(我添加了velocityXLastvelocityYLa​​st):

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>

关于javascript - Canvas 多个弹跳方 block - Clearing Canvas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34265496/

相关文章:

javascript - AJAX Autoreload 而不是 onclick,怎么样?

javascript - 如何在 Javascript 中排序日期时间字符串?

jquery - 使图像在 div 内可拖动并可调整大小

javascript点击改变背景颜色

javascript - Fabric.Js - 使已添加到 Canvas 的文本可编辑

javascript - ERR_CONNECTION_TIMED_OUT 与 google apis 字体

javascript - 从动态选择框获取值

html - Chrome CSS Perspective 不适用于所有 block

javascript - jquery,调整 Canvas 大小而不缩放

javascript - Canvas 上下文的大小不正确