forked from lakenqi/easyUtil_dynamicBg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheasyUtil.dynamicBg.min.1.2.js
4 lines (4 loc) · 5.38 KB
/
easyUtil.dynamicBg.min.1.2.js
1
2
3
4
// ==========
// = dynamicBg by qy (c) Copyright 2018 qy. All Rights Reserved. =
// ==========
"use strict";function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}var _createClass=function(){function e(e,t){for(var i=0;i<t.length;i++){var n=t[i];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,n.key,n)}}return function(t,i,n){return i&&e(t.prototype,i),n&&e(t,n),t}}(),_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};!function(e,t){for(var i=0,n=["webkit","moz"],o=0;o<n.length&&!e.requestAnimationFrame;++o)e.requestAnimationFrame=e[n[o]+"RequestAnimationFrame"],e.cancelAnimationFrame=e[n[o]+"CancelAnimationFrame"]||e[n[o]+"CancelRequestAnimationFrame"];e.requestAnimationFrame||(e.requestAnimationFrame=function(t){var n=(new Date).getTime(),o=Math.max(0,16.7-(n-i)),a=e.setTimeout(function(){t(n+o)},o);return i=n+o,a}),e.cancelAnimationFrame||(e.cancelAnimationFrame=function(e){clearTimeout(e)}),"function"==typeof define&&define.amd?define(t):"function"==typeof define?define(t):"object"===("undefined"==typeof module?"undefined":_typeof(module))&&"object"===_typeof(module.exports)?module.exports=t():e.easy_DynamicBg=t()}("undefined"!=typeof window?window:void 0,function(){function e(e,i){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},o=n.multiple,a=void 0!==o&&o,r=n.timer,l=void 0!==r&&r,s=n.bgSwitch,c=void 0!==s&&s,m=n.once,u=void 0===m||m,f=n.speed,d=void 0===f?100:f,h=n.width,g=void 0===h?0:h,y=n.height,v=void 0===y?0:y,p=n.offsetX,w=void 0===p?0:p,_=n.offsetY,b=void 0===_?0:_,x=n.x,A=void 0===x?0:x,F=n.y,T=void 0===F?0:F,k=n.callback,q=void 0===k?function(){}:k;if(!e||!i)throw new Error('TypeError: DynamicBg --\x3e "id" or "src" is undefined!');if(!i[0])throw new Error('TypeError: DynamicBg --\x3e "src" is empty!');if(a&&"[object Array]"!=={}.toString.call(i))throw new Error('TypeError: DynamicBg --\x3e For multiple model, "src" must be Array!');if(!a&&"[object Array]"==={}.toString.call(i)&&i.length>1)throw new Error('TypeError: DynamicBg --\x3e For single model, "src" only needs one, it is too many now!');var C=document.querySelector("#"+e),E=null;if(C&&(E=C.getContext?C.getContext("2d"):null),!C||!E)throw new Error('MethodError: DynamicBg --\x3e "'+e+'" can not read "getContext()", canvas is undefined or non-existent!');d<50&&(d=50);var S=new t(C,E,i,g,v,w,b,A,T,l,d,c,u,q);return a?S.__mutipleDynamicBg():S.__singleDynamicBg()}var t=function(){function e(t,i,n,o,a,r,l,s,c,m,u,f,d,h){_classCallCheck(this,e),this.canvas=t,this.ctx=i,this.src=n,this.width=o,this.height=a,this.offsetX=r,this.offsetY=l,this.x=s,this.y=c||s,this.img=new Image,this.moveX=0,this.moveY=0,this.flag=null,this.timer=m,this.speed=u,this.bgSwitch=f,this.once=d,this.callback=h}return _createClass(e,[{key:"init",value:function(){var e=this.canvas.style;this.canvas.width=document.documentElement.offsetWidth||document.body.offsetWidth,this.canvas.height=window.innerHeight,e.position="absolute",e.top=0,e.bottom=0,e.left=0,e.right=0,e.zIndex=-1,e.margin="auto"}},{key:"__draw",value:function(e,t,i,n){var o=this;return arguments.length?this.__draw=function(){return o.ctx.clearRect(0,0,t,i),o.ctx.drawImage(o.img,0,0,t,i),++e>n&&(e=1),e}:this.__draw=function(){o.ctx.clearRect(0,0,o.canvas.width,o.canvas.height),o.ctx.drawImage(o.img,o.width*o.moveX,o.height*o.moveY,o.width,o.height,o.offsetX,o.offsetY,2*o.width,2*o.height),o.moveX++,o.moveX%=o.x,o.moveX%o.x==0&&o.moveY++,o.moveY%=o.y},this.__draw()}},{key:"__playOrPause",value:function(e){var t=this,i=this.canvas.style;this.timer?setInterval(function(){"none"===i.display&&(clearTimeout(t.flag),t.flag=null),"block"!==i.display||t.flag||(t.flag=setTimeout(e,t.speed))},t.speed):setInterval(function(){"none"===i.display&&(cancelAnimationFrame(t.flag),t.flag=null),"block"!==i.display||t.flag||(t.flag=requestAnimationFrame(e))},16)}},{key:"__singleDynamicBg",value:function(){this.init();var e=this,t=null;this.timer?(t=function(){e.img.src=e.src,e.img.complete?(e.__draw(),e.flag=setTimeout(t,e.speed),e.img.onload=null):e.img.onload=function(){e.__draw(),e.flag=setTimeout(t,e.speed-50)}},e.flag=setTimeout(t,e.speed)):(e.img.src=e.src,t=function(){e.img.complete?(e.__draw(),e.flag=requestAnimationFrame(t),e.img.onload=null):e.img.onload=function(){e.__draw(),e.flag=requestAnimationFrame(t)}},e.flag=requestAnimationFrame(t)),this.bgSwitch&&this.__playOrPause(t)}},{key:"__mutipleDynamicBg",value:function(){this.init();var e=this,t=1,i=this.src.length,n=this.canvas.width,o=this.canvas.height,a=null;this.timer?(a=function(){if(t==i&&e.once)return clearTimeout(e.flag),e.flag=null,e.canvas=null,void e.callback();e.img.src=e.src[t-1],e.img.complete?(t=e.__draw(t,n,o,i),e.flag=setTimeout(a,e.speed),e.img.onload=null):e.img.onload=function(){t=e.__draw(t,n,o,i),e.flag=setTimeout(a,e.speed-50)}},this.flag=setTimeout(a,this.speed)):(a=function(){if(t==i&&e.once)return cancelAnimationFrame(e.flag),e.flag=null,e.canvas=null,void e.callback();e.img.src=e.src[t-1],e.img.complete?(t=e.__draw(t,n,o,i),e.flag=requestAnimationFrame(a),e.img.onload=null):e.img.onload=function(){t=e.__draw(t,n,o,i),e.flag=requestAnimationFrame(a)}},this.flag=requestAnimationFrame(a)),this.bgSwitch&&!e.once&&this.__playOrPause(a)}}]),e}();return{create:e}});