Skip to content

Instantly share code, notes, and snippets.

@Announcement
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save Announcement/2b88def9290ebc8e4f95 to your computer and use it in GitHub Desktop.

Select an option

Save Announcement/2b88def9290ebc8e4f95 to your computer and use it in GitHub Desktop.
ijmasodn9oasdnmads
//Framework 4.0
var ascii = {}, color, display, icon, paint, process, text = {}, setColor, shape = {};
function Values(object) {
return Object.keys(object).map(function(v){ return object[v]; });
}
function Animate(a){
var b = a.seconds,
g = a.maximum,
h = a.drawing,
i = a.exhaust,
c = new Date();
function Render(){
var d = new Date(),
e = (d - c) / (b * 1000),
f = Math.max(0, Math.min(g, g - g * e));
h(f);
if (d - c < 1000 * b) requestAnimationFrame(Render);
else i();
}
requestAnimationFrame(Render);
}
var map = Function.prototype.call.bind(Array.prototype.map);
(function init_color() {
var hex = {}, cmyk = {}, hsv = {}, hsl = {},
word = {}, rgba = {}, utils = {}, preset,
//meta = ctx.getImageData(0, 0, width, height),
buf = new ArrayBuffer(8),
buf8 = new Uint8ClampedArray(buf),
data = new Uint32Array(buf);
data[1] = 0x0a0b0c0d;
var isLittleEndian = true;
if (buf[4] === 0x0a && buf[5] === 0x0b && buf[6] === 0x0c &&
buf[7] === 0x0d) {
isLittleEndian = false;
}
function p16(a) {
return parseInt(a,16);
}
function rgb(r, g, b, a) {
return {'r': r, 'g': g, 'b': b, 'a': (a || 1)};
}
preset = {
"red": rgb(255, 0, 0),
"green": rgb(0, 255, 0),
"blue": rgb(0, 0, 255),
"yellow": rgb(255, 255, 0),
"orange": rgb(255, 127, 0),
"gray": rgb(255 / 3, 255 / 3, 255 / 3),
"grey": rgb(255 * 2/3, 255 * 2/3, 255 * 2/3),
"black": rgb(0, 0, 0),
"white": rgb(255, 255, 255),
"transparent": rgb(0, 0, 0, 1),
"shade": rgb(0, 0, 0, 1/3),
"fade": rgb(255, 255, 255, 1/3)
};
rgba.to = function toRGBA(r, g, b, a) {
return "rgba("+r+","+g+","+b+","+a+")";
}
rgba.from = function fromRGBA(str) {
return rgb.apply(this,str.split(",").map(function(v){return parseInt(v.replace(/\D+/g,""),10);}));
};
word.to = function toWORD(r, g, b, a) {
if (isLittleEndian)
return (a << 24) | (b << 16) | (g << 8) | r;
else
return (r << 24) | (g << 16) | (b << 8) | a;
};
word.from = function fromWORD(z) {
var y0 = (z >> 24) & 0xFF,
y1 = (z >> 16) & 0xFF,
y2 = (z >> 8) & 0xFF,
y3 = (z ) & 0xFF;
return ( isLittleEndian ) ? rgb(y3, y2, y1, y0) : rgb(y0, y1, y2, y3);
};
hex.to = function toHex(r, g, b) {
return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
};
hex.from = function fromHex(hex) {
var rx1, rx2, res;
rx1 = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i;
rx2 = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
res = rx1.exec(hex.replace(rx2, function(m, r, g, b){ return r+r+g+g+b+b;}));
return res && rgb.apply(this, res.slice(1).map(p16));
};
cmyk.GNU = function fromCMYK_GNU(c, m, y, k) {
var a = 255 - k * 255;
function calc(v) { return a * ( 255 - v * 255) / 255; }
return rgb.apply(this, map(arguments, calc));
};
cmyk.APS = function fromCYMK_APS(c, m, y, k) {
function calc(v) { return Math.min(255, v * 255 + k * 255); }
return rgb.apply(this, map(arguments, calc));
};
cmyk.to = function toCMYK(r, g, b) {
function out(c,m,y,k){ return {'c':c,'m':m,'y':y,'k':k}; }
function first(v) { return 1 - (v / 255); }
function calc(v) { return (v - z) / (1 - z); }
if (!(r | g | b)) return out(0, 0, 0, 1);
var a = map(arguments, first),
z = a.sort()[0];
return out.apply(this, a.map(calc).concat([z]));
};
hsv.to = function toHSV(r, g, b) {
function out(h,s,v){return {'h':h,'s':s,'v':v};}
var h = 0, s = 0, v = 0;
r = r / 255;
g = g / 255;
b = b / 255;
var min = Math.min(r, g, b),
max = Math.max(r, g, b);
if (!(min^max)) return out(0, 0, min);
var d = !(min^max)? g - b : !(b^min)? r - g : b - r,
h = !(r^min)? 3 : !(b^min)? 1 : 5;
return out(60 * (h - d / max - min), (max - min) / max, max);
};
hsv.from = function fromHSV(h, s, v) {
var i = Math.floor(h * 6),
f = h * 6 - i,
p = v * (1 - s),
q = v * (1 - f * s),
t = v * (1 - (1 - f) * s);
return ([
rgbF(v * 255, t * 255, p * 255),
rgbF(q * 255, v * 255, p * 255),
rgbF(p * 255, v * 255, t * 255),
rgbF(p * 255, q * 255, v * 255),
rgbF(t * 255, p * 255, v * 255),
rgbF(v * 255, p * 255, q * 255)
])[i%6];
};
hsl.from = function fromHSL(h, s, l) {
var r, g, b;
if (!s) r = g = b = l;
else {
function h2g(p, q, t) {
if (t < 0) t++;
if (t > 0) t--;
if (t<1/6) return p + (q - p) * 6 * t;
if (t<1/2) return q;
if (t<2/3) return p + (q - p) * (2/3 - t) * 6;
return p;
}
var q = l < 0.5 ? l * (1 + s) : l + s - l * s,
p = 2 * l - q;
r = hue2rgb(p, q, h + 1/3);
g = hue2rgb(p, q, h);
b = hue2rgb(p, q, h - 1/3);
}
return rgb.apply(this, map(arguments, Math.round));
};
hsl.to = function toHSL(r, g, b) {
r /= 255, g /= 255, b /= 255;
var max = Math.max(r, g, b),
min = Math.min(r, g, b);
var h, s, l = (max + min) / 2;
if (max==min) h=s=0;
else {
var d = max - min;
s = (l > 0.5) ? d / (2 - max - min) : d / (max + min);
switch(max){
case r: h = (g - b) / d + (g < b ? 6 : 0); break;
case g: h = (b - r) / d + 2; break;
case b: h = (r - g) / d + 4; break;
}
h /= 6;
}
return {'h': h, 's': s, 'l': l};
};
utils.invert = function(r, g, b) {
function calc(v) { return 255 - v; }
return rgb.apply(this, map(arguments, calc));
};
utils.sepia = function(r, g, b) {
return rgb(
r * 0.393 + g * 0.769 + b * 0.189,
r * 0.349 + g * 0.686 + b * 0.168,
r * 0.272 + g * 0.534 + b * 0.131
);
};
utils.desaturate = function(r, g, b) {
var a = r * 0.3 + g * 0.59 + b * 0.11;
return rgb(a, a, a);
};
function use(a, b) {
return Function.prototype.apply.bind(a, null, Values(b))
}
color = function(k) {
if (!arguments.length) {
return {
'hsv': hsv.from,
'hsl': hsl.from,
'rgba': rgba.from,
'rgb': rgb,
'cmyk': cmyk.GNU,
'hex': hex.from,
'word': word.from,
'preset': preset
};
} else {
return {
'desaturate': use(utils.desaturate, k),
'sepia': use(utils.sepia, k),
'invert': use(utils.invert, k),
'rgb': use(rgb, k),
'hsl': use(hsl.to, k),
'cmyk': use(cmyk.to, k),
'word': use(word.to, k),
'hex': use(hex.to, k),
'hsv': use(hsv.to, k),
'rgba': use(rgba.to, k)
};
}
};
}).call(this);
(function init_canvas() {
var head, link;
display = document.createElement("canvas").getContext("2d");
icon = document.createElement("canvas").getContext("2d");
document.body.appendChild(display.canvas);
display.canvas.width = 640;
display.canvas.height = 360;
icon.canvas.width = 16;
icon.canvas.height = 16;
head = document.getElementsByTagName("head")[0];
link = document.createElement("link");
link.type = "image/x-icon";
link.rel = "shortcut icon";
head.appendChild(link);
icon.clearRect(0, 0, 16, 16);
icon.beginPath();
icon.strokeStyle=color(color().preset.black).rgba();
icon.fillStyle=color(color().preset.white).rgba();
icon.arc(8,8,8,0,Math.PI*2,false);
//ci.fill();
icon.stroke();
icon.closePath();
display.clearRect(0, 0, display.width, display.height);
function update() {
link.href = icon.canvas.toDataURL("image/x-icon");
}
shape.circle = function(x, y, r) {
display.arc(x, y, r, 0, Math.PI * 2, false);
};
shape.line = function(x, y, h, k, w) {
if (w) display.lineWidth = w;
display.moveTo(x, y);
display.lineTo(h, k);
};
update();
process = function process() {
console.log("Processing...");
var meta = display.getImageData(0, 0, display.canvas.width, display.canvas.height),
buf = new ArrayBuffer(meta.data.length),
buf8 = new Uint8ClampedArray(buf),
data = new Uint32Array(buf);
var i = data.length;
function find(x, y){
return y * display.canvas.width + x;
}
function get(i) {
var r = meta.data[i * 4 + 0],
g = meta.data[i * 4 + 1],
b = meta.data[i * 4 + 2],
a = meta.data[i * 4 + 3];
return color().rgb(r, g, b, a);
}
while (i--) {
var pixel = get(i);
pixel.r = 255;
data[i] = color(pixel).word();
}
meta.data.set(buf8);
display.putImageData(meta, 0, 0);
/*var gfx = new Image();
gfx.src = display.canvas.toDataURL("image/png");
gfx.width = display.canvas.width;
gfx.height = display.canvas.height;
console.log(gfx.src);
document.body.appendChild(gfx);*/
console.log("Filtered!");
};
}).call(this);
(function init_ascii() {
ascii.nums = [
"111101101101111",//0
"010110010010111",//1
"111001111100111",//2
"111001111001111",//3
"101101111001001",//4
"111100111001111",//5
"111100111101111",//6
"111001001001001",//7
"111101111101111",//8
"111101111001111" //9
];
ascii.caps = [
"1111110001111111000110001",/*A*/
"1111010001111101000111110",/*B*/
"1111110000100001000011111",/*C*/
"1111010001100011000111110",/*D*/
"1111110000111111000011111",/*E*/
"1111110000111111000010000",/*F*/
"1111110000101111000111111",/*G*/
"1000110001111111000110001",/*H*/
"1111100100001000010011111",/*I*/
"1111100001100011000101110",/*J*/
"1000110010111001001010001",/*K*/
"1000010000100001000011111",/*L*/
"1111110101101011010110101",/*M*/
"1000111001101011001110001",/*N*/
"1111110001100011000111111",/*O*/
"1111110001111111000010000",/*P*/
"1111110001101011001111111",/*Q*/
"1111110001111111001010001",/*R*/
"1111110000111110000111111",/*S*/
"1111100100001000010000100",/*T*/
"1000110001100011000111111",/*U*/
"1000110001100010111000100",/*V*/
"1010110101101011010111111",/*W*/
"1000101010001000101010001",/*X*/
"1000101010001000010000100",/*Y*/
"1111100010001000100011111" /*Z*/
];
ascii.special = {
" " : (new Array(5*5+1)).join(0),
"?" : "0111010001000100000000010",
"!" : "0001000010000100000000010",
"." : "0000000000000000000000010",
"," : "0000000000000000011000010",
"\"": "0101001010000000000000000",
"'" : "0010000100000000000000000",
"/" : "0000100010001000100010000",
"%" : "1000100010001000100010001",
"\\": "1000001000001000001000001",
"*" : "1010101110111110111010101",
"#" : "0101011111010101111101010",
"$" : "0111110101111111010111110",
"~" : "0000001001101011001000000",
"(" : "0001000100001000010000010",
")" : "0010000010000100001000100",
">" : "1000001000001000100010000",
"<" : "0000100010001000001000001",
"-" : "0000000000011100000000000",
"+" : "0000000100111110010000000",
"=" : "0000001110000000111000000",
"_" : "0000000000000000000011111",
"[" : "1110010000100001000011100",
"]" : "0011100001000010000100111",
"}" : "0100000100110000010001000",
"{" : "0001000100000110010000010",
"|" : "0010000100000000010000100",
":" : "0000000100000000010000000",
"^" : "0010001010100010000000000",
"`" : "1000001000000000000000000",
";" : "0000000100000000010001000",
"@" : "0111010101110111010001110",
"&" : "0111011001011001101001101",
"\r":"0010101001111100100000100"};
var left = "left", right = "right", center = "center",
top = "top", bottom = "bottom", middle = "middle";
function find(char) {
var code = char.toUpperCase().charCodeAt(0);
//letter
if (code > 64 && code <= 64 + 26)
return ascii.caps[~-code - 64];
//number
if (code > 47 && code <= 47 + 10)
return ascii.nums[~-code - 47];
//special
if (ascii.special.hasOwnProperty(char))
return ascii.special[char];
//unsupported
return false;
}
function width_char(char, size) {
return (find(char).length / 5) * size;
}
text.width = function width_string(string, size) {
var width = 0, i = string.length, char;
while (i--) {
char = string[~-string.length - i];
width += ((width_char(char, size) / size) + 1) * size;
}
return width;
};
var width_string = text.width;
paint = function paint(f) {
display.beginPath();
f.apply(null, Array.prototype.slice.call(arguments, 1));
display.fill();
display.stroke();
display.closePath();
};
shape.rectangle = function rectangle(x,y,w,h) {
display.moveTo(x, y);
display.rect(x, y, w, h);
};
function draw_char(char, x, y, size) {
var a = find(char), b, h = a.length / 5, k = 5;
while(k--) {
b = a.substring(k * h, -~k * h);
for (var j = 0; j < h; j++) {
var left = x + j * size,
top = y + k * size;
if (!~-b[j]) {
//rectangle left, top, size, size
shape.rectangle(left, top, size, size);
}
}
}
}
function draw_string(string, x, y, size) {
var char, width = 0, key;
for (var i = string.length; i--; ) {
key = ~-string.length - i;
char = string[key];
draw_char(char, x + width, y, size);
width += ((width_char(char, size) / size) + 1) * size;
}
}
setColor = function setColor(c) {
display.fillStyle = c;
display.strokeStyle = c;
icon.fillStyle = c;
icon.strokeStyle = c;
};
shape.text = function text(string, size, x, y) {
var width = width_string(string, size) - size,
height = 5 * size,
h, k, c = {'w': display.canvas.width, 'h': display.canvas.height};
if (-~[x,y].indexOf("left")) h = 0;
if (-~[x,y].indexOf("center")) h = c.w / 2 - width / 2;
if (-~[x,y].indexOf("right")) h = c.w - width;
if (-~[x,y].indexOf("top")) k = 0;
if (-~[x,y].indexOf("middle")) k = c.h / 2 - height / 2;
if (-~[x,y].indexOf("bottom")) k = c.h - height;
if (typeof x === "number") h = x;
if (typeof y === "number") k = y;
if (h === undefined) h = c.w / 2 - width / 2;
if (k === undefined) k = c.h / 2 - height / 2;
draw_string(string, h, k, size);
}
display.fillStyle="#000";
}).call(this);
(function ini() {
//setColor("#000");
//paint(shape.rectangle, 0, 0, display.canvas.width, display.canvas.height);
setColor("#0f0");
text("Hello, world!", 5, "right", "bottom");
//process();
setColor("#00f");
paint(shape.circle, 64, 64, 64);
for (var i = 0; i < 360; i += 45) {
var angle = i;
var theta = Math.PI * 2 * ((angle + 270) % 360) / 360;
setColor("#f00");
paint(shape.circle, 64 + Math.cos(theta) * (64 - 16), 64 + Math.sin(theta) * (64 - 16), 16);
}
});
(function init_clock() {
function fade(x) {
var now = new Date(), c, i;
setColor("#fff");
paint(shape.rectangle, 0, 0, display.canvas.width, display.canvas.height);
function drawNumber(num) {
var str = num.toString().right(2, "0");
paint(shape.text, str, 5, 128 - text.width(str, 5) / 2 + 2.5, 128 - (5*5/2));
}
function point(value, max, radius) {
var angle = (value % max) * 360 / max,
theta = Math.PI * 2 * ((angle + 270) % 360) / 360;
return {x: Math.cos(theta) * radius, y: Math.sin(theta) * radius};
}
setColor("#000");
paint(shape.circle, 128, 128, 128);
setColor("#fff");
paint(shape.circle, 128, 128, 126);
setColor("#000");
i = 60;
while(i--) {
var p = point(i, 60, 128),
q = point(i, 60, 124);
paint(shape.line, 128 + p.x, 128 + p.y, 128 + q.x, 128 + q.y, 2);
}
i = 12;
while(i--) {
var p = point(i, 12, 128),
q = point(i, 12, 120);
paint(shape.line, 128 + p.x, 128 + p.y, 128 + q.x, 128 + q.y, 3);
}
var mp = point(now.getMinutes() + now.getSeconds() / 60, 60, 92);
paint(shape.line, 128, 128, 128 + mp.x, 128 + mp.y, 2);
var hp = point(now.getHours() + now.getMinutes() / 60, 12, 86);
paint(shape.line, 128, 128, 128 + hp.x, 128 + hp.y, 4);
setColor("#f00");
var sp = point(now.getSeconds() + now.getMilliseconds() / 1000, 60, 120);
paint(shape.line, 128, 128, 128 + sp.x, 128 + sp.y, 2);
setColor("#000");
paint(shape.circle, 128, 128, 4);
}
function start() {
Animate({
seconds: 1,
maximum: 1,
drawing: fade,
exhaust: start
});
}
setTimeout(start, 1000 - (new Date()).getMilliseconds());
}).call(this);
function Construct(a){
return function () {
this.vars = {
arraysn :[],
object:{},
number:0,
string:""
};
var result = a.apply(this, arguments);
var var_ = Values(this.vars).filter(Is.empty)[0];
delete this.vars;
return this.result || var_ || this || result || !1;
}
}
Array.prototype.add = Construct(function(){
this.push(arguments[0]);
});
Array.prototype.matrix = function(a) {
var b=[], c=this;
if (!a) a = c;
c.forEach(function(d){b=b.concat(a.before(d));});
return b;
};
Array.prototype.after = function(a) {
return this.map(function(v){return v + a;});
};
Array.prototype.before = function(a){
return this.map(function(v){return a + v;});
};
Array.prototype.backwards = function(){
return this.map(function(v){return v.reverse();});
};
Array.prototype.median = function(){
return this[this.length/2|0];
};
Array.prototype.average = function(){
var num = 0;
this.forEach(function(v,k){num+=v;});
return num/this.length;
};
Array.prototype.sum = function(){
var num = 0;
this.forEach(function(v,k){num+=v;});
return num;
};
Array.prototype.from = Function.prototype.call.bind(Array.prototype.slice);
Object.defineProperties(Array.prototype, {
'-1': {
get: function() {
return this.slice(-1)[0];
},
set: function(v){
if(this.length>0)this[this.length-1]=v;
},
enumerable: false
},
'max': {
get: function() {
return this.sort(function(a,b){return a<b;})[0];
},
set: function(v){
this[this.indexOf(this.sort(function(a,b){return a<b;})[0])] = v;
},
enumerable: false
},
'min': {
get: function() {
return this.sort(function(a,b){return a>b;})[0];
},
set: function(v){
this[this.indexOf(this.sort(function(a,b){return a>b;})[0])] = v;
},
enumerable: false
},
'matrix':
{ enumerable: false },
'median':
{ enumerable: false },
'average':
{ enumerable: false },
'before':
{ enumerable: false },
'backwards':
{ enumerable: false },
'add':
{ enumerable: false },
'sum':
{ enumerable: false },
'after':
{ enumerable: false },
'from':
{ enumerable: false }
});
String.prototype.lines = function(){
return this.match(/^.+$/mg);
};
String.prototype.repeat = function(n){
return (new Array(n+1)).join(this);
};
String.prototype.reverse = function(){
return this.split("").reverse().join("");
};
String.prototype.left = function(len, fill)
{ var str = this; if (str.length>len) return str.substring(0,len); else return str + (new Array(len-str.length+1)).join((fill||" ")); }
String.prototype.right = function(len, fill)
{ var str = this; return LMono(len,str.split("").reverse().join(""),fill).split("").reverse().join(""); }
var LMono = function LMono(a,b,c)
{ return b.left(a, c); }
var RMono = function RMono(a,b,c)
{ return b.right(a, c); }
String.prototype.format = function() {
var data = this,
info = null,
args = ([]).from(arguments);
while(info = data.match(/%(-?)(\d*)(\S)/)) {
var rplc = args.shift(),
numb = (info[2])?parseInt(info[2],10) : -1,
side = !info[1],
type = info[3];
rplc= rplc[side && "left" || "right"](numb || rplc.length, " ");
data = data.replace(info[0], rplc);
}
return data;
};
Object.defineProperties(String.prototype, {
"lines":{enumerable: false},
"reverse":{enumerable: false},
"left":{enumerable:false},
"right":{enumerable:false},
"format":{enumerable:false}
});
Number.prototype.min = function(n){
return Math.min(this,n);
};
Number.prototype.max = function(n){
return Math.max(this,n);
};
Number.prototype.more = function(n){
return this > n;
};
Number.prototype.less = function(n){
return this < n;
};
Number.prototype.eqls = function(n){
var c;
if (typeof n === "string"){
if (n.matches(/[01]+/)) c = ParseInt(n,2);
if (n.matches(/0x[0-9a-f]+/)) c = parseInt(n,16);
if (n.indexOf("0")===0) c = parseInt(n,8);
else c = parseInt(n, 10);
} else if (typeof n === "object") {
if (n instanceof Array) c = n.length;
else if (n instanceof Element) c = n.childElementCount;
else c = Object.keys(n).length;
} else if (typeof n === "number") {
c = n;
} else if (typeof n === "boolean") {
c = n * 1;
} else {
return false;
}
return this < n;
};
Object.defineProperties(Number.prototype, {
"min":{enumerable: false},
"max":{enumerable: false},
"more":{enumerable:false},
"eqls":{enumerable:false},
"less":{enumerable:false}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment