* Utility-method to render the Koch snowflake to a canvas. * * @param vectors The vectors defining the edges to be rendered. * @param canvasWidth The width of the canvas. * @param canvasHeight The height of the canvas. * @returns The canvas of the rendered edges.
(vectors, canvasWidth, canvasHeight)
| 38 | * @returns The canvas of the rendered edges. |
| 39 | */ |
| 40 | function drawToCanvas(vectors, canvasWidth, canvasHeight) { |
| 41 | const canvas = document.createElement('canvas') |
| 42 | canvas.width = canvasWidth |
| 43 | canvas.height = canvasHeight |
| 44 | |
| 45 | // Draw the edges |
| 46 | const ctx = canvas.getContext('2d') |
| 47 | ctx.beginPath() |
| 48 | ctx.moveTo(vectors[0].x, vectors[0].y) |
| 49 | for (let i = 1; i < vectors.length; i++) { |
| 50 | ctx.lineTo(vectors[i].x, vectors[i].y) |
| 51 | } |
| 52 | ctx.stroke() |
| 53 | |
| 54 | return canvas |
| 55 | } |
| 56 | |
| 57 | // plot the results if the script is executed in a browser with a window-object |
| 58 | if (typeof window !== 'undefined') { |