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

              
                <!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8"/>
 </head>
 <body onload="redrawCanvas3();">
   <canvas id="canvas3" width="400" height="300"></canvas>
 </body>
</html>
              
            
!

CSS

              
                canvas { border: 1px solid black; }

              
            
!

JS

              
                // flipY moves the origin and flips the Y axis, so the coordinate system
// is more familiar, with the origin (0,0) in the lower left corner

function flipY(ctx) {
    var canvas = ctx.canvas;
    var w = canvas.clientWidth;
    var h = canvas.clientHeight;
    ctx.translate(0,h);
    ctx.scale(1,-1);
}   

function drawFilledCircle(ctx,centerx,centery,radius) {
    /* draws a filled circle at the given location, with
       the given radius */
    ctx.beginPath();
    ctx.arc(centerx,centery,radius,0,2*Math.PI);
    ctx.fill();
}

function drawTree1(ctx,height,width,radius) {
    /* Draws a tree with the lower left corner at the origin.
       The trunk has the given height and width, and the leaves
       have the given radius */

    // trunk is a brown rectangle
    ctx.fillStyle = "brown";
    ctx.fillRect(0,0,width,height);
    // leaves are a green circle
    ctx.fillStyle = "green";
    var cx = width/2;
    var cy = height+radius;
    drawFilledCircle(ctx,cx,cy,radius);
}

function drawTreeAt(ctx,x,y,height,width,radius) {
    /* Draws a tree at the (x,y) location. Other args
       passed on to drawTree1 */
    ctx.save();
    ctx.translate(x,y);
    drawTree1(ctx,height,width,radius);
    ctx.restore();
}

function redrawCanvas1() {
    var ctx = document.getElementById('canvas1').getContext('2d');
    // flip the coordinate frame
    ctx.save();
    flipY(ctx);

    // Draw background
    ctx.fillStyle = "white";
    ctx.fillRect(0, 0, ctx.canvas.clientWidth, ctx.canvas.clientHeight);

    // drawTreeAt(ctx,x,y,height,width.radius);
    drawTreeAt(ctx,200,50,90,20,30);
    ctx.restore();
}


function drawHouse(ctx,width,height,dx,dy) {
    var ridgex = width/2;
    var ridgey = height+width/2;
    // create the front part
    ctx.beginPath();
    ctx.lineTo(width,0);
    ctx.lineTo(width,height);
    ctx.lineTo(ridgex,ridgey);
    ctx.lineTo(0,height);
    ctx.lineTo(0,0);
    // create the back part
    ctx.lineTo(width,0);
    ctx.lineTo(width+dx,dy);
    ctx.lineTo(width+dx,height+dy);
    ctx.lineTo(ridgex+dx,ridgey+dy);
    ctx.lineTo(ridgex,ridgey);
    // draw the lines
    ctx.stroke();
}

function drawHouseAt(ctx,x,y,width,height,dx,dy) {
    ctx.save();
    ctx.translate(x,y);
    drawHouse(ctx,width,height,dx,dy);
    ctx.restore();
}

function redrawCanvas2() {
    var ctx = document.getElementById('canvas2').getContext('2d');
    // flip the coordinate frame
    ctx.save();
    flipY(ctx);

    // Draw background
    ctx.fillStyle = "white";
    ctx.fillRect(0, 0, ctx.canvas.clientWidth, ctx.canvas.clientHeight);
    
    // drawHouseAt(ctx,x,y,width,height,dx,dy)
    drawHouseAt(ctx,200,50,30,40,10,6);
    ctx.restore();
}


function redrawCanvas3() {
    var ctx = document.getElementById('canvas3').getContext('2d');
    // flip the coordinate frame
    ctx.save();
    flipY(ctx);

    // Draw background
    ctx.fillStyle = "white";
    ctx.fillRect(0, 0, ctx.canvas.clientWidth, ctx.canvas.clientHeight);

    // draw trees
    function tree(x,y) { drawTreeAt(ctx,x,y,90,20,30); }
    
    tree(50,50);
    tree(80,60);
    tree(20,90);
    tree(250,150);
    tree(220,120);

    // draw houses
    function house(x,y) { drawHouseAt(ctx,x,y,30,40,10,6); }

    house(200,50);
    house(300,75);
    ctx.restore();
}

              
            
!
999px

Console