cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

            
              *, *:before, *:after {
	box-sizing: border-box;
	margin: 0;
	padding: 0;
}
body {
	background: #333;
	display: flex;
	justify-content: center;
	height: 100vh;
	overflow: hidden;
}
canvas {
	cursor: crosshair;
	display: block;
	margin: auto;
}
            
          
!
            
              console.clear();
window.requestAnimFrame = (function(){
  return window.requestAnimationFrame || 
      window.webkitRequestAnimationFrame || 
      window.mozRequestAnimationFrame || 
      window.oRequestAnimationFrame ||
      window.msRequestAnimationFrame ||
        function(callback){
          window.setTimeout(callback, 1000/60);
        };
})();
window.addEventListener("load",app);

function app() {
	var C = document.getElementsByTagName("canvas")[0],
		c = C.getContext("2d"),
		// canvas dimensions
		w = 600,
		h = 600,
		// scale, keep at 2 for best retina results
		s = 2;
	
	// set canvas dimensions with scale
	C.width = w * s;
	C.height = h * s;
	C.style.width = "auto";
    C.style.height = "100%";
	c.scale(s,s);
	
	var rand = function(min, max) {
			return Math.floor(Math.random() * ((max + 1) - min)) + min;
		},
		randFloat = function(min, max) {
			return Math.random() * ((max + 1) - min) + min;
		},
		score = 0,
		_S = function(x,y) {
			this.x = x;
			this.y = y;
			this.size = rand(1,3);
			this.brghtnss = rand(0,100)/100;
		},
		S = [],
		SLimit = 20;
	while (SLimit--)
		S.push(new _S(rand(0,w),rand(0,h)));
		
	var _D = function() {
			this.dir = rand(0,1),
			this.w = 16,
			this.h = 7,
			this.x = this.dir ? w + 100 - this.w : -100,
			this.y = rand(16,h*0.6),
			this.speed = 1,
			this.death = 15,
			this.isDying = false,
			this.pts = [[5,0],[5,1],[3,1],[3,2],[6,2],[6,1],[10,1],[10,2],[13,2],[13,1],[11,1],[11,0],[16,0],[16,1],[14,1],[14,3],[11,3],[11,7],[10,7],[10,3],[9,3],[9,5],[7,5],[7,3],[6,3],[6,7],[5,7],[5,3],[2,3],[2,1],[0,1],[0,0]],
			this.light = {
				x: 7,
				y: 1
			}
		},
		D = new _D(),
		cp1 = [[0,8],[1,8],[1,11],[3,11],[3,10],[4,10],[4,5],[7,5],[7,13],[9,13],[9,14],[11,14],[11,13],[13,13],[13,3],[16,3],[16,18],[20,18],[20,5],[21,5],[21,10],[27,10],[27,4],[29,4],[29,1],[30,1],[30,14],[38,14],[38,1],[39,1],[39,8],[46,8],[46,1],[48,1],[48,0],[0,0]],
		cp2 = [[0,8],[5,8],[5,4],[7,4],[7,11],[10,11],[10,10],[11,10],[11,6],[14,6],[14,14],[17,14],[17,6],[18,6],[18,18],[21,18],[21,5],[23,5],[23,7],[27,7],[27,4],[30,4],[30,10],[33,10],[33,9],[35,9],[35,4],[37,4],[37,16],[42,16],[42,8],[47,8],[47,4],[48,4],[48,0],[0,0]],
		cp3 = [[0,11],[1,11],[1,12],[3,12],[3,11],[4,11],[4,6],[6,6],[6,17],[8,17],[8,18],[10,18],[10,6],[12,6],[12,14],[15,14],[15,5],[17,5],[17,20],[19,20],[19,19],[20,19],[20,5],[21,5],[21,10],[26,10],[26,5],[27,5],[27,7],[28,7],[28,12],[31,12],[31,7],[32,7],[32,6],[33,6],[33,16],[36,16],[36,6],[37,6],[37,5],[38,5],[38,6],[39,6],[39,14],[42,14],[42,6],[43,6],[43,9],[47,9],[47,6],[48,6],[48,0],[0,0]],
		wi = [
				[0.5,8],[2.5,8],[0.5,10],[2.5,10],
				[6.5,8],[8.5,8],[6.5,10],[8.5,10],[6.5,12],[8.5,12],[6.5,14],[8.5,14],[6.5,16],[8.5,16],
				[13,7],[13,9],[13,11],[13,13],
				[18,7],[18,9],[18,11],[18,13],[18,15],[18,17],
				[22,7],[22,9],[24,7],[24,9],
				[29,7],[29,9],[29,11],
				[34,7],[34,9],[34,11],[34,13],[34,15],
				[40,7],[40,9],[40,11],[40,13],
				[43.5,8],[45.5,8]
			],
		_F = function(sx,sy,ex,ey){
			let r = rand(64,224),
				g = rand(64,224),
				b = rand(64,224);
			this.color = "rgb(" + r + "," + g + "," + b + ")";
			this.sx = sx;
			this.sy = sy;
			this.x = this.sx;
			this.y = this.sy;
			this.ex = ex;
			this.ey = ey;
			this.size = 2;
			this.kids = [];
			this.maxKids = 16;
			this.pSpawn = 60;
		},
		F = [],
		FChance = 0.02,
		FLimit = 50,
		_P = function(x,y,angle,color) {
			this.color = color || "#fff";
			this.brghtnss = 0;
			this.x = x;
			this.y = y;
			this.angle = angle;
			this.size = 4;
			this.speed = this.size/randFloat(1,2);
			this.friction = 0.97;
			this.gravity = 0.98;
			this.dspwnTm = 90;
		},
		P = [],
		sky = c.createLinearGradient(w/2,0,w/2,h);
	sky.addColorStop(0, "#00002a");
	sky.addColorStop(0.25, "#00002a");
	sky.addColorStop(0.25, "#003");
	sky.addColorStop(0.5, "#003");
	sky.addColorStop(0.5, "#00003b");
	sky.addColorStop(0.75, "#00003b");
	sky.addColorStop(0.75, "#004");
	sky.addColorStop(1, "#004");
		
	var draw = function() {
			// sky
			c.fillStyle = sky;
			c.fillRect(0,0,w,h);
			
			// stars
			for (let s in S) {
				// for readability, use i for unchanged property of item
				let i = S[s];
				c.fillStyle = i.brghtnss > 0.5 ? "#fff" : "#a8a8a8";
				c.fillRect(i.x,i.y,i.size,i.size);
				
				i.brghtnss > 1 ? S[s].brghtnss = 0 : S[s].brghtnss += 0.01;
			}
			
			// drone
			c.fillStyle = D.isDying ? "#fc0" : "#556";
			c.beginPath();
			c.moveTo(D.x,D.y);
			for (let pl in D.pts)
				c.lineTo(D.x + D.pts[pl][0],D.y + D.pts[pl][1]);
			c.fill();
			c.closePath();
			// change direction after exiting, reset score if missed
			if ((D.x > w && !D.dir) || (D.x < -D.w && D.dir)) {
				D = new _D(),
				score = 0;
			} else if (D.isDying) {
				--D.death;
				if (D.death < 0) {
					D = new _D();
				}
			} else {
				c.fillStyle = "#f44";
				c.fillRect(D.x + D.light.x,D.y + D.light.y,2,1);
				D.x += D.speed * (D.dir ? -1 : 1);
			}
			
			// rockets
			for (let f in F) {
				let i = F[f];
				// draw exhaust (or trail of "kids")
				c.fillStyle = i.color;
				for (let k in i.kids) {
					let j = i.kids[k];
					c.fillRect(Math.floor(j.x / i.size) * i.size,j.y,i.size,i.size);
				}
				// keep exhaust at restricted length
				if (i.kids.length > i.maxKids || F[f].y <= i.ey)
					F[f].kids.pop();
					
				if (F[f].y > i.ey) {
					// draw rocket
					c.fillStyle = "#fff";
					c.fillRect(Math.floor(i.x / i.size) * i.size,i.y,i.size,i.size);
					// generate kid
					F[f].kids.unshift({x:i.x,y:i.y});
					// X distance * (1 / steps it takes to reach Y distance)
					F[f].x += (i.ex - i.sx) * Math.abs(1/((i.ey - i.sy)/i.size));
					F[f].y -= i.size;
					
				} else if (i.kids.length == 1) {
					// spawn particles, trash rocket
					let ps = i.pSpawn;
					// contact with drone
					if (
						(F[f].x + F[f].size > D.x && F[f].x < D.x + D.w) && 
						(F[f].y + F[f].size > D.y && F[f].y < D.y + D.h) && 
						!D.isDying
					) {
						D.isDying = true;
						++score;
					}
					while (ps--)
						P.push(new _P(F[f].x,F[f].y,randFloat(0,Math.PI*2),i.color));
					
					F.shift();
				}
			}
			// particles
			for (let p in P) {
				let i = P[p];
				c.fillStyle = i.color;
				c.fillRect(i.x,i.y,Math.ceil(i.size),Math.ceil(i.size));
				// particle brightness
				if (i.brghtnss > 0) {
					c.fillStyle = "rgba(255,255,255," + i.brghtnss + ")";
					c.fillRect(i.x,i.y,i.size,i.size);
				}
				i.brghtnss > 0.9 ? P[p].brghtnss = 0 : P[p].brghtnss += 0.1;
				// movement
				P[p].x += Math.cos(i.angle) * i.speed;
				P[p].y += Math.sin(i.angle) * i.speed + i.gravity;
				P[p].speed *= i.friction;
				P[p].size -= 1/i.dspwnTm;
				i.dspwnTm < 0 ? P.shift() : --P[p].dspwnTm;
			}
			// randomly fire more rockets until user fires some
			if (Math.random() < FChance)
				F.push(new _F(rand(100,500),h,rand(100,500),rand(100,300)));
			
			// city
			let cu = 5,
				r = 3,
				cw = cu*48;
			
			while (r--) {
				// back
				c.fillStyle = "#224";
				c.beginPath();
				c.moveTo(cw*r,h);
				for (let _1 in cp1)
					c.lineTo(cw*r+cu*cp1[_1][0],h-cu*(cp1[_1][1]+8));
				c.fill();
				c.closePath();
				// middle
				c.fillStyle = "#112";
				c.beginPath();
				c.moveTo(cw*r,h);
				for (let _2 in cp2)
					c.lineTo(cw*r+cu*cp2[_2][0],h-cu*(cp2[_2][1]+4));
				c.fill();
				c.closePath();
				// front
				c.fillStyle = "#001";
				c.beginPath();
				c.moveTo(cw*r,h);
				for (let _3 in cp3)
					c.lineTo(cw*r+cu*cp3[_3][0],h-cu*cp3[_3][1]);
				c.fill();
				c.closePath();
				// windows
				c.fillStyle = "#992";
				for (let _wi in wi)
					c.fillRect(cw*r+cu*wi[_wi][0],h-cu*wi[_wi][1],cu,cu*1.5);
			}
		
			// score
			if (score) {
				c.fillStyle = "#fff";
				c.font = "2em 'Press Start 2P'";
				c.textAlign = "center";
				c.textBaseline = "middle";
				c.fillText(score,w/2,80);
			}
		},
		addF = function(e) {
			if (F.length <= FLimit) {
				FChance = 0;
				let rawX = e.pageX - C.offsetLeft,
					rawY = e.pageY - C.offsetTop,
					// (click coord * virtual dimension) / canvas dimension
					x = Math.round((rawX * w) / C.offsetWidth),
					y = Math.round((rawY * h) / C.offsetHeight),
					sx = x < w/3 ? w/4 : (x > w/3 && x < w*(2/3) ? w/2 : w*0.75),
					sy = 60;
				F.push(new _F(sx,h-sy,x,y));
			}
		},
		run = function(){
			draw();
			requestAnimFrame(run);
		},
		hold = false,
		holdSt = 0,
		isTouch = "ontouchstart" in document.documentElement,
		inputD = isTouch ? "touchstart" : "mousedown",
		inputM = isTouch ? "touchmove" : "mousemove",
		inputU = isTouch ? "touchend" : "mouseup",
		inputO = isTouch ? "touchcancel" : "mouseout";
	
	run();
	
	C.addEventListener(inputD,function(e){
		addF(e);
		hold = !hold;
		holdSt = Date.now();
	});
	C.addEventListener(inputM,function(e){
		// control how many fireworks are launched as you drag
		let rate = 4;
		if (hold && (Date.now() - holdSt) % rate === 0)
			addF(e);
	});
	// use document instead of canvas to prevent firing on hover
	document.addEventListener(inputU,function(){
		if (hold)
			hold = !hold;
	});
	document.addEventListener(inputO,function(){
		if (hold)
			hold = !hold;
	});
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console