UNPKG

@neosjs/h5-like

Version:

NeosJS H5 like

8 lines (7 loc) 2.98 kB
/*! * H5 Like * version 1.0.1 * releaseTime 2021-03-12 14:05 * (c) 2021 NeosJS (https://docs.neosjs.com/like/) */ var e,t;e=this,t=function(){var e=function(e){var t=e,n=Math.floor,i=Math.random;return e.uniform=function(e,t){return e+(t-e)*i()},e.uniformDiscrete=function(e,i){return e+n((i-e+1)*t.uniform(0,1))},e}({}),t=window.requestAnimationFrame||window.webkitRequestAnimationFrame||function(e){return setTimeout(e,16)},n=function(t,n,i){var r=e.uniformDiscrete(89,91)/100,o=1-r,u=(e.uniformDiscrete(45,60)+e.uniformDiscrete(45,60))/100,a=e.uniformDiscrete(-30,30),s=n.width/2+e.uniformDiscrete(-10,10),c=(n.width-Math.sqrt(Math.pow(t.width,2)+Math.pow(t.height,2)))/2-10,d=e.uniformDiscrete(.8*c,c)*(e.uniformDiscrete(0,1)?1:-1),f=e.uniformDiscrete(250,400),h=e.uniformDiscrete(14,18)/100;return function(e){if(!(e>=0))return!0;i.save();var c=function(e){return e>r?Math.max(((1-e)/o).toFixed(2),.1)*u:u}(e),l=a,m=function(e){return e>r?s:s+d*Math.sin(f*(r-e)*Math.PI/180)}(e),v=function(e){return t.height/2+(n.height-t.height/2)*e}(e);i.translate(m,v),i.scale(c,c),i.rotate(l*Math.PI/180),i.globalAlpha=function(e){return e>h?1:1-((h-e)/h).toFixed(2)}(e),i.drawImage(t,-t.width/2,-t.height/2,t.width,t.height),i.restore()}},i=function(){var e=this.canvas=document.createElement("canvas"),n=this.context=e.getContext("2d"),i=this._children=[],r=function(){t(r),n.clearRect(0,0,e.width,e.height);for(var o=0,u=i.length;o<u;){var a=i[o];a.render.call(null,(a.timestamp+a.duration-new Date)/a.duration)?(i.splice(o,1),u--):o++}};t(r)};i.prototype.fly=function(t,i,r){return void 0===i&&(i=e.uniformDiscrete(2400,3600)),this._children.push({render:r||n(t,this.canvas,this.context),duration:i,timestamp:+new Date}),this};var r=null,o=null,u=null,a=function(){};a.prototype.init=function(t){var n=t.icons,a=t.btnDom,s=t.flyDom,c=t.height,d=t.width,f=t.className,h=t.duration;void 0===h&&(h=3e3);var l,m=t.render;void 0===m&&(m=""),n&&(l=n,Array.isArray(l))&&s&&d&&c&&(this.destroy(),u=Math.random().toString(36).substring(3,20),n.forEach((function(e,t){n[t]=new Promise((function(t){var n=new Image;n.onload=t.bind(null,n),n.src=e}))})),Promise.all(n).then((function(t){var n=(r=new i).canvas;n.width=d,n.height=c,f&&n.setAttribute("class",f),n.setAttribute("id","__likeFly_"+u),document.getElementById(s).appendChild(n),(o=document.getElementById(a)).addEventListener("mousedown",(function(){r&&r.fly(t[e.uniformDiscrete(0,t.length-1)],h,m)}),!1)})))},a.prototype.destroy=function(){if(r){o&&o.removeEventListener("mousedown",(function(){r.fly()}),!1);var e=document.getElementById("__likeFly_"+u);e&&e.parentNode.removeChild(e),o=r=null}};var s=new Proxy(new a,{get:function(e,t,n){return Reflect.get(e,t,n)}});return s.Name="Like",s.version="1.0.1",s.updateTime="1615529141064",s},"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).NeosLike=t();