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

              
                <html>
    <body>
        <canvas id="canvas-bg"></canvas>
        <main id="main">
            <h1 id="title" class="sans-serif">Changing color title</h1>
            <div class="sans-serif"><input type="checkbox" id="switch" onclick="handleClick(event)"/>Switch Bulb</div>
        </main>
    </body>
</html>
              
            
!

CSS

              
                :root {
    --dark-color: #040020;
    --light-color: #fff;
}

body {
    background-color: var(--dark-color);
    margin: 0;
}

main {
    display: flex;
    justify-content: center;
    padding: 10px;
}

#canvas-bg {
    position: fixed;
    z-index: -1;
    width: 100%;
    height: 100%;
}

.sans-serif {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}

h1 {
    transition: all .5s;
}

.dark *{
    color: var(--dark-color);
}

.light *{
    color: var(--light-color);
}
              
            
!

JS

              
                const dark_color =  "#040020", light_color = "#fff";
let animationCount = 0;
const speed = 10;
const clickPosition = {x: 0, y: 0};

const switchBulb = document.getElementById("switch");
let lightOn = switchBulb.checked;

let canvas = document.getElementById("canvas-bg");
let ctx = canvas.getContext("2d");

function handleClick(e) {
    lightOn = switchBulb.checked;
    clickPosition.x = e.x;
    clickPosition.y = e.y;
    if(lightOn) turnOn();
    else turnOff();
    changeContent();
}

function resizeCanvas(){
    ctx.canvas.width  = window.innerWidth;
    ctx.canvas.height = window.innerHeight;
    ctx.fillStyle = lightOn ? light_color : dark_color;
    ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
};
window.addEventListener("resize", resizeCanvas);
resizeCanvas(); //resize first time to screen

function changeContent(){
    let main = document.getElementById("main");
    main.classList.add(lightOn ? "dark" : "light"); // if light is on, content is dark
    main.classList.remove(lightOn ? "light" : "dark"); // remove old class if present
}
changeContent(); //coloring content first time

function turnOn() {
    if(animationCount === 0) switchBulb.disabled = true;
    let pixelRadius = animationCount * speed;
    ctx.fillStyle = dark_color;
    ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
    ctx.beginPath();
    ctx.arc(clickPosition.x, clickPosition.y, pixelRadius, 0, 2 * Math.PI, true);
    ctx.fillStyle = light_color;
    ctx.fill();
    animationCount++;
    if(pixelRadius < (Math.sqrt(Math.pow(ctx.canvas.width,2) + Math.pow(ctx.canvas.height,2))+ 200)){
        setTimeout(turnOn, 1);
    } else {
        animationCount = 0;
        switchBulb.disabled = false;
    }
}

function turnOff() {
    let pixelRadius = animationCount * speed;
    if(animationCount === 0) {
        switchBulb.disabled = true;
        pixelRadius = (Math.sqrt(Math.pow(ctx.canvas.width,2) + Math.pow(ctx.canvas.height,2))+ 200);
        animationCount = Math.ceil(pixelRadius / speed);
    }
    ctx.fillStyle = dark_color;
    ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height);
    ctx.beginPath();
    ctx.arc(clickPosition.x, clickPosition.y, pixelRadius, 0, 2 * Math.PI, true);
    ctx.fillStyle = light_color;
    ctx.fill();
    animationCount--;
    if(animationCount > 0) setTimeout(turnOff, 1);
    else {
        ctx.fillStyle = dark_color;
        ctx.fillRect(0, 0, ctx.canvas.width, canvas.height);
        switchBulb.disabled = false;
    }
}
              
            
!
999px

Console