javascript-画布多个弹跳方块-清除画布
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript-画布多个弹跳方块-清除画布,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3395字,纯文字阅读大概需要5分钟。
内容图文
我使用画布在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)有关;但我不知道自己在做什么错.
解决方法:
您正在为每个盒子进行清理有点奇怪.通常,您会在每一帧上擦整个画布:https://jsfiddle.net/yb58fd47/
为什么会发生
您的更新功能会检查碰撞,从而可以反转速度分量.但是,渲染功能依赖于该速度分量来准确表示最后的速度.如果您不想清除整个画布,则可以跟踪该框以前的实际速度(我添加了velocityXLast和velocityYLa??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-画布多个弹跳方块-清除画布全部内容,希望文章能够帮你解决javascript-画布多个弹跳方块-清除画布所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。