Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's 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 it's URL and the proper URL extention.

+ 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

Save Automatically?

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

              
                <canvas id="canvas"></canvas>
              
            
!

CSS

              
                html,body {
    height: 100%; 
    width: 100%; 
    margin: 0; 
    padding: 0;
    background: -webkit-gradient(linear, 0 100%, 0 0, color-stop(0.3, #111A20), color-stop(0.8, #59230B), to(#2F1107));
    background: -webkit-linear-gradient(#111A20 30%, #59230B 80%, #2F1107 100%);
    background: -moz-linear-gradient(#111A20 30%, #59230B 80%, #2F1107 100%);
    background: -o-linear-gradient(#111A20 30%, #59230B 80%, #2F1107 100%);
    background: linear-gradient(#111A20 30%, #59230B 80%, #2F1107 100%);
}
canvas {
    position: absolute;
}
              
            
!

JS

              
                function Tree(x, y, d, h) {
    this.x = x; // x pos
    this.y = y; // y pos
    this.d = d; // density
    this.h = h; // height
    this.scount = h / 10; // segments for the trunk
    this.slen = h / this.scount; // segment length
    this.blen = h / 2.5; // branch length
    this.j = 1; // jitter

    this.trunk = [];
    this.branches = [];
    for (var i = 0; i < this.scount; i++) {
        this.trunk.push({
            x: this.x + (Math.random() * (this.j * 2) - this.j),
            y: this.y - this.slen * i,
            t: this.scount - i + 1
        });
        for (var j = 0; j < this.d; j++) {
            this.branches.push({
                x: this.trunk[i].x + (Math.random() * (this.trunk[i].t * 2) - this.trunk[i].t),
                y: this.trunk[i].y + (Math.random() * (this.slen * 2) - this.slen),
                l: this.blen / (this.scount / (this.trunk[i].t - 1)) + (Math.random() * (this.j * 2) - this.j),
                a: Math.random() > .5 ? (Math.random() * (this.j * 16) - this.j) : -180 + (Math.random() * (this.j * -16) - this.j)
            });
            if (this.branches[j].y > this.h) {
                this.branches[j].y = this.h;
            }
            if (this.branches[j].y < this.y) {
                this.branches[j].y = this.y - (Math.random() * (this.slen * 1.5));
            }
        }
    }
}
Tree.prototype = {
    constructor: Tree,
    render: function (context) {
        // trunk
        context.strokeStyle = '#49311c';
        context.moveTo(this.x, this.y);
        context.beginPath();
        for (var i = 0; i < this.scount; i++) {
            context.lineTo(this.trunk[i].x, this.trunk[i].y);
            context.lineWidth = this.trunk[i].t;
            context.stroke();
        }
        context.closePath();
        // branches
        context.globalAlpha = .7;
        context.lineWidth = this.h > 100 ? 1 : .5;
        for (var i = 0, l = this.branches.length; i < l; i++) {
            context.strokeStyle = i%2 ? '#254f25' : '#1E371E';
            context.beginPath();
            context.moveTo(this.branches[i].x, this.branches[i].y);
            var ex = this.branches[i].x + this.branches[i].l * Math.cos(this.branches[i].a * (Math.PI / 180));
            var ey = this.branches[i].y + this.branches[i].l * Math.sin(this.branches[i].a * (Math.PI / 180));
            context.lineTo(ex, ey);
            context.stroke();
            context.closePath();
        }
    }
}

function Terrain2D(height, width, startY, spacing, max, jitter) {
    this.height = height;
    this.width = width;
    this.jitter = jitter;
    this.points = [];
    this.spacing = spacing;
    this.trees = [];
    this.num_points = width / spacing | 0;
    this.scale_x = width / this.num_points;
    for (var i = 0; i <= this.num_points; i++) {
        this.points[i] = startY;
    }
    this.midpoint.call(this, 0, this.num_points, max);
}
Terrain2D.prototype = {
    constructor: Terrain2D,
    midpoint: function (p1, p2, max) {
        var mid = Math.round((p1 + p2) / 2);
        if (p2 - p1 <= 1 || p2 === mid || p1 === mid) {
            return;
        }
        this.points[mid] = ((this.points[p1] + this.points[p2]) / 2) + (max * (Math.random() * (this.jitter * 2) - this.jitter));
        if (Math.random() < .5) {
            var tree = new Tree(mid * this.scale_x, height - Math.random() * this.points[mid] + 65, 65, 100);
            this.trees.push(tree);
        }
        this.midpoint.call(this, p1, mid, max / 2);
        this.midpoint.call(this, mid, p2, max / 2);
    },
    render: function (context, x, y, color, opacity) {
        var i = 0;
        if (typeof opacity !== 'undefined') {
            context.globalAlpha = opacity;
        }
        context.save();
        context.translate(x, y);
        context.beginPath();
        context.moveTo(0, this.points[0]);
        for (; i < this.num_points - 1; i += 2) {
            context.lineTo(i * this.scale_x, this.points[i]);
            context.lineTo((i + 1) * this.scale_x, this.points[i + 1]);
        }
        context.lineTo(this.width, this.points[0]);
        context.lineTo(this.width, this.height);
        context.lineTo(0, this.height);
        if (typeof color !== 'undefined') {
            context.fillStyle = color;
        }
        var grad = context.createLinearGradient(0,0,0,height);
        grad.addColorStop(0,"black");
        grad.addColorStop(1,"#180D01");
        context.fillStyle = grad;
        context.fill();
        context.closePath(); 
        context.restore();
        for (var i = 0, l = this.trees.length; i < l; i++) {
            this.trees[i].render(context);
        }
    }
};

var canvas,context,height,width,terrain;
setTimeout(init, 10);
function init() {
    canvas = document.getElementById('canvas');
    context = canvas.getContext('2d');
    height = canvas.height = document.body.offsetHeight;
    width = canvas.width = document.body.offsetWidth;
    terrain = new Terrain2D(height, width, height - (height / 2), 4, 45, 1);
    terrain.render(context, 0, 0);   
}
              
            
!
999px

Console