【发布时间】:2015-12-15 06:26:28
【问题描述】:
所以我仍在使用 HTML Canvas 和 Javascript 编写弹跳框演示。到目前为止,逻辑按预期工作(好消息!) - 然而,当画布上有大量移动时,事情开始变得......像素化。这是一个小提琴来证明我的意思https://jsfiddle.net/hL8epzk3/2/
盒子的数量目前是 50(问题很明显)。将数字降低到 20,一切看起来都很好!
(小提琴中的第 67 行,var box = 20)
如果这是相关的,请在 Google Chrome
中进行测试我的代码
<!-- GAME WINDOW -->
<canvas id="canvas" width="800" height="600"></canvas>
<div id="menu">
<button onclick="addBox();">Add Box</button>
</div>
Javascript
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.possibleColors = ['#1abc9c', '#2ecc71', '#3498db', '#9b59b6', '#34495e', '#e67e22', '#c0392b', '#7f8c8d'];
var that = this;
this.update = function(){
this.x += this.velocityX;
this.y += this.velocityY;
this.collisionCheck();
};
this.render = function(){
this.context.fillStyle = this.color;
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;
this.generateColor();
}
if(this.x > 800 - this.width || this.x < 0){
this.velocityX *= -1;
this.generateColor();
}
};
this.generateColor = function(){
this.color = this.possibleColors[Math.floor((Math.random() * 10) - 1)];
};
function addBox(){
console.log('box added');
}
window.renderLayer.addObject(this);
};
var RenderLayer = function(){
this.objects = [];
this.addObject = function(obj){
this.objects[this.objects.length] = obj;
console.log(this.objects[this.objects.length - 1]);
};
this.updateObjects = function(){
for(var x = 0; x < this.objects.length; x++)
this.objects[x].update();
};
this.renderObjects = function(){
for(var x = 0; x < this.objects.length; x++)
this.objects[x].render();
};
};
function init(){
window.renderLayer = new RenderLayer();
window.box = [];
var boxes = 20;
for(var x = 0; x < boxes; x++){
window.box[x] = new Box(50, 'red', Math.floor((Math.random() * 750) + 1), Math.floor((Math.random() * 550) + 1));
}
requestAnimationFrame(update);
}
function update() {
document.getElementById('canvas').width = document.getElementById('canvas').width;
window.renderLayer.updateObjects();
requestAnimationFrame(update);
requestAnimationFrame(render);
}
function render() {
window.renderLayer.renderObjects();
}
function addBox(){
window.box[window.box.length] = new Box(50, 'red', Math.floor((Math.random() * 750) + 1), Math.floor((Math.random() * 550) + 1));
}
这是出于性能原因吗?有什么办法可以预防吗?当盒子重叠时似乎会发生这种情况,但很难说什么时候有这么多盒子。
【问题讨论】:
-
addBox 未定义
-
@RafałŁużyński 谢谢!修改了问题和代码/小提琴以否定该功能(不知道如何在 jsfiddle 上调用内联 js 语句)
-
它对我来说很好,是否有可能不是“像素化”你的意思是运动口吃?
-
@ericjbasti 奇怪!它肯定看起来像素化(质量较低)。但是,我现在想知道这是否是一种眼花缭乱,因为当我尝试在其上使用 sn-p 工具时,渲染暂停时,它看起来很好。嗯!
-
我能够获得超过 3000 个盒子而没有明显问题。我想知道您是否在浏览器和显示器之间遇到了 vsync 问题。您可以运行一些测试并查看结果testufo.com
标签: javascript html canvas drawing