Mandelbrot Fractal document body style height 200px let canvas document createElement canvas document body appendChild canvas canvas width 200 canvas height 200 var context canvas getContext 2d z n 1 z n 2 c plot 2 2i and 2 2i i2 1 a property of complex numbers for var x 0 x canvas width x for var y 0 y canvas height y var cx 2 4 x canvas width 2 to 2 var cy 2 4 y canvas height 2 to 2 z1 0 0 2 2 2 z2 z1 2 c z3 z2 2 c x iy 2 x iy x iy x 2 2ixy iy 2 where iy 2 1 x 2 2ixy 1 real x 2 img 2xy var zx0 0 var zy0 0 let i 0 for let n 0 n 256 n let zx1 zx0 zx0 zy0 zy0 cx let zy1 2 zx0 zy0 cy zx0 zx1 zy0 zy1 i 1 let mag Math sqrt zx0 zx0 zy0 zy0 if mag 1 5 break let detail 10 increase to add detail lower value smooths lower resolution pattern var color i toString 16 context beginPath context rect y x 1 1 context fillStyle i detail red blue context fill console log ready Mandelbrot Fractal document body style height 200px let canvas document createElement canvas document body appendChild canvas canvas width 200 canvas height 200 var context canvas getContext 2d z n 1 z n 2 c plot 2 2i and 2 2i i2 1 a property of complex numbers function pointInMandelbrot c var cx c x 2 to 2 var cy c y 2 to 2 z1 0 0 2 2 2 z2 z1 2 c z3 z2 2 c x iy 2 x iy x iy x 2 2ixy iy 2 where iy 2 1 x 2 2ixy 1 real x 2 img 2xy var zx0 0 var zy0 0 let i 0 for let n 0 n 256 n let zx1 zx0 zx0 zy0 zy0 cx let zy1 2 zx0 zy0 cy zx0 zx1 zy0 zy1 i 1 let mag Math sqrt zx0 zx0 zy0 zy0 if mag 1 5 break let detail 10 increase to add detail lower value smooths lower resolution pattern if i detail return true return false var color i toString 16 context beginPath context rect y x 1 1 context fillStyle i detail red blue for var x 0 x canvas width x for var y 0 y canvas height y var cx 2 4 x canvas width 2 to 2 var cy 2 4 y canvas height 2 to 2 var hit pointInMandelbrot x cx y cy var color i toString 16 context beginPath context rect y x 1 1 context fillStyle hit red blue context fill console log ready
etContext 2d z n 1 z n 2 c plot 2 2i and 2 2i i2 1 a property of complex numbers function pointInMandelbrot c var cx c x 2 to 2 var cy c y 2 to 2 z1 0 0 2 2 2 z2 z1 2 c z3 z2 2 c x iy 2 x iy x iy x 2 2ixy iy 2 where iy 2 1 x 2 2ixy 1 real x 2 img 2xy var zx0 0 var zy0 0 let i 0 for let n 0 n 256 n let zx1 zx0 zx0 zy0 zy0 cx let zy1 2 zx0 zy0 cy zx0 zx1 zy0 zy1 i 1 let mag Math sqrt zx0 zx0 zy0 zy0 if mag 1 5 break let detail 10 increase to add detail lower value smooths lower resolution pattern if i detail return true return false var color i toString 16 context beginPath context rect y x 1 1 context fillStyle i detail red blue for var x 0 x canvas width x for var y 0 y canvas height y var cx 2 4 x canvas width 2 to 2 var cy 2 4 y canvas height 2 to 2 var hit pointInMandelbrot x cx y cy var color i toString 16 context beginPath context rect y x 1 1 context fillStyle hit red blue context fill console log ready