Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <body bgcolor=#222>
<div>
<span style=
'font-size:25px'>Buddhabrot Renderer</span>
<br>
<br>Settings
<br><input id=input_iterations value=1000 type=number min=10 max=1e4> Max Iterations
<br><input id=input_alpha value=50 type=number min=1 max=255> Brightness
<br><input id=input_width value=1920 type=number min=64 max=1024*16> Canvas Width
<br><input id=input_height value=1080 type=number min=64 max=1024*16> Canvas Height
<br><input id=input_scale value=.45 type=number min=0 max=1e3> Scale
<br><input id=input_updateRate oninput="updateRate=parseInt(input_updateRate.value);" value=100 type=number min=1 max=1e3> Update Speed
<br>
<br>Fractal Type
<br><input name=input_fractalType value=0 type=radio class=input_box> Buddhabrot
<br><input name=input_fractalType value=1 type=radio class=input_box checked> Anti-Buddhabrot
<br>
<br><input id=checkbox_useSymmetry type=checkbox class=input_box checked>Make Symmetric
<br><input id=checkbox_greyScale type=checkbox class=input_box>Greyscale
<br><input id=checkbox_rotate type=checkbox class=input_box>Rotate
<br>
<br><button onclick=Screenshot()>Save</button>
<button onclick=Reset()>Restart</button>
<br><br>Load <input id=imageFileInput style='width:300px;'type="file" accept="image/*">
</div>
<center>
<canvas id=canvas></canvas>
</center>
              
            
!

CSS

              
                body
{
    overflow:hidden;
    color:#FFF;
    margin:0;
}
div
{
    position:absolute;
    text-align:left;
    left:10px;
    top:10px;
}
input
{
    width:100px;
    margin:5px;
}
input.input_box
{
    width:20px; 
    height:20px;
}
*
{
    font-size:20px;
}
              
            
!

JS

              
                // Buddhabrot Renderer
// Created by Frank Force 2020
// License GNU General Public License v3.0

'use strict';

let iterations;
let alpha;
let updateRate;
let scale;
let width;
let height;
let fractalType;
let useSymmetry;
let greyScale;
let rotate;

function Loop()
{
    requestAnimationFrame(Loop);
    
    // update params
    iterations = parseInt(input_iterations.value);
    alpha = parseInt(input_alpha.value);
    updateRate = parseInt(input_updateRate.value);
    width = parseInt(input_width.value);
    height = parseInt(input_height.value);
    scale = parseFloat(input_scale.value * height);
    useSymmetry = checkbox_useSymmetry.checked;
    greyScale = checkbox_greyScale.checked;
    rotate = checkbox_rotate.checked;
    fractalType = parseInt(document.querySelector('input[name="input_fractalType"]:checked').value);
    
    // fractal function
    const centerX = canvas.width * (rotate?.45:.5);
    const centerY = canvas.height * (rotate?.5:.67);
    for(let j = Math.min(updateRate, 1e3); j-->0; )
    {
        // mandelbrot fractal equation
        const A = Math.random()*4 - 2;
        const B = Math.random()*4 - 2;
        const P = [];
        let X = A;
        let Y = B;
        for(let i = iterations; isFinite(X)&&i-->0;)
        {
            [X, Y] = [2*X*Y + A, Y*Y - X*X + B];
            P.push({ X, Y });
        }
        
        // only use points in/out of the mandelbrot set
        if (!isFinite(X) && fractalType == 1 || isFinite(X) && fractalType == 0)
            continue;

        // draw the points
        P.map
        (
            function(p,i)
            {
                // apply color
                if (greyScale)
                    context.fillStyle = `rgb(${ alpha }, ${ alpha }, ${ alpha })`;
                else
                    context.fillStyle = `rgb(
                        ${ i>=iterations/10 ? alpha : 0 },
                        ${ i<iterations/10 && i>=iterations/50 ? alpha : 0 },
                        ${ i<iterations/50 ? alpha : 0 })`;
                    
                // reder the pixel for this point
                const X = (rotate?-p.Y : p.X)*scale;
                const Y = (rotate? p.X : p.Y)*scale;
                context.fillRect(centerX + X|0, Y + centerY|0, 1, 1);
                if (useSymmetry)
                {
                    const r = rotate ? 1 : -1;
                    context.fillRect(centerX + r*X|0, centerY - Y*r|0, 1, 1);
                }
            }
        )
    }
    
    // fit canvas to window
    const aspect = canvas.width/canvas.height;
    const styleWidth = aspect > innerWidth/innerHeight ? 
        innerWidth : innerHeight*aspect;
    canvas.style.width = styleWidth + 'px';
    document.body.style.textAlign = 'center';
}

function Reset()
{
    canvas.width = width;
    canvas.height = height;
    context.fillRect(0, 0, width, height);
    
    // user lighter operation to increment pixel colors
    context.globalCompositeOperation = 'lighter';
}

function Screenshot()
{
    download(canvas.toDataURL('image/png'),'JSBrot.png','image/png');
}

const context = canvas.getContext('2d');
Loop();
Reset();

// load an image
imageFileInput.onchange =(e)=>
{
    const file = e.target.files[0];
   
    if (!file)
        return 0;
    
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload =(readerEvent)=>
    {
        const content = readerEvent.target.result;
        const image = new Image();
        image.onload =()=>
        {
            // draw the image to the canvas
            canvas.width = width = input_width.value = image.width;
            canvas.height = height = input_height.value = image.height;
            context.fillRect(0, 0, width, height);
            context.drawImage(image, 0, 0, width, height);
            context.globalCompositeOperation = 'lighter';
        }
        
        image.src = content;
    }
}
              
            
!
999px

Console