forked from ejci/favico.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfavico.min.js
8 lines (8 loc) · 9.32 KB
/
favico.min.js
1
2
3
4
5
6
7
8
/**
* @license MIT or GPL-2.0
* @fileOverview Favico animations
* @author Miroslav Magda, http://blog.ejci.net
* @source: https://github.com/ejci/favico.js
* @version 0.3.10
*/
!function(){var e=function(e){"use strict";function t(e){if(e.paused||e.ended||w)return!1;try{c.clearRect(0,0,s,l),c.drawImage(e,0,0,s,l)}catch(o){}m=setTimeout(function(){t(e)},j.duration),k.setIcon(h)}function o(e){var t=/^#?([a-f\d])([a-f\d])([a-f\d])$/i;e=e.replace(t,function(e,t,o,n){return t+t+o+o+n+n});var o=/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(e);return o?{r:parseInt(o[1],16),g:parseInt(o[2],16),b:parseInt(o[3],16)}:!1}function n(e,t){var o,n={};for(o in e)n[o]=e[o];for(o in t)n[o]=t[o];return n}function r(){return b.hidden||b.msHidden||b.webkitHidden||b.mozHidden}e=e?e:{};var i,a,l,s,h,c,f,u,y,d,g,w,p,x,m,b,v={bgColor:"#d00",textColor:"#fff",fontFamily:"sans-serif",fontStyle:"bold",type:"circle",position:"down",animation:"slide",elementId:!1,element:null,dataUrl:!1,win:window};p={},p.ff="undefined"!=typeof InstallTrigger,p.chrome=!!window.chrome,p.opera=!!window.opera||navigator.userAgent.indexOf("Opera")>=0,p.ie=/*@cc_on!@*/!1,p.safari=Object.prototype.toString.call(window.HTMLElement).indexOf("Constructor")>0,p.supported=p.chrome||p.ff||p.opera;var C=[];g=function(){},u=w=!1;var E=function(){i=n(v,e),i.bgColor=o(i.bgColor),i.textColor=o(i.textColor),i.position=i.position.toLowerCase(),i.animation=j.types[""+i.animation]?i.animation:v.animation,b=i.win.document;var t=i.position.indexOf("up")>-1,r=i.position.indexOf("left")>-1;if(t||r)for(var u in j.types)for(var y=0;y<j.types[u].length;y++){var d=j.types[u][y];t&&(d.y<.6?d.y=d.y-.4:d.y=d.y-2*d.y+(1-d.w)),r&&(d.x<.6?d.x=d.x-.4:d.x=d.x-2*d.x+(1-d.h)),j.types[u][y]=d}i.type=M[""+i.type]?i.type:v.type,a=k.getIcons(),h=document.createElement("canvas"),f=document.createElement("img");var g=a[a.length-1];g.hasAttribute("href")?(f.setAttribute("crossOrigin","anonymous"),f.onload=function(){l=f.height>0?f.height:32,s=f.width>0?f.width:32,h.height=l,h.width=s,c=h.getContext("2d"),I.ready()},f.setAttribute("src",g.getAttribute("href"))):(l=32,s=32,f.height=l,f.width=s,h.height=l,h.width=s,c=h.getContext("2d"),I.ready())},I={};I.ready=function(){u=!0,I.reset(),g()},I.reset=function(){u&&(C=[],y=!1,d=!1,c.clearRect(0,0,s,l),c.drawImage(f,0,0,s,l),k.setIcon(h),window.clearTimeout(x),window.clearTimeout(m))},I.start=function(){if(u&&!d){var e=function(){y=C[0],d=!1,C.length>0&&(C.shift(),I.start())};if(C.length>0){d=!0;var t=function(){["type","animation","bgColor","textColor","fontFamily","fontStyle"].forEach(function(e){e in C[0].options&&(i[e]=C[0].options[e])}),j.run(C[0].options,function(){e()},!1)};y?j.run(y.options,function(){t()},!0):t()}}};var M={},T=function(e){return e.n="number"==typeof e.n?Math.abs(0|e.n):e.n,e.x=s*e.x,e.y=l*e.y,e.w=s*e.w,e.h=l*e.h,e.len=(""+e.n).length,e};M.circle=function(e){e=T(e);var t=!1;2===e.len?(e.x=e.x-.4*e.w,e.w=1.4*e.w,t=!0):e.len>=3&&(e.x=e.x-.65*e.w,e.w=1.65*e.w,t=!0),c.clearRect(0,0,s,l),c.drawImage(f,0,0,s,l),c.beginPath(),c.font=i.fontStyle+" "+Math.floor(e.h*(e.n>99||e.n.length>2?.8:.9))+"px "+i.fontFamily,c.textAlign="center",t?(c.moveTo(e.x+e.w/2,e.y),c.lineTo(e.x+e.w-e.h/2,e.y),c.quadraticCurveTo(e.x+e.w,e.y,e.x+e.w,e.y+e.h/2),c.lineTo(e.x+e.w,e.y+e.h-e.h/2),c.quadraticCurveTo(e.x+e.w,e.y+e.h,e.x+e.w-e.h/2,e.y+e.h),c.lineTo(e.x+e.h/2,e.y+e.h),c.quadraticCurveTo(e.x,e.y+e.h,e.x,e.y+e.h-e.h/2),c.lineTo(e.x,e.y+e.h/2),c.quadraticCurveTo(e.x,e.y,e.x+e.h/2,e.y)):c.arc(e.x+e.w/2,e.y+e.h/2,e.h/2,0,2*Math.PI),c.fillStyle="rgba("+i.bgColor.r+","+i.bgColor.g+","+i.bgColor.b+","+e.o+")",c.fill(),c.closePath(),c.beginPath(),c.stroke(),c.fillStyle="rgba("+i.textColor.r+","+i.textColor.g+","+i.textColor.b+","+e.o+")","number"==typeof e.n&&e.n>999?c.fillText((e.n>9999?9:Math.floor(e.n/1e3))+"k+",Math.floor(e.x+e.w/2),Math.floor(e.y+e.h-.2*e.h)):c.fillText(e.n,Math.floor(e.x+e.w/2),Math.floor(e.y+e.h-.15*e.h)),c.closePath()},M.rectangle=function(e){e=T(e);var t=!1;2===e.len?(e.x=e.x-.4*e.w,e.w=1.4*e.w,t=!0):e.len>=3&&(e.x=e.x-.65*e.w,e.w=1.65*e.w,t=!0),c.clearRect(0,0,s,l),c.drawImage(f,0,0,s,l),c.beginPath(),c.font=i.fontStyle+" "+Math.floor(e.h*(e.n>99?.9:1))+"px "+i.fontFamily,c.textAlign="center",c.fillStyle="rgba("+i.bgColor.r+","+i.bgColor.g+","+i.bgColor.b+","+e.o+")",c.fillRect(e.x,e.y,e.w,e.h),c.fillStyle="rgba("+i.textColor.r+","+i.textColor.g+","+i.textColor.b+","+e.o+")","number"==typeof e.n&&e.n>999?c.fillText((e.n>9999?9:Math.floor(e.n/1e3))+"k+",Math.floor(e.x+e.w/2),Math.floor(e.y+e.h-.2*e.h)):c.fillText(e.n,Math.floor(e.x+e.w/2),Math.floor(e.y+e.h-.15*e.h)),c.closePath()};var A=function(e,t){t=("string"==typeof t?{animation:t}:t)||{},g=function(){try{if("number"==typeof e?e>0:""!==e){var n={type:"badge",options:{n:e}};if("animation"in t&&j.types[""+t.animation]&&(n.options.animation=""+t.animation),"type"in t&&M[""+t.type]&&(n.options.type=""+t.type),["bgColor","textColor"].forEach(function(e){e in t&&(n.options[e]=o(t[e]))}),["fontStyle","fontFamily"].forEach(function(e){e in t&&(n.options[e]=t[e])}),C.push(n),C.length>100)throw new Error("Too many badges requests in queue.");I.start()}else I.reset()}catch(r){throw new Error("Error setting badge. Message: "+r.message)}},u&&g()},U=function(e){g=function(){try{var t=e.width,o=e.height,n=document.createElement("img"),r=o/l>t/s?t/s:o/l;n.setAttribute("crossOrigin","anonymous"),n.onload=function(){c.clearRect(0,0,s,l),c.drawImage(n,0,0,s,l),k.setIcon(h)},n.setAttribute("src",e.getAttribute("src")),n.height=o/r,n.width=t/r}catch(i){throw new Error("Error setting image. Message: "+i.message)}},u&&g()},R=function(e){g=function(){k.setIconSrc(e)},u&&g()},O=function(e){g=function(){try{if("stop"===e)return w=!0,I.reset(),void(w=!1);e.addEventListener("play",function(){t(this)},!1)}catch(o){throw new Error("Error setting video. Message: "+o.message)}},u&&g()},S=function(e){if(window.URL&&window.URL.createObjectURL||(window.URL=window.URL||{},window.URL.createObjectURL=function(e){return e}),p.supported){var o=!1;navigator.getUserMedia=navigator.getUserMedia||navigator.oGetUserMedia||navigator.msGetUserMedia||navigator.mozGetUserMedia||navigator.webkitGetUserMedia,g=function(){try{if("stop"===e)return w=!0,I.reset(),void(w=!1);o=document.createElement("video"),o.width=s,o.height=l,navigator.getUserMedia({video:!0,audio:!1},function(e){o.src=URL.createObjectURL(e),o.play(),t(o)},function(){})}catch(n){throw new Error("Error setting webcam. Message: "+n.message)}},u&&g()}},L=function(e,t){var n=e;(null!=t||"[object Object]"!=Object.prototype.toString.call(e))&&(n={},n[e]=t);for(var r=Object.keys(n),a=0;a<r.length;a++)"bgColor"==r[a]||"textColor"==r[a]?i[r[a]]=o(n[r[a]]):i[r[a]]=n[r[a]];C.push(y),I.start()},k={};k.getIcons=function(){var e=[],t=function(){for(var e=[],t=b.getElementsByTagName("head")[0].getElementsByTagName("link"),o=0;o<t.length;o++)/(^|\s)icon(\s|$)/i.test(t[o].getAttribute("rel"))&&e.push(t[o]);return e};return i.element?e=[i.element]:i.elementId?(e=[b.getElementById(i.elementId)],e[0].setAttribute("href",e[0].getAttribute("src"))):(e=t(),0===e.length&&(e=[b.createElement("link")],e[0].setAttribute("rel","icon"),b.getElementsByTagName("head")[0].appendChild(e[0]))),e.forEach(function(e){e.setAttribute("type","image/png")}),e},k.setIcon=function(e){var t=e.toDataURL("image/png");k.setIconSrc(t)},k.setIconSrc=function(e){if(i.dataUrl&&i.dataUrl(e),i.element)i.element.setAttribute("href",e),i.element.setAttribute("src",e);else if(i.elementId){var t=b.getElementById(i.elementId);t.setAttribute("href",e),t.setAttribute("src",e)}else{for(var o=b.getElementsByTagName("head")[0],n=b.getElementsByTagName("link"),r=0,l=n.length;l>r;r++){var s="undefined"!=typeof n[r]&&n[r].getAttribute("rel");s&&"apple-touch-icon"!==s&&s.match(/\bicon\b/)&&o.removeChild(n[r])}a=b.createElement("link"),a.setAttribute("rel","icon"),a.setAttribute("type","image/png"),a.setAttribute("href",e),o.appendChild(a)}};var j={};return j.duration=40,j.types={},j.types.fade=[{x:.4,y:.4,w:.6,h:.6,o:0},{x:.4,y:.4,w:.6,h:.6,o:.1},{x:.4,y:.4,w:.6,h:.6,o:.2},{x:.4,y:.4,w:.6,h:.6,o:.3},{x:.4,y:.4,w:.6,h:.6,o:.4},{x:.4,y:.4,w:.6,h:.6,o:.5},{x:.4,y:.4,w:.6,h:.6,o:.6},{x:.4,y:.4,w:.6,h:.6,o:.7},{x:.4,y:.4,w:.6,h:.6,o:.8},{x:.4,y:.4,w:.6,h:.6,o:.9},{x:.4,y:.4,w:.6,h:.6,o:1}],j.types.none=[{x:.4,y:.4,w:.6,h:.6,o:1}],j.types.pop=[{x:1,y:1,w:0,h:0,o:1},{x:.9,y:.9,w:.1,h:.1,o:1},{x:.8,y:.8,w:.2,h:.2,o:1},{x:.7,y:.7,w:.3,h:.3,o:1},{x:.6,y:.6,w:.4,h:.4,o:1},{x:.5,y:.5,w:.5,h:.5,o:1},{x:.4,y:.4,w:.6,h:.6,o:1}],j.types.popFade=[{x:.75,y:.75,w:0,h:0,o:0},{x:.65,y:.65,w:.1,h:.1,o:.2},{x:.6,y:.6,w:.2,h:.2,o:.4},{x:.55,y:.55,w:.3,h:.3,o:.6},{x:.5,y:.5,w:.4,h:.4,o:.8},{x:.45,y:.45,w:.5,h:.5,o:.9},{x:.4,y:.4,w:.6,h:.6,o:1}],j.types.slide=[{x:.4,y:1,w:.6,h:.6,o:1},{x:.4,y:.9,w:.6,h:.6,o:1},{x:.4,y:.9,w:.6,h:.6,o:1},{x:.4,y:.8,w:.6,h:.6,o:1},{x:.4,y:.7,w:.6,h:.6,o:1},{x:.4,y:.6,w:.6,h:.6,o:1},{x:.4,y:.5,w:.6,h:.6,o:1},{x:.4,y:.4,w:.6,h:.6,o:1}],j.run=function(e,t,o,a){var l=j.types[r()?"none":i.animation];return a=o===!0?"undefined"!=typeof a?a:l.length-1:"undefined"!=typeof a?a:0,t=t?t:function(){},a<l.length&&a>=0?(M[i.type](n(e,l[a])),x=setTimeout(function(){o?a-=1:a+=1,j.run(e,t,o,a)},j.duration),k.setIcon(h),void 0):void t()},E(),{badge:A,video:O,image:U,rawImageSrc:R,webcam:S,setOpt:L,reset:I.reset,browser:{supported:p.supported}}};"undefined"!=typeof define&&define.amd?define([],function(){return e}):"undefined"!=typeof module&&module.exports?module.exports=e:this.Favico=e}();