Estoy migrando algunos programas de Windows a tecnologías Web. Básicamente uso System.Drawing, así que estoy usando JavaScript y, por supuesto, Canvas. Como práctica conceptual, trato de dibujar una malla en Canvas (ver imagen), Genial, estoy muy cerca, pero no puedo alternar el color de las líneas oscuras. ¿Qué tengo que hacer?
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>Esto podría estar bien
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 % 100 == 0 ? 'gray' : 'silver'; ctx.beginPath(); ctx.moveTo(shift, w) ctx.lineTo(w, w - shift); shift += 10; ctx.stroke(); } <canvas id="canvas" width="300" height="300"></canvas>