我想制作一个十字准线网格(每 10 像素)。我有很多问题。可以用比 3x For 循环更简单的方式完成吗? http://jsfiddle.net/TnnRp/1/
var canvas = document.getElementById('grid');
var context = canvas.getContext('2d');
// grid
var width = canvas.width;
var height = canvas.height;
var p = 10;
var h = 10;
for (var i = 10; i <= width - 5; i += 10) {
for (var e = 10; e <= height - 5; e += 10) {
context.moveTo(h + 0.5, e - 1);
context.lineTo(h + 0.5, e + 2);
}
h += 10;
for (var f = 10; f <= width - 5; f += 10) {
context.moveTo(f - 1, p + 0.5);
context.lineTo(f + 2, p + 0.5);
}
p += 10;
}
context.stroke();