MCPcopy Create free account
hub / github.com/TheAlgorithms/JavaScript / drawToCanvas

Function drawToCanvas

Recursive/KochSnowflake.manual-test.js:40–55  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

38 * @returns The canvas of the rendered edges.
39 */
40function 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
58if (typeof window !== 'undefined') {

Callers 1

getKochSnowflakeFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected