forked from ejci/favico.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfavico-0.1.0.min.js
17 lines (17 loc) · 5.97 KB
/
favico-0.1.0.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
* @fileOverview Favico animations
* @author Miroslav Magda, http://blog.ejci.net
* @version 0.1.0
*/
var Favico=function(u){function w(a){if(a.paused||a.ended||r)return!1;b.clearRect(0,0,e,f);b.drawImage(a,0,0,e,f);setTimeout(w,g.duration,a);l.setIcon(n)}function z(a){a=a.replace(/^#?([a-f\d])([a-f\d])([a-f\d])$/i,function(a,b,d,c){return b+b+d+d+c+c});return(a=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(a))?{r:parseInt(a[1],16),g:parseInt(a[2],16),b:parseInt(a[3],16)}:!1}function A(a,b){var c={},d;for(d in a)c[d]=a[d];for(d in b)c[d]=b[d];return c}u=u?u:{};var c,t,f,e,n,b,h,B,q,v,x,k,r,m=[];
k=function(){};q=r=!1;c=A({bgColor:"#d00",textColor:"#fff",type:"circle",animation:"slide",elementId:!1},u);c.bgColor=z(c.bgColor);c.textColor=z(c.textColor);var p={ready:function(){q=!0;p.reset();k()},reset:function(){m=[];v=!1;b.clearRect(0,0,e,f);b.drawImage(h,0,0,e,f);l.setIcon(n)},start:function(){if(q&&!x){var a=function(){v=m[0];x=!1;0<m.length&&(m.shift(),p.start())};0<m.length&&(x=!0,v?g.run(v.options,function(){g.run(m[0].options,function(){a()},!1)},!0):g.run(m[0].options,function(){a()},
!1))}}},y={},C=function(a){a.n=Math.abs(a.n);a.x*=e;a.y*=f;a.w*=e;a.h*=f;return a};y.circle=function(a){a=C(a);var s=9<a.n;s&&(a.x-=0.4*a.w,a.w*=1.4);b.clearRect(0,0,e,f);b.drawImage(h,0,0,e,f);b.beginPath();b.font="bold "+Math.floor(a.h)+"px sans-serif";b.textAlign="center";s?(b.moveTo(a.x+a.w/2,a.y),b.lineTo(a.x+a.w-a.h/2,a.y),b.quadraticCurveTo(a.x+a.w,a.y,a.x+a.w,a.y+a.h/2),b.lineTo(a.x+a.w,a.y+a.h-a.h/2),b.quadraticCurveTo(a.x+a.w,a.y+a.h,a.x+a.w-a.h/2,a.y+a.h),b.lineTo(a.x+a.h/2,a.y+a.h),b.quadraticCurveTo(a.x,
a.y+a.h,a.x,a.y+a.h-a.h/2),b.lineTo(a.x,a.y+a.h/2),b.quadraticCurveTo(a.x,a.y,a.x+a.h/2,a.y)):b.arc(a.x+a.w/2,a.y+a.h/2,a.h/2,0,2*Math.PI);b.fillStyle="rgba("+c.bgColor.r+","+c.bgColor.g+","+c.bgColor.b+","+a.o+")";b.fill();b.closePath();b.beginPath();b.stroke();b.fillStyle="rgba("+c.textColor.r+","+c.textColor.g+","+c.textColor.b+","+a.o+")";b.fillText(s?"9+":a.n,Math.floor(a.x+a.w/2),Math.floor(a.y+a.h-0.15*a.h));b.closePath()};y.rectangle=function(a){a=C(a);var s=9<a.n;s&&(a.x=Math.floor(a.x-0.4*
a.w),a.w=Math.floor(1.4*a.w));b.clearRect(0,0,e,f);b.drawImage(h,0,0,e,f);b.beginPath();b.font="bold "+Math.floor(a.h)+"px sans-serif";b.textAlign="center";b.fillStyle="rgba("+c.bgColor.r+","+c.bgColor.g+","+c.bgColor.b+","+a.o+")";b.fillRect(a.x,a.y,a.w,a.h);b.fillStyle="rgba("+c.textColor.r+","+c.textColor.g+","+c.textColor.b+","+a.o+")";b.fillText(s?"9+":a.n,Math.floor(a.x+a.w/2),Math.floor(a.y+a.h-0.15*a.h));b.closePath()};var l={setIcon:function(a){a=a.toDataURL("image/png");c.elementId?document.getElementById(c.elementId).setAttribute("src",
a):(B&&(l.remove(t),t=l.create()),t.setAttribute("href",a))},getIcon:function(){var a=null;c.elementId?(a=document.getElementById(c.elementId),a.setAttribute("href",a.getAttribute("src"))):(a=l.get(),null==a&&(l.create(),a=l.get()));return a},create:function(){var a;a=document.createElement("link");a.setAttribute("rel","icon");a.setAttribute("href","data:,");document.getElementsByTagName("head")[0].appendChild(a);return a},remove:function(a){a.parentNode.removeChild(a)},get:function(){for(var a=null,
b=document.getElementsByTagName("head")[0].getElementsByTagName("link"),c=b.length-1;0<=c;c--)/icon/i.test(b[c].getAttribute("rel"))&&(a=b[c]);return a}},g={duration:40,types:{}};g.types.fade=[{x:0.4,y:0.4,w:0.6,h:0.6,o:0},{x:0.4,y:0.4,w:0.6,h:0.6,o:0.1},{x:0.4,y:0.4,w:0.6,h:0.6,o:0.2},{x:0.4,y:0.4,w:0.6,h:0.6,o:0.3},{x:0.4,y:0.4,w:0.6,h:0.6,o:0.4},{x:0.4,y:0.4,w:0.6,h:0.6,o:0.5},{x:0.4,y:0.4,w:0.6,h:0.6,o:0.6},{x:0.4,y:0.4,w:0.6,h:0.6,o:0.7},{x:0.4,y:0.4,w:0.6,h:0.6,o:0.8},{x:0.4,y:0.4,w:0.6,h:0.6,
o:0.9},{x:0.4,y:0.4,w:0.6,h:0.6,o:1}];g.types.none=[{x:0.4,y:0.4,w:0.6,h:0.6,o:1}];g.types.pop=[{x:1,y:1,w:0,h:0,o:1},{x:0.9,y:0.9,w:0.1,h:0.1,o:1},{x:0.8,y:0.8,w:0.2,h:0.2,o:1},{x:0.7,y:0.7,w:0.3,h:0.3,o:1},{x:0.6,y:0.6,w:0.4,h:0.4,o:1},{x:0.5,y:0.5,w:0.5,h:0.5,o:1},{x:0.4,y:0.4,w:0.6,h:0.6,o:1}];g.types.slide=[{x:0.4,y:1,w:0.6,h:0.6,o:1},{x:0.4,y:0.9,w:0.6,h:0.6,o:1},{x:0.4,y:0.9,w:0.6,h:0.6,o:1},{x:0.4,y:0.8,w:0.6,h:0.6,o:1},{x:0.4,y:0.7,w:0.6,h:0.6,o:1},{x:0.4,y:0.6,w:0.6,h:0.6,o:1},{x:0.4,y:0.5,
w:0.6,h:0.6,o:1},{x:0.4,y:0.4,w:0.6,h:0.6,o:1}];g.run=function(a,b,e,d){var f=g.types[c.animation];d=!0===e?"undefined"!==typeof d?d:f.length-1:"undefined"!==typeof d?d:0;b=b?b:function(){};d<f.length&&0<=d?(y[c.type](A(a,f[d])),l.setIcon(n),setTimeout(function(){d=e?d-1:d+1;g.run(a,b,e,d)},g.duration)):b()};(function(){try{t=l.getIcon(),n=document.createElement("canvas"),h=document.createElement("img"),h.setAttribute("src",t.getAttribute("href")),h.onload=function(){f=0<h.height?h.height:32;e=0<
h.width?h.width:32;n.height=f;n.width=e;b=n.getContext("2d");p.ready()},B=!0}catch(a){console.error("Error initializing favico...",a)}})();return{badge:function(a,b){k=function(){try{0<a?(g.types[""+b]&&(c.animation=b),m.push({type:"badge",options:{n:a}}),100<m.length&&console.warn("Too many badges request in queue..."),p.start()):p.reset()}catch(e){console.error("Error setting badge...",e)}};q&&k()},video:function(a){k=function(){try{"stop"===a?(r=!0,p.reset(),r=!1):a.addEventListener("play",function(){w(this)},
!1)}catch(b){throw b;}};q&&k()},image:function(a){k=function(){try{var c=a.width,g=a.height,d=document.createElement("img"),h=c/e<g/f?c/e:g/f;d.setAttribute("src",a.getAttribute("src"));console.log(c,g,h);d.height=g/h;d.width=c/h;b.clearRect(0,0,e,f);b.drawImage(d,0,0,e,f);l.setIcon(n)}catch(k){throw k;}};q&&k()},webcam:function(a){if(/chrome/i.test(navigator.userAgent.toLowerCase())){var b=!1;navigator.getUserMedia=navigator.getUserMedia||navigator.oGetUserMedia||navigator.msGetUserMedia||navigator.mozGetUserMedia||
navigator.webkitGetUserMedia;k=function(){try{"stop"===a?(r=!0,p.reset(),r=!1):(b=document.createElement("video"),b.width=e,b.height=f,navigator.getUserMedia({video:!0,audio:!1},function(a){b.src=URL.createObjectURL(a);b.play();w(b)},function(){}))}catch(c){throw c;}};q&&k()}else console.log("Sorry. Only chrome is supported yet...")},reset:p.reset}};