我正在将一些Windows程序迁移到Web技术上。基本上使用System.Drawing,所以我正在使用JavaScript,当然还有Canvas。作为一个概念实践,我尝试在Canvas上绘制网格(见图像)。很酷,我已经非常接近了,但是我无法让深色线条的颜色交替。我应该怎么做?
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
ctx.translate(0.5, 0.5) // waiting a line of one pixel
let shift = 0;
let w = canvas.clientWidth;
while (shift <= w) {
ctx.strokeStyle = shift % 40 == 0 ? 'gray' : 'silver';
ctx.moveTo(shift, w)
ctx.lineTo(w, w - shift);
shift += 10;
}
ctx.stroke();
<canvas id="canvas" class="plot" width="300" height="300"></canvas>