-
Notifications
You must be signed in to change notification settings - Fork 7
/
flipbook.min.js
1 lines (1 loc) · 4.36 KB
/
flipbook.min.js
1
!function(t){"function"==typeof define&&define.amd?define(t):"undefined"!=typeof module&&module.exports?module.exports=t():window.flipbook=t.call(this)}(function(){var t=function(t){var n,e,o,i,r,a,f,u,s,c,d,l,h,p,m,g,v,w,x,y={},M=-1,b=function(){window.requestAnimatoinFrame=J();var t=A();t?Q("invalid options, missing "+t):(k(),I(),E(),O(function(t){t?Q(t):B()}))},A=function(){var n,e;return e=t.sprite?["id","path","extension","filename","rows"]:["id","path","extension","filename","count"],e.forEach(function(e){t[e]||(n=e)}),n},k=function(){t.speed=isNaN(t.speed)?.5:Math.min(Math.max(0,parseFloat(t.speed)),1),t.sprite?t.count=t.rows.reduce(function(t,n){return t+n}):t.count=parseInt(t.count),t.path=t.path.trim(),t.path.lastIndexOf("/")!==t.path.length-1&&(t.path+="/"),x=K()||t.gif},B=function(){t.loaded&&"function"==typeof t.loaded&&t.loaded(),v=!0,P(),F(),T(0),x&&Y()},I=function(){n=document.getElementById(t.id),n.classList.add("flipbook-container"),e=document.createElement("div"),e.classList.add("flipbook-graphic"),n.appendChild(e),o=W(e),i=o.getContext("2d")},E=function(){j(n,{position:"relative",width:"100%",overflow:"hidden"}),j(e,{position:"absolute",top:0,left:0}),j(o,{position:"absolute",top:0,left:0})},F=function(){window.addEventListener("resize",P,!1),x||window.addEventListener("scroll",X,!1)},O=function(n){var e=t.sprite?t.rows.length:e=t.count;m=R(e),g=[],q(0,e,n)},q=function(t,n,e){var o=m[t];G(o,function(i,r){if(i)e("error loading image");else{o.img=r,0===t&&H(r,o.rows);for(var a=0;a<o.rows;a++)g.push({imageIndex:t,x:0,y:a});L(t,n),t++,t<m.length?q(t,n,e):e()}})},H=function(t,n){l=t.naturalWidth,h=t.naturalHeight/n,p=Math.round(l/h*1e3)/1e3},L=function(n,e){t.progress&&"function"==typeof t.progress&&t.progress({frame:n+1,total:t.count,percent:Math.round(n/(e-1)*100)})},R=function(n){for(var e=[],o=0;n>o;o++){var i=C(o+1);e[o]={src:i,rows:t.sprite?t.rows[o]:1}}return e},C=function(n){for(var e=t.filename.match(/\%\dd/),o=e[0].charAt(1),i=n.toString().length,r=Math.max(o-i,0),a=t.filename.split(/\%\dd/)[0],f=t.path+a,u=0;r>u;u++)f+="0";return f+=n+"."+t.extension},W=function(t){var n=document.createElement("canvas");return n.classList.add("flipbook-canvas"),t.appendChild(n),n},Y=function(){var t=g.length,n=0,e=function(){T(n),n++,n>=t&&(n=0),setTimeout(e,60)};e()},P=function(){if(r=n.offsetWidth,t.cover&&!x){a=innerHeight;var i=r/a;p>i?(y.width=Math.floor(i*h),y.height=h,y.offsetX=Math.floor((l-y.width)/2),y.offsetY=0):(y.height=Math.floor(l/i),y.width=l,y.offsetX=0,y.offsetY=Math.floor((h-y.height)/2))}else a=Math.floor(r/p);var s;s=x?20:innerHeight-a,u=a+s,o.width=r,o.height=a,o.style.top=Math.floor(s/2)+"px",e.style.height=u+"px",e.style.width=r+"px";var d=D();f=x?u:innerHeight*d,n.style.height=f+"px",S(),!x&&c&&(z(),N(pageYOffset-c,!0))},S=function(){var t=n.getBoundingClientRect();s=t.left,c=t.top,d=t.bottom-e.offsetHeight},X=function(){w||requestAnimationFrame(z),w=!0},z=function(){w=!1,v&&(S(),0>c&&d>0?(j(e,{position:"fixed",top:0,left:s+"px",bottom:"auto"}),N(Math.abs(c))):0>d?(j(e,{position:"absolute",top:"auto",left:0,bottom:0}),N(f)):c>0&&(j(e,{position:"absolute",top:0,left:0,bottom:"auto"}),N(0)))},N=function(n,e){var o=n/(f-u);o=Math.min(Math.max(o,0),1);var i=Math.floor(o*(t.count-1));(e||M!==i)&&(M=i,T(i))},T=function(n){i.clearRect(0,0,r,a);var e,o,f,u,s=g[n],c=m[s.imageIndex];t.cover&&!x?(e=y.offsetX,o=y.offsetY+h*s.y,f=y.width,u=y.height):(e=0,o=h*s.y,f=l,u=h),i.drawImage(c.img,e,o,f,u,0,0,r,a)},j=function(t,n){for(var e in n)t.style[e]=n[e]},D=function(){var n=10,e=2;return e+n*(1-t.speed)},G=function(t,n){var e=new Image;e.onload=function(){n(null,e)},e.onerror=function(){n("error loading image: "+t.src)},e.src=t.src},J=function(){return window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.msRequestAnimationFrame||function(t){return setTimeout(t,1e3/60)}},K=function(){var t={Android:function(){return navigator.userAgent.match(/Android/i)},BlackBerry:function(){return navigator.userAgent.match(/BlackBerry/i)},iOS:function(){return navigator.userAgent.match(/iPhone|iPad|iPod/i)},Opera:function(){return navigator.userAgent.match(/Opera Mini/i)},Windows:function(){return navigator.userAgent.match(/IEMobile/i)},any:function(){return t.Android()||t.BlackBerry()||t.iOS()||t.Opera()||t.Windows()}};return t.any()},Q=function(t){console&&console.error&&console.error("::flipbook:: "+t)};b()};return t});