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

              
                <p><a href="https://getbutterfly.com/canvas-a-simple-drawing-application/" target="_blank" rel="external">Read more about the implementation here</a></p>

<div class="toolbar-wrapper">
    <div class="toolbar">
        Color
        <svg xmlns="http://www.w3.org/2000/svg" id="redPen" viewBox="0 0 400 400" alt="Red Brush" onclick="changeColor('rgb(212,21,29)', this)" style="height:48px">
            <circle cx="200" cy="200" r="100" fill="red" stroke="#000" stroke-width="10" />
        </svg>
        <svg xmlns="http://www.w3.org/2000/svg" id="greenPen" viewBox="0 0 400 400" alt="Green Brush" onclick="changeColor('rgb(131,190,61)', this)" style="height:48px">
            <circle cx="200" cy="200" r="100" fill="green" stroke="#000" stroke-width="10" />
        </svg>
        <svg xmlns="http://www.w3.org/2000/svg" id="bluePen" viewBox="0 0 400 400" alt="Blue Brush" onclick="changeColor('rgb(0,86,166)', this)" style="height:48px">
            <circle cx="200" cy="200" r="100" fill="blue" stroke="#000" stroke-width="10" />
        </svg>
    </div>

    <div class="toolbar">
        Thickness
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 400" alt="Thin Brush" onclick="changeThickness(1, this)" style="height:32px">
            <circle cx="200" cy="200" r="100" fill="grey" stroke="#000" stroke-width="10" />
        </svg>
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 400" alt="Thin Brush" onclick="changeThickness(5, this)" style="height:48px">
            <circle cx="200" cy="200" r="100" fill="grey" stroke="#000" stroke-width="10" />
        </svg>
        <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 400 400" alt="Thin Brush" onclick="changeThickness(10, this)" style="height:64px">
            <circle cx="200" cy="200" r="100" fill="grey" stroke="#000" stroke-width="10" />
        </svg>
    </div>
</div>

<div class="CanvasContainer">
    <canvas id="drawingCanvas" width="600" height="300"></canvas>
</div>
<div class="Toolbar">
    <button onclick="saveCanvas()">Save Canvas Content</button>
    <button onclick="clearCanvas()">Clear Canvas</button>
    <div id="savedCopyContainer">
        <img id="savedImageCopy"><br>
        Right click to save
    </div>
</div>
              
            
!

CSS

              
                * {
    box-sizing: border-box;
}

body {
    background: white;
}

.toolbar-wrapper {
    display: flex;
    width: 600px;
}
.toolbar {
    display: flex;
    align-items: center;
    flex-basis: 50%;
    font-size: 14px;
    background: #f2f7fe;
    padding: 16px;
    margin-bottom: 1px;
    margin-right: 1px;
    border: 1px solid #7b899b;
}

canvas {
    border: 1px solid #7b899b;
}

svg {
    padding: 2px;
    border: 2px solid #f2f7fe;
}

svg:hover {
    border: 2px groove #e4f0fe;
    background: white;
}

svg.Selected {
    border: 2px groove #e4f0fe;
}

#savedCopyContainer {
    display: none;
}

#savedCopyContainer img {
    width: 250px;
    height: 150px;
}

              
            
!

JS

              
                const canvas = document.getElementById("drawingCanvas");
const context = canvas.getContext("2d");

window.onload = () => {
    canvas.onmousedown = startDrawing;
    canvas.onmouseup = stopDrawing;
    canvas.onmouseout = stopDrawing;
    canvas.onmousemove = draw;
};

const previousColorElement = null;
let isDrawing = false;

const changeColor = (color, imgElement) => {
    // Change the current drawing color to the specified color
    context.strokeStyle = color;

    // Change the class of the clicked img element to "Selected"
    imgElement.className = "Selected";

    // Reset the class of the previously selected img element to its default state.
    // Set the previousColorElement variable to the current img element.
    if (previousColorElement !== null) {
        previousColorElement.className = "";
    }
    previousColorElement = imgElement;
};

// Track the <img> element for the line thickness that was previously clicked on
var previousThicknessElement;

function changeThickness(thickness, imgElement) {
    // Change the current line thickness
    context.lineWidth = thickness;

    // Change the style of the <img> element that was clicked on
    imgElement.className = "Selected";

    // Return the previously selected <img> element to its normal state
    if (previousThicknessElement != null)
        previousThicknessElement.className = "";

    previousThicknessElement = imgElement;
}

function startDrawing(e) {
    // Start drawing
    isDrawing = true;

    // Create a new path (with the current color and line thickness)
    context.beginPath();

    // By clicking the left mouse button, we place the "brush" on the canvas
    context.moveTo(e.pageX - canvas.offsetLeft, e.pageY - canvas.offsetTop);
}

function draw(event) {
    // Determine current mouse pointer coordinates
    if (isDrawing == true) {
        var x = event.pageX - canvas.offsetLeft;
        var y = event.pageY - canvas.offsetTop;

        // Draw line to new coordinates
        context.lineTo(x, y);
        context.stroke();
    }
}

function stopDrawing() {
    isDrawing = false;
}

function clearCanvas() {
    context.clearRect(0, 0, canvas.width, canvas.height);
}

function saveCanvas() {
    // Find the <img> element
    var imageCopy = document.getElementById("savedImageCopy");

    // Display canvas data in the <img> element
    imageCopy.src = canvas.toDataURL();

    // Show the <div> element, making the image visible
    var imageContainer = document.getElementById("savedCopyContainer");
    imageContainer.style.display = "block";
}

              
            
!
999px

Console