-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheasyUtil.dynamicBg.min.1.1.js
4 lines (4 loc) · 5.09 KB
/
easyUtil.dynamicBg.min.1.1.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,s=void 0!==r&&r,l=n.bgSwitch,m=void 0!==l&&l,c=n.speed,u=void 0===c?100:c,f=n.width,d=void 0===f?0:f,h=n.height,g=void 0===h?0:h,y=n.offsetX,v=void 0===y?0:y,p=n.offsetY,w=void 0===p?0:p,_=n.x,b=void 0===_?0:_,x=n.y,A=void 0===x?0:x;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 F=document.querySelector("#"+e),T=null;if(F&&(T=F.getContext?F.getContext("2d"):null),!F||!T)throw new Error('MethodError: DynamicBg --\x3e "'+e+'" can not read "getContext()", canvas is undefined or non-existent!');u<50&&(u=50);var q=new t(F,T,i,d,g,v,w,b,A,s,u,m);return a?q.__mutipleDynamicBg():q.__singleDynamicBg()}var t=function(){function e(t,i,n,o,a,r,s,l,m,c,u,f){_classCallCheck(this,e),this.canvas=t,this.ctx=i,this.src=n,this.width=o,this.height=a,this.offsetX=r,this.offsetY=s,this.x=l,this.y=m||l,this.img=new Image,this.moveX=0,this.moveY=0,this.flag=null,this.timer=c,this.speed=u,this.bgSwitch=f}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(){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(){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&&this.__playOrPause(a)}}]),e}();return{create:e}});