Programmatically draw mountains and export them as PNG images using pure JavaScript

Today I needed some mountain clipart for a game I am working on, and doing some searches on Google I stumbled upon – by the way, is there someone who remembers StumbleUpon? – a nice image for sale on VectorStock, this one:

I liked the design of the mountains, but at the same time I found it really interesting to draw them programmatically, all in all I am a programmer and each mountain is made by only four polygons: two for the snow and two for the mountain itself, or two for the light side and two for the dark side if you prefer.

Anyway, something like this:

Press “Reload” to generate a new random mountain or “Download” to open the PNG file – the sky will be transparent – in a new tab. Then you can download it and use in your projects.

I also added some room for customization as usual, and the code is quite easy to understand and change to fit your needs, it’s just a matter of drawing polygons:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8"/>
        <script type="text/javascript">
            function draw() {
                let downloadButton = document.getElementById("download");
                downloadButton.addEventListener("click", function(){
                    let dataURL = canvas.toDataURL("image/png");
                    let win = window.open();
                    win.document.write('<iframe src="' + dataURL  + '" frameborder="0" style="border:0; top:0px; left:0px; bottom:0px; right:0px; width:100%; height:100%;" allowfullscreen></iframe>');
                });
                let reloadButton = document.getElementById("reload");
                reloadButton.addEventListener("click", function(){
                    window.location.reload()
                });
                let mountainData = {
                    width: 500,
                    height: 500,
                    widthVariation: 50,
                    heightVariation: 50,
                    centerVariation: 100
                }
                let canvas = document.createElement("canvas");
                canvas.width = mountainData.width;
                canvas.height = mountainData.height;
                canvas.style.zIndex = 8;
                canvas.style.position = "absolute";
                canvas.style.border = "1px solid";
                canvas.style.backgroundColor = "#57cee4"
                document.body.appendChild(canvas);
                let ctx = canvas.getContext("2d");
                ctx.fillStyle = 'rgba(0, 0, 200, 0.5)';
                let leftMountainBaseX = randomIntInRange(0, mountainData.widthVariation);
                let rightMountainBaseX = mountainData.width - randomIntInRange(0, mountainData.widthVariation);
                let mountainTopY = randomIntInRange(0, mountainData.heightVariation);
                let mountainTopX = (mountainData.width - mountainData.centerVariation) / 2 + randomIntInRange(0, mountainData.centerVariation);
                let leftSnow = randomPointOnSegment(leftMountainBaseX, mountainData.height, mountainTopX, mountainTopY, 60, 80);
                let rightSnow = randomPointOnSegment(rightMountainBaseX, mountainData.height, mountainTopX, mountainTopY, 60, 80);
                let midSnow = {
                    x: mountainTopX,
                    y: (leftSnow.y + rightSnow.y) / 2
                }
                let leftSnowPoints = [];
                let rightSnowPoints = [];
                for(let i = 1; i <= 2; i++){
                    leftSnowPoints.push(randomPointOnSegment(leftSnow.x, leftSnow.y, midSnow.x, midSnow.y, 100 / 3 * i, 100 / 3 * i));
                    leftSnowPoints[i - 1].y += randomIntInRange(10, 20) * (1 - 2 * (i % 2))
                    rightSnowPoints.push(randomPointOnSegment(midSnow.x, midSnow.y, rightSnow.x, rightSnow.y, 100 / 3 * i, 100 / 3 * i));
                    rightSnowPoints[i - 1].y += randomIntInRange(10, 20) * (-1 + 2 * (i % 2))
                }
                ctx.fillStyle = "rgb(236, 239, 244)";
                ctx.beginPath();
                ctx.moveTo(leftSnow.x, leftSnow.y);
                leftSnowPoints.forEach(function(point){
                    ctx.lineTo(point.x, point.y);
                })
                ctx.lineTo(midSnow.x, midSnow.y);
                ctx.lineTo(mountainTopX, mountainTopY)
                ctx.fill();
                //
                ctx.fillStyle = "rgb(130, 176, 209)";
                ctx.beginPath();
                ctx.moveTo(midSnow.x, midSnow.y);
                rightSnowPoints.forEach(function(point){
                    ctx.lineTo(point.x, point.y);
                })
                ctx.lineTo(rightSnow.x, rightSnow.y);
                ctx.lineTo(mountainTopX, mountainTopY)
                ctx.fill();
                //
                ctx.fillStyle = "rgb(73, 121, 141)";
                ctx.beginPath();
                ctx.moveTo(leftMountainBaseX, mountainData.height);
                ctx.lineTo(leftSnow.x, leftSnow.y);
                leftSnowPoints.forEach(function(point){
                    ctx.lineTo(point.x, point.y);
                })
                ctx.lineTo(midSnow.x, midSnow.y);
                ctx.lineTo(midSnow.x, mountainData.height)
                ctx.fill();
                ctx.fillStyle = "rgb(62, 105, 121)";
                ctx.beginPath();
                ctx.moveTo(midSnow.x, midSnow.y);
                rightSnowPoints.forEach(function(point){
                    ctx.lineTo(point.x, point.y);
                })
                ctx.lineTo(rightSnow.x, rightSnow.y);
                ctx.lineTo(rightMountainBaseX, mountainData.height);
                ctx.lineTo(mountainTopX, mountainData.height);
                ctx.fill();


            }
            function randomIntInRange(from, to){
                return from + Math.floor(Math.random() * (to + 1 - from));
            }
            function randomPointOnSegment(x1, y1, x2, y2, min, max){
                let n = randomIntInRange(min, max) / 100;
                let a = (y2 - y1) / (x2 - x1);
                let x = x1 + (x2 - x1) * n;
                let y = a * (x - x1) + y1;
                return {
                    x: x,
                    y: y
                }
            }
        </script>
        <style type="text/css">
            canvas{
                border: 1px solid black;
            }
        </style>
    </head>
    <body onload="draw();">
        <div><button type = "button" id = "download">Download</button> <button type = "button" id = "reload">Reload</button>  </div>
    </body>
</html>

Prepare to see a lot of mountains in my next projects, and it would be also interesting to programmatically create a complete landscape with hills, trees and clouds. And mountains, of course. Download the source code.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers