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 x-data="ringGenerator" x-init="generateRings" class="bg-medium min-h-screen grid lg:grid-cols-[auto_1fr] overflow-hidden">
    
    <aside class="flex flex-col bg-dark text-neutral-400">
        <h1 class="py-8 px-4 bg-teal-500 text-dark font-bold text-xl mb-4 uppercase flex items-center before:h-0.5 before:flex-1 before:bg-current before:mr-2 after:h-0.5 after:flex-1 after:bg-current after:ml-2">Shape settings</h1>
        <!-- Settings -->
        <ul class="flex flex-col items-start flex-1 p-4 space-y-8">
            <!-- Shape style -->
            <li class="flex items-center justify-between w-full space-x-8">
                <label for="shape">Shape:</label>
                <select id="shape" x-model="shape" @change="generateRings" class="p-1 rounded bg-light text-zinc-100 flex-1 max-w-40">
                    <option value="circle">Circle</option>
                    <option value="rectangle">Rectangle</option>
                </select>
            </li>
            <!-- Border Radius -->
            <li x-show="shape === 'rectangle'" class="flex items-center justify-between w-full space-x-8">
                <label for="shapeRadius">Border Radius:</label>
                <input type="number" id="shapeRadius" min="0" x-model.number="shapeRadius" @change="generateRings" class="p-1 rounded bg-light text-zinc-100 flex-1 max-w-40">
            </li>
            <!-- Shape position on X axis -->
            <li class="flex items-center justify-between w-full space-x-8">
                <label for="posX">Position X:</label>
                <select id="posX" x-model="posX" @change="generateRings" class="p-1 rounded bg-light text-zinc-100 flex-1 max-w-40">
                    <option value="left">Left</option>
                    <option value="center">Center</option>
                    <option value="right">Right</option>
                </select>                
            </li>
            <!-- Shape position on Y axis -->
            <li class="flex items-center justify-between w-full space-x-8">
                <label for="posY">Position Y:</label>
                <select id="posY" x-model="posY" @change="generateRings" class="p-1 rounded bg-light text-zinc-100 flex-1 max-w-40">
                    <option value="top">Top</option>
                    <option value="center">Center</option>
                    <option value="bottom">Bottom</option>
                </select>             
            </li>
            <!-- Shape color -->
            <li class="flex items-center justify-between w-full space-x-8">
                <label for="shapeColor">Shape Color:</label>
                <input type="color" id="shapeColor" x-model="shapeColor" @change="generateRings" class="h-10 rounded bg-light flex-1 max-w-40">
            </li>
            <!-- Shape Ring Count -->
            <li class="flex items-center justify-between w-full space-x-8">
                <label for="shapeCount">Shape Count:</label>
                <input type="number" id="shapeCount" min="2" x-model.number="shapeCount" @change="generateRings" class="p-1 rounded bg-light text-zinc-100 flex-1 max-w-40">
            </li>
            <!-- Shape Ring Width -->
            <li class="flex items-center justify-between w-full space-x-8">
                <label for="shapeWidth">Shape Width:</label>
                <input type="number" id="shapeWidth" min="1" x-model.number="shapeWidth" @change="generateRings" class="p-1 rounded bg-light text-zinc-100 flex-1 max-w-40">       
            </li>
            <!-- Shape Spacing between Rings -->
            <li class="flex items-center justify-between w-full space-x-8">
                <label for="shapeSpacing">Shape Spacing:</label>
                <input type="number" id="shapeSpacing" min="0" x-model.number="shapeSpacing" @change="generateRings" class="p-1 rounded bg-light text-zinc-100 flex-1 max-w-40">
            </li>
            <!-- Shape Opacity -->
            <li class="flex items-center justify-between w-full space-x-8">
                <label for="shapeOpacity">Shape Opacity:</label>
                <input type="checkbox" id="shapeOpacity" x-model.boolean="shapeOpacity" @change="generateRings" class="p-1 rounded bg-zinc-400 text-zinc-100 scale-150">
            </li>
            <!-- Shape Opacity Range -->
            <li x-show="shapeOpacity" class="flex items-center justify-between w-full space-x-4" x-cloak>
                <label for="opacityRange">Opacity Start:</label>
                <input type="range" min="0" max="0.9" step="0.1" id="opacityRange" x-model.number="opacityRange" @change="generateRings" class="p-1 rounded bg-zinc-400 text-zinc-100 flex-1 max-w-40">
                <span x-text="opacityRange"></span>
            </li>
            <!-- Shape Dynamic Size -->
<!--             <li class="flex items-center justify-between w-full space-x-8">
                <label for="dynamicSize">Dynamic Size:</label>
                <input type="checkbox" id="dynamicSize" x-model.boolean="dynamicSize" @change="generateRings" class="p-1 rounded bg-zinc-400 text-zinc-100 scale-150">
            </li> -->
        </ul>
        
        <!-- Buttons -->
        <div class="grid gap-2 p-4 mt-8 text-center font-semibold">
            <!-- Open Modal and show SVG Code -->
            <button type="button" @click="showModal = true" class="border-2 border-zinc-500 text-zinc-500 rounded px-4 py-2 hover:text-dark hover:bg-zinc-500">Show SVG Code</button>
           
            <!-- Download SVG Image File -->
            <a :href="downloadImageURL" download="lw_generated_shape.svg" class="border-2 border-zinc-500 text-zinc-500 rounded px-4 py-2 hover:text-dark hover:bg-zinc-500">Download as image</a>
             <!-- Copy To Clipboard -->
            <button type="button" @click="copyToClipboard" class="text-dark bg-teal-500 rounded px-4 py-2 hover:bg-teal-400">
                <span>Copy to Clipboard</span>
            </button>
        </div> 
    </aside>
    
    <!-- Shape Container -->
    <div class="flex-1 flex justify-center items-center p-8 size-full relative overflow-auto">
        <svg xmlns="http://www.w3.org/2000/svg" id="svgContainer" fill="none" aria-hidden="true"></svg> 
    </div>
    
    <!-- Modal -->
    <div
        x-show="showModal"
        @keydown.escape.prevent.stop="showModal = false"
        role="dialog"
        aria-modal="true"
        x-id="['modal-title']"
        :aria-labelledby="$id('modal-title')"
        class="fixed inset-0 z-10 overflow-y-auto"
        x-cloak
    >
        <!-- Overlay -->
        <div x-show="showModal" x-transition.opacity class="fixed inset-0 bg-black bg-opacity-50 backdrop-blur"></div>
 
        <!-- Panel -->
        <div
            x-show="showModal" x-transition
            @click="showModal = false"
            class="relative flex min-h-screen items-center justify-center p-4"
        >
            <div
                @click.stop
                x-trap.noscroll.inert="showModal"
                class="relative w-full max-w-2xl overflow-y-auto rounded-xl bg-dark border border-medium text-neutral-400 p-8 shadow-lg space-y-4"
            >
                <!-- Title -->
                <h2 class="text-3xl font-bold" :id="$id('modal-title')">Output HTML:</h2>
 
                <!-- Content -->
                <label for="outputCode"></label>
                <textarea id="outputCode" rows="10" cols="30" x-model="outputHTML" readonly class="p-2 w-full rounded border-2 border-zinc-500 bg-medium"></textarea>
 
                <!-- Buttons -->
                <div class="flex space-x-2 font-semibold">
                    <button type="button" @click="copyToClipboard" class="text-dark bg-teal-500 rounded px-4 py-2 hover:bg-teal-400">
                        Copy to Clipboard
                    </button>
                    <button type="button" @click="showModal = false" class="border-2 border-zinc-500 text-zinc-500 rounded px-4 py-2 hover:text-dark hover:bg-zinc-500">
                        Cancel
                    </button>
                </div>
            </div>
        </div>
    </div>
    
    <!-- Toast -->
    <div x-show="showToast" class="fixed right-4 bottom-4 py-2 pr-4 text-sm text-green-500 rounded shadow-lg bg-dark border border-zinc-800 z-10 flex" role="status" x-cloak x-transition>
        <p class="before:h-6 before:w-1.5 before:rounded before:bg-green-500 before:mx-2 inline-flex items-center leading-none">
            Copied to clipboard
        </p>
    </div>
</div>



              
            
!

CSS

              
                [x-cloak] { display: none !important; }
              
            
!

JS

              
                import Alpine from "https://esm.sh/alpinejs";

Alpine.data('ringGenerator', () => ({
    svg: document.getElementById('svgContainer'),
    shape: 'circle',
    shapeColor: '#14b8a6',
    shapeCount: 20,
    shapeWidth: 10,
    shapeSpacing: 30,
    posX: 'center',
    posY: 'top',
    rotation: 0,
    shapeOpacity: false,
    opacityRange: 0,
    dynamicSize: false,
    shapeRadius: 0,
    
    showModal: false,
    outputHTML: '',
    downloadImageURL: '',
    
    showToast: false,
    
    generateRings() {
        this.svg.innerHTML = '';
        
        const totalSize = this.shapeCount * this.shapeWidth + (this.shapeCount - 1) * this.shapeSpacing;
        this.svg.setAttribute('height', this.posY === 'center' ? totalSize : totalSize / 2);
        this.svg.setAttribute('width', this.posX === 'center' ? totalSize : totalSize / 2);
        this.svg.setAttribute('stroke', this.shapeColor)
        this.svg.setAttribute('viewBox', `0 0 ${this.posX === 'center' ? totalSize : totalSize / 2} ${this.posY === 'center' ? totalSize : totalSize / 2}`)
        this.dynamicSize ? this.svg.setAttribute('preserveAspectRatio', 'none') : this.svg.removeAttribute('preserveAspectRatio')
        
        for (let i = 0; i < +this.shapeCount; i++) {
            const cx = this.posX === 'left' ? 0 : this.posX === 'center' ? totalSize / 2 : totalSize / 2;
            const cy = this.posY === 'top' ? 0 : this.posY === 'center' ? totalSize / 2 : totalSize / 2;
            const halfSize = ((this.shapeWidth + this.shapeSpacing) * i) / 2;
            const x = cx - halfSize;
            const y = cy - halfSize; 
            const size = (this.shapeWidth + this.shapeSpacing) * i;
            const radius = this.shapeRadius > 0 && i === this.shapeCount - 1 ? this.shapeRadius : (this.shapeRadius / this.shapeCount) * i
            // calculating opacity
            const startOpacityIdx = Math.ceil(this.opacityRange * this.shapeCount)
            const opacity = (this.shapeOpacity && i >= startOpacityIdx)
                           ? 1 - (i - startOpacityIdx) / (this.shapeCount - startOpacityIdx)
                           : 1

            const shapeElement = `
                <rect x="${x}" 
                      y="${y}" 
                      width="${size}" 
                      height="${size}" 
                      stroke-width="${this.shapeWidth / 2}" 
                      rx="${this.shape === 'circle' ? '100%' : radius.toFixed(2)}" opacity="${opacity.toFixed(2)}" 
                />`;
            
            this.svg.innerHTML += shapeElement;
        }

        // Update the output HTML
        this.outputHTML = this.svg.outerHTML;
        
        // Generate new download url
        this.downloadSVG()
    },
    
    copyToClipboard() {
        const outputCode = document.getElementById('outputCode');
        outputCode.select();
        document.execCommand('copy');
        
        this.toast()
    },
    
    toast() {
        this.showToast = true
        
        setTimeout(() => {this.showToast = false}, 1000)
    },
    
    downloadSVG() {
        const svgData = new XMLSerializer().serializeToString(this.svg);
        const blob = new Blob([svgData], { type: 'image/svg+xml' });
        const url = URL.createObjectURL(blob);
        this.downloadImageURL = url
    }

}));

Alpine.start();
              
            
!
999px

Console