document body style minHeight 200px let div document createElement div document body appendChild div div style width 400px div style height 400px div style position relative div style border 1px solid purple div style padding 0px div style margin 0px let svg document createElementNS http www w3 org 2000 svg svg svg style width 400px svg style height 400px svg style border 1px solid blue svg style position absolute svg style left 0px svg style top 0px svg style padding 0px svg style margin 0px svg setAttributeNS null width 400 svg setAttributeNS null height 400 div appendChild svg let canvas document createElement canvas div appendChild canvas canvas width 400 canvas height 400 canvas style width 400px canvas style height 400px canvas style position absolute canvas style left 0px canvas style top 0px canvas style border 1px solid orange canvas style padding 0px canvas style margin 0px 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 let cx 2 0 4 0 c x 400 0 2 to 2 let cy 2 0 4 0 c y 400 0 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 let zx0 0 let 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 false return true var color i toString 16 context beginPath context rect y x 1 1 context fillStyle i detail red blue for var xx 0 xx canvas width xx for var yy 0 yy canvas height yy var hit pointInMandelbrot x xx y yy var color i toString 16 context beginPath context rect xx yy 1 1 context fillStyle hit rgba 0 0 255 0 2 transparent context fill function addCircle c r id col green var newCircle document createElementNS http www w3 org 2000 svg circle newCircle id id newCircle setAttribute cx c x newCircle setAttribute cy c y newCircle setAttribute r r newCircle setAttribute fill transparent newCircle setAttribute stroke width 2 newCircle setAttribute stroke col svg appendChild newCircle addCircle x 200 y 100 50 c0 let shapes shapes push c x 160 y 110 r 50 id c0 type mandelbrot function showNormal p n col black let n0 document createElementNS http www w3 org 2000 svg line n0 style color col n0 setAttribute stroke col let e add p scale n 15 0 svg appendChild n0 n0 setAttribute x1 p x n0 setAttribute y1 p y n0 setAttribute x2 e x n0 setAttribute y2 e y n0 setAttribute marker end url arrowhead all this code just for an arrow head let arrowID arrow Math random let defs document createElementNS http www w3 org 2000 svg defs let marker document createElementNS http www w3 org 2000 svg marker let id arrowID marker setAttribute id id marker setAttribute markerWidth 10 marker setAttribute markerHeight 7 marker setAttribute markerUnits strokeWidth marker setAttribute refX 0 warning case sensitive refx refX marker setAttribute refY 3 5 marker setAttribute orient auto let path document createElementNS http www w3 org 2000 svg path path setAttribute d M 0 0 L 10 3 5 L 0 7 z path setAttribute fill col arrow head marker appendChild path defs appendChild marker svg appendChild defs n0 setAttribute marker end url arrowID function setLine id s e col let i0 document getElementById id if i0 null i0 document createElementNS http www w3 org 2000 svg line i0 id id var randomColor 00 Math floor Math random 16777215 toString 16 substring 0 4 i0 setAttribute stroke randomColor svg appendChild i0 i0 setAttribute x1 s x i0 setAttribute y1 s y i0 setAttribute x2 e x i0 setAttribute y2 e y if col i0 setAttribute stroke col if typeof opt undefined Object entries opt forEach o const k v o i0 setAttribute k v let ww window innerWidth let wh window innerHeight function sub a b return x a x b x y a y b y function add a b return x a x b x y a y b y function dist a b return Math sqrt a x b x a x b x a y b y a y b y function norm a let d Math sqrt a x a x a y a y return x a x d y a y d function len a return Math sqrt a x a x a y a y function scale a s return x a x s y a y s function dot a b return a x b x a y b y function lineIntersectCircle line circle if pointInMandelbrot line e return hit true hitpoint line e normal norm sub line e line s pen 0 inside false return hit false normal x 0 y 0 hitpoint x 0 y 0 function reflect i n I 2 0 dot N I N return norm sub scale i 1 scale n 2 dot n i source material index of refraction IOR destination material IOR function refract i n eta k 1 0 eta eta 1 0 dot N I dot N I if k 0 0 R vec 0 0 else R eta I eta dot N I sqrt k N let k 1 0 eta eta 1 0 dot n i dot n i if k 0 0 normal and the incident are tangental k 0 01 let s eta dot n i Math sqrt k return norm sub scale i eta scale n s let lines lines push id iv0 s x 20 y 55 e x 60 y 70 speed 5 alive true function updaterays if lines length 30 throw max lines reached lines length status innerHTML Lines lines length lines forEach line let hit false if line alive false return shapes forEach s let res lineIntersectCircle line s if res hit showNormal res hitpoint res normal line alive false hit true setLine line id line s line e red incident let i norm sub line e line s refraction transparent through the object let normal res normal if res inside inside to outside normal scale normal 1 another way of checking inverting the normal you could also use dot i n 0 means leaving the sphere if dot i n 0 res inside showNormal line e normal blue showNormal line e i red eta is the ratio between the refractive index of the medium being left air is 1 and the medium being entered Once you enter an object and hit the back side you need to refract by 1 eta basically let eta 1 5 if res inside eta 1 eta let rf refract i normal eta showNormal line e rf let hp add res hitpoint scale norm sub line e line s 2 0 showNormal hp rf let s0 hp let e0 add hp scale rf 2 0 let newline id refa lines length s s0 e e0 speed 5 alive true lines push newline if res inside setLine newline id newline s newline e opt stroke dasharray 4 reflected bounce off the object if res inside outside inside let rl reflect i res normal let s0 add line e scale norm sub line e line s 1 0 let e0 add s0 scale rl 2 0 let newline id refb lines length s s0 e e0 speed 5 alive true showNormal s0 scale rl 1 lines push newline return end foreach shapes if hit return let d dist line s line e line e add line s scale norm sub line e line s d line speed setLine line id line s line e if line e x 400 line e y 400 line e x 0 line e y 0 console log new line line alive false let n scale norm sub line e line s line speed 0 5 if line e y 0 line e y 400 n y 1 if line e x 0 line e x 400 n x 1 let ee add line e scale n 1 0 let newline id id lines length s line e e ee speed 5 alive true lines push newline end forEach window onclick function updaterays let count 0 let iid setInterval try count if count 15000 clearInterval iid updaterays catch e console log error e clearInterval iid 50 console log ready document body style minHeight 200px let div document createElement div document body appendChild div div style width 400px div style height 400px div style position relative div style border 1px solid purple div style padding 0px div style margin 0px let svg document createElementNS http www w3 org 2000 svg svg svg style width 400px svg style height 400px svg style border 1px solid blue svg style position absolute svg style left 0px svg style top 0px svg style padding 0px svg style margin 0px svg setAttributeNS null width 400 svg setAttributeNS null height 400 div appendChild svg let canvas document createElement canvas div appendChild canvas canvas width 400 canvas height 400 canvas style width 400px canvas style height 400px canvas style position absolute canvas style left 0px canvas style top 0px canvas style border 1px solid orange canvas style padding 0px canvas style margin 0px 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 let cx 2 0 4 0 c x 400 0 2 to 2 let cy 2 0 4 0 c y 400 0 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 let zx0 0 let 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 5 increase to add detail lower value smooths lower resolution pattern if i detail return false return true for var xx 0 xx canvas width xx for var yy 0 yy canvas height yy var hit pointInMandelbrot x xx y yy var color i toString 16 context beginPath context rect xx yy 1 1 context fillStyle hit rgba 0 0 255 0 2 transparent context fill function addCircle c r id col green var newCircle document createElementNS http www w3 org 2000 svg circle newCircle id id newCircle setAttribute cx c x newCircle setAttribute cy c y newCircle setAttribute r r newCircle setAttribute fill transparent newCircle setAttribute stroke width 2 newCircle setAttribute stroke col svg appendChild newCircle addCircle x 200 y 100 50 c0 let shapes shapes push c x 160 y 110 r 50 id c0 type mandelbrot function showNormal p n col black n norm n let n0 document createElementNS http www w3 org 2000 svg line n0 style color col n0 setAttribute stroke col let e add p scale n 15 0 svg appendChild n0 n0 setAttribute x1 p x n0 setAttribute y1 p y n0 setAttribute x2 e x n0 setAttribute y2 e y all this code just for an arrow head let arrowID arrow Math random let defs document createElementNS http www w3 org 2000 svg defs let marker document createElementNS http www w3 org 2000 svg marker let id arrowID marker setAttribute id id marker setAttribute markerWidth 10 marker setAttribute markerHeight 7 marker setAttribute markerUnits strokeWidth marker setAttribute refX 0 warning case sensitive refx refX marker setAttribute refY 3 5 marker setAttribute orient auto let path document createElementNS http www w3 org 2000 svg path path setAttribute d M 0 0 L 10 3 5 L 0 7 z path setAttribute fill col arrow head marker appendChild path defs appendChild marker svg appendChild defs n0 setAttribute marker end url arrowID function setLine id s e col let i0 document getElementById id if i0 null i0 document createElementNS http www w3 org 2000 svg line i0 id id var randomColor 00 Math floor Math random 16777215 toString 16 substring 0 4 i0 setAttribute stroke randomColor svg appendChild i0 i0 setAttribute x1 s x i0 setAttribute y1 s y i0 setAttribute x2 e x i0 setAttribute y2 e y if col i0 setAttribute stroke col if typeof opt undefined Object entries opt forEach o const k v o i0 setAttribute k v let ww window innerWidth let wh window innerHeight function sub a b return x a x b x y a y b y function add a b return x a x b x y a y b y function dist a b return Math sqrt a x b x a x b x a y b y a y b y function norm a let d Math sqrt a x a x a y a y return x a x d y a y d function len a return Math sqrt a x a x a y a y function scale a s return x a x s y a y s function dot a b return a x b x a y b y function lineIntersectCircle line circle info if pointInMandelbrot line e binary search for an accruate position let dir norm sub line s line e let pnt line e let dst 5 0 let stp 5 0 for let b 0 b 8 b let newpnt add line e scale dir dst if pointInMandelbrot newpnt dst stp else stp stp 0 5 dst stp let fix add line e scale dir dst let fix pnt add line e scale norm sub line s line e 2 0 let s 1 0 let pts x s y 0 x s y 0 x 0 y s x 0 y s x s y s x s y s x s y s x s y s let n x 0 y 0 for let b 0 b pts length b pts b add pts b fix if pointInMandelbrot pts b db pts b n add n norm sub pts b fix if len n 0 1 if info console log all outside n n sub line s line e let ee 0 01 n x ee Math random 2 ee n y ee Math random 2 ee n norm n function db pp if info console log pp pp addCircle pp 1 rd Math random sinside false einside true going from outside to inside return hit true hitpoint fix normal n pen 0 einside true return hit false normal x 0 y 0 hitpoint x 0 y 0 function reflect i n I 2 0 dot N I N return norm sub scale i 1 scale n 2 dot n i source material index of refraction IOR destination material IOR function refract i n eta k 1 0 eta eta 1 0 dot N I dot N I if k 0 0 R vec 0 0 else R eta I eta dot N I sqrt k N let k 1 0 eta eta 1 0 dot n i dot n i if k 0 0 normal and the incident are tangental k 0 01 let s eta dot n i Math sqrt k return norm sub scale i eta scale n s let lines lines push id iv0 s x 20 y 55 e x 60 y 70 speed 15 alive true function updaterays if lines length 30 throw max lines reached lines length status innerHTML Lines lines length lines forEach line let hit false if line alive false return shapes forEach s let res lineIntersectCircle line s true if res hit showNormal res hitpoint res normal addCircle res hitpoint 1 rd Math random line alive false hit true line e res hitpoint setLine line id line s line e red incident let i norm sub line e line s refraction transparent through the object let normal res normal if res einside inside to outside normal scale normal 1 another way of checking inverting the normal you could also use dot i n 0 means leaving the sphere if dot i n 0 res einside showNormal line e normal blue showNormal line e i red eta is the ratio between the refractive index of the medium being left air is 1 and the medium being entered Once you enter an object and hit the back side you need to refract by 1 eta basically let eta 1 5 if res einside eta 1 eta let rf refract i normal eta showNormal line e rf let hp add res hitpoint scale norm sub line e line s 2 0 showNormal hp rf let s0 hp let e0 add hp scale rf 2 0 let newline id refa lines length s s0 e e0 speed 5 alive true lines push newline if res einside setLine newline id newline s newline e opt stroke dasharray 4 reflected bounce off the object if res einside outside inside let rl reflect i res normal let s0 add res hitpoint scale norm sub line s line e 2 0 let e0 add s0 scale rl 1 0 let newline id refb lines length s s0 e e0 speed 5 alive true showNormal s0 res normal scale rl 1 lines push newline return end foreach shapes if hit return let d dist line s line e line e add line s scale norm sub line e line s d line speed setLine line id line s line e if line e x 400 line e y 400 line e x 0 line e y 0 console log new line line alive false let n scale norm sub line e line s line speed 0 5 if line e y 0 line e y 400 n y 1 if line e x 0 line e x 400 n x 1 let ee add line e scale n 1 0 let newline id id lines length s line e e ee speed 5 alive true lines push newline end forEach window onclick function updaterays let count 0 let iid setInterval try count if count 15000 clearInterval iid updaterays catch e console log error e clearInterval iid 50 console log ready
as style width 400px canvas style height 400px canvas style position absolute canvas style left 0px canvas style top 0px canvas style border 1px solid orange canvas style padding 0px canvas style margin 0px 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 let cx 2 0 4 0 c x 400 0 2 to 2 let cy 2 0 4 0 c y 400 0 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 let zx0 0 let 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 5 increase to add detail lower value smooths lower resolution pattern if i detail return false return true for var xx 0 xx canvas width xx for var yy 0 yy canvas height yy var hit pointInMandelbrot x xx y yy var color i toString 16 context beginPath context rect xx yy 1 1 context fillStyle hit rgba 0 0 255 0 2 transparent context fill function addCircle c r id col green var newCircle document createElementNS http www w3 org 2000 svg circle newCircle id id newCircle setAttribute cx c x newCircle setAttribute cy c y newCircle setAttribute r r newCircle setAttribute fill transparent newCircle setAttribute stroke width 2 newCircle setAttribute stroke col svg appendChild newCircle addCircle x 200 y 100 50 c0 let shapes shapes push c x 160 y 110 r 50 id c0 type mandelbrot function showNormal p n col black n norm n let n0 document createElementNS http www w3 org 2000 svg line n0 style color col n0 setAttribute stroke col let e add p scale n 15 0 svg appendChild n0 n0 setAttribute x1 p x n0 setAttribute y1 p y n0 setAttribute x2 e x n0 setAttribute y2 e y all this code just for an arrow head let arrowID arrow Math random let defs document createElementNS http www w3 org 2000 svg defs let marker document createElementNS http www w3 org 2000 svg marker let id arrowID marker setAttribute id id marker setAttribute markerWidth 10 marker setAttribute markerHeight 7 marker setAttribute markerUnits strokeWidth marker setAttribute refX 0 warning case sensitive refx refX marker setAttribute refY 3 5 marker setAttribute orient auto let path document createElementNS http www w3 org 2000 svg path path setAttribute d M 0 0 L 10 3 5 L 0 7 z path setAttribute fill col arrow head marker appendChild path defs appendChild marker svg appendChild defs n0 setAttribute marker end url arrowID function setLine id s e col let i0 document getElementById id if i0 null i0 document createElementNS http www w3 org 2000 svg line i0 id id var randomColor 00 Math floor Math random 16777215 toString 16 substring 0 4 i0 setAttribute stroke randomColor svg appendChild i0 i0 setAttribute x1 s x i0 setAttribute y1 s y i0 setAttribute x2 e x i0 setAttribute y2 e y if col i0 setAttribute stroke col if typeof opt undefined Object entries opt forEach o const k v o i0 setAttribute k v let ww window innerWidth let wh window innerHeight function sub a b return x a x b x y a y b y function add a b return x a x b x y a y b y function dist a b return Math sqrt a x b x a x b x a y b y a y b y function norm a let d Math sqrt a x a x a y a y return x a x d y a y d function len a return Math sqrt a x a x a y a y function scale a s return x a x s y a y s function dot a b return a x b x a y b y function lineIntersectCircle line circle info if pointInMandelbrot line e binary search for an accruate position let dir norm sub line s line e let pnt line e let dst 5 0 let stp 5 0 for let b 0 b 8 b let newpnt add line e scale dir dst if pointInMandelbrot newpnt dst stp else stp stp 0 5 dst stp let fix add line e scale dir dst let fix pnt add line e scale norm sub line s line e 2 0 let s 1 0 let pts x s y 0 x s y 0 x 0 y s x 0 y s x s y s x s y s x s y s x s y s let n x 0 y 0 for let b 0 b pts length b pts b add pts b fix if pointInMandelbrot pts b db pts b n add n norm sub pts b fix if len n 0 1 if info console log all outside n n sub line s line e let ee 0 01 n x ee Math random 2 ee n y ee Math random 2 ee n norm n function db pp if info console log pp pp addCircle pp 1 rd Math random sinside false einside true going from outside to inside return hit true hitpoint fix normal n pen 0 einside true return hit false normal x 0 y 0 hitpoint x 0 y 0 function reflect i n I 2 0 dot N I N return norm sub scale i 1 scale n 2 dot n i source material index of refraction IOR destination material IOR function refract i n eta k 1 0 eta eta 1 0 dot N I dot N I if k 0 0 R vec 0 0 else R eta I eta dot N I sqrt k N let k 1 0 eta eta 1 0 dot n i dot n i if k 0 0 normal and the incident are tangental k 0 01 let s eta dot n i Math sqrt k return norm sub scale i eta scale n s let lines lines push id iv0 s x 20 y 55 e x 60 y 70 speed 15 alive true function updaterays if lines length 30 throw max lines reached lines length status innerHTML Lines lines length lines forEach line let hit false if line alive false return shapes forEach s let res lineIntersectCircle line s true if res hit showNormal res hitpoint res normal addCircle res hitpoint 1 rd Math random line alive false hit true line e res hitpoint setLine line id line s line e red incident let i norm sub line e line s refraction transparent through the object let normal res normal if res einside inside to outside normal scale normal 1 another way of checking inverting the normal you could also use dot i n 0 means leaving the sphere if dot i n 0 res einside showNormal line e normal blue showNormal line e i red eta is the ratio between the refractive index of the medium being left air is 1 and the medium being entered Once you enter an object and hit the back side you need to refract by 1 eta basically let eta 1 5 if res einside eta 1 eta let rf refract i normal eta showNormal line e rf let hp add res hitpoint scale norm sub line e line s 2 0 showNormal hp rf let s0 hp let e0 add hp scale rf 2 0 let newline id refa lines length s s0 e e0 speed 5 alive true lines push newline if res einside setLine newline id newline s newline e opt stroke dasharray 4 reflected bounce off the object if res einside outside inside let rl reflect i res normal let s0 add res hitpoint scale norm sub line s line e 2 0 let e0 add s0 scale rl 1 0 let newline id refb lines length s s0 e e0 speed 5 alive true showNormal s0 res normal scale rl 1 lines push newline return end foreach shapes if hit return let d dist line s line e line e add line s scale norm sub line e line s d line speed setLine line id line s line e if line e x 400 line e y 400 line e x 0 line e y 0 console log new line line alive false let n scale norm sub line e line s line speed 0 5 if line e y 0 line e y 400 n y 1 if line e x 0 line e x 400 n x 1 let ee add line e scale n 1 0 let newline id id lines length s line e e ee speed 5 alive true lines push newline end forEach window onclick function updaterays let count 0 let iid setInterval try count if count 15000 clearInterval iid updaterays catch e console log error e clearInterval iid 50 console log ready