Fractal Fun Take a basic shape circle then them mix in random deformations surface displacements creates an interesting patterns shapes Candy for the eyes The concept isn t overly complex however the result is very interesting in fact it sort of spoils the magic once you ve seen how it s generated See details example of the fractal concept here LINK https notebook xbdev net index php page fractals2 Future ideas projects Explore other shapes e g maybe try splines that represent letters or pictures of objects UI control magic number parameters e g people can play around with all the numbers create all sorts of results Nested shells shapes within small shapes transparent drawing internal aspects Instead of solid surface only draw parts use lines instead Use a textured image for the stretched colors lookup from texture Add environmental mapping lookup for shinny reflection Try different mathematical models quaternions Resolution very high resolution zoom in and show only a small portion of the surface changing Noise algorithm to the color Perlin Try different noise functions mix in other trignometric functions Advanced projects Ask users to rate the different generated images store associated algorithm parameters feed them into a machine learning algorithm which generates patterns that it thinks people like Random parameter changing to generate mixture of shapes colors map the generated texture onto 3d models The principle works by creating points for the surfaces of the circle e g use sin cos to go around and build the points Then link them together link list so you know which point should be connected to which other point After that you should be able to move any of the points and you ll still get your shape but it ll no longer be a circle style body background color ffffff color 333333 style canvas id displayCanvas width 500px height 500px canvas script var displayCanvas document getElementById displayCanvas var context displayCanvas getContext 2d var displayWidth displayCanvas width var displayHeight displayCanvas height var numCircles var maxMaxRad var minMaxRad var minRadFactor var circles var iterations var timer var bgColor urlColor var TWO_PI 2 Math PI var lineWidth init function init Try out different deviations types of noise correlating factors numCircles 1 maxMaxRad 200 minMaxRad 200 You draw closed curves with varying radius The factor below should be set between 0 and 1 representing the size of the smallest radius with respect to the largest possible minRadFactor 0 The number of subdividing steps to take when creating a single fractal curve Can use more but anything over 10 thus 1024 points is overkill for a moderately sized canvas iterations 9 number of curves to draw on every tick of the timer drawsPerFrame 1 bgColor FFFFFF urlColor EEEEEE lineWidth 2 startGenerate function startGenerate context setTransform 1 0 0 1 0 0 background you are drawing it for the sake of exported png images otherwise background will be transparent context fillStyle bgColor context fillRect 0 0 displayWidth displayHeight setCircles onDraw function setCircles var i var maxR minR var grad circles for i 0 i numCircles i maxR minMaxRad Math random maxMaxRad minMaxRad minR minRadFactor maxR var newCircle centerX displayWidth 2 centerY displayHeight 2 maxRad maxR minRad minR color 000000 can set a gradient or solid color here fillColor dddddd phase Math random TWO_PI the phase to use for a single fractal curve circles push newCircle newCircle pointList1 setLinePoints iterations newCircle pointList2 setLinePoints iterations function onDraw clear screen context clearRect 0 0 displayWidth displayHeight draw fractal circles for let i 0 i numCircles i let c circles i c pointList1 setLinePoints iterations context strokeStyle c color context lineWidth lineWidth context fillStyle c fillColor context beginPath let point1 c pointList1 let theta c phase let rad c minRad point1 y c maxRad c minRad Drawing the shape involves stepping through a linked list of points let x0 c centerX rad Math cos theta let y0 c centerY rad Math sin theta context lineTo x0 y0 while point1 next null point1 point1 next theta TWO_PI point1 x c phase rad c minRad point1 y c maxRad c minRad x0 c centerX rad Math cos theta y0 c centerY rad Math sin theta context lineTo x0 y0 context closePath context fill context stroke This is the magic Defines a noisy but not wildly varying data set which you will use to draw the curves function setLinePoints iterations const pointList x 0 y 1 pointList next x 1 y 1 var minY 1 var maxY 1 for var i 0 i iterations i let point pointList while point next null let nextPoint point next let dx point x nextPoint x let dy point y nextPoint y let newX point x 0 5 dx dy Math random 2 1 0 2 let newY point y 0 5 dy dx Math random 2 1 Try Circle let newX point x 0 5 dx let newY point y 0 5 dy Mix in other trig random functions sin cos var newPoint x newX y newY minY Math min newY minY maxY Math max newY maxY add linklist between points newPoint next nextPoint point next newPoint point nextPoint normalize to values between 0 and 1 let yRange maxY minY if Math abs yRange 0 001 minY 0 yRange 1 0 var normalizeRate 1 yRange point pointList while point null point y normalizeRate point y minY point point next return pointList console log ready script Interpolate style body background color ffffff color 333333 style canvas id displayCanvas width 500px height 500px canvas script var displayCanvas document getElementById displayCanvas var context displayCanvas getContext 2d var displayWidth displayCanvas width var displayHeight displayCanvas height var numCircles var maxMaxRad var minMaxRad var minRadFactor var circles var iterations var timer var bgColor urlColor var TWO_PI 2 Math PI var lineWidth init function init In other experiments you may wish to use more fractal curves circles and allow the radius of them to vary If so modify the next three variables numCircles 1 maxMaxRad 200 minMaxRad 200 You draw closed curves with varying radius The factor below should be set between 0 and 1 representing the size of the smallest radius with respect to the largest possible minRadFactor 0 The number of subdividing steps to take when creating a single fractal curve Can use more but anything over 10 thus 1024 points is overkill for a moderately sized canvas iterations 9 number of curves to draw on every tick of the timer drawsPerFrame 1 bgColor FFFFFF urlColor EEEEEE lineWidth 2 startGenerate function startGenerate context setTransform 1 0 0 1 0 0 background you are drawing it for the sake of exported png images otherwise background will be transparent context fillStyle bgColor context fillRect 0 0 displayWidth displayHeight setCircles if timer clearInterval timer timer setInterval onTimer 1000 50 function setCircles var i var maxR minR var grad circles for i 0 i numCircles i maxR minMaxRad Math random maxMaxRad minMaxRad minR minRadFactor maxR var newCircle centerX displayWidth 2 centerY displayHeight 2 maxRad maxR minRad minR color 000000 can set a gradient or solid color here fillColor CCCCCC param 0 changeSpeed 1 100 phase Math random TWO_PI the phase to use for a single fractal curve circles push newCircle newCircle pointList1 setLinePoints iterations newCircle pointList2 setLinePoints iterations function onTimer var i j var c var rad var point1 point2 var x0 y0 var cosParam var yOffset clear screen context clearRect 0 0 displayWidth displayHeight draw circles for j 0 j drawsPerFrame j for i 0 i numCircles i c circles i c param c changeSpeed if c param 1 c param 0 c pointList1 c pointList2 c pointList2 setLinePoints iterations cosParam 0 5 0 5 Math cos Math PI c param context strokeStyle c color context lineWidth lineWidth context fillStyle c fillColor context beginPath point1 c pointList1 first point2 c pointList2 first slowly rotate c phase 0 0002 theta c phase rad c minRad point1 y cosParam point2 y point1 y c maxRad c minRad Drawing the curve involves stepping through a linked list of points defined by a fractal subdivision process It is like drawing a circle except with varying radius x0 c centerX rad Math cos theta y0 c centerY rad Math sin theta context lineTo x0 y0 while point1 next null point1 point1 next point2 point2 next theta TWO_PI point1 x cosParam point2 x point1 x c phase rad c minRad point1 y cosParam point2 y point1 y c maxRad c minRad x0 c centerX rad Math cos theta y0 c centerY rad Math sin theta context lineTo x0 y0 context closePath context fill context stroke Here is the function that defines a noisy but not wildly varying data set which you will use to draw the curves function setLinePoints iterations var pointList pointList first x 0 y 1 var lastPoint x 1 y 1 var minY 1 var maxY 1 var point var nextPoint var dx newX newY var ratio pointList first next lastPoint for var i 0 i iterations i point pointList first while point next null nextPoint point next let dx point x nextPoint x let dy point y nextPoint y let newX point x 0 5 dx dy Math random 2 1 let newY point y 0 5 dy dx Math random 2 1 Try experimenting with these 2 lines e g let newX point x 0 5 dx dy Math random 2 1 let newY point y 0 5 dy dx Math random 2 1 Math sin iterations 0 01 var newPoint x newX y newY min max if newY minY minY newY else if newY maxY maxY newY put between points newPoint next nextPoint point next newPoint point nextPoint normalize to values between 0 and 1 if maxY minY var normalizeRate 1 maxY minY point pointList first while point null point y normalizeRate point y minY point point next unlikely that max min but could happen if using zero iterations In this case set all points equal to 1 else point pointList first while point null point y 1 point point next return pointList console log ready script Moving the pattern around but not clearing the screen style type text css body background color ffffff color 888888 style canvas id displayCanvas height 500 width 800 canvas input type button id btnRegenerate value regenerate input type button id btnExport value export image png script var displayCanvas document getElementById displayCanvas var context displayCanvas getContext 2d var displayWidth displayCanvas width var displayHeight displayCanvas height off screen canvas used only when exporting image var exportCanvas document createElement canvas exportCanvas width displayWidth exportCanvas height displayHeight var exportCanvasContext exportCanvas getContext 2d buttons var btnExport document getElementById btnExport btnExport addEventListener click exportPressed false var btnRegenerate document getElementById btnRegenerate btnRegenerate addEventListener click regeneratePressed false var numCircles var maxMaxRad var minMaxRad var minRadFactor var circles var iterations var timer var drawsPerFrame var drawCount var bgColor urlColor var TWO_PI 2 Math PI var lineWidth init function init In other experiments you may wish to use more fractal curves circles and allow the radius of them to vary If so modify the next three variables numCircles 1 maxMaxRad 200 minMaxRad 200 You ll draw closed curves with varying radius The factor below should be set between 0 and 1 representing the size of the smallest radius with respect to the largest possible minRadFactor 0 The number of subdividing steps to take when creating a single fractal curve Can use more but anything over 10 thus 1024 points is overkill for a moderely sized canvas iterations 8 number of curves to draw on every tick of the timer drawsPerFrame 5 bgColor 000000 urlColor 444444 lineWidth 3 startGenerate function startGenerate drawCount 0 context setTransform 1 0 0 1 0 0 context clearRect 0 0 displayWidth displayHeight setCircles if timer clearInterval timer timer setInterval onTimer 1000 60 function setCircles var i var r g b a var maxR minR var grad circles for i 0 i numCircles i maxR minMaxRad Math random maxMaxRad minMaxRad minR minRadFactor maxR var newCircle centerX maxR centerY displayHeight 2 20 maxRad maxR minRad minR color rgba 0 220 255 1 can set a gradient or solid color here fillColor rgba 0 0 0 1 param 0 changeSpeed 1 200 phase Math random TWO_PI the phase to use for a single fractal curve globalPhase Math random TWO_PI the curve as a whole will rise and fall by a sinusoid circles push newCircle newCircle pointList1 setLinePoints iterations newCircle pointList2 setLinePoints iterations function onTimer var i j var c var rad var point1 point2 var x0 y0 var cosParam var xSqueeze 1 cheap 3D effect by shortening in x direction var yOffset draw circles for j 0 j drawsPerFrame j drawCount for i 0 i numCircles i c circles i c param c changeSpeed if c param 1 c param 0 c pointList1 c pointList2 c pointList2 setLinePoints iterations cosParam 0 5 0 5 Math cos Math PI c param slowly rotate c phase 0 002 move center c centerX 0 4 c centerY 0 015 stop when off screen if c centerX displayWidth maxMaxRad clearInterval timer timer null You are drawing in new position by applying a transform This so gradients will move with the drawing context setTransform xSqueeze 0 0 1 c centerX c centerY Drawing the curve involves stepping through a linked list of points defined by a fractal subdivision process It is like drawing a circle except with varying radius You will draw the curve twice first colored then low alpha black on top with an offset context strokeStyle c color context lineWidth lineWidth context beginPath point1 c pointList1 first point2 c pointList2 first theta c phase rad c minRad point1 y cosParam point2 y point1 y c maxRad c minRad x0 xSqueeze rad Math cos theta 1 y0 rad Math sin theta context lineTo x0 y0 while point1 next null point1 point1 next point2 point2 next theta TWO_PI point1 x cosParam point2 x point1 x c phase rad c minRad point1 y cosParam point2 y point1 y c maxRad c minRad x0 xSqueeze rad Math cos theta 1 y0 rad Math sin theta context lineTo x0 y0 context closePath context stroke now draw second time in low alpha black context strokeStyle rgba 0 0 0 0 22 context lineWidth lineWidth context beginPath point1 c pointList1 first point2 c pointList2 first theta c phase rad c minRad point1 y cosParam point2 y point1 y c maxRad c minRad x0 xSqueeze rad Math cos theta y0 rad Math sin theta context lineTo x0 y0 while point1 next null point1 point1 next point2 point2 next theta TWO_PI point1 x cosParam point2 x point1 x c phase rad c minRad point1 y cosParam point2 y point1 y c maxRad c minRad x0 xSqueeze rad Math cos theta y0 rad Math sin theta context lineTo x0 y0 context closePath context stroke Here is the function that defines a noisy but not wildly varying data set which you ll use to draw the curves function setLinePoints iterations var pointList pointList first x 0 y 1 var lastPoint x 1 y 1 var minY 1 var maxY 1 var point var nextPoint var dx newX newY var ratio pointList first next lastPoint for var i 0 i iterations i point pointList first while point next null nextPoint point next dx nextPoint x point x newX 0 5 point x nextPoint x newY 0 5 point y nextPoint y newY dx Math random 2 1 var newPoint x newX y newY min max if newY minY minY newY else if newY maxY maxY newY put between points newPoint next nextPoint point next newPoint point nextPoint normalize to values between 0 and 1 if maxY minY var normalizeRate 1 maxY minY point pointList first while point null point y normalizeRate point y minY point point next unlikely that max min but could happen if using zero iterations In this case set all points equal to 1 else point pointList first while point null point y 1 point point next return pointList function exportPressed evt background otherwise background will be transparent exportCanvasContext fillStyle bgColor exportCanvasContext fillRect 0 0 displayWidth displayHeight draw exportCanvasContext drawImage displayCanvas 0 0 displayWidth displayHeight 0 0 displayWidth displayHeight add printed url to image exportCanvasContext fillStyle urlColor exportCanvasContext font bold italic 16px Helvetica Arial sans serif exportCanvasContext textBaseline top var metrics exportCanvasContext measureText rectangleworld com exportCanvasContext fillText rectangleworld com displayWidth metrics width 10 5 You ll open a new window with the image contained within retrieve canvas image as data URL var dataURL exportCanvas toDataURL image png open a new window of appropriate size to hold the image var imageWindow window open fractalLineImage left 0 top 0 width displayWidth height displayHeight toolbar 0 resizable 0 write some html into the new window creating an empty image imageWindow document write title Export Image title imageWindow document write img id exportImage alt height displayHeight width displayWidth style position absolute left 0 top 0 imageWindow document close copy the image into the empty img in the newly opened window var exportImage imageWindow document getElementById exportImage exportImage src dataURL function regeneratePressed evt startGenerate console log ready script Making things a bit more funky gradient color style type text css body background color ffffff color 888888 style canvas id displayCanvas height 500 width 800 canvas input type button id btnRegenerate value regenerate input type button id btnExport value export image png script var displayCanvas document getElementById displayCanvas var context displayCanvas getContext 2d var displayWidth displayCanvas width var displayHeight displayCanvas height off screen canvas used only when exporting image var exportCanvas document createElement canvas exportCanvas width displayWidth exportCanvas height displayHeight var exportCanvasContext exportCanvas getContext 2d buttons var btnExport document getElementById btnExport btnExport addEventListener click exportPressed false var btnRegenerate document getElementById btnRegenerate btnRegenerate addEventListener click regeneratePressed false var numCircles var maxMaxRad var minMaxRad var minRadFactor var circles var iterations var timer var drawsPerFrame var drawCount var bgColor urlColor var TWO_PI 2 Math PI var lineWidth init function init In other experiments you may wish to use more fractal curves circles and allow the radius of them to vary If so modify the next three variables numCircles 1 maxMaxRad 200 minMaxRad 200 You ll draw closed curves with varying radius The factor below should be set between 0 and 1 representing the size of the smallest radius with respect to the largest possible minRadFactor 0 The number of subdividing steps to take when creating a single fractal curve Can use more but anything over 10 thus 1024 points is overkill for a moderately sized canvas iterations 8 n umber of curves to draw on every tick of the timer drawsPerFrame 4 bgColor 000000 urlColor 444444 lineWidth 1 startGenerate function startGenerate drawCount 0 context setTransform 1 0 0 1 0 0 context clearRect 0 0 displayWidth displayHeight setCircles if timer clearInterval timer timer setInterval onTimer 1000 60 function setCircles var i var r g b a var maxR minR var grad circles for i 0 i numCircles i maxR minMaxRad Math random maxMaxRad minMaxRad minR minRadFactor maxR define gradient grad context createLinearGradient 0 85 maxR 0 0 7 maxR 0 grad addColorStop 1 rgba 255 0 0 0 2 grad addColorStop 0 rgba 20 220 0 0 4 var newCircle centerX maxR centerY displayHeight 2 50 maxRad maxR minRad minR color grad can set a gradient or solid color here fillColor rgba 0 0 0 1 param 0 changeSpeed 1 250 phase Math random TWO_PI the phase to use for a single fractal curve globalPhase Math random TWO_PI the curve as a whole will rise and fall by a sinusoid circles push newCircle newCircle pointList1 setLinePoints iterations newCircle pointList2 setLinePoints iterations function onTimer var i j var c var rad var point1 point2 var x0 y0 var cosParam var xSqueeze 0 75 cheap 3D effect by shortening in x direction var yOffset draw circles for j 0 j drawsPerFrame j drawCount for i 0 i numCircles i c circles i c param c changeSpeed if c param 1 c param 0 c pointList1 c pointList2 c pointList2 setLinePoints iterations cosParam 0 5 0 5 Math cos Math PI c param context strokeStyle c color context lineWidth lineWidth context fillStyle c fillColor context beginPath point1 c pointList1 first point2 c pointList2 first slowly rotate c phase 0 0002 theta c phase rad c minRad point1 y cosParam point2 y point1 y c maxRad c minRad move center c centerX 0 5 c centerY 0 04 yOffset 40 Math sin c globalPhase drawCount 1000 6 28 stop when off screen if c centerX displayWidth maxMaxRad clearInterval timer timer null w you are drawing in new position by applying a transform Wyouare doing this so the gradient will move with the drawing context setTransform xSqueeze 0 0 1 c centerX c centerY yOffset Drawing the curve involves stepping through a linked list of points defined by a fractal subdivision process It is like drawing a circle except with varying radius x0 xSqueeze rad Math cos theta y0 rad Math sin theta context lineTo x0 y0 while point1 next null point1 point1 next point2 point2 next theta TWO_PI point1 x cosParam point2 x point1 x c phase rad c minRad point1 y cosParam point2 y point1 y c maxRad c minRad x0 xSqueeze rad Math cos theta y0 rad Math sin theta context lineTo x0 y0 context closePath context stroke context fill Here is the function that defines a noisy but not wildly varying data set which you ll use to draw the curves function setLinePoints iterations var pointList pointList first x 0 y 1 var lastPoint x 1 y 1 var minY 1 var maxY 1 var point var nextPoint var dx newX newY var ratio pointList first next lastPoint for var i 0 i iterations i point pointList first while point next null nextPoint point next dx nextPoint x point x newX 0 5 point x nextPoint x newY 0 5 point y nextPoint y newY dx Math random 2 1 var newPoint x newX y newY min max if newY minY minY newY else if newY maxY maxY newY put between points newPoint next nextPoint point next newPoint point nextPoint normalize to values between 0 and 1 if maxY minY var normalizeRate 1 maxY minY point pointList first while point null point y normalizeRate point y minY point point next unlikely that max min but could happen if using zero iterations In this case set all points equal to 1 else point pointList first while point null point y 1 point point next return pointList function exportPressed evt background otherwise background will be transparent exportCanvasContext fillStyle bgColor exportCanvasContext fillRect 0 0 displayWidth displayHeight draw exportCanvasContext drawImage displayCanvas 0 0 displayWidth displayHeight 0 0 displayWidth displayHeight add printed url to image exportCanvasContext fillStyle urlColor exportCanvasContext font bold italic 16px Helvetica Arial sans serif exportCanvasContext textBaseline top var metrics exportCanvasContext measureText rectangleworld com exportCanvasContext fillText rectangleworld com displayWidth metrics width 10 5 You ll open a new window with the image contained within retrieve canvas image as data URL var dataURL exportCanvas toDataURL image png open a new window of appropriate size to hold the image var imageWindow window open fractalLineImage left 0 top 0 width displayWidth height displayHeight toolbar 0 resizable 0 write some html into the new window creating an empty image imageWindow document write title Export Image title imageWindow document write img id exportImage alt height displayHeight width displayWidth style position absolute left 0 top 0 imageWindow document close copy the image into the empty img in the newly opened window var exportImage imageWindow document getElementById exportImage exportImage src dataURL function regeneratePressed evt startGenerate console log ready script Why have just one fractal Add more have them interact pass through one another style type text css body background color ffffff color 888888 style canvas id displayCanvas height 500 width 800 canvas input type button id btnRegenerate value regenerate input type button id btnExport value export image png input type checkbox id cbColor randomize colors input script var displayCanvas document getElementById displayCanvas var context displayCanvas getContext 2d var displayWidth displayCanvas width var displayHeight displayCanvas height off screen canvas used only when exporting image var exportCanvas document createElement canvas exportCanvas width displayWidth exportCanvas height displayHeight var exportCanvasContext exportCanvas getContext 2d var exportImage document createElement img buttons var btnExport document getElementById btnExport btnExport addEventListener click exportPressed false var btnRegenerate document getElementById btnRegenerate btnRegenerate addEventListener click regeneratePressed false var cbColor document getElementById cbColor var numCircles var maxMaxRad var minMaxRad var minRadFactor var circles var iterations var timer var drawsPerFrame var drawCount var bgColor urlColor var TWO_PI 2 Math PI var lineWidth init function init In other experiments you may wish to use more fractal curves circles and allow the radius of them to vary If so modify the next three variables numCircles 2 maxMaxRad 200 minMaxRad 200 You ll draw closed curves with varying radius The factor below should be set between 0 and 1 representing the size of the smallest radius with respect to the largest possible minRadFactor 0 The number of subdividing steps to take when creating a single fractal curve Can use more but anything over 10 thus 1024 points is overkill for a moderately sized canvas iterations 8 number of curves to draw on every tick of the timer drawsPerFrame 5 bgColor 000000 urlColor 444444 lineWidth 2 startGenerate function startGenerate drawCount 0 context setTransform 1 0 0 1 0 0 context clearRect 0 0 displayWidth displayHeight setCircles if timer clearInterval timer timer setInterval onTimer 1000 50 function setCircles var i var r0 g0 b0 var r1 g1 b1 var c0 c1 var maxR minR var grad var phase Math random TWO_PI var phaseVariance 0 2 Math PI 2 circles var randomizeColor cbColor checked for i 0 i numCircles i maxR minMaxRad Math random maxMaxRad minMaxRad minR minRadFactor maxR if randomizeColor r0 Math floor Math random 255 g0 Math floor Math random 255 b0 Math floor Math random 255 r1 Math floor Math random 255 g1 Math floor Math random 255 b1 Math floor Math random 255 c0 rgba r0 g0 b0 0 5 c1 rgba r1 g1 b1 0 5 grad context createRadialGradient 0 0 minR 0 0 maxR grad addColorStop 0 c0 grad addColorStop 1 c1 else if i 0 grad context createRadialGradient 0 0 minR 0 0 maxR grad addColorStop 0 rgba 200 140 0 0 1 grad addColorStop 1 rgba 220 10 0 0 1 grad addColorStop 1 rgba 0 170 255 0 5 grad addColorStop 0 rgba 255 0 170 0 5 else grad context createRadialGradient 0 0 minR 0 0 maxR grad addColorStop 0 rgba 220 100 40 0 5 grad addColorStop 1 rgba 255 255 40 0 5 var newCircle centerX maxMaxRad centerY displayHeight 2 50 maxRad maxR minRad minR color grad can set a gradient or solid color here fillColor rgba 0 0 0 1 param 0 changeSpeed 1 220 phase Math random TWO_PI the phase to use for a single fractal curve globalPhase phase 2 Math random 1 phaseVariance the curve as a whole will rise and fall by a sinusoid circles push newCircle newCircle pointList1 setLinePoints iterations newCircle pointList2 setLinePoints iterations function onTimer var i j var c var rad var point1 point2 var x0 y0 var cosParam var xSqueeze 0 75 cheap 3D effect by shortening in x direction var yOffset draw circles for j 0 j drawsPerFrame j drawCount for i 0 i numCircles i c circles i c param c changeSpeed if c param 1 c param 0 c pointList1 c pointList2 c pointList2 setLinePoints iterations cosParam 0 5 0 5 Math cos Math PI c param context strokeStyle c color context lineWidth lineWidth context fillStyle c fillColor context beginPath point1 c pointList1 first point2 c pointList2 first slowly rotate c phase 0 0002 theta c phase rad c minRad point1 y cosParam point2 y point1 y c maxRad c minRad move center c centerX 0 5 c centerY 0 04 yOffset 40 Math sin c globalPhase drawCount 2500 TWO_PI stop when off screen if c centerX displayWidth maxMaxRad clearInterval timer timer null you are drawing in new position by applying a transform This so the gradient will move with the drawing context setTransform xSqueeze 0 0 1 c centerX c centerY yOffset Drawing the curve involves stepping through a linked list of points defined by a fractal subdivision process It is like drawing a circle except with varying radius x0 xSqueeze rad Math cos theta y0 rad Math sin theta context lineTo x0 y0 while point1 next null point1 point1 next point2 point2 next theta TWO_PI point1 x cosParam point2 x point1 x c phase rad c minRad point1 y cosParam point2 y point1 y c maxRad c minRad x0 xSqueeze rad Math cos theta y0 rad Math sin theta context lineTo x0 y0 context closePath context stroke context fill Here is the function that defines a noisy but not wildly varying data set which we will use to draw the curves function setLinePoints iterations var pointList pointList first x 0 y 1 var lastPoint x 1 y 1 var minY 1 var maxY 1 var point var nextPoint var dx newX newY var ratio pointList first next lastPoint for var i 0 i iterations i point pointList first while point next null nextPoint point next dx nextPoint x point x newX 0 5 point x nextPoint x newY 0 5 point y nextPoint y newY dx Math random 2 1 var newPoint x newX y newY min max if newY minY minY newY else if newY maxY maxY newY put between points newPoint next nextPoint point next newPoint point nextPoint normalize to values between 0 and 1 if maxY minY var normalizeRate 1 maxY minY point pointList first while point null point y normalizeRate point y minY point point next unlikely that max min but could happen if using zero iterations In this case set all points equal to 1 else point pointList first while point null point y 1 point point next return pointList function exportPressed evt background otherwise background will be transparent exportCanvasContext fillStyle bgColor exportCanvasContext fillRect 0 0 displayWidth displayHeight draw exportCanvasContext drawImage displayCanvas 0 0 displayWidth displayHeight 0 0 displayWidth displayHeight add printed url to image exportCanvasContext fillStyle urlColor exportCanvasContext font bold italic 16px Helvetica Arial sans serif exportCanvasContext textBaseline top var metrics exportCanvasContext measureText rectangleworld com exportCanvasContext fillText rectangleworld com displayWidth metrics width 10 5 you will open a new window with the image contained within retrieve canvas image as data URL var dataURL exportCanvas toDataURL image png open a new window of appropriate size to hold the image var imageWindow window open fractalLineImage left 0 top 0 width displayWidth height displayHeight toolbar 0 resizable 0 write some html into the new window creating an empty image imageWindow document write title Export Image title imageWindow document write img id exportImage alt height displayHeight width displayWidth style position absolute left 0 top 0 imageWindow document close copy the image into the empty img in the newly opened window var exportImage imageWindow document getElementById exportImage exportImage src dataURL function regeneratePressed evt startGenerate console log ready script Different gradient types this uses a radial gradient style type text css body background color ffffff color 888888 style canvas id displayCanvas height 500 width 800 canvas input type button id btnRegenerate value regenerate input type button id btnExport value export image png script var displayCanvas document getElementById displayCanvas var context displayCanvas getContext 2d var displayWidth displayCanvas width var displayHeight displayCanvas height off screen canvas used only when exporting image var exportCanvas document createElement canvas exportCanvas width displayWidth exportCanvas height displayHeight var exportCanvasContext exportCanvas getContext 2d var exportImage document createElement img buttons var btnExport document getElementById btnExport btnExport addEventListener click exportPressed false var btnRegenerate document getElementById btnRegenerate btnRegenerate addEventListener click regeneratePressed false var numCircles var maxMaxRad var minMaxRad var minRadFactor var circles var iterations var timer var drawsPerFrame var drawCount var bgColor urlColor var TWO_PI 2 Math PI var lineWidth init function init In other experiments you may wish to use more fractal curves circles and allow the radius of them to vary If so modify the next three variables numCircles 1 maxMaxRad 200 minMaxRad 200 We draw closed curves with varying radius The factor below should be set between 0 and 1 representing the size of the smallest radius with respect to the largest possible minRadFactor 0 The number of subdividing steps to take when creating a single fractal curve Can use more but anything over 10 thus 1024 points is overkill for a moderately sized canvas iterations 8 number of curves to draw on every tick of the timer drawsPerFrame 4 bgColor FFFFFF urlColor EEEEEE lineWidth 1 startGenerate function startGenerate drawCount 0 context setTransform 1 0 0 1 0 0 context clearRect 0 0 displayWidth displayHeight setCircles if timer clearInterval timer timer setInterval onTimer 1000 50 function setCircles var i var r g b a var maxR minR var grad circles for i 0 i numCircles i maxR minMaxRad Math random maxMaxRad minMaxRad minR minRadFactor maxR define gradient grad context createRadialGradient 0 0 minR 0 0 maxR grad addColorStop 1 rgba 0 200 140 0 2 grad addColorStop 0 rgba 0 180 10 0 2 var newCircle centerX maxR centerY displayHeight 2 50 maxRad maxR minRad minR color grad can set a gradient or solid color here fillColor rgba 0 0 0 1 param 0 changeSpeed 1 250 phase Math random TWO_PI the phase to use for a single fractal curve globalPhase Math random TWO_PI the curve as a whole will rise and fall by a sinusoid circles push newCircle newCircle pointList1 setLinePoints iterations newCircle pointList2 setLinePoints iterations function onTimer var i j var c var rad var point1 point2 var x0 y0 var cosParam var xSqueeze 0 75 cheap 3D effect by shortening in x direction var yOffset draw circles for j 0 j drawsPerFrame j drawCount for i 0 i numCircles i c circles i c param c changeSpeed if c param 1 c param 0 c pointList1 c pointList2 c pointList2 setLinePoints iterations cosParam 0 5 0 5 Math cos Math PI c param context strokeStyle c color context lineWidth lineWidth context fillStyle c fillColor context beginPath point1 c pointList1 first point2 c pointList2 first slowly rotate c phase 0 0002 theta c phase rad c minRad point1 y cosParam point2 y point1 y c maxRad c minRad move center c centerX 0 5 c centerY 0 04 yOffset 40 Math sin c globalPhase drawCount 1000 TWO_PI stop when off screen if c centerX displayWidth maxMaxRad clearInterval timer timer null we are drawing in new position by applying a transform We are doing this so the gradient will move with the drawing context setTransform xSqueeze 0 0 1 c centerX c centerY yOffset Drawing the curve involves stepping through a linked list of points defined by a fractal subdivision process It is like drawing a circle except with varying radius x0 xSqueeze rad Math cos theta y0 rad Math sin theta context lineTo x0 y0 while point1 next null point1 point1 next point2 point2 next theta TWO_PI point1 x cosParam point2 x point1 x c phase rad c minRad point1 y cosParam point2 y point1 y c maxRad c minRad x0 xSqueeze rad Math cos theta y0 rad Math sin theta context lineTo x0 y0 context closePath context stroke context fill Here is the function that defines a noisy but not wildly varying data set which we will use to draw the curves function setLinePoints iterations var pointList pointList first x 0 y 1 var lastPoint x 1 y 1 var minY 1 var maxY 1 var point var nextPoint var dx newX newY var ratio pointList first next lastPoint for var i 0 i iterations i point pointList first while point next null nextPoint point next dx nextPoint x point x newX 0 5 point x nextPoint x newY 0 5 point y nextPoint y newY dx Math random 2 1 var newPoint x newX y newY min max if newY minY minY newY else if newY maxY maxY newY put between points newPoint next nextPoint point next newPoint point nextPoint normalize to values between 0 and 1 if maxY minY var normalizeRate 1 maxY minY point pointList first while point null point y normalizeRate point y minY point point next unlikely that max min but could happen if using zero iterations In this case set all points equal to 1 else point pointList first while point null point y 1 point point next return pointList function exportPressed evt background otherwise background will be transparent exportCanvasContext fillStyle bgColor exportCanvasContext fillRect 0 0 displayWidth displayHeight draw exportCanvasContext drawImage displayCanvas 0 0 displayWidth displayHeight 0 0 displayWidth displayHeight add printed url to image exportCanvasContext fillStyle urlColor exportCanvasContext font bold italic 16px Helvetica Arial sans serif exportCanvasContext textBaseline top var metrics exportCanvasContext measureText rectangleworld com exportCanvasContext fillText rectangleworld com displayWidth metrics width 10 5 we will open a new window with the image contained within retrieve canvas image as data URL var dataURL exportCanvas toDataURL image png open a new window of appropriate size to hold the image var imageWindow window open fractalLineImage left 0 top 0 width displayWidth height displayHeight toolbar 0 resizable 0 write some html into the new window creating an empty image imageWindow document write title Export Image title imageWindow document write img id exportImage alt height displayHeight width displayWidth style position absolute left 0 top 0 imageWindow document close copy the image into the empty img in the newly opened window var exportImage imageWindow document getElementById exportImage exportImage src dataURL function regeneratePressed evt startGenerate console log ready script More Mixing style type text css body background color ffffff color 888888 style canvas id displayCanvas height 500 width 800 canvas input type button id btnRegenerate value regenerate input type button id btnExport value export image png script var displayCanvas document getElementById displayCanvas var context displayCanvas getContext 2d var displayWidth displayCanvas width var displayHeight displayCanvas height off screen canvas used only when exporting image var exportCanvas document createElement canvas exportCanvas width displayWidth exportCanvas height displayHeight var exportCanvasContext exportCanvas getContext 2d buttons var btnExport document getElementById btnExport btnExport addEventListener click exportPressed false var btnRegenerate document getElementById btnRegenerate btnRegenerate addEventListener click regeneratePressed false var numCircles var maxMaxRad var minMaxRad var minRadFactor var circles var iterations var timer var drawsPerFrame var drawCount var bgColor urlColor var TWO_PI 2 Math PI var lineWidth init function init In other experiments you may wish to use more fractal curves circles and allow the radius of them to vary If so modify the next three variables numCircles 2 maxMaxRad 200 minMaxRad 200 You ll draw closed curves with varying radius The factor below should be set between 0 and 1 representing the size of the smallest radius with respect to the largest possible minRadFactor 0 The number of subdividing steps to take when creating a single fractal curve Can use more but anything over 10 thus 1024 points is overkill for a moderately sized canvas iterations 9 number of curves to draw on every tick of the timer drawsPerFrame 5 bgColor FFFFFF urlColor EEEEEE lineWidth 2 startGenerate function startGenerate drawCount 0 context setTransform 1 0 0 1 0 0 context clearRect 0 0 displayWidth displayHeight setCircles if timer clearInterval timer timer setInterval onTimer 1000 50 function setCircles var i var r0 g0 b0 var r1 g1 b1 var c0 c1 var maxR minR var grad var phase Math random TWO_PI circles for i 0 i numCircles i maxR minMaxRad Math random maxMaxRad minMaxRad minR minRadFactor maxR r 100 Math floor Math random 155 g 100 Math floor Math random 155 b 100 Math floor Math random 155 var newCircle centerX maxMaxRad centerY displayHeight 2 50 maxRad maxR minRad minR color rgba 0 0 0 0 15 fillColor rgba r g b 1 param 0 changeSpeed 1 300 phase Math random TWO_PI the phase to use for a single fractal curve globalPhase phase the curve as a whole will rise and fall by a sinusoid circles push newCircle newCircle pointList1 setLinePoints iterations newCircle pointList2 setLinePoints iterations function onTimer var i j var c var rad var point1 point2 var x0 y0 var cosParam var xSqueeze 0 8 cheap 3D effect by shortening in x direction var yOffset draw circles for j 0 j drawsPerFrame j drawCount for i 0 i numCircles i c circles i c param c changeSpeed if c param 1 c param 0 c pointList1 c pointList2 c pointList2 setLinePoints iterations cosParam 0 5 0 5 Math cos Math PI c param context strokeStyle c color context lineWidth lineWidth context fillStyle c fillColor context beginPath point1 c pointList1 first point2 c pointList2 first slowly rotate c phase 0 0001 theta c phase rad c minRad point1 y cosParam point2 y point1 y c maxRad c minRad move center c centerX 0 25 c centerY 0 02 stop when off screen if c centerX displayWidth maxMaxRad clearInterval timer timer null Drawing the curve involves stepping through a linked list of points defined by a fractal subdivision process It is like drawing a circle except with varying radius x0 c centerX xSqueeze rad Math cos theta y0 c centerY rad Math sin theta context lineTo x0 y0 while point1 next null point1 point1 next point2 point2 next theta TWO_PI point1 x cosParam point2 x point1 x c phase rad c minRad point1 y cosParam point2 y point1 y c maxRad c minRad x0 c centerX xSqueeze rad Math cos theta y0 c centerY rad Math sin theta context lineTo x0 y0 context closePath context stroke context fill Here is the function that defines a noisy but not wildly varying data set which we will use to draw the curves function setLinePoints iterations var pointList pointList first x 0 y 1 var lastPoint x 1 y 1 var minY 1 var maxY 1 var point var nextPoint var dx newX newY var ratio pointList first next lastPoint for var i 0 i iterations i point pointList first while point next null nextPoint point next let dx point x nextPoint x let dy point y nextPoint y Try experimenting with these 2 lines e g let newX point x 0 5 dx dy Math random 2 1 let newY point y 0 5 dy dx Math random 2 1 var newPoint x newX y newY min max if newY minY minY newY else if newY maxY maxY newY put between points newPoint next nextPoint point next newPoint point nextPoint normalize to values between 0 and 1 if maxY minY var normalizeRate 1 maxY minY point pointList first while point null point y normalizeRate point y minY point point next unlikely that max min but could happen if using zero iterations In this case set all points equal to 1 else point pointList first while point null point y 1 point point next return pointList function exportPressed evt background otherwise background will be transparent exportCanvasContext fillStyle bgColor exportCanvasContext fillRect 0 0 displayWidth displayHeight draw exportCanvasContext drawImage displayCanvas 0 0 displayWidth displayHeight 0 0 displayWidth displayHeight add printed url to image exportCanvasContext fillStyle urlColor exportCanvasContext font bold italic 16px Helvetica Arial sans serif exportCanvasContext textBaseline top var metrics exportCanvasContext measureText rectangleworld com exportCanvasContext fillText rectangleworld com displayWidth metrics width 10 5 you ll open a new window with the image contained within retrieve canvas image as data URL var dataURL exportCanvas toDataURL image png open a new window of appropriate size to hold the image var imageWindow window open fractalLineImage left 0 top 0 width displayWidth height displayHeight toolbar 0 resizable 0 write some html into the new window creating an empty image imageWindow document write title Export Image title imageWindow document write img id exportImage alt height displayHeight width displayWidth style position absolute left 0 top 0 imageWindow document close copy the image into the empty img in the newly opened window var exportImage imageWindow document getElementById exportImage exportImage src dataURL function regeneratePressed evt startGenerate console log ready script Experimenting with the patterns style type text css body background color ffffff color 888888 style canvas id displayCanvas height 500 width 800 canvas input type button id btnRegenerate value regenerate input type button id btnExport value export image png script var displayCanvas document getElementById displayCanvas var context displayCanvas getContext 2d var displayWidth displayCanvas width var displayHeight displayCanvas height off screen canvas used only when exporting image var exportCanvas document createElement canvas exportCanvas width displayWidth exportCanvas height displayHeight var exportCanvasContext exportCanvas getContext 2d buttons var btnExport document getElementById btnExport btnExport addEventListener click exportPressed false var btnRegenerate document getElementById btnRegenerate btnRegenerate addEventListener click regeneratePressed false var numCircles var maxMaxRad var minMaxRad var minRadFactor var circles var iterations var timer var drawsPerFrame var drawCount var bgColor urlColor var TWO_PI 2 Math PI var lineWidth init function init In other experiments you may wish to use more fractal curves circles and allow the radius of them to vary If so modify the next three variables numCircles 3 maxMaxRad 200 minMaxRad 200 You ll draw closed curves with varying radius The factor below should be set between 0 and 1 representing the size of the smallest radius with respect to the largest possible minRadFactor 0 The number of subdividing steps to take when creating a single fractal curve Can use more but anything over 10 thus 1024 points is overkill for a moderately sized canvas iterations 9 number of curves to draw on every tick of the timer drawsPerFrame 5 bgColor FFFFFF urlColor EEEEEE lineWidth 2 startGenerate function startGenerate drawCount 0 context setTransform 1 0 0 1 0 0 context clearRect 0 0 displayWidth displayHeight setCircles if timer clearInterval timer timer setInterval onTimer 1000 50 function setCircles var i var r0 g0 b0 var r1 g1 b1 var c0 c1 var maxR minR var grad var phase Math random TWO_PI circles for i 0 i numCircles i maxR minMaxRad Math random maxMaxRad minMaxRad minR minRadFactor maxR r 100 Math floor Math random 155 g 100 Math floor Math random 155 b 100 Math floor Math random 155 var newCircle centerX maxMaxRad centerY displayHeight 2 50 maxRad maxR minRad minR color rgba 0 0 0 0 15 fillColor rgba r g b 1 param 0 changeSpeed 1 300 phase Math random TWO_PI the phase to use for a single fractal curve globalPhase phase the curve as a whole will rise and fall by a sinusoid circles push newCircle newCircle pointList1 setLinePoints iterations newCircle pointList2 setLinePoints iterations function onTimer var i j var c var rad var point1 point2 var x0 y0 var cosParam var xSqueeze 0 8 cheap 3D effect by shortening in x direction var yOffset draw circles for j 0 j drawsPerFrame j drawCount for i 0 i numCircles i c circles i c param c changeSpeed if c param 1 c param 0 c pointList1 c pointList2 c pointList2 setLinePoints iterations cosParam 0 5 0 5 Math cos Math PI c param context strokeStyle c color context lineWidth lineWidth context fillStyle c fillColor context beginPath point1 c pointList1 first point2 c pointList2 first slowly rotate c phase 0 0001 theta c phase rad c minRad point1 y cosParam point2 y point1 y c maxRad c minRad move center c centerX 0 25 c centerY 0 02 stop when off screen if c centerX displayWidth maxMaxRad clearInterval timer timer null Drawing the curve involves stepping through a linked list of points defined by a fractal subdivision process It is like drawing a circle except with varying radius x0 c centerX xSqueeze rad Math cos theta y0 c centerY rad Math sin theta context lineTo x0 y0 while point1 next null point1 point1 next point2 point2 next theta TWO_PI point1 x cosParam point2 x point1 x c phase rad c minRad point1 y cosParam point2 y point1 y c maxRad c minRad x0 c centerX xSqueeze rad Math cos theta y0 c centerY rad Math sin theta context lineTo x0 y0 context closePath context stroke context fill var hack 0 0 Here is the function that defines a noisy but not wildly varying data set which we will use to draw the curves function setLinePoints iterations var pointList pointList first x 0 y 1 var lastPoint x 1 y 1 var minY 1 var maxY 1 var point var nextPoint var dx newX newY var ratio hack 0 01 pointList first next lastPoint for var i 0 i iterations i point pointList first while point next null nextPoint point next let dx point x nextPoint x let dy point y nextPoint y Try experimenting with these 2 lines e g let newX point x 0 5 dx dy Math random 2 1 0 001 let newY point y 0 5 dy dx Math random 2 1 Math sin hack 0 5 0 5 0 05 var newPoint x newX y newY min max if newY minY minY newY else if newY maxY maxY newY put between points newPoint next nextPoint point next newPoint point nextPoint normalize to values between 0 and 1 if maxY minY var normalizeRate 1 maxY minY point pointList first while point null point y normalizeRate point y minY point point next unlikely that max min but could happen if using zero iterations In this case set all points equal to 1 else point pointList first while point null point y 1 point point next return pointList function exportPressed evt background otherwise background will be transparent exportCanvasContext fillStyle bgColor exportCanvasContext fillRect 0 0 displayWidth displayHeight draw exportCanvasContext drawImage displayCanvas 0 0 displayWidth displayHeight 0 0 displayWidth displayHeight add printed url to image exportCanvasContext fillStyle urlColor exportCanvasContext font bold italic 16px Helvetica Arial sans serif exportCanvasContext textBaseline top var metrics exportCanvasContext measureText rectangleworld com exportCanvasContext fillText rectangleworld com displayWidth metrics width 10 5 you ll open a new window with the image contained within retrieve canvas image as data URL var dataURL exportCanvas toDataURL image png open a new window of appropriate size to hold the image var imageWindow window open fractalLineImage left 0 top 0 width displayWidth height displayHeight toolbar 0 resizable 0 write some html into the new window creating an empty image imageWindow document write title Export Image title imageWindow document write img id exportImage alt height displayHeight width displayWidth style position absolute left 0 top 0 imageWindow document close copy the image into the empty img in the newly opened window var exportImage imageWindow document getElementById exportImage exportImage src dataURL function regeneratePressed evt startGenerate console log ready script
r1 g1 b1 var c0 c1 var maxR minR var grad var phase Math random TWO_PI var phaseVariance 0 2 Math PI 2 circles var randomizeColor cbColor checked for i 0 i numCircles i maxR minMaxRad Math random maxMaxRad minMaxRad minR minRadFactor maxR if randomizeColor r0 Math floor Math random 255 g0 Math floor Math random 255 b0 Math floor Math random 255 r1 Math floor Math random 255 g1 Math floor Math random 255 b1 Math floor Math random 255 c0 rgba r0 g0 b0 0 5 c1 rgba r1 g1 b1 0 5 grad context createRadialGradient 0 0 minR 0 0 maxR grad addColorStop 0 c0 grad addColorStop 1 c1 else if i 0 grad context createRadialGradient 0 0 minR 0 0 maxR grad addColorStop 0 rgba 200 140 0 0 1 grad addColorStop 1 rgba 220 10 0 0 1 grad addColorStop 1 rgba 0 170 255 0 5 grad addColorStop 0 rgba 255 0 170 0 5 else grad context createRadialGradient 0 0 minR 0 0 maxR grad addColorStop 0 rgba 220 100 40 0 5 grad addColorStop 1 rgba 255 255 40 0 5 var newCircle centerX maxMaxRad centerY displayHeight 2 50 maxRad maxR minRad minR color grad can set a gradient or solid color here fillColor rgba 0 0 0 1 param 0 changeSpeed 1 220 phase Math random TWO_PI the phase to use for a single fractal curve globalPhase phase 2 Math random 1 phaseVariance the curve as a whole will rise and fall by a sinusoid circles push newCircle newCircle pointList1 setLinePoints iterations newCircle pointList2 setLinePoints iterations function onTimer var i j var c var rad var point1 point2 var x0 y0 var cosParam var xSqueeze 0 75 cheap 3D effect by shortening in x direction var yOffset draw circles for j 0 j drawsPerFrame j drawCount for i 0 i numCircles i c circles i c param c changeSpeed if c param 1 c param 0 c pointList1 c pointList2 c pointList2 setLinePoints iterations cosParam 0 5 0 5 Math cos Math PI c param context strokeStyle c color context lineWidth lineWidth context fillStyle c fillColor context beginPath point1 c pointList1 first point2 c pointList2 first slowly rotate c phase 0 0002 theta c phase rad c minRad point1 y cosParam point2 y point1 y c maxRad c minRad move center c centerX 0 5 c centerY 0 04 yOffset 40 Math sin c globalPhase drawCount 2500 TWO_PI stop when off screen if c centerX displayWidth maxMaxRad clearInterval timer timer null you are drawing in new position by applying a transform This so the gradient will move with the drawing context setTransform xSqueeze 0 0 1 c centerX c centerY yOffset Drawing the curve involves stepping through a linked list of points defined by a fractal subdivision process It is like drawing a circle except with varying radius x0 xSqueeze rad Math cos theta y0 rad Math sin theta context lineTo x0 y0 while point1 next null point1 point1 next point2 point2 next theta TWO_PI point1 x cosParam point2 x point1 x c phase rad c minRad point1 y cosParam point2 y point1 y c maxRad c minRad x0 xSqueeze rad Math cos theta y0 rad Math sin theta context lineTo x0 y0 context closePath context stroke context fill Here is the function that defines a noisy but not wildly varying data set which we will use to draw the curves function setLinePoints iterations var pointList pointList first x 0 y 1 var lastPoint x 1 y 1 var minY 1 var maxY 1 var point var nextPoint var dx newX newY var ratio pointList first next lastPoint for var i 0 i iterations i point pointList first while point next null nextPoint point next dx nextPoint x point x newX 0 5 point x nextPoint x newY 0 5 point y nextPoint y newY dx Math random 2 1 var newPoint x newX y newY min max if newY minY minY newY else if newY maxY maxY newY put between points newPoint next nextPoint point next newPoint point nextPoint normalize to values between 0 and 1 if maxY minY var normalizeRate 1 maxY minY point pointList first while point null point y normalizeRate point y minY point point next unlikely that max min but could happen if using zero iterations In this case set all points equal to 1 else point pointList first while point null point y 1 point point next return pointList function exportPressed evt background otherwise background will be transparent exportCanvasContext fillStyle bgColor exportCanvasContext fillRect 0 0 displayWidth displayHeight draw exportCanvasContext drawImage displayCanvas 0 0 displayWidth displayHeight 0 0 displayWidth displayHeight add printed url to image exportCanvasContext fillStyle urlColor exportCanvasContext font bold italic 16px Helvetica Arial sans serif exportCanvasContext textBaseline top var metrics exportCanvasContext measureText rectangleworld com exportCanvasContext fillText rectangleworld com displayWidth metrics width 10 5 you will open a new window with the image contained within retrieve canvas image as data URL var dataURL exportCanvas toDataURL image png open a new window of appropriate size to hold the image var imageWindow window open fractalLineImage left 0 top 0 width displayWidth height displayHeight toolbar 0 resizable 0 write some html into the new window creating an empty image imageWindow document write title Export Image title imageWindow document write img id exportImage alt height displayHeight width displayWidth style position absolute left 0 top 0 imageWindow document close copy the image into the empty img in the newly opened window var exportImage imageWindow document getElementById exportImage exportImage src dataURL function regeneratePressed evt startGenerate console log ready script Different gradient types this uses a radial gradient style type text css body background color ffffff color 888888 style canvas id displayCanvas height 500 width 800 canvas input type button id btnRegenerate value regenerate input type button id btnExport value export image png script var displayCanvas document getElementById displayCanvas var context displayCanvas getContext 2d var displayWidth displayCanvas width var displayHeight displayCanvas height off screen canvas used only when exporting image var exportCanvas document createElement canvas exportCanvas width displayWidth exportCanvas height displayHeight var exportCanvasContext exportCanvas getContext 2d var exportImage document createElement img buttons var btnExport document getElementById btnExport btnExport addEventListener click exportPressed false var btnRegenerate document getElementById btnRegenerate btnRegenerate addEventListener click regeneratePressed false var numCircles var maxMaxRad var minMaxRad var minRadFactor var circles var iterations var timer var drawsPerFrame var drawCount var bgColor urlColor var TWO_PI 2 Math PI var lineWidth init function init In other experiments you may wish to use more fractal curves circles and allow the radius of them to vary If so modify the next three variables numCircles 1 maxMaxRad 200 minMaxRad 200 We draw closed curves with varying radius The factor below should be set between 0 and 1 representing the size of the smallest radius with respect to the largest possible minRadFactor 0 The number of subdividing steps to take when creating a single fractal curve Can use more but anything over 10 thus 1024 points is overkill for a moderately sized canvas iterations 8 number of curves to draw on every tick of the timer drawsPerFrame 4 bgColor FFFFFF urlColor EEEEEE lineWidth 1 startGenerate function startGenerate drawCount 0 context setTransform 1 0 0 1 0 0 context clearRect 0 0 displayWidth displayHeight setCircles if timer clearInterval timer timer setInterval onTimer 1000 50 function setCircles var i var r g b a var maxR minR var grad circles for i 0 i numCircles i maxR minMaxRad Math random maxMaxRad minMaxRad minR minRadFactor maxR define gradient grad context createRadialGradient 0 0 minR 0 0 maxR grad addColorStop 1 rgba 0 200 140 0 2 grad addColorStop 0 rgba 0 180 10 0 2 var newCircle centerX maxR centerY displayHeight 2 50 maxRad maxR minRad minR color grad can set a gradient or solid color here fillColor rgba 0 0 0 1 param 0 changeSpeed 1 250 phase Math random TWO_PI the phase to use for a single fractal curve globalPhase Math random TWO_PI the curve as a whole will rise and fall by a sinusoid circles push newCircle newCircle pointList1 setLinePoints iterations newCircle pointList2 setLinePoints iterations function onTimer var i j var c var rad var point1 point2 var x0 y0 var cosParam var xSqueeze 0 75 cheap 3D effect by shortening in x direction var yOffset draw circles for j 0 j drawsPerFrame j drawCount for i 0 i numCircles i c circles i c param c changeSpeed if c param 1 c param 0 c pointList1 c pointList2 c pointList2 setLinePoints iterations cosParam 0 5 0 5 Math cos Math PI c param context strokeStyle c color context lineWidth lineWidth context fillStyle c fillColor context beginPath point1 c pointList1 first point2 c pointList2 first slowly rotate c phase 0 0002 theta c phase rad c minRad point1 y cosParam point2 y point1 y c maxRad c minRad move center c centerX 0 5 c centerY 0 04 yOffset 40 Math sin c globalPhase drawCount 1000 TWO_PI stop when off screen if c centerX displayWidth maxMaxRad clearInterval timer timer null we are drawing in new position by applying a transform We are doing this so the gradient will move with the drawing context setTransform xSqueeze 0 0 1 c centerX c centerY yOffset Drawing the curve involves stepping through a linked list of points defined by a fractal subdivision process It is like drawing a circle except with varying radius x0 xSqueeze rad Math cos theta y0 rad Math sin theta context lineTo x0 y0 while point1 next null point1 point1 next point2 point2 next theta TWO_PI point1 x cosParam point2 x point1 x c phase rad c minRad point1 y cosParam point2 y point1 y c maxRad c minRad x0 xSqueeze rad Math cos theta y0 rad Math sin theta context lineTo x0 y0 context closePath context stroke context fill Here is the function that defines a noisy but not wildly varying data set which we will use to draw the curves function setLinePoints iterations var pointList pointList first x 0 y 1 var lastPoint x 1 y 1 var minY 1 var maxY 1 var point var nextPoint var dx newX newY var ratio pointList first next lastPoint for var i 0 i iterations i point pointList first while point next null nextPoint point next dx nextPoint x point x newX 0 5 point x nextPoint x newY 0 5 point y nextPoint y newY dx Math random 2 1 var newPoint x newX y newY min max if newY minY minY newY else if newY maxY maxY newY put between points newPoint next nextPoint point next newPoint point nextPoint normalize to values between 0 and 1 if maxY minY var normalizeRate 1 maxY minY point pointList first while point null point y normalizeRate point y minY point point next unlikely that max min but could happen if using zero iterations In this case set all points equal to 1 else point pointList first while point null point y 1 point point next return pointList function exportPressed evt background otherwise background will be transparent exportCanvasContext fillStyle bgColor exportCanvasContext fillRect 0 0 displayWidth displayHeight draw exportCanvasContext drawImage displayCanvas 0 0 displayWidth displayHeight 0 0 displayWidth displayHeight add printed url to image exportCanvasContext fillStyle urlColor exportCanvasContext font bold italic 16px Helvetica Arial sans serif exportCanvasContext textBaseline top var metrics exportCanvasContext measureText rectangleworld com exportCanvasContext fillText rectangleworld com displayWidth metrics width 10 5 we will open a new window with the image contained within retrieve canvas image as data URL var dataURL exportCanvas toDataURL image png open a new window of appropriate size to hold the image var imageWindow window open fractalLineImage left 0 top 0 width displayWidth height displayHeight toolbar 0 resizable 0 write some html into the new window creating an empty image imageWindow document write title Export Image title imageWindow document write img id exportImage alt height displayHeight width displayWidth style position absolute left 0 top 0 imageWindow document close copy the image into the empty img in the newly opened window var exportImage imageWindow document getElementById exportImage exportImage src dataURL function regeneratePressed evt startGenerate console log ready script More Mixing style type text css body background color ffffff color 888888 style canvas id displayCanvas height 500 width 800 canvas input type button id btnRegenerate value regenerate input type button id btnExport value export image png script var displayCanvas document getElementById displayCanvas var context displayCanvas getContext 2d var displayWidth displayCanvas width var displayHeight displayCanvas height off screen canvas used only when exporting image var exportCanvas document createElement canvas exportCanvas width displayWidth exportCanvas height displayHeight var exportCanvasContext exportCanvas getContext 2d buttons var btnExport document getElementById btnExport btnExport addEventListener click exportPressed false var btnRegenerate document getElementById btnRegenerate btnRegenerate addEventListener click regeneratePressed false var numCircles var maxMaxRad var minMaxRad var minRadFactor var circles var iterations var timer var drawsPerFrame var drawCount var bgColor urlColor var TWO_PI 2 Math PI var lineWidth init function init In other experiments you may wish to use more fractal curves circles and allow the radius of them to vary If so modify the next three variables numCircles 2 maxMaxRad 200 minMaxRad 200 You ll draw closed curves with varying radius The factor below should be set between 0 and 1 representing the size of the smallest radius with respect to the largest possible minRadFactor 0 The number of subdividing steps to take when creating a single fractal curve Can use more but anything over 10 thus 1024 points is overkill for a moderately sized canvas iterations 9 number of curves to draw on every tick of the timer drawsPerFrame 5 bgColor FFFFFF urlColor EEEEEE lineWidth 2 startGenerate function startGenerate drawCount 0 context setTransform 1 0 0 1 0 0 context clearRect 0 0 displayWidth displayHeight setCircles if timer clearInterval timer timer setInterval onTimer 1000 50 function setCircles var i var r0 g0 b0 var r1 g1 b1 var c0 c1 var maxR minR var grad var phase Math random TWO_PI circles for i 0 i numCircles i maxR minMaxRad Math random maxMaxRad minMaxRad minR minRadFactor maxR r 100 Math floor Math random 155 g 100 Math floor Math random 155 b 100 Math floor Math random 155 var newCircle centerX maxMaxRad centerY displayHeight 2 50 maxRad maxR minRad minR color rgba 0 0 0 0 15 fillColor rgba r g b 1 param 0 changeSpeed 1 300 phase Math random TWO_PI the phase to use for a single fractal curve globalPhase phase the curve as a whole will rise and fall by a sinusoid circles push newCircle newCircle pointList1 setLinePoints iterations newCircle pointList2 setLinePoints iterations function onTimer var i j var c var rad var point1 point2 var x0 y0 var cosParam var xSqueeze 0 8 cheap 3D effect by shortening in x direction var yOffset draw circles for j 0 j drawsPerFrame j drawCount for i 0 i numCircles i c circles i c param c changeSpeed if c param 1 c param 0 c pointList1 c pointList2 c pointList2 setLinePoints iterations cosParam 0 5 0 5 Math cos Math PI c param context strokeStyle c color context lineWidth lineWidth context fillStyle c fillColor context beginPath point1 c pointList1 first point2 c pointList2 first slowly rotate c phase 0 0001 theta c phase rad c minRad point1 y cosParam point2 y point1 y c maxRad c minRad move center c centerX 0 25 c centerY 0 02 stop when off screen if c centerX displayWidth maxMaxRad clearInterval timer timer null Drawing the curve involves stepping through a linked list of points defined by a fractal subdivision process It is like drawing a circle except with varying radius x0 c centerX xSqueeze rad Math cos theta y0 c centerY rad Math sin theta context lineTo x0 y0 while point1 next null point1 point1 next point2 point2 next theta TWO_PI point1 x cosParam point2 x point1 x c phase rad c minRad point1 y cosParam point2 y point1 y c maxRad c minRad x0 c centerX xSqueeze rad Math cos theta y0 c centerY rad Math sin theta context lineTo x0 y0 context closePath context stroke context fill Here is the function that defines a noisy but not wildly varying data set which we will use to draw the curves function setLinePoints iterations var pointList pointList first x 0 y 1 var lastPoint x 1 y 1 var minY 1 var maxY 1 var point var nextPoint var dx newX newY var ratio pointList first next lastPoint for var i 0 i iterations i point pointList first while point next null nextPoint point next let dx point x nextPoint x let dy point y nextPoint y Try experimenting with these 2 lines e g let newX point x 0 5 dx dy Math random 2 1 let newY point y 0 5 dy dx Math random 2 1 var newPoint x newX y newY min max if newY minY minY newY else if newY maxY maxY newY put between points newPoint next nextPoint point next newPoint point nextPoint normalize to values between 0 and 1 if maxY minY var normalizeRate 1 maxY minY point pointList first while point null point y normalizeRate point y minY point point next unlikely that max min but could happen if using zero iterations In this case set all points equal to 1 else point pointList first while point null point y 1 point point next return pointList function exportPressed evt background otherwise background will be transparent exportCanvasContext fillStyle bgColor exportCanvasContext fillRect 0 0 displayWidth displayHeight draw exportCanvasContext drawImage displayCanvas 0 0 displayWidth displayHeight 0 0 displayWidth displayHeight add printed url to image exportCanvasContext fillStyle urlColor exportCanvasContext font bold italic 16px Helvetica Arial sans serif exportCanvasContext textBaseline top var metrics exportCanvasContext measureText rectangleworld com exportCanvasContext fillText rectangleworld com displayWidth metrics width 10 5 you ll open a new window with the image contained within retrieve canvas image as data URL var dataURL exportCanvas toDataURL image png open a new window of appropriate size to hold the image var imageWindow window open fractalLineImage left 0 top 0 width displayWidth height displayHeight toolbar 0 resizable 0 write some html into the new window creating an empty image imageWindow document write title Export Image title imageWindow document write img id exportImage alt height displayHeight width displayWidth style position absolute left 0 top 0 imageWindow document close copy the image into the empty img in the newly opened window var exportImage imageWindow document getElementById exportImage exportImage src dataURL function regeneratePressed evt startGenerate console log ready script Experimenting with the patterns style type text css body background color ffffff color 888888 style canvas id displayCanvas height 500 width 800 canvas input type button id btnRegenerate value regenerate input type button id btnExport value export image png script var displayCanvas document getElementById displayCanvas var context displayCanvas getContext 2d var displayWidth displayCanvas width var displayHeight displayCanvas height off screen canvas used only when exporting image var exportCanvas document createElement canvas exportCanvas width displayWidth exportCanvas height displayHeight var exportCanvasContext exportCanvas getContext 2d buttons var btnExport document getElementById btnExport btnExport addEventListener click exportPressed false var btnRegenerate document getElementById btnRegenerate btnRegenerate addEventListener click regeneratePressed false var numCircles var maxMaxRad var minMaxRad var minRadFactor var circles var iterations var timer var drawsPerFrame var drawCount var bgColor urlColor var TWO_PI 2 Math PI var lineWidth init function init In other experiments you may wish to use more fractal curves circles and allow the radius of them to vary If so modify the next three variables numCircles 3 maxMaxRad 200 minMaxRad 200 You ll draw closed curves with varying radius The factor below should be set between 0 and 1 representing the size of the smallest radius with respect to the largest possible minRadFactor 0 The number of subdividing steps to take when creating a single fractal curve Can use more but anything over 10 thus 1024 points is overkill for a moderately sized canvas iterations 9 number of curves to draw on every tick of the timer drawsPerFrame 5 bgColor FFFFFF urlColor EEEEEE lineWidth 2 startGenerate function startGenerate drawCount 0 context setTransform 1 0 0 1 0 0 context clearRect 0 0 displayWidth displayHeight setCircles if timer clearInterval timer timer setInterval onTimer 1000 50 function setCircles var i var r0 g0 b0 var r1 g1 b1 var c0 c1 var maxR minR var grad var phase Math random TWO_PI circles for i 0 i numCircles i maxR minMaxRad Math random maxMaxRad minMaxRad minR minRadFactor maxR r 100 Math floor Math random 155 g 100 Math floor Math random 155 b 100 Math floor Math random 155 var newCircle centerX maxMaxRad centerY displayHeight 2 50 maxRad maxR minRad minR color rgba 0 0 0 0 15 fillColor rgba r g b 1 param 0 changeSpeed 1 300 phase Math random TWO_PI the phase to use for a single fractal curve globalPhase phase the curve as a whole will rise and fall by a sinusoid circles push newCircle newCircle pointList1 setLinePoints iterations newCircle pointList2 setLinePoints iterations function onTimer var i j var c var rad var point1 point2 var x0 y0 var cosParam var xSqueeze 0 8 cheap 3D effect by shortening in x direction var yOffset draw circles for j 0 j drawsPerFrame j drawCount for i 0 i numCircles i c circles i c param c changeSpeed if c param 1 c param 0 c pointList1 c pointList2 c pointList2 setLinePoints iterations cosParam 0 5 0 5 Math cos Math PI c param context strokeStyle c color context lineWidth lineWidth context fillStyle c fillColor context beginPath point1 c pointList1 first point2 c pointList2 first slowly rotate c phase 0 0001 theta c phase rad c minRad point1 y cosParam point2 y point1 y c maxRad c minRad move center c centerX 0 25 c centerY 0 02 stop when off screen if c centerX displayWidth maxMaxRad clearInterval timer timer null Drawing the curve involves stepping through a linked list of points defined by a fractal subdivision process It is like drawing a circle except with varying radius x0 c centerX xSqueeze rad Math cos theta y0 c centerY rad Math sin theta context lineTo x0 y0 while point1 next null point1 point1 next point2 point2 next theta TWO_PI point1 x cosParam point2 x point1 x c phase rad c minRad point1 y cosParam point2 y point1 y c maxRad c minRad x0 c centerX xSqueeze rad Math cos theta y0 c centerY rad Math sin theta context lineTo x0 y0 context closePath context stroke context fill var hack 0 0 Here is the function that defines a noisy but not wildly varying data set which we will use to draw the curves function setLinePoints iterations var pointList pointList first x 0 y 1 var lastPoint x 1 y 1 var minY 1 var maxY 1 var point var nextPoint var dx newX newY var ratio hack 0 01 pointList first next lastPoint for var i 0 i iterations i point pointList first while point next null nextPoint point next let dx point x nextPoint x let dy point y nextPoint y Try experimenting with these 2 lines e g let newX point x 0 5 dx dy Math random 2 1 0 001 let newY point y 0 5 dy dx Math random 2 1 Math sin hack 0 5 0 5 0 05 var newPoint x newX y newY min max if newY minY minY newY else if newY maxY maxY newY put between points newPoint next nextPoint point next newPoint point nextPoint normalize to values between 0 and 1 if maxY minY var normalizeRate 1 maxY minY point pointList first while point null point y normalizeRate point y minY point point next unlikely that max min but could happen if using zero iterations In this case set all points equal to 1 else point pointList first while point null point y 1 point point next return pointList function exportPressed evt background otherwise background will be transparent exportCanvasContext fillStyle bgColor exportCanvasContext fillRect 0 0 displayWidth displayHeight draw exportCanvasContext drawImage displayCanvas 0 0 displayWidth displayHeight 0 0 displayWidth displayHeight add printed url to image exportCanvasContext fillStyle urlColor exportCanvasContext font bold italic 16px Helvetica Arial sans serif exportCanvasContext textBaseline top var metrics exportCanvasContext measureText rectangleworld com exportCanvasContext fillText rectangleworld com displayWidth metrics width 10 5 you ll open a new window with the image contained within retrieve canvas image as data URL var dataURL exportCanvas toDataURL image png open a new window of appropriate size to hold the image var imageWindow window open fractalLineImage left 0 top 0 width displayWidth height displayHeight toolbar 0 resizable 0 write some html into the new window creating an empty image imageWindow document write title Export Image title imageWindow document write img id exportImage alt height displayHeight width displayWidth style position absolute left 0 top 0 imageWindow document close copy the image into the empty img in the newly opened window var exportImage imageWindow document getElementById exportImage exportImage src dataURL function regeneratePressed evt startGenerate console log ready script