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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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="myCanvas" width="800" height="500"></canvas>
<h1 id="status"></h1>
              
            
!

CSS

              
                body{ 
    margin:10px; 
    background:#CCC; 
}

#myCanvas{ background: #48b4e0; border:#000 1px solid; }
              
            
!

JS

              
                var bg = new Image();
/* bg.src = "xmas_scene.jpg"; */

function initCanvas(){
	var ctx = document.getElementById('myCanvas').getContext('2d');
	var cW = ctx.canvas.width, cH = ctx.canvas.height;
	var flakes = [];

    function house() {
    // the triangle
    ctx.beginPath();
    ctx.moveTo(200, 100);
    ctx.lineTo(100, 200);
    ctx.lineTo(300, 200);
    ctx.closePath();
 
    // the outline
    ctx.lineWidth = 10;
    ctx.strokeStyle = '#666666';
    ctx.stroke();
 
    // the fill color
    ctx.fillStyle = "#8B4513";
    ctx.fill();

    ctx.fillRect(100, 210, 200, 150);
    ctx.lineWidth = 10;
    ctx.strokeStyle = '#666666';
    ctx.stroke();
    ctx.strokeRect(100, 210, 200, 150);
    ctx.fill();

    ctx.fillStyle = "#000000";
    ctx.fillRect(170, 280, 50, 75);	
    } 

    function snowman() {
    // hat
    ctx.beginPath();
    ctx.fillRect(500 - 63, 92, 126, 5);
    ctx.fillRect(500 - 39, 37, 76, 60);
    ctx.stroke();
    ctx.fillStyle = "#000000";
    ctx.fill();

    //snowballs
    ctx.beginPath();
    ctx.arc(500, 165, 70, 0, 2 * Math.PI);
    ctx.stroke();
    ctx.fillStyle = "#FFFFFF";
    ctx.fill();
    ctx.beginPath();
    ctx.arc(500, 325, 90, 0, 2 * Math.PI);
    ctx.stroke();
    ctx.fillStyle = "#FFFFFF";
    ctx.fill();

    //ground
    ctx.beginPath();
    ctx.fillRect(0, 430, 800, 300);
    ctx.stroke();
    ctx.fillStyle = "#FFFFFF";
    ctx.fill();
    ctx.closePath();
    //eyes
    ctx.beginPath();
    ctx.arc(480, 130, 10, 0, 2 * Math.PI);
    ctx.stroke();
    ctx.fillStyle = "#000000";
    ctx.fill();
    ctx.beginPath();
    ctx.arc(520, 130, 10, 0, 2 * Math.PI);
    ctx.stroke();
    ctx.fillStyle = "#000000";
    ctx.fill();

    //triangle nose
    ctx.beginPath();
    ctx.moveTo(515, 170);
    ctx.lineTo(500, 155);
    ctx.lineTo(500, 180);
    ctx.closePath();
    ctx.stroke();
    ctx.fillStyle = "#FFA500";
    ctx.fill();

    //Mouth (clockwise)
    ctx.beginPath();
    ctx.arc(505, 190, 26, -3, Math.PI * 2, true); 
    ctx.stroke();

    //buttons
    ctx.beginPath();
    ctx.arc(500, 280, 10, 0, 2 * Math.PI);
    ctx.stroke();
    ctx.fillStyle = "#FF0000 ";
    ctx.fill();
    ctx.beginPath();
    ctx.arc(500, 320, 10, 0, 2 * Math.PI);
    ctx.stroke();
    ctx.fillStyle = "#FF0000 ";
    ctx.fill();
    ctx.beginPath();
    ctx.arc(500, 360, 10, 0, 2 * Math.PI);
    ctx.closePath();
    ctx.stroke();
    ctx.fillStyle = "#FF0000 ";
    ctx.fill();


    // left arm
    ctx.beginPath();
    ctx.moveTo(420, 280);
    ctx.lineTo(550 - 210, 220);
    ctx.stroke();
    ctx.closePath();
    ctx.fillStyle = "#000000";
    ctx.fill();

    ctx.beginPath();
    ctx.moveTo(370, 250);
    ctx.lineTo(490 - 170, 270);
    ctx.stroke();
    ctx.closePath();
    ctx.fillStyle = "#000000";
    ctx.fill();

    ctx.beginPath();
    ctx.moveTo(380, 250);
    ctx.lineTo(450 - 50, 200);
    ctx.stroke();
    ctx.closePath();
    ctx.fillStyle = "#000000";
    ctx.fill();

    // right arm
    ctx.beginPath();
    ctx.moveTo(590, 290);
    ctx.lineTo(520 + 160, 240);
    ctx.stroke();
    ctx.closePath();
    ctx.fillStyle = "#000000";
    ctx.fill();

    ctx.beginPath();
    ctx.moveTo(610, 280);
    ctx.lineTo(600 + 70, 270);
    ctx.stroke();
    ctx.closePath();
    ctx.fillStyle = "#000000";
    ctx.fill();

    ctx.beginPath();
    ctx.moveTo(600, 250);
    ctx.lineTo(440 + 180, 280);
    ctx.stroke();
    ctx.closePath();
    ctx.fillStyle = "#000000";
    ctx.fill();
    }
    
	function addFlake(){
		var x = Math.floor(Math.random() * cW) + 1;
		var y = 0;
		var s = Math.floor(Math.random() * 3) + 1;
		flakes.push({"x":x,"y":y,"s":s});
	}
	
	function snow(){
		addFlake();
		addFlake();
		addFlake();
		for(var i = 0; i < flakes.length; i++){
			ctx.fillStyle = "rgba(255,255,255,.75)";
			ctx.beginPath();
			ctx.arc(flakes[i].x, flakes[i].y+=flakes[i].s*.5, flakes[i].s*.5, 0, Math.PI*2, false);
			ctx.fill();
			if(flakes[i].y > cH){
				flakes.splice(i,1);
			}
			document.getElementById('status').innerHTML = "Snow Flake Count = "+ flakes.length;
		}
		house();
		snowman();
    }
    
	function animate(){
		ctx.save();
		ctx.clearRect(0, 0, cW, cH);
		ctx.drawImage(bg,0,0);
		//ctx.rotate(0);
		snow();
		ctx.restore();
	}
	
	var animateInterval = setInterval(animate, 30);
}

window.addEventListener('load', function(event) {
	initCanvas();
});


              
            
!
999px

Console