Created
February 27, 2023 14:46
-
-
Save christophergorexyz/b00c5f32d96a477508d86998950414f8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <html> | |
| <head> | |
| <title>hitomezashi stitching</title> | |
| </head> | |
| <body> | |
| <input id="seedX" /> | |
| <input id="seedY" /> | |
| <button id="generate">generate</button> | |
| <br /><br /> | |
| <canvas id="hitomezashi"></canvas> | |
| <script type="text/javascript"> | |
| let cellSize = 10; | |
| document.getElementById('generate').addEventListener('click', handleGenerate); | |
| let canvas = document.getElementById('hitomezashi') | |
| let context = canvas.getContext('2d'); | |
| context.strokeStyle = 'black'; | |
| let seedX = 0; | |
| let seedY = 0; | |
| function getEdgeCode(x, y) { | |
| let yCross = parseInt(seedX[x]) ^ y % 2; | |
| let xCross = parseInt(seedY[y]) ^ x % 2; | |
| return ((xCross << 1) + yCross) | |
| } | |
| function handleGenerate() { | |
| seedX = parseInt(document.getElementById('seedX').value).toString(2); | |
| seedY = parseInt(document.getElementById('seedY').value).toString(2); | |
| generate() | |
| } | |
| function generate() { | |
| canvas.width = (seedX.length) * cellSize | |
| canvas.height = (seedY.length) * cellSize | |
| context.clearRect(0, 0, canvas.width, canvas.height); | |
| for (let x = 0; x < seedX.length; x++) { | |
| for (let y = 0; y < seedY.length; y++) { | |
| draw(x, y, getEdgeCode(x, y)); | |
| } | |
| } | |
| } | |
| //TODO: rewrite this to make it corners | |
| function draw(x, y, edgeCode) { | |
| switch (edgeCode) { | |
| case 0b11: | |
| //both edges | |
| context.beginPath(); | |
| context.moveTo(x * cellSize, y * cellSize); | |
| context.lineTo(x * cellSize + cellSize, y * cellSize); | |
| context.stroke(); | |
| context.beginPath(); | |
| context.moveTo(x * cellSize, y * cellSize); | |
| context.lineTo(x * cellSize, y * cellSize + cellSize); | |
| context.stroke(); | |
| break; | |
| case 0b10: | |
| //only horizontal edge | |
| context.beginPath(); | |
| context.moveTo(x * cellSize, y * cellSize); | |
| context.lineTo(x * cellSize + cellSize, y * cellSize); | |
| context.stroke(); | |
| break; | |
| case 0b01: | |
| //only vertical edge | |
| context.beginPath(); | |
| context.moveTo(x * cellSize, y * cellSize); | |
| context.lineTo(x * cellSize, y * cellSize + cellSize); | |
| context.stroke(); | |
| break; | |
| case 0b00: | |
| default: | |
| //do nothing, no edge to draw | |
| break; | |
| } | |
| } | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment