yoone-snow/dist/assets/SnowAnimator-Dem4j_A7.js

2 lines
11 KiB
JavaScript

function g(){return window.YooneSnowShapeRenderers||(window.YooneSnowShapeRenderers={}),window.YooneSnowShapeRenderers}function p(t){if(!t)return{img:null,ready:!1};const o=window.YooneSnowImageCache=window.YooneSnowImageCache||{},n=o[t];if(n&&n.ready||n&&!n.ready)return n;const e=new Image,i={img:e,ready:!1};o[t]=i;try{e.decoding="async"}catch{}try{e.fetchPriority="low"}catch{}return e.onload=function(){i.ready=!0},e.onerror=function(){delete o[t]},e.src=t,i}function X(t,o){if(!t){o(!1);return}const n=window.YooneSnowImageCache=window.YooneSnowImageCache||{},e=n[t];if(e&&e.ready){o(!0);return}if(e||(n[t]={img:null,ready:!1}),typeof fetch=="function"&&typeof window.createImageBitmap=="function")fetch(t,{cache:"force-cache"}).then(i=>i.blob()).then(i=>window.createImageBitmap(i)).then(i=>{n[t]={img:i,ready:!0},o(!0)}).catch(()=>{const i=p(t);let s=!1;if(i&&i.img instanceof HTMLImageElement){const a=()=>{i.ready=!0};i.img.onload=function(){s||(s=!0,a(),o(!0))},i.img.onerror=function(){s||(s=!0,o(!1))}}else o(!1)});else{const i=p(t);let s=!1;if(i&&i.img instanceof HTMLImageElement){const a=()=>{i.ready=!0};i.img.onload=function(){s||(s=!0,a(),o(!0))},i.img.onerror=function(){s||(s=!0,o(!1))}}else o(!1)}}function M(t,o,n,e,i,s){const a=n-i/2,c=e-s/2;t.drawImage(o,a,c,i,s)}window.YooneSnowGetOrLoadImage=p;window.YooneSnowLoadAssetViaFetch=X;window.YooneSnowDrawCenteredImage=M;const D=g();D.dot=function(t,o,n,e){t.beginPath(),t.arc(o,n,e,0,Math.PI*2),t.fillStyle="rgba(255,255,255,0.9)",t.fill()};const H=g();H.flake=function(t,o,n,e){const i=e*3;t.save(),t.translate(o,n),t.fillStyle="rgba(255,255,255,0.9)",t.strokeStyle="rgba(255,255,255,0.9)",t.lineWidth=i*.15;for(let s=0;s<6;s++)t.rotate(Math.PI/3),t.beginPath(),t.moveTo(0,0),t.lineTo(0,i),t.stroke(),t.beginPath(),t.moveTo(0,i*.3),t.lineTo(i*.3,i*.5),t.stroke(),t.beginPath(),t.moveTo(0,i*.5),t.lineTo(-i*.3,i*.7),t.stroke(),t.beginPath(),t.moveTo(0,i*.7),t.lineTo(i*.3,i*.9),t.stroke();t.restore()};const z=g();z.yuanbao=function(t,o,n,e){t.save(),t.translate(o,n),t.scale(e*.08,e*.08),t.fillStyle="rgba(255,215,0,0.9)",t.beginPath(),t.moveTo(-20,0),t.quadraticCurveTo(0,-12,20,0),t.quadraticCurveTo(0,12,-20,0),t.fill(),t.beginPath(),t.arc(0,0,6,0,Math.PI*2),t.fill(),t.restore()};const E=g();E.coin=function(t,o,n,e){const i=t.createRadialGradient(o,n,e*.2,o,n,e);i.addColorStop(0,"rgba(255,230,120,0.95)"),i.addColorStop(1,"rgba(240,180,60,0.85)"),t.fillStyle=i,t.beginPath(),t.arc(o,n,e*2.5,0,Math.PI*2),t.fill()};const U=g();U.santa_hat=function(t,o,n,e){const s=(window.YooneSnowSettings&&window.YooneSnowSettings.assetsMap?window.YooneSnowSettings.assetsMap:{}).santa_hat||"",a=p(s);if(!a||!a.ready||!a.img)return;const c=e*8,m=c;M(t,a.img,o,n,m,c)};const q=g();q.candy_cane=function(t,o,n,e){const s=(window.YooneSnowSettings&&window.YooneSnowSettings.assetsMap?window.YooneSnowSettings.assetsMap:{}).candy_cane||"",a=p(s);if(!a||!a.ready||!a.img)return;const c=e*8,m=c;M(t,a.img,o,n,m,c)};const B=g();B.christmas_sock=function(t,o,n,e){const s=(window.YooneSnowSettings&&window.YooneSnowSettings.assetsMap?window.YooneSnowSettings.assetsMap:{}).christmas_sock||"",a=p(s);if(!a||!a.ready||!a.img)return;const c=e*8,m=c;M(t,a.img,o,n,m,c)};const G=g();G.christmas_tree=function(t,o,n,e){const s=(window.YooneSnowSettings&&window.YooneSnowSettings.assetsMap?window.YooneSnowSettings.assetsMap:{}).christmas_tree||"",a=p(s);if(!a||!a.ready||!a.img)return;const c=e*8,m=c;M(t,a.img,o,n,m,c)};const N=g();N.reindeer=function(t,o,n,e){const s=(window.YooneSnowSettings&&window.YooneSnowSettings.assetsMap?window.YooneSnowSettings.assetsMap:{}).reindeer||"",a=p(s);if(!a||!a.ready||!a.img)return;const c=e*8,m=c;M(t,a.img,o,n,m,c)};const J=g();J.christmas_berry=function(t,o,n,e){const s=(window.YooneSnowSettings&&window.YooneSnowSettings.assetsMap?window.YooneSnowSettings.assetsMap:{}).christmas_berry||"",a=p(s);if(!a||!a.ready||!a.img)return;const c=e*8,m=c;M(t,a.img,o,n,m,c)};class K{constructor(o,n,e){this.onFrame=o,this.isDone=n,this.onStopped=e,this.lastTs=performance.now(),this.rafId=null,this.running=!1,this.loop=()=>{if(!this.running)return;const i=performance.now(),s=Math.max(0,(i-this.lastTs)/1e3);if(this.lastTs=i,this.onFrame(s),this.isDone()){this.stop(),this.onStopped&&this.onStopped();return}this.rafId=requestAnimationFrame(this.loop)}}init(){this.lastTs=performance.now(),this.running=!1}update(){this.running=!0,this.rafId=requestAnimationFrame(this.loop)}stop(){if(this.rafId!==null&&typeof cancelAnimationFrame=="function"){try{cancelAnimationFrame(this.rafId)}catch{}this.rafId=null}this.running=!1}}class I{init(o,n){}update(o,n,e){}}class Q{constructor(o){this.outOfView=!1,this.components=[],this.positionX=o.positionX,this.positionY=o.positionY,this.radius=o.radius,this.driftSpeed=o.driftSpeed,this.swingAmplitude=o.swingAmplitude,this.shapeType=o.shapeType,this.imageUrl=o.imageUrl||null,this.emojiText=o.emojiText||null}addComponent(o){this.components.push(o)}init(o){for(let n=0;n<this.components.length;n++)try{this.components[n].init(o,this)}catch{}}update(o,n){for(let e=0;e<this.components.length;e++)try{this.components[e].update(o,this,n)}catch{}}}class Z extends Q{render(o){const n=o.context;if(this.shapeType==="media_image"&&this.imageUrl){const s=p(this.imageUrl);if(s&&s.ready&&s.img){const a=this.radius*8,c=a;M(n,s.img,this.positionX,this.positionY,c,a)}return}if(this.shapeType==="emoji_text"&&this.emojiText){n.save();const s=Math.max(12,this.radius*6);n.font=`${Math.floor(s)}px system-ui, Apple Color Emoji, Segoe UI Emoji, Noto Color Emoji`,n.textAlign="center",n.textBaseline="middle",n.fillText(String(this.emojiText),this.positionX,this.positionY),n.restore();return}if(this.shapeType==="text_label"&&this.emojiText){n.save();const s=Math.max(12,this.radius*5.5);n.font=`${Math.floor(s)}px system-ui, -apple-system, Segoe UI, Roboto, Noto Sans`,n.textAlign="center",n.textBaseline="middle",n.fillStyle="rgba(255,255,255,0.9)",n.fillText(String(this.emojiText),this.positionX,this.positionY),n.restore();return}const e=window.YooneSnowShapeRenderers||{},i=e[this.shapeType]||e.dot;typeof i=="function"&&i(n,this.positionX,this.positionY,this.radius)}}class ee extends I{update(o,n,e){const i=Math.max(.5,Math.min(2,e*60)),s=(n.driftSpeed*2+n.radius*.25)*i;n.positionY+=s}}class te extends I{update(o,n,e){const i=Math.max(.5,Math.min(2,e*60)),s=Math.sin(n.positionY*.01)*n.swingAmplitude*i;n.positionX+=s}}class ne extends I{update(o,n,e){n.positionY>o.getViewportHeight()+5&&(n.outOfView=!0)}}function ie(t,o,n,e){let i=o(),s=n();const a={getViewportWidth:()=>i,getViewportHeight:()=>s,context:t},c=[];let m=0,S=Math.random();const x=.61803398875,_=performance.now();let Y=!1;function C(){const r=Math.min(i,s),l=u=>{const h=i*s;return Math.floor(u==="small"?Math.min(80,Math.max(40,h/36e3)):u==="medium"?Math.min(200,Math.max(100,h/18e3)):Math.min(300,Math.max(140,h/12e3)))};let d=0;return r<=480?d=e.maxCountSmall>0?e.maxCountSmall:l("small"):r<=960?d=e.maxCountMedium>0?e.maxCountMedium:l("medium"):d=e.maxCountLarge>0?e.maxCountLarge:l("large"),d<=0&&(d=e.maxCount>0?e.maxCount:l(r<=480?"small":r<=960?"medium":"large")),Math.max(1,d)}let y=C();function P(){const r=[];for(let h=0;h<e.selectedShapes.length;h++){const f=e.selectedShapes[h],w=typeof e.shapeWeights[f]<"u"?e.shapeWeights[f]:1;w>0&&r.push({kind:"shape",key:f,weight:w})}for(let h=0;h<e.emojiItems.length;h++){const f=String(e.emojiItems[h]||"").trim();if(f==="")continue;const w=e.emojiWeights[f]??1;w>0&&r.push({kind:"emoji",text:f,weight:w})}for(let h=0;h<e.textItems.length;h++){const f=String(e.textItems[h]||"").trim();if(f==="")continue;const w=e.textWeights[f]??1;w>0&&r.push({kind:"text",text:f,weight:w})}for(let h=0;h<e.mediaItems.length;h++){const f=e.mediaItems[h],w=e.mediaWeights[f]??1;w>0&&r.push({kind:"media",url:f,weight:w})}if(r.length===0)return null;let l=0;for(let h=0;h<r.length;h++)l+=r[h].weight;const d=Math.random()*l;let u=0;for(let h=0;h<r.length;h++)if(u+=r[h].weight,d<=u){const f=r[h];return f.kind==="shape"?{type:String(f.key),url:null,text:null}:f.kind==="media"?{type:"media_image",url:String(f.url),text:null}:f.kind==="emoji"?{type:"emoji_text",url:null,text:String(f.text)}:{type:"text_label",url:null,text:String(f.text)}}return null}function A(r,l){const d=P();if(!d)return null;const u=new Z({positionX:typeof r=="number"?r:Math.random()*i,positionY:typeof l=="number"?l:-1-Math.random()*4,radius:(Math.random()*(e.radiusMax-e.radiusMin)+e.radiusMin)*e.radiusMin,driftSpeed:Math.random()*(e.driftMax-e.driftMin)+e.driftMin,swingAmplitude:(Math.random()*(e.swingMax-e.swingMin)+e.swingMin)*e.swingMin,shapeType:d.type,imageUrl:d.url,emojiText:d.text});return u.addComponent(new ee),u.addComponent(new te),u.addComponent(new ne),u.init(a),u}function b(){let r=0,l=0;for(let h=0;h<c.length;h++){const f=c[h];if(f.outOfView)continue;const w=(f.driftSpeed*2+f.radius*.25)*60;l+=w,r++}if(r>0)return l/r;const d=(e.driftMin+e.driftMax)*.5,u=(e.radiusMin+e.radiusMax)*.5*e.radiusMin;return(d*2+u*.25)*60}function j(){const r=b(),l=(s+5)/Math.max(.001,r),d=y/Math.max(.001,l),u=Math.min(1.2,Math.max(.6,l*.2)),h=Math.floor(d*u),f=Math.floor(y*.45);return Math.max(8,Math.min(f,h))}function k(r,l){const d=A(r,l);return d?(c.push(d),!0):!1}function V(){if(Y||c.length>0)return;const r=j();for(let l=0;l<r;l++){const d=S%1*i;S=(S+x)%1;const u=-Math.random()*(s*.4);k(d,u)}}V();function $(r){for(let l=0;l<c.length;l++){const d=c[l];if(d&&typeof d.update=="function")d.update(a,r);else{const u=Math.max(.5,Math.min(2,r*60));d.positionY+=(d.driftSpeed*2+d.radius*.25)*u,d.positionX+=Math.sin(d.positionY*.01)*d.swingAmplitude*u,d.positionY>s+5&&(d.outOfView=!0)}}}function W(){t.clearRect(0,0,i,s);for(let r=0;r<c.length;r++){const l=c[r];if(!l.outOfView&&(l.render&&l.render(a),!l.render)){const d=window.YooneSnowShapeRenderers||{},u=d[l.shapeType]||d.dot;typeof u=="function"&&u(t,l.positionX,l.positionY,l.radius)}}}function L(r){for(let l=c.length-1;l>=0;l--)c[l].outOfView&&c.splice(l,1);if(!Y){const l=b(),d=(s+5)/Math.max(.001,l),u=y/Math.max(.001,d);m+=u*Math.max(0,r);const h=Math.max(0,y-c.length);let f=Math.min(h,Math.floor(m));f===0&&h>0&&(f=1);const w=Math.max(1,Math.floor(y*.05));f>w&&(f=w);let T=0;for(let v=0;v<f;v++){const R=S%1*i;S=(S+x)%1,k(R,void 0)&&T++}T>0&&(m=Math.max(0,m-T))}$(r)}function F(){W()}function O(){return e.displayDurationSeconds>0&&!Y&&(performance.now()-_)/1e3>=e.displayDurationSeconds&&(Y=!0),Y?c.every(r=>r.outOfView):!1}return{engine:a,recomputeTarget(){y=C()},updateSystem:L,renderSystem:F,shouldStop:O,setViewport(r,l){i=r,s=l}}}function oe(t,o){const n=t.getContext("2d");let e=window.innerWidth,i=window.innerHeight;const s=window.devicePixelRatio||1;function a(){e=window.innerWidth,i=window.innerHeight,t.style.width=`${e}px`,t.style.height=`${i}px`,t.width=Math.floor(e*s),t.height=Math.floor(i*s),n.setTransform(s,0,0,s,0,0),c.setViewport(e,i),c.recomputeTarget()}a();const c=ie(n,()=>e,()=>i,o),m=new K(x=>{c.updateSystem(x),c.renderSystem()},()=>c.shouldStop(),()=>{n.clearRect(0,0,e,i),t.style.display="none"});function S(){a()}return window.addEventListener("resize",S),m.init(),m.update(),{stop(){m.stop(),window.removeEventListener("resize",S)}}}export{oe as r};