Skip to main content
100%

Sierpinski Triangle Example

✓ Published0🌍 Public
MMilan Košir
Last edited Dec 12, 2023
Created on Dec 12, 2023

This example shows a Sierpinski triangle fractal rendered as an interactive tree-like structure on a Canvas element. The fractal is drawn with recursive triangle subdivision, and the branching depth changes dynamically based on the mouse position. The code uses the Canvas 2D API with `beginPath`, `moveTo`, `lineTo`, and `fill` methods to draw triangles. It accommodates high pixel density displays by scaling the canvas resolution using `window.devicePixelRatio`. The mouse event listener recalculates the depth from the cursor's horizontal position, then clears and redraws the entire fractal.

AI-generated description

Interactive tree fractal with Canvas.

Move the mouse to change the branching!

Draws from HTML5 Examples.

Extended to use high resolution Canvas for high pixel density displays.

MIT Licensed

Similar vizzes