• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Drawing fractals patterns with JavaScript canvas

Joined
Apr 9, 2018
Messages
781 (0.32/day)
This is an interesting programming project I did a few years ago when I was super interested in playing with code.
The "coolFractals" program was originally not mine (it was an example program included with Just Basic) but my idea was to port it to JavaScript.
Obviously the benefits of HTML is that the code can now be run on any computer using just a browser.
Anyone who's interested in experimenting with it, just copy the code into Notepad and save it with a ".html" extension.

'coolFractal.bas contributed by Rod
nomainwin
dim col$(12)
WindowWidth = DisplayWidth
WindowHeight = DisplayHeight
UpperLeftX = int((DisplayWidth-WindowWidth)/2)
UpperLeftY = int((DisplayHeight-WindowHeight)/2)
midx=int(WindowWidth/2)
midy=int(WindowHeight/2)
button #1, "Clear", [clear], LR, 50, 10
open "Fractals" for graphics_nsb as #1
print #1, "trapclose [quit]"

[clear]
print #1, "down ; fill black"

[draw]
'set up some random colors
for c=0 to 12
col$(c)=str$(int(rnd(0)*256))+" "+str$(int(rnd(0)*256))+" "+str$(int(rnd(0)*256))
next c

'set up some random starting positions
a=rnd(0)
b=0.9998
c=2-2*a
dots=12000
x=j=0
y=rnd(0)*12+0.1

'calculate and draw the points
for i=0 to dots
scan
z=x
x=b*y+j
j=a*x+c*(x^2)/(1+x^2)
y=j-z
xp=(x*20)+midx
yp=(y*20)+midy
print #1, "color ";col$(i/1000)
print #1, "set ";xp;" ";yp
next i
goto [draw]

[quit]
close #1
end

My updated version of the code for HTML:

HTML:
<!DOCTYPE html>
<html>

<body>

<canvas id="myCanvas" style="border:1px solid #000000">
</canvas>

<script language="javaScript">

//Variables
    var a;
    var b;
    var c;
    var x;
    var y;
    var z;
    var j;
    var dots;
    var xp;
    var yp;
    var i;
    var colors;
    var randomColour;
    var canvas = document.getElementById("myCanvas");
    var context = canvas.getContext("2d");

//Resize and fill in canvas
canvas.width = screen.width;
canvas.height = screen.height;
context.fillStyle = "#000000";
context.fillRect(0, 0, canvas.width, canvas.height);

//Run code immediately to remove timeout() delay.
    //Set up some random starting positions
        a = Math.random();
        b = 0.9998;
        c = 2 - 2*a;
        dots = 80000;
        x = 0;
        j = 0;
        y = Math.random()*12 + 0.1;
    //Calculate and draw the points
        i = 0;
        function draw() {
            if (i<=dots) {
                z = x;
                x = b*y+j;
                j = a*x+(c*(Math.pow(x,2)))/(1+Math.pow(x,2));
                y = j-z;
                xp = (x*20) + (canvas.width/2);
                yp = (y*20) + (canvas.height/2);
                context.fillRect(xp, yp, 2, 2);
                i++;
                setTimeout(draw);
            };
        };
        setTimeout(draw)
    timeout();

function timeout() {
        //Generate a random hex colour
            colors = ["#FF0000","#FFD700","#008000","#0000FF"]
            randomColour = colors[Math.floor(Math.random() * colors.length)];
            context.fillStyle = randomColour;
    setTimeout(function () {
        //Set up some random starting positions
            a = Math.random();
            b = 0.9998;
            c = 2 - 2*a;
            dots = 50000;
            x = 0;
            j = 0;
            y = Math.random()*12 + 0.1;
        //Calculate and draw the points
            i = 0;
            function draw() {
                if (i<=dots) {
                    z = x;
                    x = b*y+j;
                    j = a*x+(c*(Math.pow(x,2)))/(1+Math.pow(x,2));
                    y = j-z;
                    xp = (x*20) + (canvas.width/2);
                    yp = (y*20) + (canvas.height/2);
                    context.fillRect(xp, yp, 2, 2);
                    i++;
                    setTimeout(draw);
                };
            };
            setTimeout(draw)
        timeout();
    }, 6000);
}

timeout();

</script>
  
</body>
</html>

Screenshot:

132346
 
Last edited:

Solaris17

Super Dainty Moderator
Staff member
Joined
Aug 16, 2005
Messages
26,967 (3.83/day)
Location
Alabama
System Name RogueOne
Processor Xeon W9-3495x
Motherboard ASUS w790E Sage SE
Cooling SilverStone XE360-4677
Memory 128gb Gskill Zeta R5 DDR5 RDIMMs
Video Card(s) MSI SUPRIM Liquid X 4090
Storage 1x 2TB WD SN850X | 2x 8TB GAMMIX S70
Display(s) 49" Philips Evnia OLED (49M2C8900)
Case Thermaltake Core P3 Pro Snow
Audio Device(s) Moondrop S8's on schitt Gunnr
Power Supply Seasonic Prime TX-1600
Mouse Lamzu Atlantis mini (White)
Keyboard Monsgeek M3 Lavender, Moondrop Luna lights
VR HMD Quest 3
Software Windows 11 Pro Workstation
Benchmark Scores I dont have time for that.
This is cool thanks for sharing.
 
Joined
Mar 23, 2016
Messages
4,844 (1.53/day)
Processor Core i7-13700
Motherboard MSI Z790 Gaming Plus WiFi
Cooling Cooler Master RGB something
Memory Corsair DDR5-6000 small OC to 6200
Video Card(s) XFX Speedster SWFT309 AMD Radeon RX 6700 XT CORE Gaming
Storage 970 EVO NVMe M.2 500GB,,WD850N 2TB
Display(s) Samsung 28” 4K monitor
Case Phantek Eclipse P400S
Audio Device(s) EVGA NU Audio
Power Supply EVGA 850 BQ
Mouse Logitech G502 Hero
Keyboard Logitech G G413 Silver
Software Windows 11 Professional v23H2
Pegs the RTX 2060 more than the Ryzen 3600
Untitled.jpg
 

Solaris17

Super Dainty Moderator
Staff member
Joined
Aug 16, 2005
Messages
26,967 (3.83/day)
Location
Alabama
System Name RogueOne
Processor Xeon W9-3495x
Motherboard ASUS w790E Sage SE
Cooling SilverStone XE360-4677
Memory 128gb Gskill Zeta R5 DDR5 RDIMMs
Video Card(s) MSI SUPRIM Liquid X 4090
Storage 1x 2TB WD SN850X | 2x 8TB GAMMIX S70
Display(s) 49" Philips Evnia OLED (49M2C8900)
Case Thermaltake Core P3 Pro Snow
Audio Device(s) Moondrop S8's on schitt Gunnr
Power Supply Seasonic Prime TX-1600
Mouse Lamzu Atlantis mini (White)
Keyboard Monsgeek M3 Lavender, Moondrop Luna lights
VR HMD Quest 3
Software Windows 11 Pro Workstation
Benchmark Scores I dont have time for that.
Top