Bucle în canvas pe JavaScript
Desenarea pe canvas poate fi introdusă în bucle. Haideți să desenăm mai multe linii folosind o buclă:
html<canvas width="200" height="200" style="background: #f4f4f4;"></canvas>javascriptlet canvas = document.querySelector('canvas');
let ctx = canvas.getContext('2d');
for (let i = 1; i <= 10; i++) {
ctx.moveTo(50, 15 * i);
ctx.lineTo(150, 15 * i);
}
ctx.stroke();
Результат:
Probleme practice
Задание 1
Задание 2