Html 5 网格绘制问题
//Set the number of rows and columns for the board
var rows = 10;
var columns = 10;
var offset= 0.5;
//Create the board, setting random squares to be obstacles
var board = [];
for (var x = 0; x < columns; x++)
{
board[x] = [];
for (var y = 0; y < rows; y++)
{
board[x][y] = 0;
}
}
function draw_grid(size, amount, ctx){
ctx.strokeStyle = "#FFF";
for (var i=0.5; i<size*amount+1; i+=size){
ctx.moveTo(0,i);
ctx.lineTo(size*amount, i);
ctx.moveTo(i,0);
ctx.lineTo(i, size*amount);
ctx.stroke();
}
}
function update_grid(grid, ctx){
ctx.fillStyle = "#000";
for (var i=0; i<grid.length; i+=1){
for (var a=0; a<grid[i].length; a+=1){
if (grid[i][a]==1){
ctx.fillRect((i*32)+offset, (a*32)+offset, 32, 32);
}
}
}
}
game_area = document.getElementById("a");
context = game_area.getContext('2d');
board[0][3] = 1;
function on_enter_frame(){
context.clearRect(0,0, game_area.width, game_area.height);
context.fillStyle = "#28F";
context.fillRect(0,0,500,500);
draw_grid(32, 10, context);
update_grid(board, context);
}
setInterval(on_enter_frame,30);
这段代码似乎占用了大量内存,有人知道为什么吗?刚刚开始在html5中编程...我相信这与update_grid函数中的for循环有关,帮助赞赏
//Set the number of rows and columns for the board
var rows = 10;
var columns = 10;
var offset= 0.5;
//Create the board, setting random squares to be obstacles
var board = [];
for (var x = 0; x < columns; x++)
{
board[x] = [];
for (var y = 0; y < rows; y++)
{
board[x][y] = 0;
}
}
function draw_grid(size, amount, ctx){
ctx.strokeStyle = "#FFF";
for (var i=0.5; i<size*amount+1; i+=size){
ctx.moveTo(0,i);
ctx.lineTo(size*amount, i);
ctx.moveTo(i,0);
ctx.lineTo(i, size*amount);
ctx.stroke();
}
}
function update_grid(grid, ctx){
ctx.fillStyle = "#000";
for (var i=0; i<grid.length; i+=1){
for (var a=0; a<grid[i].length; a+=1){
if (grid[i][a]==1){
ctx.fillRect((i*32)+offset, (a*32)+offset, 32, 32);
}
}
}
}
game_area = document.getElementById("a");
context = game_area.getContext('2d');
board[0][3] = 1;
function on_enter_frame(){
context.clearRect(0,0, game_area.width, game_area.height);
context.fillStyle = "#28F";
context.fillRect(0,0,500,500);
draw_grid(32, 10, context);
update_grid(board, context);
}
setInterval(on_enter_frame,30);
This code seems to be eating up a lot of memory, does anyone have any idea of why? Just started programming in html5... I believe that it has something to do with the for loops in the update_grid function, help apreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只是一个建议,您在循环中进行了大量计算,并且 setInterval 周期非常短 - 您需要如此频繁地更新网格吗?
在你的draw_grid()函数中,你计算了size*amount 3次,为什么不在循环外部作为局部变量执行一次并在需要的地方重用该变量呢?
进行绘画的另一种方法可能是使用 mozRequestAnimationFrame,它可能更如果您需要如此频繁地进行绘画,效率会很高。
Just a suggestion, you're doing a lot of calculations in your loops and your setInterval period is very short - do you need to update your grid so frequently?
In your draw_grid() function you're calculating size*amount 3 times, why not do it once at the outside the loop as a local variable and reuse that variable where you need it?
An alternative way of doing your painting may be to use mozRequestAnimationFrame, it may be more efficient if you need to do your painting so frequently.
这两个变量都是在不使用 var 关键字的情况下声明的...这样做很危险,并且可能会导致内存泄漏,具体取决于您如何使用它们。
这就是前进的方向:
另外,最好不要访问全局变量,因为 JavaScript VM 必须执行成本相当高的查找。更好的方法是这样做:
然后使用当前作用域中已存在的所需参数调用该方法:
Both those variables are declared without using the var keyword... dangerous to do that and can cause memory leaks depending upon how you're using them.
This is the way forward:
Also it is good practise not to access global variables because the JavaScript VM has to do a lookup that is quite costly. A better way would be to do this:
Then call that method with the required parameters that exist in the current scope already: