<canvas> : <canvas id="canvas"></canvas> var canva = document.getElementById('canvas'); canva.width = window.innerWidth; canva.height = window.innerHeight; var ctx = canva.getContext('2d'); function clear() { ctx.clearRect(0, 0, canva.width, canva.height); ctx.fillStyle = 'rgba(0,0,0,1)'; ctx.fillRect(0, 0, canva.width, canva.height); } clear() function clears the entire work area. Why there immediately clearRect and full fill fillRect ? And so for sure .figure array with the size of NxN, whose elements take on values 0 and 1 (0 is a transparent block, 1 is a color block). function PrintFigure(x, y, size, style) { var sizeElem = Math.floor(size / N); for (var i = 0; i < N; i++) for (var k = 0; k < N; k++) if (figure[i][k] == 1) { ctx.fillStyle = style; ctx.fillRect(x + i * sizeElem, y + k * sizeElem, sizeElem, sizeElem); } } var MAX_X = canva.width, MAX_Y = canva.height; function CreateTFractal() { clear(); var size = N; var countByX = Math.floor(MAX_X / size), countByY = Math.floor(MAX_Y / size); while ((countByX > 0) && (countByY > 0)) { // for (var i = 0; i <= countByX; i++) { for (var k = 0; k <= countByY; k++) { PrintFigure(i * size, k * size, size, 'rgba(255, 255, 255, 0.3)'); } // k } // i size *= 2; // countByX = Math.floor(MAX_X / size), countByY = Math.floor(MAX_Y / size); } } while in theory, you can replace it with OR .
<canvas> in PNG by javascript: window.open(document.getElementById("canvas").toDataURL("image/png"),"tfract_save"); Source: https://habr.com/ru/post/145801/
All Articles