masoneffect 0.1.18 → 0.1.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/README.md +1 -1
  2. package/dist/core/index.d.ts.map +1 -1
  3. package/dist/index.cjs +1 -0
  4. package/dist/index.mjs +1 -0
  5. package/dist/react/MasonEffect.cjs +1 -526
  6. package/dist/react/MasonEffect.d.ts.map +1 -1
  7. package/dist/react/MasonEffect.mjs +1 -0
  8. package/dist/react/core/index.d.ts +0 -1
  9. package/dist/react/core/index.d.ts.map +1 -1
  10. package/dist/react/index.d.ts +2 -8
  11. package/dist/react/index.mjs +1 -0
  12. package/dist/react/index.umd.d.ts +0 -1
  13. package/dist/react/react/MasonEffect.d.ts +0 -1
  14. package/dist/react/react/MasonEffect.d.ts.map +1 -1
  15. package/dist/react/react/index.d.ts +2 -0
  16. package/dist/react/react/index.d.ts.map +1 -0
  17. package/dist/react/vue/index.d.ts +5 -0
  18. package/dist/react/vue/index.d.ts.map +1 -0
  19. package/dist/vue/index.cjs +1 -0
  20. package/dist/vue/index.d.ts +90 -0
  21. package/{src/core/index.ts → dist/vue/index.mjs} +227 -237
  22. package/package.json +14 -15
  23. package/dist/index.esm.js +0 -387
  24. package/dist/index.esm.js.map +0 -1
  25. package/dist/index.esm.min.js +0 -1
  26. package/dist/index.js +0 -392
  27. package/dist/index.js.map +0 -1
  28. package/dist/index.min.js +0 -1
  29. package/dist/index.umd.js +0 -396
  30. package/dist/index.umd.js.map +0 -1
  31. package/dist/index.umd.min.js +0 -1
  32. package/dist/react/MasonEffect.cjs.map +0 -1
  33. package/dist/react/MasonEffect.js +0 -524
  34. package/dist/react/MasonEffect.js.map +0 -1
  35. package/dist/react/MasonEffect.min.cjs +0 -1
  36. package/dist/react/MasonEffect.min.js +0 -1
  37. package/src/index.ts +0 -14
  38. package/src/index.umd.ts +0 -10
  39. package/src/react/MasonEffect.tsx +0 -228
  40. package/src/react/index.js +0 -2
  41. package/src/vue/MasonEffect.vue +0 -140
package/README.md CHANGED
@@ -41,7 +41,7 @@ effect.scatter();
41
41
  #### Using CDN (UMD)
42
42
 
43
43
  ```html
44
- <script src="https://cdn.jsdelivr.net/npm/masoneffect@0.1.15/dist/index.umd.min.js"></script>
44
+ <script src="https://cdn.jsdelivr.net/npm/masoneffect@0.1.18/dist/index.umd.min.js"></script>
45
45
  <script>
46
46
  const container = document.getElementById('my-container');
47
47
  const effect = new MasonEffect(container, {
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/core/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,MAAM,WAAW,kBAAkB;IACjC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,gBAAgB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACjC,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,WAAW,KAAK,IAAI,CAAC;IAC1C,QAAQ,CAAC,EAAE,CAAC,QAAQ,EAAE,WAAW,KAAK,IAAI,CAAC;CAC5C;AAED,MAAM,WAAW,QAAQ;IACvB,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,EAAE,EAAE,MAAM,CAA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I;IAQ5B,YAAY,IAAI,IAAI;IAIpB,eAAe,CAAC,CAAC,EAAE,UAAU,GAAG,IAAI;IAMpC,gBAAgB,IAAI,IAAI;IAIxB,eAAe,IAAI,IAAI;IAIvB,aAAa,IAAI,IAAI;IAKrB,YAAY,CAAC,SAAS,EAAE,OAAO,CAAC,kBAAkB,CAAC,GAAG,IAAI;IAK1D,qBAAqB,CAAC,SAAS,EAAE,OAAO,CAAC,kBAAkB,CAAC,GAAG,IAAI;IAQnE,OAAO,IAAI,IAAI;CAWhB;AAGD,eAAe,WAAW,CAAC"}
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ function t(t,s){let i=null;return function(...h){null!==i&&clearTimeout(i),i=setTimeout(()=>{i=null,t.apply(this,h)},s)}}Object.defineProperty(exports,"t",{value:!0});class s{constructor(s,i={}){if(this.container="string"==typeof s?document.querySelector(s):s,!this.container)throw new Error("Container element not found");this.config={text:i.text||"mason effect",densityStep:i.densityStep??2,maxParticles:i.maxParticles??3200,pointSize:i.pointSize??.5,ease:i.ease??.05,repelRadius:i.repelRadius??150,repelStrength:i.repelStrength??1,particleColor:i.particleColor||"#fff",fontFamily:i.fontFamily||"Inter, system-ui, Arial",fontSize:i.fontSize||null,width:i.width||null,height:i.height||null,devicePixelRatio:i.devicePixelRatio??null,onReady:i.onReady||null,onUpdate:i.onUpdate||null},this.canvas=document.createElement("canvas");const h=this.canvas.getContext("2d");if(!h)throw new Error("Canvas context not available");this.ctx=h,this.container.appendChild(this.canvas),this.canvas.style.display="block",this.offCanvas=document.createElement("canvas");const e=this.offCanvas.getContext("2d");if(!e)throw new Error("Offscreen canvas context not available");this.offCtx=e,this.W=0,this.H=0,this.DPR=this.config.devicePixelRatio||Math.min(window.devicePixelRatio||1,1.8),this.particles=[],this.mouse={x:0,y:0,down:!1},this.animationId=null,this.isRunning=!1,this.isVisible=!1,this.intersectionObserver=null,this.debounceDelay=i.debounceDelay??150;const o=this.handleResize.bind(this);this.handleResize=t(o,this.debounceDelay),this.handleMouseMove=this.handleMouseMove.bind(this),this.handleMouseLeave=this.handleMouseLeave.bind(this),this.handleMouseDown=this.handleMouseDown.bind(this),this.handleMouseUp=this.handleMouseUp.bind(this),this.i=t(this.h.bind(this),this.debounceDelay),this.o=t(this.l.bind(this),this.debounceDelay),this.init()}init(){this.resize(),this.setupEventListeners(),this.setupIntersectionObserver(),this.config.onReady&&this.config.onReady(this)}setupIntersectionObserver(){if("undefined"==typeof window||void 0===window.IntersectionObserver)return this.isVisible=!0,void this.start();this.intersectionObserver||(this.intersectionObserver=new IntersectionObserver(t=>{for(const s of t)s.target===this.container&&(s.isIntersecting?(this.isVisible=!0,this.start()):(this.isVisible=!1,this.stop()))},{threshold:.1}),this.intersectionObserver.observe(this.container))}resize(){const t=this.config.width||this.container.clientWidth||window.innerWidth,s=this.config.height||this.container.clientHeight||.7*window.innerHeight;if(t<=0||s<=0)return;this.W=Math.floor(t*this.DPR),this.H=Math.floor(s*this.DPR);const i=4096;if(this.W>i||this.H>i){const t=Math.min(i/this.W,i/this.H);this.W=Math.floor(this.W*t),this.H=Math.floor(this.H*t),this.DPR=this.DPR*t}this.canvas.width=this.W,this.canvas.height=this.H,this.canvas.style.width=t+"px",this.canvas.style.height=s+"px",this.W>0&&this.H>0&&(this.buildTargets(),this.particles.length||this.initParticles())}buildTargets(){if(this.W<=0||this.H<=0)return;const t=this.config.text;this.offCanvas.width=this.W,this.offCanvas.height=this.H,this.offCtx.clearRect(0,0,this.offCanvas.width,this.offCanvas.height);const s=Math.min(this.W,this.H),i=this.config.fontSize||Math.max(80,Math.floor(.18*s));this.offCtx.fillStyle="#ffffff",this.offCtx.textAlign="center",this.offCtx.textBaseline="middle",this.offCtx.font=`400 ${i}px ${this.config.fontFamily}`;const h=t.split(""),e=.05*i,o=this.offCtx.measureText(t).width+e*(h.length-1);let n=this.W/2-o/2;for(const t of h)this.offCtx.fillText(t,n+this.offCtx.measureText(t).width/2,this.H/2),n+=this.offCtx.measureText(t).width+e;const a=Math.max(2,this.config.densityStep),r=this.offCtx.getImageData(0,0,this.W,this.H).data,l=[];for(let t=0;t<this.H;t+=a)for(let s=0;s<this.W;s+=a){const i=4*(t*this.W+s);r[i]+r[i+1]+r[i+2]>600&&l.push({x:s,y:t})}for(;l.length>this.config.maxParticles;)l.splice(Math.floor(Math.random()*l.length),1);if(this.particles.length<l.length){const t=l.length-this.particles.length;for(let s=0;s<t;s++)this.particles.push(this.makeParticle())}else this.particles.length>l.length&&(this.particles.length=l.length);for(let t=0;t<this.particles.length;t++){const s=this.particles[t],i=l[t];s.tx=i.x,s.ty=i.y}}makeParticle(){const t=Math.random()*this.W,s=Math.random()*this.H;return{x:t,y:s,vx:0,vy:0,tx:t,ty:s,initialX:t,initialY:s,j:Math.random()*Math.PI*2}}initParticles(){for(const t of this.particles){const s=Math.random()*this.W,i=Math.random()*this.H;t.x=s,t.y=i,t.vx=t.vy=0,t.initialX=s,t.initialY=i}}scatter(){for(const t of this.particles)void 0!==t.initialX&&void 0!==t.initialY?(t.tx=t.initialX,t.ty=t.initialY):(t.initialX=t.x,t.initialY=t.y,t.tx=t.initialX,t.ty=t.initialY)}morph(t){this.i(t)}h(t){if(0!==this.W&&0!==this.H||this.resize(),"string"==typeof t)this.config.text=t,this.buildTargets();else if(t&&"object"==typeof t){const s=void 0!==t.text;this.config={...this.config,...t},s&&this.buildTargets()}else this.buildTargets()}update(){this.ctx.clearRect(0,0,this.W,this.H);for(const t of this.particles){let s=(t.tx-t.x)*this.config.ease,i=(t.ty-t.y)*this.config.ease;if(this.mouse.x||this.mouse.y){const h=t.x-this.mouse.x,e=t.y-this.mouse.y,o=h*h+e*e,n=this.config.repelRadius*this.DPR;if(o<n*n){const t=Math.sqrt(o)+1e-4,a=(this.mouse.down?-1:1)*this.config.repelStrength*(1-t/n);s+=h/t*a*6,i+=e/t*a*6}}t.j+=2,s+=.05*Math.cos(t.j),i+=.05*Math.sin(1.3*t.j),t.vx=(t.vx+s)*Math.random(),t.vy=(t.vy+i)*Math.random(),t.x+=t.vx,t.y+=t.vy}this.ctx.fillStyle=this.config.particleColor;const t=this.config.pointSize*this.DPR;for(const s of this.particles)this.ctx.beginPath(),this.ctx.arc(s.x,s.y,t,0,2*Math.PI),this.ctx.fill();this.config.onUpdate&&this.config.onUpdate(this)}animate(){this.isRunning&&(this.update(),this.animationId=requestAnimationFrame(()=>this.animate()))}start(){this.isRunning||(this.isRunning=!0,this.animate())}stop(){this.isRunning=!1,this.animationId&&(cancelAnimationFrame(this.animationId),this.animationId=null)}setupEventListeners(){window.addEventListener("resize",this.handleResize),this.canvas.addEventListener("mousemove",this.handleMouseMove),this.canvas.addEventListener("mouseleave",this.handleMouseLeave),this.canvas.addEventListener("mousedown",this.handleMouseDown),window.addEventListener("mouseup",this.handleMouseUp)}removeEventListeners(){window.removeEventListener("resize",this.handleResize),this.canvas.removeEventListener("mousemove",this.handleMouseMove),this.canvas.removeEventListener("mouseleave",this.handleMouseLeave),this.canvas.removeEventListener("mousedown",this.handleMouseDown),window.removeEventListener("mouseup",this.handleMouseUp)}handleResize(){this.resize()}handleMouseMove(t){const s=this.canvas.getBoundingClientRect();this.mouse.x=(t.clientX-s.left)*this.DPR,this.mouse.y=(t.clientY-s.top)*this.DPR}handleMouseLeave(){this.mouse.x=this.mouse.y=0}handleMouseDown(){this.mouse.down=!0}handleMouseUp(){this.mouse.down=!1}updateConfig(t){this.o(t)}l(t){this.config={...this.config,...t},t.text&&this.buildTargets()}destroy(){this.stop(),this.removeEventListeners(),this.intersectionObserver&&(this.intersectionObserver.disconnect(),this.intersectionObserver=null),this.canvas&&this.canvas.parentNode&&this.canvas.parentNode.removeChild(this.canvas)}}exports.MasonEffect=s,exports.default=s;
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ function t(t,s){let i=null;return function(...h){null!==i&&clearTimeout(i),i=setTimeout(()=>{i=null,t.apply(this,h)},s)}}class s{constructor(s,i={}){if(this.container="string"==typeof s?document.querySelector(s):s,!this.container)throw new Error("Container element not found");this.config={text:i.text||"mason effect",densityStep:i.densityStep??2,maxParticles:i.maxParticles??3200,pointSize:i.pointSize??.5,ease:i.ease??.05,repelRadius:i.repelRadius??150,repelStrength:i.repelStrength??1,particleColor:i.particleColor||"#fff",fontFamily:i.fontFamily||"Inter, system-ui, Arial",fontSize:i.fontSize||null,width:i.width||null,height:i.height||null,devicePixelRatio:i.devicePixelRatio??null,onReady:i.onReady||null,onUpdate:i.onUpdate||null},this.canvas=document.createElement("canvas");const h=this.canvas.getContext("2d");if(!h)throw new Error("Canvas context not available");this.ctx=h,this.container.appendChild(this.canvas),this.canvas.style.display="block",this.offCanvas=document.createElement("canvas");const e=this.offCanvas.getContext("2d");if(!e)throw new Error("Offscreen canvas context not available");this.offCtx=e,this.W=0,this.H=0,this.DPR=this.config.devicePixelRatio||Math.min(window.devicePixelRatio||1,1.8),this.particles=[],this.mouse={x:0,y:0,down:!1},this.animationId=null,this.isRunning=!1,this.isVisible=!1,this.intersectionObserver=null,this.debounceDelay=i.debounceDelay??150;const n=this.handleResize.bind(this);this.handleResize=t(n,this.debounceDelay),this.handleMouseMove=this.handleMouseMove.bind(this),this.handleMouseLeave=this.handleMouseLeave.bind(this),this.handleMouseDown=this.handleMouseDown.bind(this),this.handleMouseUp=this.handleMouseUp.bind(this),this.t=t(this.i.bind(this),this.debounceDelay),this.h=t(this.o.bind(this),this.debounceDelay),this.init()}init(){this.resize(),this.setupEventListeners(),this.setupIntersectionObserver(),this.config.onReady&&this.config.onReady(this)}setupIntersectionObserver(){if("undefined"==typeof window||void 0===window.IntersectionObserver)return this.isVisible=!0,void this.start();this.intersectionObserver||(this.intersectionObserver=new IntersectionObserver(t=>{for(const s of t)s.target===this.container&&(s.isIntersecting?(this.isVisible=!0,this.start()):(this.isVisible=!1,this.stop()))},{threshold:.1}),this.intersectionObserver.observe(this.container))}resize(){const t=this.config.width||this.container.clientWidth||window.innerWidth,s=this.config.height||this.container.clientHeight||.7*window.innerHeight;if(t<=0||s<=0)return;this.W=Math.floor(t*this.DPR),this.H=Math.floor(s*this.DPR);const i=4096;if(this.W>i||this.H>i){const t=Math.min(i/this.W,i/this.H);this.W=Math.floor(this.W*t),this.H=Math.floor(this.H*t),this.DPR=this.DPR*t}this.canvas.width=this.W,this.canvas.height=this.H,this.canvas.style.width=t+"px",this.canvas.style.height=s+"px",this.W>0&&this.H>0&&(this.buildTargets(),this.particles.length||this.initParticles())}buildTargets(){if(this.W<=0||this.H<=0)return;const t=this.config.text;this.offCanvas.width=this.W,this.offCanvas.height=this.H,this.offCtx.clearRect(0,0,this.offCanvas.width,this.offCanvas.height);const s=Math.min(this.W,this.H),i=this.config.fontSize||Math.max(80,Math.floor(.18*s));this.offCtx.fillStyle="#ffffff",this.offCtx.textAlign="center",this.offCtx.textBaseline="middle",this.offCtx.font=`400 ${i}px ${this.config.fontFamily}`;const h=t.split(""),e=.05*i,n=this.offCtx.measureText(t).width+e*(h.length-1);let o=this.W/2-n/2;for(const t of h)this.offCtx.fillText(t,o+this.offCtx.measureText(t).width/2,this.H/2),o+=this.offCtx.measureText(t).width+e;const a=Math.max(2,this.config.densityStep),r=this.offCtx.getImageData(0,0,this.W,this.H).data,l=[];for(let t=0;t<this.H;t+=a)for(let s=0;s<this.W;s+=a){const i=4*(t*this.W+s);r[i]+r[i+1]+r[i+2]>600&&l.push({x:s,y:t})}for(;l.length>this.config.maxParticles;)l.splice(Math.floor(Math.random()*l.length),1);if(this.particles.length<l.length){const t=l.length-this.particles.length;for(let s=0;s<t;s++)this.particles.push(this.makeParticle())}else this.particles.length>l.length&&(this.particles.length=l.length);for(let t=0;t<this.particles.length;t++){const s=this.particles[t],i=l[t];s.tx=i.x,s.ty=i.y}}makeParticle(){const t=Math.random()*this.W,s=Math.random()*this.H;return{x:t,y:s,vx:0,vy:0,tx:t,ty:s,initialX:t,initialY:s,j:Math.random()*Math.PI*2}}initParticles(){for(const t of this.particles){const s=Math.random()*this.W,i=Math.random()*this.H;t.x=s,t.y=i,t.vx=t.vy=0,t.initialX=s,t.initialY=i}}scatter(){for(const t of this.particles)void 0!==t.initialX&&void 0!==t.initialY?(t.tx=t.initialX,t.ty=t.initialY):(t.initialX=t.x,t.initialY=t.y,t.tx=t.initialX,t.ty=t.initialY)}morph(t){this.t(t)}i(t){if(0!==this.W&&0!==this.H||this.resize(),"string"==typeof t)this.config.text=t,this.buildTargets();else if(t&&"object"==typeof t){const s=void 0!==t.text;this.config={...this.config,...t},s&&this.buildTargets()}else this.buildTargets()}update(){this.ctx.clearRect(0,0,this.W,this.H);for(const t of this.particles){let s=(t.tx-t.x)*this.config.ease,i=(t.ty-t.y)*this.config.ease;if(this.mouse.x||this.mouse.y){const h=t.x-this.mouse.x,e=t.y-this.mouse.y,n=h*h+e*e,o=this.config.repelRadius*this.DPR;if(n<o*o){const t=Math.sqrt(n)+1e-4,a=(this.mouse.down?-1:1)*this.config.repelStrength*(1-t/o);s+=h/t*a*6,i+=e/t*a*6}}t.j+=2,s+=.05*Math.cos(t.j),i+=.05*Math.sin(1.3*t.j),t.vx=(t.vx+s)*Math.random(),t.vy=(t.vy+i)*Math.random(),t.x+=t.vx,t.y+=t.vy}this.ctx.fillStyle=this.config.particleColor;const t=this.config.pointSize*this.DPR;for(const s of this.particles)this.ctx.beginPath(),this.ctx.arc(s.x,s.y,t,0,2*Math.PI),this.ctx.fill();this.config.onUpdate&&this.config.onUpdate(this)}animate(){this.isRunning&&(this.update(),this.animationId=requestAnimationFrame(()=>this.animate()))}start(){this.isRunning||(this.isRunning=!0,this.animate())}stop(){this.isRunning=!1,this.animationId&&(cancelAnimationFrame(this.animationId),this.animationId=null)}setupEventListeners(){window.addEventListener("resize",this.handleResize),this.canvas.addEventListener("mousemove",this.handleMouseMove),this.canvas.addEventListener("mouseleave",this.handleMouseLeave),this.canvas.addEventListener("mousedown",this.handleMouseDown),window.addEventListener("mouseup",this.handleMouseUp)}removeEventListeners(){window.removeEventListener("resize",this.handleResize),this.canvas.removeEventListener("mousemove",this.handleMouseMove),this.canvas.removeEventListener("mouseleave",this.handleMouseLeave),this.canvas.removeEventListener("mousedown",this.handleMouseDown),window.removeEventListener("mouseup",this.handleMouseUp)}handleResize(){this.resize()}handleMouseMove(t){const s=this.canvas.getBoundingClientRect();this.mouse.x=(t.clientX-s.left)*this.DPR,this.mouse.y=(t.clientY-s.top)*this.DPR}handleMouseLeave(){this.mouse.x=this.mouse.y=0}handleMouseDown(){this.mouse.down=!0}handleMouseUp(){this.mouse.down=!1}updateConfig(t){this.h(t)}o(t){this.config={...this.config,...t},t.text&&this.buildTargets()}destroy(){this.stop(),this.removeEventListeners(),this.intersectionObserver&&(this.intersectionObserver.disconnect(),this.intersectionObserver=null),this.canvas&&this.canvas.parentNode&&this.canvas.parentNode.removeChild(this.canvas)}}export{s as MasonEffect,s as default};
@@ -1,526 +1 @@
1
- 'use strict';
2
-
3
- var React = require('react');
4
-
5
- /**
6
- * MasonEffect - 파티클 모핑 효과 라이브러리
7
- * 바닐라 JS 코어 클래스
8
- */
9
- // 디바운스 유틸리티 함수
10
- function debounce(func, wait) {
11
- let timeout = null;
12
- return function executedFunction(...args) {
13
- const later = () => {
14
- timeout = null;
15
- func.apply(this, args);
16
- };
17
- if (timeout !== null) {
18
- clearTimeout(timeout);
19
- }
20
- timeout = setTimeout(later, wait);
21
- };
22
- }
23
- class MasonEffect {
24
- constructor(container, options = {}) {
25
- // 컨테이너 요소
26
- this.container = typeof container === 'string'
27
- ? document.querySelector(container)
28
- : container;
29
- if (!this.container) {
30
- throw new Error('Container element not found');
31
- }
32
- // 설정값들
33
- this.config = {
34
- text: options.text || 'mason effect',
35
- densityStep: options.densityStep ?? 2,
36
- maxParticles: options.maxParticles ?? 3200,
37
- pointSize: options.pointSize ?? 0.5,
38
- ease: options.ease ?? 0.05,
39
- repelRadius: options.repelRadius ?? 150,
40
- repelStrength: options.repelStrength ?? 1,
41
- particleColor: options.particleColor || '#fff',
42
- fontFamily: options.fontFamily || 'Inter, system-ui, Arial',
43
- fontSize: options.fontSize || null,
44
- width: options.width || null,
45
- height: options.height || null,
46
- devicePixelRatio: options.devicePixelRatio ?? null,
47
- onReady: options.onReady || null,
48
- onUpdate: options.onUpdate || null,
49
- };
50
- // 캔버스 생성
51
- this.canvas = document.createElement('canvas');
52
- const ctx = this.canvas.getContext('2d');
53
- if (!ctx) {
54
- throw new Error('Canvas context not available');
55
- }
56
- this.ctx = ctx;
57
- this.container.appendChild(this.canvas);
58
- this.canvas.style.display = 'block';
59
- // 오프스크린 캔버스 (텍스트 렌더링용)
60
- this.offCanvas = document.createElement('canvas');
61
- const offCtx = this.offCanvas.getContext('2d');
62
- if (!offCtx) {
63
- throw new Error('Offscreen canvas context not available');
64
- }
65
- this.offCtx = offCtx;
66
- // 상태
67
- this.W = 0;
68
- this.H = 0;
69
- this.DPR = this.config.devicePixelRatio || Math.min(window.devicePixelRatio || 1, 1.8);
70
- this.particles = [];
71
- this.mouse = { x: 0, y: 0, down: false };
72
- this.animationId = null;
73
- this.isRunning = false;
74
- this.isVisible = false;
75
- this.intersectionObserver = null;
76
- // 디바운스 설정 (ms)
77
- this.debounceDelay = options.debounceDelay ?? 150;
78
- // 이벤트 핸들러 바인딩 (디바운스 적용 전에 바인딩)
79
- const boundHandleResize = this.handleResize.bind(this);
80
- this.handleResize = debounce(boundHandleResize, this.debounceDelay);
81
- this.handleMouseMove = this.handleMouseMove.bind(this);
82
- this.handleMouseLeave = this.handleMouseLeave.bind(this);
83
- this.handleMouseDown = this.handleMouseDown.bind(this);
84
- this.handleMouseUp = this.handleMouseUp.bind(this);
85
- // morph와 updateConfig를 위한 디바운스된 내부 메서드
86
- this._debouncedMorph = debounce(this._morphInternal.bind(this), this.debounceDelay);
87
- this._debouncedUpdateConfig = debounce(this._updateConfigInternal.bind(this), this.debounceDelay);
88
- // 초기화
89
- this.init();
90
- }
91
- init() {
92
- this.resize();
93
- this.setupEventListeners();
94
- this.setupIntersectionObserver();
95
- if (this.config.onReady) {
96
- this.config.onReady(this);
97
- }
98
- }
99
- setupIntersectionObserver() {
100
- // IntersectionObserver가 지원되지 않는 환경에서는 항상 재생
101
- if (typeof window === 'undefined' || typeof window.IntersectionObserver === 'undefined') {
102
- this.isVisible = true;
103
- this.start();
104
- return;
105
- }
106
- // 이미 설정되어 있다면 다시 만들지 않음
107
- if (this.intersectionObserver) {
108
- return;
109
- }
110
- this.intersectionObserver = new IntersectionObserver((entries) => {
111
- for (const entry of entries) {
112
- if (entry.target !== this.container)
113
- continue;
114
- if (entry.isIntersecting) {
115
- this.isVisible = true;
116
- this.start();
117
- }
118
- else {
119
- this.isVisible = false;
120
- this.stop();
121
- }
122
- }
123
- }, {
124
- threshold: 0.1, // 10% 이상 보일 때 동작
125
- });
126
- this.intersectionObserver.observe(this.container);
127
- }
128
- resize() {
129
- const width = this.config.width || this.container.clientWidth || window.innerWidth;
130
- const height = this.config.height || this.container.clientHeight || window.innerHeight * 0.7;
131
- this.W = Math.floor(width * this.DPR);
132
- this.H = Math.floor(height * this.DPR);
133
- this.canvas.width = this.W;
134
- this.canvas.height = this.H;
135
- this.canvas.style.width = width + 'px';
136
- this.canvas.style.height = height + 'px';
137
- this.buildTargets();
138
- if (!this.particles.length) {
139
- this.initParticles();
140
- }
141
- }
142
- buildTargets() {
143
- const text = this.config.text;
144
- this.offCanvas.width = this.W;
145
- this.offCanvas.height = this.H;
146
- this.offCtx.clearRect(0, 0, this.offCanvas.width, this.offCanvas.height);
147
- const base = Math.min(this.W, this.H);
148
- const fontSize = this.config.fontSize || Math.max(80, Math.floor(base * 0.18));
149
- this.offCtx.fillStyle = '#ffffff';
150
- this.offCtx.textAlign = 'center';
151
- this.offCtx.textBaseline = 'middle';
152
- this.offCtx.font = `400 ${fontSize}px ${this.config.fontFamily}`;
153
- // 글자 간격 계산 및 그리기
154
- const chars = text.split('');
155
- const spacing = fontSize * 0.05;
156
- const totalWidth = this.offCtx.measureText(text).width + spacing * (chars.length - 1);
157
- let x = this.W / 2 - totalWidth / 2;
158
- for (const ch of chars) {
159
- this.offCtx.fillText(ch, x + this.offCtx.measureText(ch).width / 2, this.H / 2);
160
- x += this.offCtx.measureText(ch).width + spacing;
161
- }
162
- // 픽셀 샘플링
163
- const step = Math.max(2, this.config.densityStep);
164
- const img = this.offCtx.getImageData(0, 0, this.W, this.H).data;
165
- const targets = [];
166
- for (let y = 0; y < this.H; y += step) {
167
- for (let x = 0; x < this.W; x += step) {
168
- const i = (y * this.W + x) * 4;
169
- if (img[i] + img[i + 1] + img[i + 2] > 600) {
170
- targets.push({ x, y });
171
- }
172
- }
173
- }
174
- // 파티클 수 제한
175
- while (targets.length > this.config.maxParticles) {
176
- targets.splice(Math.floor(Math.random() * targets.length), 1);
177
- }
178
- // 파티클 수 조정
179
- if (this.particles.length < targets.length) {
180
- const need = targets.length - this.particles.length;
181
- for (let i = 0; i < need; i++) {
182
- this.particles.push(this.makeParticle());
183
- }
184
- }
185
- else if (this.particles.length > targets.length) {
186
- this.particles.length = targets.length;
187
- }
188
- // 목표 좌표 할당
189
- for (let i = 0; i < this.particles.length; i++) {
190
- const p = this.particles[i];
191
- const t = targets[i];
192
- p.tx = t.x;
193
- p.ty = t.y;
194
- }
195
- }
196
- makeParticle() {
197
- // 캔버스 전체에 골고루 분포 (여백 없이)
198
- const sx = Math.random() * this.W;
199
- const sy = Math.random() * this.H;
200
- return {
201
- x: sx,
202
- y: sy,
203
- vx: 0,
204
- vy: 0,
205
- tx: sx,
206
- ty: sy,
207
- initialX: sx, // 초기 위치 저장 (scatter 시 돌아갈 위치)
208
- initialY: sy,
209
- j: Math.random() * Math.PI * 2,
210
- };
211
- }
212
- initParticles() {
213
- // 캔버스 전체에 골고루 분포 (여백 없이)
214
- for (const p of this.particles) {
215
- const sx = Math.random() * this.W;
216
- const sy = Math.random() * this.H;
217
- p.x = sx;
218
- p.y = sy;
219
- p.vx = p.vy = 0;
220
- // 초기 위치 저장 (scatter 시 돌아갈 위치)
221
- p.initialX = sx;
222
- p.initialY = sy;
223
- }
224
- }
225
- scatter() {
226
- // 각 파티클을 초기 위치로 돌아가도록 설정
227
- for (const p of this.particles) {
228
- // 초기 위치가 저장되어 있으면 그 위치로, 없으면 현재 위치 유지
229
- if (p.initialX !== undefined && p.initialY !== undefined) {
230
- p.tx = p.initialX;
231
- p.ty = p.initialY;
232
- }
233
- else {
234
- // 초기 위치가 없으면 현재 위치를 초기 위치로 저장
235
- p.initialX = p.x;
236
- p.initialY = p.y;
237
- p.tx = p.initialX;
238
- p.ty = p.initialY;
239
- }
240
- }
241
- }
242
- morph(textOrOptions) {
243
- // 즉시 실행이 필요한 경우 (예: 초기화 시)를 위해 내부 메서드 직접 호출
244
- // 일반적인 경우에는 디바운스 적용
245
- this._debouncedMorph(textOrOptions);
246
- }
247
- _morphInternal(textOrOptions) {
248
- // W와 H가 0이면 resize 먼저 실행
249
- if (this.W === 0 || this.H === 0) {
250
- this.resize();
251
- }
252
- if (typeof textOrOptions === 'string') {
253
- // 문자열인 경우: 기존 동작 유지
254
- this.config.text = textOrOptions;
255
- this.buildTargets();
256
- }
257
- else if (textOrOptions && typeof textOrOptions === 'object') {
258
- // 객체인 경우: 텍스트와 함께 다른 설정도 변경
259
- const needsRebuild = textOrOptions.text !== undefined;
260
- this.config = { ...this.config, ...textOrOptions };
261
- if (needsRebuild) {
262
- this.buildTargets();
263
- }
264
- }
265
- else {
266
- // null이거나 undefined인 경우: 현재 텍스트로 재빌드
267
- this.buildTargets();
268
- }
269
- }
270
- update() {
271
- this.ctx.clearRect(0, 0, this.W, this.H);
272
- for (const p of this.particles) {
273
- // 목표 좌표로 당기는 힘
274
- let ax = (p.tx - p.x) * this.config.ease;
275
- let ay = (p.ty - p.y) * this.config.ease;
276
- // 마우스 반발/흡입
277
- if (this.mouse.x || this.mouse.y) {
278
- const dx = p.x - this.mouse.x;
279
- const dy = p.y - this.mouse.y;
280
- const d2 = dx * dx + dy * dy;
281
- const r = this.config.repelRadius * this.DPR;
282
- if (d2 < r * r) {
283
- const d = Math.sqrt(d2) + 0.0001;
284
- const f = (this.mouse.down ? -1 : 1) * this.config.repelStrength * (1 - d / r);
285
- ax += (dx / d) * f * 6.0;
286
- ay += (dy / d) * f * 6.0;
287
- }
288
- }
289
- // 진동 효과
290
- p.j += 2;
291
- ax += Math.cos(p.j) * 0.05;
292
- ay += Math.sin(p.j * 1.3) * 0.05;
293
- // 속도와 위치 업데이트
294
- p.vx = (p.vx + ax) * Math.random();
295
- p.vy = (p.vy + ay) * Math.random();
296
- p.x += p.vx;
297
- p.y += p.vy;
298
- }
299
- // 파티클 그리기
300
- this.ctx.fillStyle = this.config.particleColor;
301
- const r = this.config.pointSize * this.DPR;
302
- for (const p of this.particles) {
303
- this.ctx.beginPath();
304
- this.ctx.arc(p.x, p.y, r, 0, Math.PI * 2);
305
- this.ctx.fill();
306
- }
307
- if (this.config.onUpdate) {
308
- this.config.onUpdate(this);
309
- }
310
- }
311
- animate() {
312
- if (!this.isRunning)
313
- return;
314
- this.update();
315
- this.animationId = requestAnimationFrame(() => this.animate());
316
- }
317
- start() {
318
- if (this.isRunning)
319
- return;
320
- this.isRunning = true;
321
- this.animate();
322
- }
323
- stop() {
324
- this.isRunning = false;
325
- if (this.animationId) {
326
- cancelAnimationFrame(this.animationId);
327
- this.animationId = null;
328
- }
329
- }
330
- setupEventListeners() {
331
- window.addEventListener('resize', this.handleResize);
332
- this.canvas.addEventListener('mousemove', this.handleMouseMove);
333
- this.canvas.addEventListener('mouseleave', this.handleMouseLeave);
334
- this.canvas.addEventListener('mousedown', this.handleMouseDown);
335
- window.addEventListener('mouseup', this.handleMouseUp);
336
- }
337
- removeEventListeners() {
338
- window.removeEventListener('resize', this.handleResize);
339
- this.canvas.removeEventListener('mousemove', this.handleMouseMove);
340
- this.canvas.removeEventListener('mouseleave', this.handleMouseLeave);
341
- this.canvas.removeEventListener('mousedown', this.handleMouseDown);
342
- window.removeEventListener('mouseup', this.handleMouseUp);
343
- }
344
- handleResize() {
345
- this.resize();
346
- }
347
- handleMouseMove(e) {
348
- const rect = this.canvas.getBoundingClientRect();
349
- this.mouse.x = (e.clientX - rect.left) * this.DPR;
350
- this.mouse.y = (e.clientY - rect.top) * this.DPR;
351
- }
352
- handleMouseLeave() {
353
- this.mouse.x = this.mouse.y = 0;
354
- }
355
- handleMouseDown() {
356
- this.mouse.down = true;
357
- }
358
- handleMouseUp() {
359
- this.mouse.down = false;
360
- }
361
- // 설정 업데이트
362
- updateConfig(newConfig) {
363
- // 디바운스 적용
364
- this._debouncedUpdateConfig(newConfig);
365
- }
366
- _updateConfigInternal(newConfig) {
367
- this.config = { ...this.config, ...newConfig };
368
- if (newConfig.text) {
369
- this.buildTargets();
370
- }
371
- }
372
- // 파괴 및 정리
373
- destroy() {
374
- this.stop();
375
- this.removeEventListeners();
376
- if (this.intersectionObserver) {
377
- this.intersectionObserver.disconnect();
378
- this.intersectionObserver = null;
379
- }
380
- if (this.canvas && this.canvas.parentNode) {
381
- this.canvas.parentNode.removeChild(this.canvas);
382
- }
383
- }
384
- }
385
-
386
- const MasonEffectComponent = React.forwardRef((props, ref) => {
387
- const containerRef = React.useRef(null);
388
- const instanceRef = React.useRef(null);
389
- React.useEffect(() => {
390
- if (!containerRef.current)
391
- return;
392
- let resizeObserver = null;
393
- let initTimeout = null;
394
- // 컨테이너가 실제 크기를 가지도록 대기
395
- const initEffect = () => {
396
- const container = containerRef.current;
397
- if (!container)
398
- return;
399
- // 컨테이너 크기가 0이면 다음 프레임에 다시 시도
400
- const rect = container.getBoundingClientRect();
401
- if (rect.width === 0 || rect.height === 0) {
402
- initTimeout = window.setTimeout(initEffect, 50);
403
- return;
404
- }
405
- const { className, style, text, densityStep, maxParticles, pointSize, ease, repelRadius, repelStrength, particleColor, fontFamily, fontSize, width, height, devicePixelRatio, onReady, onUpdate, } = props;
406
- const options = {
407
- text,
408
- densityStep,
409
- maxParticles,
410
- pointSize,
411
- ease,
412
- repelRadius,
413
- repelStrength,
414
- particleColor,
415
- fontFamily,
416
- fontSize,
417
- width,
418
- height,
419
- devicePixelRatio,
420
- onReady,
421
- onUpdate,
422
- };
423
- instanceRef.current = new MasonEffect(container, options);
424
- // ResizeObserver로 컨테이너 크기 변경 감지
425
- if (typeof ResizeObserver !== 'undefined') {
426
- resizeObserver = new ResizeObserver(() => {
427
- if (instanceRef.current) {
428
- instanceRef.current.resize();
429
- }
430
- });
431
- resizeObserver.observe(container);
432
- }
433
- };
434
- // 다음 프레임에 초기화 (DOM이 완전히 렌더링된 후)
435
- requestAnimationFrame(initEffect);
436
- return () => {
437
- if (initTimeout) {
438
- clearTimeout(initTimeout);
439
- }
440
- if (resizeObserver) {
441
- resizeObserver.disconnect();
442
- }
443
- if (instanceRef.current) {
444
- instanceRef.current.destroy();
445
- instanceRef.current = null;
446
- }
447
- };
448
- }, []);
449
- // props 변경 시 설정 업데이트
450
- React.useEffect(() => {
451
- if (!instanceRef.current)
452
- return;
453
- const { text, densityStep, maxParticles, pointSize, ease, repelRadius, repelStrength, particleColor, fontFamily, fontSize, width, height, devicePixelRatio, } = props;
454
- instanceRef.current.updateConfig({
455
- text,
456
- densityStep,
457
- maxParticles,
458
- pointSize,
459
- ease,
460
- repelRadius,
461
- repelStrength,
462
- particleColor,
463
- fontFamily,
464
- fontSize,
465
- width,
466
- height,
467
- devicePixelRatio,
468
- });
469
- }, [
470
- props.text,
471
- props.densityStep,
472
- props.maxParticles,
473
- props.pointSize,
474
- props.ease,
475
- props.repelRadius,
476
- props.repelStrength,
477
- props.particleColor,
478
- props.fontFamily,
479
- props.fontSize,
480
- props.width,
481
- props.height,
482
- props.devicePixelRatio,
483
- ]);
484
- React.useImperativeHandle(ref, () => ({
485
- morph: (textOrOptions) => {
486
- if (!instanceRef.current) {
487
- console.warn('MasonEffect: 인스턴스가 아직 초기화되지 않았습니다.');
488
- return;
489
- }
490
- instanceRef.current.morph(textOrOptions);
491
- },
492
- scatter: () => {
493
- if (!instanceRef.current) {
494
- console.warn('MasonEffect: 인스턴스가 아직 초기화되지 않았습니다.');
495
- return;
496
- }
497
- instanceRef.current.scatter();
498
- },
499
- updateConfig: (config) => {
500
- if (!instanceRef.current) {
501
- console.warn('MasonEffect: 인스턴스가 아직 초기화되지 않았습니다.');
502
- return;
503
- }
504
- instanceRef.current.updateConfig(config);
505
- },
506
- destroy: () => {
507
- if (instanceRef.current) {
508
- instanceRef.current.destroy();
509
- instanceRef.current = null;
510
- }
511
- },
512
- }));
513
- // 기본 스타일: 컨테이너가 크기를 가지도록 함
514
- const defaultStyle = {
515
- width: '100%',
516
- height: '100%',
517
- minHeight: props.height || 400,
518
- position: 'relative',
519
- ...props.style,
520
- };
521
- return (React.createElement("div", { ref: containerRef, className: props.className, style: defaultStyle }));
522
- });
523
- MasonEffectComponent.displayName = 'MasonEffect';
524
-
525
- module.exports = MasonEffectComponent;
526
- //# sourceMappingURL=MasonEffect.cjs.map
1
+ var t=require("react");function i(t,i){let s=null;return function(...h){null!==s&&clearTimeout(s),s=setTimeout(()=>{s=null,t.apply(this,h)},i)}}class s{constructor(t,s={}){if(this.container="string"==typeof t?document.querySelector(t):t,!this.container)throw new Error("Container element not found");this.config={text:s.text||"mason effect",densityStep:s.densityStep??2,maxParticles:s.maxParticles??3200,pointSize:s.pointSize??.5,ease:s.ease??.05,repelRadius:s.repelRadius??150,repelStrength:s.repelStrength??1,particleColor:s.particleColor||"#fff",fontFamily:s.fontFamily||"Inter, system-ui, Arial",fontSize:s.fontSize||null,width:s.width||null,height:s.height||null,devicePixelRatio:s.devicePixelRatio??null,onReady:s.onReady||null,onUpdate:s.onUpdate||null},this.canvas=document.createElement("canvas");const h=this.canvas.getContext("2d");if(!h)throw new Error("Canvas context not available");this.ctx=h,this.container.appendChild(this.canvas),this.canvas.style.display="block",this.offCanvas=document.createElement("canvas");const e=this.offCanvas.getContext("2d");if(!e)throw new Error("Offscreen canvas context not available");this.offCtx=e,this.W=0,this.H=0,this.DPR=this.config.devicePixelRatio||Math.min(window.devicePixelRatio||1,1.8),this.particles=[],this.mouse={x:0,y:0,down:!1},this.animationId=null,this.isRunning=!1,this.isVisible=!1,this.intersectionObserver=null,this.debounceDelay=s.debounceDelay??150;const n=this.handleResize.bind(this);this.handleResize=i(n,this.debounceDelay),this.handleMouseMove=this.handleMouseMove.bind(this),this.handleMouseLeave=this.handleMouseLeave.bind(this),this.handleMouseDown=this.handleMouseDown.bind(this),this.handleMouseUp=this.handleMouseUp.bind(this),this.t=i(this.i.bind(this),this.debounceDelay),this.h=i(this.o.bind(this),this.debounceDelay),this.init()}init(){this.resize(),this.setupEventListeners(),this.setupIntersectionObserver(),this.config.onReady&&this.config.onReady(this)}setupIntersectionObserver(){if("undefined"==typeof window||void 0===window.IntersectionObserver)return this.isVisible=!0,void this.start();this.intersectionObserver||(this.intersectionObserver=new IntersectionObserver(t=>{for(const i of t)i.target===this.container&&(i.isIntersecting?(this.isVisible=!0,this.start()):(this.isVisible=!1,this.stop()))},{threshold:.1}),this.intersectionObserver.observe(this.container))}resize(){const t=this.config.width||this.container.clientWidth||window.innerWidth,i=this.config.height||this.container.clientHeight||.7*window.innerHeight;if(t<=0||i<=0)return;this.W=Math.floor(t*this.DPR),this.H=Math.floor(i*this.DPR);const s=4096;if(this.W>s||this.H>s){const t=Math.min(s/this.W,s/this.H);this.W=Math.floor(this.W*t),this.H=Math.floor(this.H*t),this.DPR=this.DPR*t}this.canvas.width=this.W,this.canvas.height=this.H,this.canvas.style.width=t+"px",this.canvas.style.height=i+"px",this.W>0&&this.H>0&&(this.buildTargets(),this.particles.length||this.initParticles())}buildTargets(){if(this.W<=0||this.H<=0)return;const t=this.config.text;this.offCanvas.width=this.W,this.offCanvas.height=this.H,this.offCtx.clearRect(0,0,this.offCanvas.width,this.offCanvas.height);const i=Math.min(this.W,this.H),s=this.config.fontSize||Math.max(80,Math.floor(.18*i));this.offCtx.fillStyle="#ffffff",this.offCtx.textAlign="center",this.offCtx.textBaseline="middle",this.offCtx.font=`400 ${s}px ${this.config.fontFamily}`;const h=t.split(""),e=.05*s,n=this.offCtx.measureText(t).width+e*(h.length-1);let o=this.W/2-n/2;for(const t of h)this.offCtx.fillText(t,o+this.offCtx.measureText(t).width/2,this.H/2),o+=this.offCtx.measureText(t).width+e;const a=Math.max(2,this.config.densityStep),r=this.offCtx.getImageData(0,0,this.W,this.H).data,l=[];for(let t=0;t<this.H;t+=a)for(let i=0;i<this.W;i+=a){const s=4*(t*this.W+i);r[s]+r[s+1]+r[s+2]>600&&l.push({x:i,y:t})}for(;l.length>this.config.maxParticles;)l.splice(Math.floor(Math.random()*l.length),1);if(this.particles.length<l.length){const t=l.length-this.particles.length;for(let i=0;i<t;i++)this.particles.push(this.makeParticle())}else this.particles.length>l.length&&(this.particles.length=l.length);for(let t=0;t<this.particles.length;t++){const i=this.particles[t],s=l[t];i.tx=s.x,i.ty=s.y}}makeParticle(){const t=Math.random()*this.W,i=Math.random()*this.H;return{x:t,y:i,vx:0,vy:0,tx:t,ty:i,initialX:t,initialY:i,j:Math.random()*Math.PI*2}}initParticles(){for(const t of this.particles){const i=Math.random()*this.W,s=Math.random()*this.H;t.x=i,t.y=s,t.vx=t.vy=0,t.initialX=i,t.initialY=s}}scatter(){for(const t of this.particles)void 0!==t.initialX&&void 0!==t.initialY?(t.tx=t.initialX,t.ty=t.initialY):(t.initialX=t.x,t.initialY=t.y,t.tx=t.initialX,t.ty=t.initialY)}morph(t){this.t(t)}i(t){if(0!==this.W&&0!==this.H||this.resize(),"string"==typeof t)this.config.text=t,this.buildTargets();else if(t&&"object"==typeof t){const i=void 0!==t.text;this.config={...this.config,...t},i&&this.buildTargets()}else this.buildTargets()}update(){this.ctx.clearRect(0,0,this.W,this.H);for(const t of this.particles){let i=(t.tx-t.x)*this.config.ease,s=(t.ty-t.y)*this.config.ease;if(this.mouse.x||this.mouse.y){const h=t.x-this.mouse.x,e=t.y-this.mouse.y,n=h*h+e*e,o=this.config.repelRadius*this.DPR;if(n<o*o){const t=Math.sqrt(n)+1e-4,a=(this.mouse.down?-1:1)*this.config.repelStrength*(1-t/o);i+=h/t*a*6,s+=e/t*a*6}}t.j+=2,i+=.05*Math.cos(t.j),s+=.05*Math.sin(1.3*t.j),t.vx=(t.vx+i)*Math.random(),t.vy=(t.vy+s)*Math.random(),t.x+=t.vx,t.y+=t.vy}this.ctx.fillStyle=this.config.particleColor;const t=this.config.pointSize*this.DPR;for(const i of this.particles)this.ctx.beginPath(),this.ctx.arc(i.x,i.y,t,0,2*Math.PI),this.ctx.fill();this.config.onUpdate&&this.config.onUpdate(this)}animate(){this.isRunning&&(this.update(),this.animationId=requestAnimationFrame(()=>this.animate()))}start(){this.isRunning||(this.isRunning=!0,this.animate())}stop(){this.isRunning=!1,this.animationId&&(cancelAnimationFrame(this.animationId),this.animationId=null)}setupEventListeners(){window.addEventListener("resize",this.handleResize),this.canvas.addEventListener("mousemove",this.handleMouseMove),this.canvas.addEventListener("mouseleave",this.handleMouseLeave),this.canvas.addEventListener("mousedown",this.handleMouseDown),window.addEventListener("mouseup",this.handleMouseUp)}removeEventListeners(){window.removeEventListener("resize",this.handleResize),this.canvas.removeEventListener("mousemove",this.handleMouseMove),this.canvas.removeEventListener("mouseleave",this.handleMouseLeave),this.canvas.removeEventListener("mousedown",this.handleMouseDown),window.removeEventListener("mouseup",this.handleMouseUp)}handleResize(){this.resize()}handleMouseMove(t){const i=this.canvas.getBoundingClientRect();this.mouse.x=(t.clientX-i.left)*this.DPR,this.mouse.y=(t.clientY-i.top)*this.DPR}handleMouseLeave(){this.mouse.x=this.mouse.y=0}handleMouseDown(){this.mouse.down=!0}handleMouseUp(){this.mouse.down=!1}updateConfig(t){this.h(t)}o(t){this.config={...this.config,...t},t.text&&this.buildTargets()}destroy(){this.stop(),this.removeEventListeners(),this.intersectionObserver&&(this.intersectionObserver.disconnect(),this.intersectionObserver=null),this.canvas&&this.canvas.parentNode&&this.canvas.parentNode.removeChild(this.canvas)}}const h=t.forwardRef((i,h)=>{const e=t.useRef(null),n=t.useRef(null);t.useEffect(()=>{if(!e.current)return;let t=null,h=null;const o=()=>{const a=e.current;if(!a)return;if(n.current)return;const r=a.querySelector("canvas");r&&r.remove();const l=a.getBoundingClientRect();if(l.width<=0||l.height<=0)return void(h=window.setTimeout(o,50));const c=100;(l.width<c||l.height<c)&&(a.style.minWidth=c+"px",a.style.minHeight=c+"px");const{className:f,style:u,text:d,densityStep:p,maxParticles:m,pointSize:w,ease:v,repelRadius:M,repelStrength:y,particleColor:x,fontFamily:S,fontSize:g,width:z,height:R,devicePixelRatio:P,onReady:C,onUpdate:b}=i,E={text:d,densityStep:p,maxParticles:m,pointSize:w,ease:v,repelRadius:M,repelStrength:y,particleColor:x,fontFamily:S,fontSize:g,width:z,height:R,devicePixelRatio:P,onReady:C,onUpdate:b};n.current=new s(a,E),"undefined"!=typeof ResizeObserver&&(t=new ResizeObserver(()=>{n.current&&n.current.resize()}),t.observe(a))};return requestAnimationFrame(o),()=>{h&&(clearTimeout(h),h=null),t&&(t.disconnect(),t=null),n.current&&(n.current.destroy(),n.current=null)}},[]),t.useEffect(()=>{if(!n.current)return;const{text:t,densityStep:s,maxParticles:h,pointSize:e,ease:o,repelRadius:a,repelStrength:r,particleColor:l,fontFamily:c,fontSize:f,width:u,height:d,devicePixelRatio:p}=i;n.current.updateConfig({text:t,densityStep:s,maxParticles:h,pointSize:e,ease:o,repelRadius:a,repelStrength:r,particleColor:l,fontFamily:c,fontSize:f,width:u,height:d,devicePixelRatio:p})},[i.text,i.densityStep,i.maxParticles,i.pointSize,i.ease,i.repelRadius,i.repelStrength,i.particleColor,i.fontFamily,i.fontSize,i.width,i.height,i.devicePixelRatio]),t.useImperativeHandle(h,()=>({morph:t=>{n.current?n.current.morph(t):console.warn("MasonEffect: 인스턴스가 아직 초기화되지 않았습니다.")},scatter:()=>{n.current?n.current.scatter():console.warn("MasonEffect: 인스턴스가 아직 초기화되지 않았습니다.")},updateConfig:t=>{n.current?n.current.updateConfig(t):console.warn("MasonEffect: 인스턴스가 아직 초기화되지 않았습니다.")},destroy:()=>{n.current&&(n.current.destroy(),n.current=null)}}));const o={width:"100%",height:"100%",minHeight:i.height||400,position:"relative",...i.style};return t.createElement("div",{ref:e,className:i.className,style:o})});h.displayName="MasonEffect",module.exports=h;
@@ -1 +1 @@
1
- {"version":3,"file":"MasonEffect.d.ts","sourceRoot":"","sources":["../../src/react/MasonEffect.tsx"],"names":[],"mappings":"AAAA,OAAO,KAA6D,MAAM,OAAO,CAAC;AAClF,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAE/C,MAAM,WAAW,kBAAkB;IACjC,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACzB,KAAK,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACtB,MAAM,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,gBAAgB,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IACjC,OAAO,CAAC,EAAE,CAAC,QAAQ,EAAE,WAAW,KAAK,IAAI,CAAC;IAC1C,QAAQ,CAAC,EAAE,CAAC,QAAQ,EAAE,WAAW,KAAK,IAAI,CAAC;CAC5C;AAED,MAAM,WAAW,cAAc;IAC7B,KAAK,EAAE,CAAC,aAAa,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,kBAAkB,CAAC,KAAK,IAAI,CAAC;IACtE,OAAO,EAAE,MAAM,IAAI,CAAC;IACpB,YAAY,EAAE,CAAC,MAAM,EAAE,OAAO,CAAC,kBAAkB,CAAC,KAAK,IAAI,CAAC;IAC5D,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB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E,CAAC,MAAM,EAAE,OAAO,CAAC,kBAAkB,CAAC,KAAK,IAAI,CAAC;IAC5D,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB;AAED,UAAU,gBAAiB,SAAQ,kBAAkB;IACnD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC;CAC7B;AAED,QAAA,MAAM,oBAAoB,yFAkNzB,CAAC;AAIF,eAAe,oBAAoB,CAAC"}
@@ -0,0 +1 @@
1
+ import t,{forwardRef as i,useRef as s,useEffect as h,useImperativeHandle as e}from"react";function n(t,i){let s=null;return function(...h){null!==s&&clearTimeout(s),s=setTimeout(()=>{s=null,t.apply(this,h)},i)}}class o{constructor(t,i={}){if(this.container="string"==typeof t?document.querySelector(t):t,!this.container)throw new Error("Container element not found");this.config={text:i.text||"mason effect",densityStep:i.densityStep??2,maxParticles:i.maxParticles??3200,pointSize:i.pointSize??.5,ease:i.ease??.05,repelRadius:i.repelRadius??150,repelStrength:i.repelStrength??1,particleColor:i.particleColor||"#fff",fontFamily:i.fontFamily||"Inter, system-ui, Arial",fontSize:i.fontSize||null,width:i.width||null,height:i.height||null,devicePixelRatio:i.devicePixelRatio??null,onReady:i.onReady||null,onUpdate:i.onUpdate||null},this.canvas=document.createElement("canvas");const s=this.canvas.getContext("2d");if(!s)throw new Error("Canvas context not available");this.ctx=s,this.container.appendChild(this.canvas),this.canvas.style.display="block",this.offCanvas=document.createElement("canvas");const h=this.offCanvas.getContext("2d");if(!h)throw new Error("Offscreen canvas context not available");this.offCtx=h,this.W=0,this.H=0,this.DPR=this.config.devicePixelRatio||Math.min(window.devicePixelRatio||1,1.8),this.particles=[],this.mouse={x:0,y:0,down:!1},this.animationId=null,this.isRunning=!1,this.isVisible=!1,this.intersectionObserver=null,this.debounceDelay=i.debounceDelay??150;const e=this.handleResize.bind(this);this.handleResize=n(e,this.debounceDelay),this.handleMouseMove=this.handleMouseMove.bind(this),this.handleMouseLeave=this.handleMouseLeave.bind(this),this.handleMouseDown=this.handleMouseDown.bind(this),this.handleMouseUp=this.handleMouseUp.bind(this),this.t=n(this.i.bind(this),this.debounceDelay),this.h=n(this.o.bind(this),this.debounceDelay),this.init()}init(){this.resize(),this.setupEventListeners(),this.setupIntersectionObserver(),this.config.onReady&&this.config.onReady(this)}setupIntersectionObserver(){if("undefined"==typeof window||void 0===window.IntersectionObserver)return this.isVisible=!0,void this.start();this.intersectionObserver||(this.intersectionObserver=new IntersectionObserver(t=>{for(const i of t)i.target===this.container&&(i.isIntersecting?(this.isVisible=!0,this.start()):(this.isVisible=!1,this.stop()))},{threshold:.1}),this.intersectionObserver.observe(this.container))}resize(){const t=this.config.width||this.container.clientWidth||window.innerWidth,i=this.config.height||this.container.clientHeight||.7*window.innerHeight;if(t<=0||i<=0)return;this.W=Math.floor(t*this.DPR),this.H=Math.floor(i*this.DPR);const s=4096;if(this.W>s||this.H>s){const t=Math.min(s/this.W,s/this.H);this.W=Math.floor(this.W*t),this.H=Math.floor(this.H*t),this.DPR=this.DPR*t}this.canvas.width=this.W,this.canvas.height=this.H,this.canvas.style.width=t+"px",this.canvas.style.height=i+"px",this.W>0&&this.H>0&&(this.buildTargets(),this.particles.length||this.initParticles())}buildTargets(){if(this.W<=0||this.H<=0)return;const t=this.config.text;this.offCanvas.width=this.W,this.offCanvas.height=this.H,this.offCtx.clearRect(0,0,this.offCanvas.width,this.offCanvas.height);const i=Math.min(this.W,this.H),s=this.config.fontSize||Math.max(80,Math.floor(.18*i));this.offCtx.fillStyle="#ffffff",this.offCtx.textAlign="center",this.offCtx.textBaseline="middle",this.offCtx.font=`400 ${s}px ${this.config.fontFamily}`;const h=t.split(""),e=.05*s,n=this.offCtx.measureText(t).width+e*(h.length-1);let o=this.W/2-n/2;for(const t of h)this.offCtx.fillText(t,o+this.offCtx.measureText(t).width/2,this.H/2),o+=this.offCtx.measureText(t).width+e;const a=Math.max(2,this.config.densityStep),r=this.offCtx.getImageData(0,0,this.W,this.H).data,l=[];for(let t=0;t<this.H;t+=a)for(let i=0;i<this.W;i+=a){const s=4*(t*this.W+i);r[s]+r[s+1]+r[s+2]>600&&l.push({x:i,y:t})}for(;l.length>this.config.maxParticles;)l.splice(Math.floor(Math.random()*l.length),1);if(this.particles.length<l.length){const t=l.length-this.particles.length;for(let i=0;i<t;i++)this.particles.push(this.makeParticle())}else this.particles.length>l.length&&(this.particles.length=l.length);for(let t=0;t<this.particles.length;t++){const i=this.particles[t],s=l[t];i.tx=s.x,i.ty=s.y}}makeParticle(){const t=Math.random()*this.W,i=Math.random()*this.H;return{x:t,y:i,vx:0,vy:0,tx:t,ty:i,initialX:t,initialY:i,j:Math.random()*Math.PI*2}}initParticles(){for(const t of this.particles){const i=Math.random()*this.W,s=Math.random()*this.H;t.x=i,t.y=s,t.vx=t.vy=0,t.initialX=i,t.initialY=s}}scatter(){for(const t of this.particles)void 0!==t.initialX&&void 0!==t.initialY?(t.tx=t.initialX,t.ty=t.initialY):(t.initialX=t.x,t.initialY=t.y,t.tx=t.initialX,t.ty=t.initialY)}morph(t){this.t(t)}i(t){if(0!==this.W&&0!==this.H||this.resize(),"string"==typeof t)this.config.text=t,this.buildTargets();else if(t&&"object"==typeof t){const i=void 0!==t.text;this.config={...this.config,...t},i&&this.buildTargets()}else this.buildTargets()}update(){this.ctx.clearRect(0,0,this.W,this.H);for(const t of this.particles){let i=(t.tx-t.x)*this.config.ease,s=(t.ty-t.y)*this.config.ease;if(this.mouse.x||this.mouse.y){const h=t.x-this.mouse.x,e=t.y-this.mouse.y,n=h*h+e*e,o=this.config.repelRadius*this.DPR;if(n<o*o){const t=Math.sqrt(n)+1e-4,a=(this.mouse.down?-1:1)*this.config.repelStrength*(1-t/o);i+=h/t*a*6,s+=e/t*a*6}}t.j+=2,i+=.05*Math.cos(t.j),s+=.05*Math.sin(1.3*t.j),t.vx=(t.vx+i)*Math.random(),t.vy=(t.vy+s)*Math.random(),t.x+=t.vx,t.y+=t.vy}this.ctx.fillStyle=this.config.particleColor;const t=this.config.pointSize*this.DPR;for(const i of this.particles)this.ctx.beginPath(),this.ctx.arc(i.x,i.y,t,0,2*Math.PI),this.ctx.fill();this.config.onUpdate&&this.config.onUpdate(this)}animate(){this.isRunning&&(this.update(),this.animationId=requestAnimationFrame(()=>this.animate()))}start(){this.isRunning||(this.isRunning=!0,this.animate())}stop(){this.isRunning=!1,this.animationId&&(cancelAnimationFrame(this.animationId),this.animationId=null)}setupEventListeners(){window.addEventListener("resize",this.handleResize),this.canvas.addEventListener("mousemove",this.handleMouseMove),this.canvas.addEventListener("mouseleave",this.handleMouseLeave),this.canvas.addEventListener("mousedown",this.handleMouseDown),window.addEventListener("mouseup",this.handleMouseUp)}removeEventListeners(){window.removeEventListener("resize",this.handleResize),this.canvas.removeEventListener("mousemove",this.handleMouseMove),this.canvas.removeEventListener("mouseleave",this.handleMouseLeave),this.canvas.removeEventListener("mousedown",this.handleMouseDown),window.removeEventListener("mouseup",this.handleMouseUp)}handleResize(){this.resize()}handleMouseMove(t){const i=this.canvas.getBoundingClientRect();this.mouse.x=(t.clientX-i.left)*this.DPR,this.mouse.y=(t.clientY-i.top)*this.DPR}handleMouseLeave(){this.mouse.x=this.mouse.y=0}handleMouseDown(){this.mouse.down=!0}handleMouseUp(){this.mouse.down=!1}updateConfig(t){this.h(t)}o(t){this.config={...this.config,...t},t.text&&this.buildTargets()}destroy(){this.stop(),this.removeEventListeners(),this.intersectionObserver&&(this.intersectionObserver.disconnect(),this.intersectionObserver=null),this.canvas&&this.canvas.parentNode&&this.canvas.parentNode.removeChild(this.canvas)}}const a=i((i,n)=>{const a=s(null),r=s(null);h(()=>{if(!a.current)return;let t=null,s=null;const h=()=>{const e=a.current;if(!e)return;if(r.current)return;const n=e.querySelector("canvas");n&&n.remove();const l=e.getBoundingClientRect();if(l.width<=0||l.height<=0)return void(s=window.setTimeout(h,50));const c=100;(l.width<c||l.height<c)&&(e.style.minWidth=c+"px",e.style.minHeight=c+"px");const{className:f,style:u,text:d,densityStep:p,maxParticles:m,pointSize:w,ease:v,repelRadius:M,repelStrength:x,particleColor:y,fontFamily:S,fontSize:g,width:z,height:R,devicePixelRatio:P,onReady:C,onUpdate:b}=i,E={text:d,densityStep:p,maxParticles:m,pointSize:w,ease:v,repelRadius:M,repelStrength:x,particleColor:y,fontFamily:S,fontSize:g,width:z,height:R,devicePixelRatio:P,onReady:C,onUpdate:b};r.current=new o(e,E),"undefined"!=typeof ResizeObserver&&(t=new ResizeObserver(()=>{r.current&&r.current.resize()}),t.observe(e))};return requestAnimationFrame(h),()=>{s&&(clearTimeout(s),s=null),t&&(t.disconnect(),t=null),r.current&&(r.current.destroy(),r.current=null)}},[]),h(()=>{if(!r.current)return;const{text:t,densityStep:s,maxParticles:h,pointSize:e,ease:n,repelRadius:o,repelStrength:a,particleColor:l,fontFamily:c,fontSize:f,width:u,height:d,devicePixelRatio:p}=i;r.current.updateConfig({text:t,densityStep:s,maxParticles:h,pointSize:e,ease:n,repelRadius:o,repelStrength:a,particleColor:l,fontFamily:c,fontSize:f,width:u,height:d,devicePixelRatio:p})},[i.text,i.densityStep,i.maxParticles,i.pointSize,i.ease,i.repelRadius,i.repelStrength,i.particleColor,i.fontFamily,i.fontSize,i.width,i.height,i.devicePixelRatio]),e(n,()=>({morph:t=>{r.current?r.current.morph(t):console.warn("MasonEffect: 인스턴스가 아직 초기화되지 않았습니다.")},scatter:()=>{r.current?r.current.scatter():console.warn("MasonEffect: 인스턴스가 아직 초기화되지 않았습니다.")},updateConfig:t=>{r.current?r.current.updateConfig(t):console.warn("MasonEffect: 인스턴스가 아직 초기화되지 않았습니다.")},destroy:()=>{r.current&&(r.current.destroy(),r.current=null)}}));const l={width:"100%",height:"100%",minHeight:i.height||400,position:"relative",...i.style};return t.createElement("div",{ref:a,className:i.className,style:l})});a.displayName="MasonEffect";export{a as default};
@@ -83,4 +83,3 @@ export declare class MasonEffect {
83
83
  destroy(): void;
84
84
  }
85
85
  export default MasonEffect;
86
- //# sourceMappingURL=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