Skip to content

Instantly share code, notes, and snippets.

@AaronGhent
Created January 12, 2015 02:34
Show Gist options
  • Save AaronGhent/9f233f293822b98750ae to your computer and use it in GitHub Desktop.
Save AaronGhent/9f233f293822b98750ae to your computer and use it in GitHub Desktop.
hadoken easter egg
/*
Hadoken Easter Egg
Author: Aaron Ghent
*/
/* global $ */
var hadoken = function() {
if($('.hadoken').length > 0) {
return;
}
var nCanvas = $('<canvas>').attr({
class: 'hadoken',
width: window.innerWidth
}).css({
'pointer-events': 'none',
display: 'block',
position: 'fixed',
bottom: '0',
left: '0',
}).appendTo(document.body);
var __slice = [].slice;
var FPS = 30;
var PI_2 = 2 * Math.PI;
var SQRT_3 = Math.sqrt(3);
var canvas = nCanvas[0];
var ctx = canvas.getContext("2d");
var windowResizeHandler = function() {
canvas.width = window.innerWidth;
return canvas.height = window.innerHeight;
};
window.addEventListener("resize", windowResizeHandler, false);
window.addEventListener("load", function() {
return setTimeout(windowResizeHandler, 0);
});
window.requestAnimationFrame = (function() {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(cb) {
return window.setTimeout(cb, 1000 / FPS);
};
})();
var range = function(a, b) {
return (b - a) * Math.random() + a;
};
var randInt = function(a, b) {
return Math.floor(range(a, b + 1));
};
var Trail = (function() {
function Trail(x, y, r) {
this.opacity = 1;
this.xOrig = x + r - 5;
this.yOrig = y;
this.rOrig = r;
var rs = r * 0.4;
this.y = range(y - rs, y + rs);
var yd = Math.abs(this.y - y) / rs;
this.x = this.xOrig - (rs * yd / SQRT_3) + range(-1, 8);
this.ystep = range(-1, 1);
this.xstep = range(-r / 4 * (1 - yd), 0);
this.r = range(1, 8);
}
Trail.prototype.step = function() {
if (this.opacity < 0.6) {
this.opacity -= 0.06;
} else {
this.opacity -= 0.09;
}
if (this.opacity <= 0) {
return false;
}
this.x += this.xstep;
this.y += this.ystep;
return true;
};
Trail.prototype.draw = function() {
var r = this.r * this.opacity * 3;
var style = ctx.createRadialGradient(this.x, this.y, 0, this.x, this.y, r);
if (this.opacity < 0.6) {
style.addColorStop(0, "rgba(0,90,255," + this.opacity + ")");
style.addColorStop(1, "rgba(0,90,255,0)");
} else {
style.addColorStop(0, "rgba(0,255,255,0.5)");
style.addColorStop(1, "rgba(0,120,255,0)");
}
ctx.beginPath();
ctx.arc(this.x, this.y, this.r * 2, 0, PI_2, true);
ctx.fillStyle = style;
return ctx.fill();
};
return Trail;
})();
var Hadouken = (function() {
function Hadouken() {
this.x = -50;
this.xstep = 5;
this.r = 35;
this.r2 = 2 * this.r;
this.priority = 1;
}
Hadouken.prototype.destructor = function() {
$('canvas.hadoken').remove(); // hack to remove hadoken element
return null; // dont loop
};
Hadouken.prototype.step = function() {
this.x += this.xstep;
this.y = canvas.height / 2;
return this.x < canvas.width && this.y < canvas.height;
};
Hadouken.prototype.draw = function() {
ctx.beginPath();
ctx.arc(this.x + 3, this.y, this.r2, 0, PI_2, true);
ctx.fillStyle = (function(_this) {
return function() {
var g;
g = ctx.createRadialGradient(_this.x + _this.r2, _this.y, 0, _this.x + 3, _this.y, _this.r2);
g.addColorStop(0, "rgba(0,255,255,0.6)");
g.addColorStop(1, "rgba(0,0,255,0)");
return g;
};
})(this)();
ctx.fill();
ctx.beginPath();
ctx.arc(this.x + this.r - 3 + range(-3, 3), this.y + range(-4, 4), this.r, 0, PI_2, true);
ctx.fillStyle = (function(_this) {
return function() {
var g;
g = ctx.createRadialGradient(_this.x + _this.r2, _this.y, 0, _this.x + _this.r, _this.y, _this.r);
g.addColorStop(0, "rgba(255,255,255,0.8)");
g.addColorStop(1, "rgba(255,255,255,0)");
return g;
};
})(this)();
ctx.fill();
var _results = [];
var i, _i;
for (i = _i = 1; _i <= 15; i = ++_i) {
_results.push(w.addEntity(Trail, this.x, this.y, this.r2));
}
return _results;
};
return Hadouken;
})();
var World = (function() {
function World() {
this.entities = {};
this.i = 0;
this.loop();
}
World.prototype.addEntity = function() {
var args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
var klass = arguments[0];
this.entities[this.i] = (function(func, args, Ctor) {
Ctor.prototype = func.prototype;
var child = new Ctor();
var result = func.apply(child, args);
return Object(result) === result ? result : child;
})(klass, args, function(){});
return this.i++;
};
World.prototype.loop = function() {
requestAnimationFrame((function(_this) {
return function() {
return _this.loop();
};
})(this));
ctx.clearRect(0, 0, canvas.width, canvas.height);
var ids = [];
var _ref = this.entities;
var k;
var entity;
for (k in _ref) {
entity = _ref[k];
if (entity.priority === 1) {
ids.push(k);
continue;
}
if (!entity.step()) {
delete this.entities[k];
continue;
}
entity.draw();
}
var _results = [];
var _i, _len;
for (_i = 0, _len = ids.length; _i < _len; _i++) {
k = ids[_i];
entity = this.entities[k];
if (!entity.step()) {
entity.destructor();
delete this.entities[k];
continue;
}
_results.push(entity.draw());
}
return this.entities;
};
return World;
})();
var w = new World();
w.addEntity(Hadouken);
};
export default hadoken;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment