masoneffect 0.1.13 → 0.1.15

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