我想创建一个简单的画布网格,它将适合玩家的当前缩放级别,但也适用于某个画布高度/宽度成比例的屏幕限制。以下是我走到这一步:画布网格在不同的缩放级别变得模糊
JS:
var bw = window.innerWidth/2; //canvas size before padding
var bh = window.innerHeight/1.3; //canvas size before padding
//padding around grid, h and w
var pW = 30;
var pH = 2;
var lLimit = 0; //9 line limit for both height and width to create 8x8
//size of canvas - it will consist the padding around the grid from all sides + the grid itself. it's a total sum
var cw = bw + pW;
var ch = bh + pH;
var canvas = $('<canvas/>').attr({width: cw, height: ch}).appendTo('body');
var context = canvas.get(0).getContext("2d");
function drawBoard(){
for (var x = 0; lLimit <= 8; x += bw/8) { //handling the height grid
context.moveTo(x, 0);
context.lineTo(x, bh);
lLimit++;
}
for (var x = 0; lLimit <= 17; x += bh/8) { //handling the width grid
context.moveTo(0, x); //begin the line at this cord
context.lineTo(bw, x); //end the line at this cord
lLimit++;
}
//context.lineWidth = 0.5; what should I put here?
context.strokeStyle = "black";
context.stroke();
}
drawBoard();
现在,我终于成功了制作画布是在每个屏幕分辨率缩放级别相同的比例水平。这是我想要实现的一部分。我也尝试实现细线,在所有不同的缩放级别看起来都是一样的,当然也是为了消除模糊。现在线条的厚度 根据缩放级别而变化,并且有时模糊。
这里的jsfiddle(虽然的jsfiddle窗口本身很小,所以你会发现几乎没有区别):
https://jsfiddle.net/wL60jo5n/
帮助将不胜感激。
当你说缩放级别,你的意思是缩放内置到大多数浏览器(使用控制+ mwheel上/下)? –
是的。 window.innerWidth会在页面加载时考虑用户的当前缩放级别。 – RunningFromShia
我尝试实现的网格系统的实例:http://slither.io/无论您进入网页的缩放级别如何,或者即使在游戏过程中放大和缩小,网格系统看起来都一样。 – RunningFromShia