Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <div class="container">

    <div class="controllers">
        <div class="control">
            Parts<br>
            <input class="input" type="number" name="parts" min="2" max="24" step="2" value="12">
        </div>

        <div class="control">
            Color<br>
            <input class="color" type="color" name="color" value="#ff0000">
        </div>

        <div class="control">
            Line width<br>
            <input class="input" type="number" name="width" min="1" max="10" step="1" value="2">
        </div>

        <div class="control">
            <label>
                <input type="checkbox" onchange="setGrid(this);" checked>
                Grid
            </label>
            <label>
                <input type="checkbox" onchange="setMirrored(this);" checked>
                Mirrored
            </label>
        </div>

        <div class="buttons">
            <button onclick="clearCanvas();">
                <i class="fas fa-trash"></i>
                Clear
            </button>
            <button onclick="download()">
                <i class="fas fa-save"></i>
                Save
            </button>
        </div>
    </div>

    <div class="mandala">
        <canvas class="draw"></canvas>
        <canvas class="base show"></canvas>
    </div>
</div>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css2?family=Yellowtail&display=swap');

* {
    padding: 0;
    margin: 0 auto;
    box-sizing: border-box;
}

body {
    font-family: 'Yellowtail', cursive;
    background-color: #ddd;
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}

.container {
    position: relative;
    width: 1200px;
    height: 800px;
}

.mandala {
    position: absolute;
    top: 0; left: 25px;
    width: 800px; height: 800px;
    background-color: white;
    border-radius: 50%;
    box-shadow: 5px 5px 10px #bbb;

    & .base {
        pointer-events: none;
        position: absolute;
        left: 0; top: 0;
        width: 800px; height: 800px;
        border-radius: 50%;
        opacity: 0;
        transition: opacity 0.5s;

        &.show {
            opacity: 1;
        }
    }
    
    & .draw {
        cursor: crosshair;
        position: absolute;
        left: 0; top: 0;
        width: 800px; height: 800px;
        border-radius: 50%;
    }
}

.controllers {
    position: absolute;
    top: 50%; left: 0;
    transform: translateY(-50%);
    padding: 0 1em 0 850px;
    border: 1px solid #fff;
    border-radius: 4px;
    width: 1200px;
    user-select: none;
    
    .control {
        font-size: 1.25em;
        display: block;
        width: 100%;
        padding: 1em;
        border-bottom: 1px solid #fff;
    }

    .input {
        font-family: 'Yellowtail', cursive;
        width: 100%;
        font-size: 1em;
        text-align: center;
    }

    .color {
        font-size: 1em;
        width: 100%;
        height: 2.5em;
        padding: 0;
    }

    label {
        display: block;

        &+label {
            margin-top: 0.5em;
        }
    }

    .buttons {
        padding: 1em 0;
        text-align: right;

        button {
            font-size: 1em;
            font-family: 'Yellowtail', cursive;
            background: none;
            border: 1px solid #fff;
            display: inline-block;
            padding: 0.5em;
            margin: 0.25em
        }
    }
}
              
            
!

JS

              
                function _(e, all=false) {
    let divs = document.querySelectorAll(e);
    if (all || (divs.length > 1)) { return divs; }
    return divs[0];
}

const base = _('.base');
const baseCTX = base.getContext("2d");

const draw = _('.draw');
const ctx = draw.getContext("2d");

var cwidth = 1024;
var cheight = 1024;
base.width = cwidth;
base.height = cheight;
draw.width = cwidth;
draw.height = cheight;

let parts = 12;
let lineWidth = 2;
let rect = false;
let mirror = true;

let lastX = 0;
let lastY = 0;
let lastAngle = 0;
let lastDistance = 0;

draw.lineWidth = lineWidth;
draw.lineCap = "round";

ctx.strokeStyle = "#f00";
ctx.fillStyle = "#f00";

baseCTX.lineWidth = 2;
baseCTX.strokeStyle = "rgba(0, 0, 0, 0.1)";

document.addEventListener('mousedown', function(e) {
    rect = draw.getBoundingClientRect();
    lastX = (e.clientX - rect.x) / rect.width * 1024;
    lastY = (e.clientY - rect.y) / rect.width * 1024;
    lastAngle = (((Math.atan2(lastY - 512, lastX - 512) * 180 / Math.PI) + 450) % 360) / 90;
    lastDistance = Math.sqrt(Math.pow(lastY - 512, 2) + Math.pow(lastX - 512, 2));
});

document.addEventListener('mouseup', function(e) {
    rect = false;
});

draw.addEventListener('mousemove', drawCanvas);

_('[name="parts"]').addEventListener('change', function(e) {
    if (e.target.value < 2) { return false; }
    if (e.target.value > 24) { return false; }
    if ((e.target.value/2) != Math.round(e.target.value/2)) { return false; }
    parts = e.target.value;
    drawParts();
});

_('[name="width"]').addEventListener('change', function(e) {
    if (e.target.value < 1) { return false; }
    if (e.target.value > 10) { return false; }
    lineWidth = Math.round(e.target.value);
    ctx.lineWidth = lineWidth;
});

_('[name="color"]').addEventListener('change', function(e) {
    ctx.strokeStyle = e.target.value;
    ctx.fillStyle = e.target.value;
});

clearCanvas();
drawParts();

function clearCanvas() {
    let lastFllStyle = ctx.fillStyle
    ctx.fillStyle = "#ffffff";
    ctx.fillRect(0, 0, cwidth, cheight);
    ctx.fillStyle = lastFllStyle;
}

function drawParts() {

    baseCTX.clearRect(0, 0, cwidth, cheight);

    baseCTX.beginPath();
    baseCTX.arc(512, 512, 128, 0, 2 * Math.PI);      
    baseCTX.stroke();

    baseCTX.beginPath();
    baseCTX.arc(512, 512, 256, 0, 2 * Math.PI);      
    baseCTX.stroke();

    baseCTX.beginPath();
    baseCTX.arc(512, 512, 384, 0, 2 * Math.PI);      
    baseCTX.stroke();

    for (let i = 0; i < parts; i++) {

        let thisAngle = 4 / parts * i
        let thisX = 512 + (512 * Math.sin(thisAngle * (Math.PI / 2)));
        let thisY = 512 + (512 * Math.sin((thisAngle - 1) * (Math.PI / 2)));

        baseCTX.beginPath();
        baseCTX.moveTo(512, 512);
        baseCTX.lineTo(thisX,thisY);   
        baseCTX.stroke();
    }
}

function drawCanvas(e) {
    if (!rect) { return false; }

    let thisX = (e.clientX - rect.x) / rect.width * 1024;
    let thisY = (e.clientY - rect.y) / rect.width * 1024;

    let thisAngle = (((Math.atan2(thisY - 512, thisX - 512) * 180 / Math.PI) + 450) % 360) / 90;
    let thisDistance = Math.sqrt(Math.pow(thisY - 512, 2) + Math.pow(thisX - 512, 2));
    
    for (let i = 0; i < parts; i++) {

        let newAngle1;
        let newAngle2;

        if (mirror && ((i/2) != Math.round(i/2))) {

            newAngle1 = ((4-thisAngle) - ((4 / parts) * (i-1)));
            newAngle2 = ((4-lastAngle) - ((4 / parts) * (i-1)));
            
        } else {

            newAngle1 = (thisAngle + ((4 / parts) * i));
            newAngle2 = (lastAngle + ((4 / parts) * i));
        }

        let cX = 512 + (thisDistance * Math.sin(newAngle1 * (Math.PI / 2)));
        let cY = 512 + (thisDistance * Math.sin((newAngle1 - 1) * (Math.PI / 2)));

        let dX = 512 + (lastDistance * Math.sin(newAngle2 * (Math.PI / 2)));
        let dY = 512 + (lastDistance * Math.sin((newAngle2 - 1) * (Math.PI / 2)));

        ctx.beginPath();
        ctx.arc(cX, cY, (lineWidth-1)/2, 0, 2 * Math.PI);      
        ctx.fill();

        ctx.beginPath();
        ctx.moveTo(cX, cY);
        ctx.lineTo(dX, dY);   
        ctx.stroke();
    }

    lastAngle = thisAngle;
    lastDistance = thisDistance;
}

function setGrid(e) {
    if (e.checked) {
        _('.base').classList.add('show');
    } else {
        _('.base').classList.remove('show');
    }
}

function setMirrored(e) {
    mirror = e.checked;
}

function download(){
    let link = document.createElement('a');
    link.setAttribute('download', 'mandala.png');
    link.setAttribute('href', draw.toDataURL("image/png").replace("image/png", "image/octet-stream"));
    link.click();
}
              
            
!
999px

Console