masoneffect 0.1.13 → 0.1.14

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.
@@ -4,423 +4,381 @@ import React, { forwardRef, useRef, useEffect, useImperativeHandle } from 'react
4
4
  * MasonEffect - 파티클 모핑 효과 라이브러리
5
5
  * 바닐라 JS 코어 클래스
6
6
  */
7
-
8
7
  // 디바운스 유틸리티 함수
9
8
  function debounce(func, wait) {
10
- let timeout;
11
- return function executedFunction(...args) {
12
- const later = () => {
13
- clearTimeout(timeout);
14
- func.apply(this, args);
9
+ let timeout = null;
10
+ return function executedFunction(...args) {
11
+ const later = () => {
12
+ timeout = null;
13
+ func.apply(this, args);
14
+ };
15
+ if (timeout !== null) {
16
+ clearTimeout(timeout);
17
+ }
18
+ timeout = setTimeout(later, wait);
15
19
  };
16
- clearTimeout(timeout);
17
- timeout = setTimeout(later, wait);
18
- };
19
20
  }
20
-
21
21
  class MasonEffect {
22
- constructor(container, options = {}) {
23
- // 컨테이너 요소
24
- this.container = typeof container === 'string'
25
- ? document.querySelector(container)
26
- : container;
27
-
28
- if (!this.container) {
29
- throw new Error('Container element not found');
30
- }
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, // null이면 자동 계산
44
- width: options.width || null, // null이면 컨테이너 크기
45
- height: options.height || null, // null이면 컨테이너 크기
46
- devicePixelRatio: options.devicePixelRatio ?? null, // null이면 자동
47
- onReady: options.onReady || null,
48
- onUpdate: options.onUpdate || null,
49
- };
50
-
51
- // 캔버스 생성
52
- this.canvas = document.createElement('canvas');
53
- this.ctx = this.canvas.getContext('2d');
54
- this.container.appendChild(this.canvas);
55
- this.canvas.style.display = 'block';
56
-
57
- // 오프스크린 캔버스 (텍스트 렌더링용)
58
- this.offCanvas = document.createElement('canvas');
59
- this.offCtx = this.offCanvas.getContext('2d');
60
-
61
- // 상태
62
- this.W = 0;
63
- this.H = 0;
64
- this.DPR = this.config.devicePixelRatio || Math.min(window.devicePixelRatio || 1, 1.8);
65
- this.particles = [];
66
- this.mouse = { x: 0, y: 0, down: false };
67
- this.animationId = null;
68
- this.isRunning = false;
69
- this.isVisible = false;
70
- this.intersectionObserver = null;
71
-
72
- // 디바운스 설정 (ms)
73
- this.debounceDelay = options.debounceDelay ?? 150; // 기본 150ms
74
-
75
- // 이벤트 핸들러 바인딩 (디바운스 적용 전에 바인딩)
76
- const boundHandleResize = this.handleResize.bind(this);
77
- this.handleResize = debounce(boundHandleResize, this.debounceDelay);
78
- this.handleMouseMove = this.handleMouseMove.bind(this);
79
- this.handleMouseLeave = this.handleMouseLeave.bind(this);
80
- this.handleMouseDown = this.handleMouseDown.bind(this);
81
- this.handleMouseUp = this.handleMouseUp.bind(this);
82
-
83
- // morph와 updateConfig를 위한 디바운스된 내부 메서드
84
- this._debouncedMorph = debounce(this._morphInternal.bind(this), this.debounceDelay);
85
- this._debouncedUpdateConfig = debounce(this._updateConfigInternal.bind(this), this.debounceDelay);
86
-
87
- // 초기화
88
- this.init();
89
- }
90
-
91
- init() {
92
- this.resize();
93
- this.setupEventListeners();
94
- this.setupIntersectionObserver();
95
-
96
- if (this.config.onReady) {
97
- this.config.onReady(this);
98
- }
99
- }
100
-
101
- setupIntersectionObserver() {
102
- // IntersectionObserver가 지원되지 않는 환경에서는 항상 재생
103
- if (typeof window === 'undefined' || typeof window.IntersectionObserver === 'undefined') {
104
- this.isVisible = true;
105
- this.start();
106
- return;
22
+ constructor(container, options = {}) {
23
+ // 컨테이너 요소
24
+ this.container = typeof container === 'string'
25
+ ? document.querySelector(container)
26
+ : container;
27
+ if (!this.container) {
28
+ throw new Error('Container element not found');
29
+ }
30
+ // 설정값들
31
+ this.config = {
32
+ text: options.text || 'mason effect',
33
+ densityStep: options.densityStep ?? 2,
34
+ maxParticles: options.maxParticles ?? 3200,
35
+ pointSize: options.pointSize ?? 0.5,
36
+ ease: options.ease ?? 0.05,
37
+ repelRadius: options.repelRadius ?? 150,
38
+ repelStrength: options.repelStrength ?? 1,
39
+ particleColor: options.particleColor || '#fff',
40
+ fontFamily: options.fontFamily || 'Inter, system-ui, Arial',
41
+ fontSize: options.fontSize || null,
42
+ width: options.width || null,
43
+ height: options.height || null,
44
+ devicePixelRatio: options.devicePixelRatio ?? null,
45
+ onReady: options.onReady || null,
46
+ onUpdate: options.onUpdate || null,
47
+ };
48
+ // 캔버스 생성
49
+ this.canvas = document.createElement('canvas');
50
+ const ctx = this.canvas.getContext('2d');
51
+ if (!ctx) {
52
+ throw new Error('Canvas context not available');
53
+ }
54
+ this.ctx = ctx;
55
+ this.container.appendChild(this.canvas);
56
+ this.canvas.style.display = 'block';
57
+ // 오프스크린 캔버스 (텍스트 렌더링용)
58
+ this.offCanvas = document.createElement('canvas');
59
+ const offCtx = this.offCanvas.getContext('2d');
60
+ if (!offCtx) {
61
+ throw new Error('Offscreen canvas context not available');
62
+ }
63
+ this.offCtx = offCtx;
64
+ // 상태
65
+ this.W = 0;
66
+ this.H = 0;
67
+ this.DPR = this.config.devicePixelRatio || Math.min(window.devicePixelRatio || 1, 1.8);
68
+ this.particles = [];
69
+ this.mouse = { x: 0, y: 0, down: false };
70
+ this.animationId = null;
71
+ this.isRunning = false;
72
+ this.isVisible = false;
73
+ this.intersectionObserver = null;
74
+ // 디바운스 설정 (ms)
75
+ this.debounceDelay = options.debounceDelay ?? 150;
76
+ // 이벤트 핸들러 바인딩 (디바운스 적용 전에 바인딩)
77
+ const boundHandleResize = this.handleResize.bind(this);
78
+ this.handleResize = debounce(boundHandleResize, this.debounceDelay);
79
+ this.handleMouseMove = this.handleMouseMove.bind(this);
80
+ this.handleMouseLeave = this.handleMouseLeave.bind(this);
81
+ this.handleMouseDown = this.handleMouseDown.bind(this);
82
+ this.handleMouseUp = this.handleMouseUp.bind(this);
83
+ // morph와 updateConfig를 위한 디바운스된 내부 메서드
84
+ this._debouncedMorph = debounce(this._morphInternal.bind(this), this.debounceDelay);
85
+ this._debouncedUpdateConfig = debounce(this._updateConfigInternal.bind(this), this.debounceDelay);
86
+ // 초기화
87
+ this.init();
107
88
  }
108
-
109
- // 이미 설정되어 있다면 다시 만들지 않음
110
- if (this.intersectionObserver) {
111
- return;
89
+ init() {
90
+ this.resize();
91
+ this.setupEventListeners();
92
+ this.setupIntersectionObserver();
93
+ if (this.config.onReady) {
94
+ this.config.onReady(this);
95
+ }
112
96
  }
113
-
114
- this.intersectionObserver = new IntersectionObserver(
115
- (entries) => {
116
- for (const entry of entries) {
117
- if (entry.target !== this.container) continue;
118
-
119
- if (entry.isIntersecting) {
97
+ setupIntersectionObserver() {
98
+ // IntersectionObserver가 지원되지 않는 환경에서는 항상 재생
99
+ if (typeof window === 'undefined' || typeof window.IntersectionObserver === 'undefined') {
120
100
  this.isVisible = true;
121
101
  this.start();
122
- } else {
123
- this.isVisible = false;
124
- this.stop();
125
- }
102
+ return;
126
103
  }
127
- },
128
- {
129
- threshold: 0.1, // 10% 이상 보일 때 동작
130
- }
131
- );
132
-
133
- this.intersectionObserver.observe(this.container);
134
- }
135
-
136
- resize() {
137
- const width = this.config.width || this.container.clientWidth || window.innerWidth;
138
- const height = this.config.height || this.container.clientHeight || window.innerHeight * 0.7;
139
-
140
- this.W = Math.floor(width * this.DPR);
141
- this.H = Math.floor(height * this.DPR);
142
-
143
- this.canvas.width = this.W;
144
- this.canvas.height = this.H;
145
- this.canvas.style.width = width + 'px';
146
- this.canvas.style.height = height + 'px';
147
-
148
- this.buildTargets();
149
- if (!this.particles.length) {
150
- this.initParticles();
104
+ // 이미 설정되어 있다면 다시 만들지 않음
105
+ if (this.intersectionObserver) {
106
+ return;
107
+ }
108
+ this.intersectionObserver = new IntersectionObserver((entries) => {
109
+ for (const entry of entries) {
110
+ if (entry.target !== this.container)
111
+ continue;
112
+ if (entry.isIntersecting) {
113
+ this.isVisible = true;
114
+ this.start();
115
+ }
116
+ else {
117
+ this.isVisible = false;
118
+ this.stop();
119
+ }
120
+ }
121
+ }, {
122
+ threshold: 0.1, // 10% 이상 보일 때 동작
123
+ });
124
+ this.intersectionObserver.observe(this.container);
151
125
  }
152
- }
153
-
154
- buildTargets() {
155
- const text = this.config.text;
156
- this.offCanvas.width = this.W;
157
- this.offCanvas.height = this.H;
158
- this.offCtx.clearRect(0, 0, this.offCanvas.width, this.offCanvas.height);
159
-
160
- const base = Math.min(this.W, this.H);
161
- const fontSize = this.config.fontSize || Math.max(80, Math.floor(base * 0.18));
162
- this.offCtx.fillStyle = '#ffffff';
163
- this.offCtx.textAlign = 'center';
164
- this.offCtx.textBaseline = 'middle';
165
- this.offCtx.font = `400 ${fontSize}px ${this.config.fontFamily}`;
166
-
167
- // 글자 간격 계산 및 그리기
168
- const chars = text.split('');
169
- const spacing = fontSize * 0.05;
170
- const totalWidth = this.offCtx.measureText(text).width + spacing * (chars.length - 1);
171
- let x = this.W / 2 - totalWidth / 2;
172
-
173
- for (const ch of chars) {
174
- this.offCtx.fillText(ch, x + this.offCtx.measureText(ch).width / 2, this.H / 2);
175
- x += this.offCtx.measureText(ch).width + spacing;
126
+ resize() {
127
+ const width = this.config.width || this.container.clientWidth || window.innerWidth;
128
+ const height = this.config.height || this.container.clientHeight || window.innerHeight * 0.7;
129
+ this.W = Math.floor(width * this.DPR);
130
+ this.H = Math.floor(height * this.DPR);
131
+ this.canvas.width = this.W;
132
+ this.canvas.height = this.H;
133
+ this.canvas.style.width = width + 'px';
134
+ this.canvas.style.height = height + 'px';
135
+ this.buildTargets();
136
+ if (!this.particles.length) {
137
+ this.initParticles();
138
+ }
176
139
  }
177
-
178
- // 픽셀 샘플링
179
- const step = Math.max(2, this.config.densityStep);
180
- const img = this.offCtx.getImageData(0, 0, this.W, this.H).data;
181
- const targets = [];
182
-
183
- for (let y = 0; y < this.H; y += step) {
184
- for (let x = 0; x < this.W; x += step) {
185
- const i = (y * this.W + x) * 4;
186
- if (img[i] + img[i + 1] + img[i + 2] > 600) {
187
- targets.push({ x, y });
140
+ buildTargets() {
141
+ const text = this.config.text;
142
+ this.offCanvas.width = this.W;
143
+ this.offCanvas.height = this.H;
144
+ this.offCtx.clearRect(0, 0, this.offCanvas.width, this.offCanvas.height);
145
+ const base = Math.min(this.W, this.H);
146
+ const fontSize = this.config.fontSize || Math.max(80, Math.floor(base * 0.18));
147
+ this.offCtx.fillStyle = '#ffffff';
148
+ this.offCtx.textAlign = 'center';
149
+ this.offCtx.textBaseline = 'middle';
150
+ this.offCtx.font = `400 ${fontSize}px ${this.config.fontFamily}`;
151
+ // 글자 간격 계산 및 그리기
152
+ const chars = text.split('');
153
+ const spacing = fontSize * 0.05;
154
+ const totalWidth = this.offCtx.measureText(text).width + spacing * (chars.length - 1);
155
+ let x = this.W / 2 - totalWidth / 2;
156
+ for (const ch of chars) {
157
+ this.offCtx.fillText(ch, x + this.offCtx.measureText(ch).width / 2, this.H / 2);
158
+ x += this.offCtx.measureText(ch).width + spacing;
159
+ }
160
+ // 픽셀 샘플링
161
+ const step = Math.max(2, this.config.densityStep);
162
+ const img = this.offCtx.getImageData(0, 0, this.W, this.H).data;
163
+ const targets = [];
164
+ for (let y = 0; y < this.H; y += step) {
165
+ for (let x = 0; x < this.W; x += step) {
166
+ const i = (y * this.W + x) * 4;
167
+ if (img[i] + img[i + 1] + img[i + 2] > 600) {
168
+ targets.push({ x, y });
169
+ }
170
+ }
171
+ }
172
+ // 파티클 수 제한
173
+ while (targets.length > this.config.maxParticles) {
174
+ targets.splice(Math.floor(Math.random() * targets.length), 1);
175
+ }
176
+ // 파티클 수 조정
177
+ if (this.particles.length < targets.length) {
178
+ const need = targets.length - this.particles.length;
179
+ for (let i = 0; i < need; i++) {
180
+ this.particles.push(this.makeParticle());
181
+ }
182
+ }
183
+ else if (this.particles.length > targets.length) {
184
+ this.particles.length = targets.length;
185
+ }
186
+ // 목표 좌표 할당
187
+ for (let i = 0; i < this.particles.length; i++) {
188
+ const p = this.particles[i];
189
+ const t = targets[i];
190
+ p.tx = t.x;
191
+ p.ty = t.y;
188
192
  }
189
- }
190
193
  }
191
-
192
- // 파티클 제한
193
- while (targets.length > this.config.maxParticles) {
194
- targets.splice(Math.floor(Math.random() * targets.length), 1);
194
+ makeParticle() {
195
+ // 캔버스 전체에 골고루 분포 (여백 없이)
196
+ const sx = Math.random() * this.W;
197
+ const sy = Math.random() * this.H;
198
+ return {
199
+ x: sx,
200
+ y: sy,
201
+ vx: 0,
202
+ vy: 0,
203
+ tx: sx,
204
+ ty: sy,
205
+ initialX: sx, // 초기 위치 저장 (scatter 시 돌아갈 위치)
206
+ initialY: sy,
207
+ j: Math.random() * Math.PI * 2,
208
+ };
195
209
  }
196
-
197
- // 파티클 조정
198
- if (this.particles.length < targets.length) {
199
- const need = targets.length - this.particles.length;
200
- for (let i = 0; i < need; i++) {
201
- this.particles.push(this.makeParticle());
202
- }
203
- } else if (this.particles.length > targets.length) {
204
- this.particles.length = targets.length;
210
+ initParticles() {
211
+ // 캔버스 전체에 골고루 분포 (여백 없이)
212
+ for (const p of this.particles) {
213
+ const sx = Math.random() * this.W;
214
+ const sy = Math.random() * this.H;
215
+ p.x = sx;
216
+ p.y = sy;
217
+ p.vx = p.vy = 0;
218
+ // 초기 위치 저장 (scatter 시 돌아갈 위치)
219
+ p.initialX = sx;
220
+ p.initialY = sy;
221
+ }
205
222
  }
206
-
207
- // 목표 좌표 할당
208
- for (let i = 0; i < this.particles.length; i++) {
209
- const p = this.particles[i];
210
- const t = targets[i];
211
- p.tx = t.x;
212
- p.ty = t.y;
223
+ scatter() {
224
+ // 파티클을 초기 위치로 돌아가도록 설정
225
+ for (const p of this.particles) {
226
+ // 초기 위치가 저장되어 있으면 그 위치로, 없으면 현재 위치 유지
227
+ if (p.initialX !== undefined && p.initialY !== undefined) {
228
+ p.tx = p.initialX;
229
+ p.ty = p.initialY;
230
+ }
231
+ else {
232
+ // 초기 위치가 없으면 현재 위치를 초기 위치로 저장
233
+ p.initialX = p.x;
234
+ p.initialY = p.y;
235
+ p.tx = p.initialX;
236
+ p.ty = p.initialY;
237
+ }
238
+ }
213
239
  }
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
-
233
- initParticles() {
234
- // 캔버스 전체에 골고루 분포 (여백 없이)
235
- for (const p of this.particles) {
236
- const sx = Math.random() * this.W;
237
- const sy = Math.random() * this.H;
238
- p.x = sx;
239
- p.y = sy;
240
- p.vx = p.vy = 0;
241
- // 초기 위치 저장 (scatter 시 돌아갈 위치)
242
- p.initialX = sx;
243
- p.initialY = sy;
240
+ morph(textOrOptions) {
241
+ // 즉시 실행이 필요한 경우 (예: 초기화 시)를 위해 내부 메서드 직접 호출
242
+ // 일반적인 경우에는 디바운스 적용
243
+ this._debouncedMorph(textOrOptions);
244
244
  }
245
- }
246
-
247
- scatter() {
248
- // 각 파티클을 초기 위치로 돌아가도록 설정
249
- for (const p of this.particles) {
250
- // 초기 위치가 저장되어 있으면 그 위치로, 없으면 현재 위치 유지
251
- if (p.initialX !== undefined && p.initialY !== undefined) {
252
- p.tx = p.initialX;
253
- p.ty = p.initialY;
254
- } else {
255
- // 초기 위치가 없으면 현재 위치를 초기 위치로 저장
256
- p.initialX = p.x;
257
- p.initialY = p.y;
258
- p.tx = p.initialX;
259
- p.ty = p.initialY;
260
- }
245
+ _morphInternal(textOrOptions) {
246
+ // W와 H가 0이면 resize 먼저 실행
247
+ if (this.W === 0 || this.H === 0) {
248
+ this.resize();
249
+ }
250
+ if (typeof textOrOptions === 'string') {
251
+ // 문자열인 경우: 기존 동작 유지
252
+ this.config.text = textOrOptions;
253
+ this.buildTargets();
254
+ }
255
+ else if (textOrOptions && typeof textOrOptions === 'object') {
256
+ // 객체인 경우: 텍스트와 함께 다른 설정도 변경
257
+ const needsRebuild = textOrOptions.text !== undefined;
258
+ this.config = { ...this.config, ...textOrOptions };
259
+ if (needsRebuild) {
260
+ this.buildTargets();
261
+ }
262
+ }
263
+ else {
264
+ // null이거나 undefined인 경우: 현재 텍스트로 재빌드
265
+ this.buildTargets();
266
+ }
261
267
  }
262
- }
263
-
264
- morph(textOrOptions = null) {
265
- // 즉시 실행이 필요한 경우 (예: 초기화 시)를 위해 내부 메서드 직접 호출
266
- // 일반적인 경우에는 디바운스 적용
267
- this._debouncedMorph(textOrOptions);
268
- }
269
-
270
- _morphInternal(textOrOptions = null) {
271
- // W와 H가 0이면 resize 먼저 실행
272
- if (this.W === 0 || this.H === 0) {
273
- this.resize();
268
+ update() {
269
+ this.ctx.clearRect(0, 0, this.W, this.H);
270
+ for (const p of this.particles) {
271
+ // 목표 좌표로 당기는
272
+ let ax = (p.tx - p.x) * this.config.ease;
273
+ let ay = (p.ty - p.y) * this.config.ease;
274
+ // 마우스 반발/흡입
275
+ if (this.mouse.x || this.mouse.y) {
276
+ const dx = p.x - this.mouse.x;
277
+ const dy = p.y - this.mouse.y;
278
+ const d2 = dx * dx + dy * dy;
279
+ const r = this.config.repelRadius * this.DPR;
280
+ if (d2 < r * r) {
281
+ const d = Math.sqrt(d2) + 0.0001;
282
+ const f = (this.mouse.down ? -1 : 1) * this.config.repelStrength * (1 - d / r);
283
+ ax += (dx / d) * f * 6.0;
284
+ ay += (dy / d) * f * 6.0;
285
+ }
286
+ }
287
+ // 진동 효과
288
+ p.j += 2;
289
+ ax += Math.cos(p.j) * 0.05;
290
+ ay += Math.sin(p.j * 1.3) * 0.05;
291
+ // 속도와 위치 업데이트
292
+ p.vx = (p.vx + ax) * Math.random();
293
+ p.vy = (p.vy + ay) * Math.random();
294
+ p.x += p.vx;
295
+ p.y += p.vy;
296
+ }
297
+ // 파티클 그리기
298
+ this.ctx.fillStyle = this.config.particleColor;
299
+ const r = this.config.pointSize * this.DPR;
300
+ for (const p of this.particles) {
301
+ this.ctx.beginPath();
302
+ this.ctx.arc(p.x, p.y, r, 0, Math.PI * 2);
303
+ this.ctx.fill();
304
+ }
305
+ if (this.config.onUpdate) {
306
+ this.config.onUpdate(this);
307
+ }
274
308
  }
275
-
276
- if (typeof textOrOptions === 'string') {
277
- // 문자열인 경우: 기존 동작 유지
278
- this.config.text = textOrOptions;
279
- this.buildTargets();
280
- } else if (textOrOptions && typeof textOrOptions === 'object') {
281
- // 객체인 경우: 텍스트와 함께 다른 설정도 변경
282
- const needsRebuild = textOrOptions.text !== undefined;
283
- this.config = { ...this.config, ...textOrOptions };
284
- if (needsRebuild) {
285
- this.buildTargets();
286
- }
287
- } else {
288
- // null이거나 undefined인 경우: 현재 텍스트로 재빌드
289
- this.buildTargets();
309
+ animate() {
310
+ if (!this.isRunning)
311
+ return;
312
+ this.update();
313
+ this.animationId = requestAnimationFrame(() => this.animate());
290
314
  }
291
- }
292
-
293
- update() {
294
- this.ctx.clearRect(0, 0, this.W, this.H);
295
-
296
- for (const p of this.particles) {
297
- // 목표 좌표로 당기는 힘
298
- let ax = (p.tx - p.x) * this.config.ease;
299
- let ay = (p.ty - p.y) * this.config.ease;
300
-
301
- // 마우스 반발/흡입
302
- if (this.mouse.x || this.mouse.y) {
303
- const dx = p.x - this.mouse.x;
304
- const dy = p.y - this.mouse.y;
305
- const d2 = dx * dx + dy * dy;
306
- const r = this.config.repelRadius * this.DPR;
307
- if (d2 < r * r) {
308
- const d = Math.sqrt(d2) + 0.0001;
309
- const f = (this.mouse.down ? -1 : 1) * this.config.repelStrength * (1 - d / r);
310
- ax += (dx / d) * f * 6.0;
311
- ay += (dy / d) * f * 6.0;
315
+ start() {
316
+ if (this.isRunning)
317
+ return;
318
+ this.isRunning = true;
319
+ this.animate();
320
+ }
321
+ stop() {
322
+ this.isRunning = false;
323
+ if (this.animationId) {
324
+ cancelAnimationFrame(this.animationId);
325
+ this.animationId = null;
312
326
  }
313
- }
314
-
315
- // 진동 효과
316
- p.j += 2;
317
- ax += Math.cos(p.j) * 0.05;
318
- ay += Math.sin(p.j * 1.3) * 0.05;
319
-
320
- // 속도와 위치 업데이트
321
- p.vx = (p.vx + ax) * Math.random();
322
- p.vy = (p.vy + ay) * Math.random();
323
- p.x += p.vx;
324
- p.y += p.vy;
325
327
  }
326
-
327
- // 파티클 그리기
328
- this.ctx.fillStyle = this.config.particleColor;
329
- const r = this.config.pointSize * this.DPR;
330
- for (const p of this.particles) {
331
- this.ctx.beginPath();
332
- this.ctx.arc(p.x, p.y, r, 0, Math.PI * 2);
333
- this.ctx.fill();
328
+ setupEventListeners() {
329
+ window.addEventListener('resize', this.handleResize);
330
+ this.canvas.addEventListener('mousemove', this.handleMouseMove);
331
+ this.canvas.addEventListener('mouseleave', this.handleMouseLeave);
332
+ this.canvas.addEventListener('mousedown', this.handleMouseDown);
333
+ window.addEventListener('mouseup', this.handleMouseUp);
334
334
  }
335
-
336
- if (this.config.onUpdate) {
337
- this.config.onUpdate(this);
335
+ removeEventListeners() {
336
+ window.removeEventListener('resize', this.handleResize);
337
+ this.canvas.removeEventListener('mousemove', this.handleMouseMove);
338
+ this.canvas.removeEventListener('mouseleave', this.handleMouseLeave);
339
+ this.canvas.removeEventListener('mousedown', this.handleMouseDown);
340
+ window.removeEventListener('mouseup', this.handleMouseUp);
338
341
  }
339
- }
340
-
341
- animate() {
342
- if (!this.isRunning) return;
343
- this.update();
344
- this.animationId = requestAnimationFrame(() => this.animate());
345
- }
346
-
347
- start() {
348
- if (this.isRunning) return;
349
- this.isRunning = true;
350
- this.animate();
351
- }
352
-
353
- stop() {
354
- this.isRunning = false;
355
- if (this.animationId) {
356
- cancelAnimationFrame(this.animationId);
357
- this.animationId = null;
342
+ handleResize() {
343
+ this.resize();
358
344
  }
359
- }
360
-
361
- setupEventListeners() {
362
- window.addEventListener('resize', this.handleResize);
363
- this.canvas.addEventListener('mousemove', this.handleMouseMove);
364
- this.canvas.addEventListener('mouseleave', this.handleMouseLeave);
365
- this.canvas.addEventListener('mousedown', this.handleMouseDown);
366
- window.addEventListener('mouseup', this.handleMouseUp);
367
- }
368
-
369
- removeEventListeners() {
370
- window.removeEventListener('resize', this.handleResize);
371
- this.canvas.removeEventListener('mousemove', this.handleMouseMove);
372
- this.canvas.removeEventListener('mouseleave', this.handleMouseLeave);
373
- this.canvas.removeEventListener('mousedown', this.handleMouseDown);
374
- window.removeEventListener('mouseup', this.handleMouseUp);
375
- }
376
-
377
- handleResize() {
378
- this.resize();
379
- }
380
-
381
- handleMouseMove(e) {
382
- const rect = this.canvas.getBoundingClientRect();
383
- this.mouse.x = (e.clientX - rect.left) * this.DPR;
384
- this.mouse.y = (e.clientY - rect.top) * this.DPR;
385
- }
386
-
387
- handleMouseLeave() {
388
- this.mouse.x = this.mouse.y = 0;
389
- }
390
-
391
- handleMouseDown() {
392
- this.mouse.down = true;
393
- }
394
-
395
- handleMouseUp() {
396
- this.mouse.down = false;
397
- }
398
-
399
- // 설정 업데이트
400
- updateConfig(newConfig) {
401
- // 디바운스 적용
402
- this._debouncedUpdateConfig(newConfig);
403
- }
404
-
405
- _updateConfigInternal(newConfig) {
406
- this.config = { ...this.config, ...newConfig };
407
- if (newConfig.text) {
408
- this.buildTargets();
345
+ handleMouseMove(e) {
346
+ const rect = this.canvas.getBoundingClientRect();
347
+ this.mouse.x = (e.clientX - rect.left) * this.DPR;
348
+ this.mouse.y = (e.clientY - rect.top) * this.DPR;
409
349
  }
410
- }
411
-
412
- // 파괴 및 정리
413
- destroy() {
414
- this.stop();
415
- this.removeEventListeners();
416
- if (this.intersectionObserver) {
417
- this.intersectionObserver.disconnect();
418
- this.intersectionObserver = null;
350
+ handleMouseLeave() {
351
+ this.mouse.x = this.mouse.y = 0;
352
+ }
353
+ handleMouseDown() {
354
+ this.mouse.down = true;
419
355
  }
420
- if (this.canvas && this.canvas.parentNode) {
421
- this.canvas.parentNode.removeChild(this.canvas);
356
+ handleMouseUp() {
357
+ this.mouse.down = false;
358
+ }
359
+ // 설정 업데이트
360
+ updateConfig(newConfig) {
361
+ // 디바운스 적용
362
+ this._debouncedUpdateConfig(newConfig);
363
+ }
364
+ _updateConfigInternal(newConfig) {
365
+ this.config = { ...this.config, ...newConfig };
366
+ if (newConfig.text) {
367
+ this.buildTargets();
368
+ }
369
+ }
370
+ // 파괴 및 정리
371
+ destroy() {
372
+ this.stop();
373
+ this.removeEventListeners();
374
+ if (this.intersectionObserver) {
375
+ this.intersectionObserver.disconnect();
376
+ this.intersectionObserver = null;
377
+ }
378
+ if (this.canvas && this.canvas.parentNode) {
379
+ this.canvas.parentNode.removeChild(this.canvas);
380
+ }
422
381
  }
423
- }
424
382
  }
425
383
 
426
384
  const MasonEffectComponent = forwardRef((props, ref) => {