-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheaster.js
171 lines (156 loc) · 5.22 KB
/
easter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Konami-JS ~
* :: Now with support for touch events and multiple instances for
* :: those situations that call for multiple easter eggs!
* Code: http://konami-js.googlecode.com/
* Examples: http://www.snaptortoise.com/konami-js
* Copyright (c) 2009 George Mandis (georgemandis.com, snaptortoise.com)
* Version: 1.4.2 (9/2/2013)
* Licensed under the MIT License (http://opensource.org/licenses/MIT)
* Tested in: Safari 4+, Google Chrome 4+, Firefox 3+, IE7+
*/
var Konami = function (callback) {
var konami = {
addEvent: function (obj, type, fn, ref_obj) {
if (obj.addEventListener)
obj.addEventListener(type, fn, false);
else if (obj.attachEvent) {
// IE
obj["e" + type + fn] = fn;
obj[type + fn] = function () {
obj["e" + type + fn](window.event, ref_obj);
}
obj.attachEvent("on" + type, obj[type + fn]);
}
},
input: "",
pattern: "38384040373937396665",
load: function (link) {
this.addEvent(document, "keydown", function (e, ref_obj) {
if (ref_obj) konami = ref_obj; // IE
konami.input += e ? e.keyCode : event.keyCode;
if (konami.input.length > konami.pattern.length)
konami.input = konami.input.substr((konami.input.length - konami.pattern.length));
if (konami.input == konami.pattern) {
konami.code(link);
konami.input = "";
e.preventDefault();
return false;
}
}, this);
},
code: function (link) {
window.location = link
}
}
typeof callback === "string" && konami.load(callback);
if (typeof callback === "function") {
konami.code = callback;
konami.load();
}
return konami;
};
Math.easeInSine = function (t, b, c, d) {
return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
};
/**
* Easter fuzz
* */
var Easter = function(){
var easter = {
canvas : null,
img: null,
ctx : null,
msStart : null,
aframe : null,
pic: '/assets/images/easter.gif',
init : function(){
this.setCanvas();
// added toggle to get 30 FPS instead of 60 FPS
var toggle = true,
_self = this;
(function loop() {
toggle = !toggle;
if (toggle) {
_self.aframe = requestAnimationFrame(loop);
return;
}
var cont = _self.noise(_self.ctx);
if (cont) {
_self.aframe = requestAnimationFrame(loop);
}
else {
_self.bam();
}
})();
},
bam: function() {
this.img = document.createElement('div');
this.img.style.backgroundImage = 'url(' + this.pic + ')';
this.img.style.backgroundPosition = 'center center';
this.img.style.backgroundRepeat = 'no-repeat';
this.img.style.position = 'fixed';
this.img.style.top = 0;
this.img.style.zIndex = 1002;
this.img.style.height = '100vh';
this.img.style.width = '100vw';
document.body.insertBefore(this.img, document.body.firstChild);
var _self = this;
document.onkeypress = function (e) {
_self.cleanup();
e.target.removeEventListener(e.type, listener)
};
document.onmouseover = function(e) {
_self.cleanup();
e.target.removeEventListener(e.type, listener)
}
},
cleanup : function() {
cancelAnimationFrame(this.afram);
this.img.parentNode.removeChild(this.img);
this.canvas.parentNode.removeChild(this.canvas);
},
setCanvas : function() {
this.canvas = document.createElement('canvas');
document.body.insertBefore(this.canvas, document.body.firstChild);
this.canvas.style.position = 'fixed';
this.canvas.style.zIndex = 1001;
this.canvas.style.top = 0;
this.canvas.style.width = window.innerWidth + 'px';
this.canvas.style.height = window.innerHeight + 'px';
this.ctx = this.canvas.getContext('2d')
},
noise : function(ctx) {
var w = ctx.canvas.width,
h = ctx.canvas.height,
idata = ctx.createImageData(w, h),
buffer32 = new Uint32Array(idata.data.buffer),
len = buffer32.length,
i = 0,
t = 0;
for(; i < len;i++) {
t = this.getNoiseThreshold();
if (Math.random() < t){
buffer32[i] = 0xff000000;
}
}
ctx.putImageData(idata, 0, 0);
if (t > 0.99) {
ctx.fillStyle="#000000";
ctx.rect(0,0,2000,2000);
ctx.fill();
return false;
}
return true;
},
getNoiseThreshold : function() {
this.msStart = (!this.msStart ? Date.now() : this.msStart);
var elapsed = Date.now() - this.msStart,
rate = 4000,
threshold = (elapsed % rate) / rate;
return threshold;
}
};
easter.init();
};
Konami(Easter);