masoneffect 0.1.19 → 0.1.21

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.
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,552 +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
- // 최소 크기 보장
132
- if (width <= 0 || height <= 0) {
133
- return;
134
- }
135
- this.W = Math.floor(width * this.DPR);
136
- this.H = Math.floor(height * this.DPR);
137
- // 캔버스 크기 제한 (메모리 오류 방지)
138
- // getImageData는 최대 약 268MB (4096x4096x4)까지 지원
139
- const MAX_CANVAS_SIZE = 4096;
140
- if (this.W > MAX_CANVAS_SIZE || this.H > MAX_CANVAS_SIZE) {
141
- const scale = Math.min(MAX_CANVAS_SIZE / this.W, MAX_CANVAS_SIZE / this.H);
142
- this.W = Math.floor(this.W * scale);
143
- this.H = Math.floor(this.H * scale);
144
- this.DPR = this.DPR * scale;
145
- }
146
- this.canvas.width = this.W;
147
- this.canvas.height = this.H;
148
- this.canvas.style.width = width + 'px';
149
- this.canvas.style.height = height + 'px';
150
- // 크기가 유효할 때만 buildTargets 실행
151
- if (this.W > 0 && this.H > 0) {
152
- this.buildTargets();
153
- if (!this.particles.length) {
154
- this.initParticles();
155
- }
156
- }
157
- }
158
- buildTargets() {
159
- // 크기 검증
160
- if (this.W <= 0 || this.H <= 0) {
161
- return;
162
- }
163
- const text = this.config.text;
164
- this.offCanvas.width = this.W;
165
- this.offCanvas.height = this.H;
166
- this.offCtx.clearRect(0, 0, this.offCanvas.width, this.offCanvas.height);
167
- const base = Math.min(this.W, this.H);
168
- const fontSize = this.config.fontSize || Math.max(80, Math.floor(base * 0.18));
169
- this.offCtx.fillStyle = '#ffffff';
170
- this.offCtx.textAlign = 'center';
171
- this.offCtx.textBaseline = 'middle';
172
- this.offCtx.font = `400 ${fontSize}px ${this.config.fontFamily}`;
173
- // 글자 간격 계산 및 그리기
174
- const chars = text.split('');
175
- const spacing = fontSize * 0.05;
176
- const totalWidth = this.offCtx.measureText(text).width + spacing * (chars.length - 1);
177
- let x = this.W / 2 - totalWidth / 2;
178
- for (const ch of chars) {
179
- this.offCtx.fillText(ch, x + this.offCtx.measureText(ch).width / 2, this.H / 2);
180
- x += this.offCtx.measureText(ch).width + spacing;
181
- }
182
- // 픽셀 샘플링
183
- const step = Math.max(2, this.config.densityStep);
184
- const img = this.offCtx.getImageData(0, 0, this.W, this.H).data;
185
- const targets = [];
186
- for (let y = 0; y < this.H; y += step) {
187
- for (let x = 0; x < this.W; x += step) {
188
- const i = (y * this.W + x) * 4;
189
- if (img[i] + img[i + 1] + img[i + 2] > 600) {
190
- targets.push({ x, y });
191
- }
192
- }
193
- }
194
- // 파티클 수 제한
195
- while (targets.length > this.config.maxParticles) {
196
- targets.splice(Math.floor(Math.random() * targets.length), 1);
197
- }
198
- // 파티클 수 조정
199
- if (this.particles.length < targets.length) {
200
- const need = targets.length - this.particles.length;
201
- for (let i = 0; i < need; i++) {
202
- this.particles.push(this.makeParticle());
203
- }
204
- }
205
- else if (this.particles.length > targets.length) {
206
- this.particles.length = targets.length;
207
- }
208
- // 목표 좌표 할당
209
- for (let i = 0; i < this.particles.length; i++) {
210
- const p = this.particles[i];
211
- const t = targets[i];
212
- p.tx = t.x;
213
- p.ty = t.y;
214
- }
215
- }
216
- makeParticle() {
217
- // 캔버스 전체에 골고루 분포 (여백 없이)
218
- const sx = Math.random() * this.W;
219
- const sy = Math.random() * this.H;
220
- return {
221
- x: sx,
222
- y: sy,
223
- vx: 0,
224
- vy: 0,
225
- tx: sx,
226
- ty: sy,
227
- initialX: sx, // 초기 위치 저장 (scatter 시 돌아갈 위치)
228
- initialY: sy,
229
- j: Math.random() * Math.PI * 2,
230
- };
231
- }
232
- initParticles() {
233
- // 캔버스 전체에 골고루 분포 (여백 없이)
234
- for (const p of this.particles) {
235
- const sx = Math.random() * this.W;
236
- const sy = Math.random() * this.H;
237
- p.x = sx;
238
- p.y = sy;
239
- p.vx = p.vy = 0;
240
- // 초기 위치 저장 (scatter 시 돌아갈 위치)
241
- p.initialX = sx;
242
- p.initialY = sy;
243
- }
244
- }
245
- scatter() {
246
- // 각 파티클을 초기 위치로 돌아가도록 설정
247
- for (const p of this.particles) {
248
- // 초기 위치가 저장되어 있으면 그 위치로, 없으면 현재 위치 유지
249
- if (p.initialX !== undefined && p.initialY !== undefined) {
250
- p.tx = p.initialX;
251
- p.ty = p.initialY;
252
- }
253
- else {
254
- // 초기 위치가 없으면 현재 위치를 초기 위치로 저장
255
- p.initialX = p.x;
256
- p.initialY = p.y;
257
- p.tx = p.initialX;
258
- p.ty = p.initialY;
259
- }
260
- }
261
- }
262
- morph(textOrOptions) {
263
- // 즉시 실행이 필요한 경우 (예: 초기화 시)를 위해 내부 메서드 직접 호출
264
- // 일반적인 경우에는 디바운스 적용
265
- this._debouncedMorph(textOrOptions);
266
- }
267
- _morphInternal(textOrOptions) {
268
- // W와 H가 0이면 resize 먼저 실행
269
- if (this.W === 0 || this.H === 0) {
270
- this.resize();
271
- }
272
- if (typeof textOrOptions === 'string') {
273
- // 문자열인 경우: 기존 동작 유지
274
- this.config.text = textOrOptions;
275
- this.buildTargets();
276
- }
277
- else if (textOrOptions && typeof textOrOptions === 'object') {
278
- // 객체인 경우: 텍스트와 함께 다른 설정도 변경
279
- const needsRebuild = textOrOptions.text !== undefined;
280
- this.config = { ...this.config, ...textOrOptions };
281
- if (needsRebuild) {
282
- this.buildTargets();
283
- }
284
- }
285
- else {
286
- // null이거나 undefined인 경우: 현재 텍스트로 재빌드
287
- this.buildTargets();
288
- }
289
- }
290
- update() {
291
- this.ctx.clearRect(0, 0, this.W, this.H);
292
- for (const p of this.particles) {
293
- // 목표 좌표로 당기는 힘
294
- let ax = (p.tx - p.x) * this.config.ease;
295
- let ay = (p.ty - p.y) * this.config.ease;
296
- // 마우스 반발/흡입
297
- if (this.mouse.x || this.mouse.y) {
298
- const dx = p.x - this.mouse.x;
299
- const dy = p.y - this.mouse.y;
300
- const d2 = dx * dx + dy * dy;
301
- const r = this.config.repelRadius * this.DPR;
302
- if (d2 < r * r) {
303
- const d = Math.sqrt(d2) + 0.0001;
304
- const f = (this.mouse.down ? -1 : 1) * this.config.repelStrength * (1 - d / r);
305
- ax += (dx / d) * f * 6.0;
306
- ay += (dy / d) * f * 6.0;
307
- }
308
- }
309
- // 진동 효과
310
- p.j += 2;
311
- ax += Math.cos(p.j) * 0.05;
312
- ay += Math.sin(p.j * 1.3) * 0.05;
313
- // 속도와 위치 업데이트
314
- p.vx = (p.vx + ax) * Math.random();
315
- p.vy = (p.vy + ay) * Math.random();
316
- p.x += p.vx;
317
- p.y += p.vy;
318
- }
319
- // 파티클 그리기
320
- this.ctx.fillStyle = this.config.particleColor;
321
- const r = this.config.pointSize * this.DPR;
322
- for (const p of this.particles) {
323
- this.ctx.beginPath();
324
- this.ctx.arc(p.x, p.y, r, 0, Math.PI * 2);
325
- this.ctx.fill();
326
- }
327
- if (this.config.onUpdate) {
328
- this.config.onUpdate(this);
329
- }
330
- }
331
- animate() {
332
- if (!this.isRunning)
333
- return;
334
- this.update();
335
- this.animationId = requestAnimationFrame(() => this.animate());
336
- }
337
- start() {
338
- if (this.isRunning)
339
- return;
340
- this.isRunning = true;
341
- this.animate();
342
- }
343
- stop() {
344
- this.isRunning = false;
345
- if (this.animationId) {
346
- cancelAnimationFrame(this.animationId);
347
- this.animationId = null;
348
- }
349
- }
350
- setupEventListeners() {
351
- window.addEventListener('resize', this.handleResize);
352
- this.canvas.addEventListener('mousemove', this.handleMouseMove);
353
- this.canvas.addEventListener('mouseleave', this.handleMouseLeave);
354
- this.canvas.addEventListener('mousedown', this.handleMouseDown);
355
- window.addEventListener('mouseup', this.handleMouseUp);
356
- }
357
- removeEventListeners() {
358
- window.removeEventListener('resize', this.handleResize);
359
- this.canvas.removeEventListener('mousemove', this.handleMouseMove);
360
- this.canvas.removeEventListener('mouseleave', this.handleMouseLeave);
361
- this.canvas.removeEventListener('mousedown', this.handleMouseDown);
362
- window.removeEventListener('mouseup', this.handleMouseUp);
363
- }
364
- handleResize() {
365
- this.resize();
366
- }
367
- handleMouseMove(e) {
368
- const rect = this.canvas.getBoundingClientRect();
369
- this.mouse.x = (e.clientX - rect.left) * this.DPR;
370
- this.mouse.y = (e.clientY - rect.top) * this.DPR;
371
- }
372
- handleMouseLeave() {
373
- this.mouse.x = this.mouse.y = 0;
374
- }
375
- handleMouseDown() {
376
- this.mouse.down = true;
377
- }
378
- handleMouseUp() {
379
- this.mouse.down = false;
380
- }
381
- // 설정 업데이트
382
- updateConfig(newConfig) {
383
- // 디바운스 적용
384
- this._debouncedUpdateConfig(newConfig);
385
- }
386
- _updateConfigInternal(newConfig) {
387
- this.config = { ...this.config, ...newConfig };
388
- if (newConfig.text) {
389
- this.buildTargets();
390
- }
391
- }
392
- // 파괴 및 정리
393
- destroy() {
394
- this.stop();
395
- this.removeEventListeners();
396
- if (this.intersectionObserver) {
397
- this.intersectionObserver.disconnect();
398
- this.intersectionObserver = null;
399
- }
400
- if (this.canvas && this.canvas.parentNode) {
401
- this.canvas.parentNode.removeChild(this.canvas);
402
- }
403
- }
404
- }
405
-
406
- const MasonEffectComponent = React.forwardRef((props, ref) => {
407
- const containerRef = React.useRef(null);
408
- const instanceRef = React.useRef(null);
409
- React.useEffect(() => {
410
- if (!containerRef.current)
411
- return;
412
- let resizeObserver = null;
413
- let initTimeout = null;
414
- // 컨테이너가 실제 크기를 가지도록 대기
415
- const initEffect = () => {
416
- const container = containerRef.current;
417
- if (!container)
418
- return;
419
- // 컨테이너 크기가 0이거나 너무 작으면 다음 프레임에 다시 시도
420
- const rect = container.getBoundingClientRect();
421
- if (rect.width <= 0 || rect.height <= 0) {
422
- initTimeout = window.setTimeout(initEffect, 50);
423
- return;
424
- }
425
- // 최소 크기 보장 (너무 작으면 기본값 사용)
426
- const minSize = 100;
427
- if (rect.width < minSize || rect.height < minSize) {
428
- container.style.minWidth = minSize + 'px';
429
- container.style.minHeight = minSize + 'px';
430
- }
431
- const { className, style, text, densityStep, maxParticles, pointSize, ease, repelRadius, repelStrength, particleColor, fontFamily, fontSize, width, height, devicePixelRatio, onReady, onUpdate, } = props;
432
- const options = {
433
- text,
434
- densityStep,
435
- maxParticles,
436
- pointSize,
437
- ease,
438
- repelRadius,
439
- repelStrength,
440
- particleColor,
441
- fontFamily,
442
- fontSize,
443
- width,
444
- height,
445
- devicePixelRatio,
446
- onReady,
447
- onUpdate,
448
- };
449
- instanceRef.current = new MasonEffect(container, options);
450
- // ResizeObserver로 컨테이너 크기 변경 감지
451
- if (typeof ResizeObserver !== 'undefined') {
452
- resizeObserver = new ResizeObserver(() => {
453
- if (instanceRef.current) {
454
- instanceRef.current.resize();
455
- }
456
- });
457
- resizeObserver.observe(container);
458
- }
459
- };
460
- // 다음 프레임에 초기화 (DOM이 완전히 렌더링된 후)
461
- requestAnimationFrame(initEffect);
462
- return () => {
463
- if (initTimeout) {
464
- clearTimeout(initTimeout);
465
- }
466
- if (resizeObserver) {
467
- resizeObserver.disconnect();
468
- }
469
- if (instanceRef.current) {
470
- instanceRef.current.destroy();
471
- instanceRef.current = null;
472
- }
473
- };
474
- }, []);
475
- // props 변경 시 설정 업데이트
476
- React.useEffect(() => {
477
- if (!instanceRef.current)
478
- return;
479
- const { text, densityStep, maxParticles, pointSize, ease, repelRadius, repelStrength, particleColor, fontFamily, fontSize, width, height, devicePixelRatio, } = props;
480
- instanceRef.current.updateConfig({
481
- text,
482
- densityStep,
483
- maxParticles,
484
- pointSize,
485
- ease,
486
- repelRadius,
487
- repelStrength,
488
- particleColor,
489
- fontFamily,
490
- fontSize,
491
- width,
492
- height,
493
- devicePixelRatio,
494
- });
495
- }, [
496
- props.text,
497
- props.densityStep,
498
- props.maxParticles,
499
- props.pointSize,
500
- props.ease,
501
- props.repelRadius,
502
- props.repelStrength,
503
- props.particleColor,
504
- props.fontFamily,
505
- props.fontSize,
506
- props.width,
507
- props.height,
508
- props.devicePixelRatio,
509
- ]);
510
- React.useImperativeHandle(ref, () => ({
511
- morph: (textOrOptions) => {
512
- if (!instanceRef.current) {
513
- console.warn('MasonEffect: 인스턴스가 아직 초기화되지 않았습니다.');
514
- return;
515
- }
516
- instanceRef.current.morph(textOrOptions);
517
- },
518
- scatter: () => {
519
- if (!instanceRef.current) {
520
- console.warn('MasonEffect: 인스턴스가 아직 초기화되지 않았습니다.');
521
- return;
522
- }
523
- instanceRef.current.scatter();
524
- },
525
- updateConfig: (config) => {
526
- if (!instanceRef.current) {
527
- console.warn('MasonEffect: 인스턴스가 아직 초기화되지 않았습니다.');
528
- return;
529
- }
530
- instanceRef.current.updateConfig(config);
531
- },
532
- destroy: () => {
533
- if (instanceRef.current) {
534
- instanceRef.current.destroy();
535
- instanceRef.current = null;
536
- }
537
- },
538
- }));
539
- // 기본 스타일: 컨테이너가 크기를 가지도록 함
540
- const defaultStyle = {
541
- width: '100%',
542
- height: '100%',
543
- minHeight: props.height || 400,
544
- position: 'relative',
545
- ...props.style,
546
- };
547
- return (React.createElement("div", { ref: containerRef, className: props.className, style: defaultStyle }));
548
- });
549
- MasonEffectComponent.displayName = 'MasonEffect';
550
-
551
- module.exports = MasonEffectComponent;
552
- //# 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;AAED,UAAU,gBAAiB,SAAQ,kBAAkB;IACnD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC;CAC7B;AAED,QAAA,MAAM,oBAAoB,yFAoMzB,CAAC;AAIF,eAAe,oBAAoB,CAAC"}
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;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};
@@ -0,0 +1,7 @@
1
+ 'use strict';
2
+
3
+ var MasonEffect = require('./MasonEffect');
4
+
5
+
6
+
7
+ module.exports = MasonEffect;
@@ -0,0 +1 @@
1
+ export { default } from './MasonEffect';
@@ -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;AAED,UAAU,gBAAiB,SAAQ,kBAAkB;IACnD,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,KAAK,CAAC,aAAa,CAAC;CAC7B;AAED,QAAA,MAAM,oBAAoB,yFAoMzB,CAAC;AAIF,eAAe,oBAAoB,CAAC"}
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;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,5 @@
1
+ /**
2
+ * Vue component entry point
3
+ * Exports the compiled Vue component
4
+ */
5
+ export { default } from './MasonEffect.vue';