masoneffect 0.1.0
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/LICENSE +22 -0
- package/README.md +160 -0
- package/dist/index.esm.js +318 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.esm.min.js +1 -0
- package/dist/index.js +323 -0
- package/dist/index.js.map +1 -0
- package/dist/index.min.js +1 -0
- package/dist/index.umd.js +329 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/index.umd.min.js +1 -0
- package/dist/react/MasonEffect.cjs +408 -0
- package/dist/react/MasonEffect.cjs.map +1 -0
- package/dist/react/MasonEffect.d.ts +32 -0
- package/dist/react/MasonEffect.d.ts.map +1 -0
- package/dist/react/MasonEffect.js +406 -0
- package/dist/react/MasonEffect.js.map +1 -0
- package/dist/react/MasonEffect.min.cjs +1 -0
- package/dist/react/MasonEffect.min.js +1 -0
- package/dist/react/react/MasonEffect.d.ts +32 -0
- package/dist/react/react/MasonEffect.d.ts.map +1 -0
- package/package.json +79 -0
- package/src/core/index.d.ts +59 -0
- package/src/core/index.js +319 -0
- package/src/index.js +7 -0
- package/src/react/MasonEffect.tsx +170 -0
- package/src/react/index.js +2 -0
- package/src/vue/MasonEffect.vue +190 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
# MasonEffect
|
|
2
|
+
|
|
3
|
+
파티클 모핑 효과를 제공하는 라이브러리입니다. React, Vue, 그리고 바닐라 JavaScript에서 사용할 수 있습니다.
|
|
4
|
+
|
|
5
|
+
## 설치
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install masoneffect
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## 사용법
|
|
12
|
+
|
|
13
|
+
### 바닐라 JavaScript
|
|
14
|
+
|
|
15
|
+
```javascript
|
|
16
|
+
import { MasonEffect } from 'masoneffect';
|
|
17
|
+
|
|
18
|
+
const container = document.getElementById('my-container');
|
|
19
|
+
const effect = new MasonEffect(container, {
|
|
20
|
+
text: 'Hello World',
|
|
21
|
+
particleColor: '#00ff88',
|
|
22
|
+
maxParticles: 2000,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
// 텍스트 변경
|
|
26
|
+
effect.morph('New Text');
|
|
27
|
+
|
|
28
|
+
// 파티클 흩어지기
|
|
29
|
+
effect.scatter();
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### React
|
|
33
|
+
|
|
34
|
+
```jsx
|
|
35
|
+
import React, { useRef } from 'react';
|
|
36
|
+
import MasonEffect from 'masoneffect/react';
|
|
37
|
+
|
|
38
|
+
function App() {
|
|
39
|
+
const effectRef = useRef(null);
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
<div style={{ width: '100%', height: '70vh' }}>
|
|
43
|
+
<MasonEffect
|
|
44
|
+
ref={effectRef}
|
|
45
|
+
text="Hello React"
|
|
46
|
+
particleColor="#00ff88"
|
|
47
|
+
maxParticles={2000}
|
|
48
|
+
onReady={(instance) => {
|
|
49
|
+
console.log('Ready!', instance);
|
|
50
|
+
}}
|
|
51
|
+
/>
|
|
52
|
+
<button onClick={() => effectRef.current?.morph('New Text')}>
|
|
53
|
+
Morph
|
|
54
|
+
</button>
|
|
55
|
+
<button onClick={() => effectRef.current?.scatter()}>
|
|
56
|
+
Scatter
|
|
57
|
+
</button>
|
|
58
|
+
</div>
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Vue 3
|
|
64
|
+
|
|
65
|
+
```vue
|
|
66
|
+
<template>
|
|
67
|
+
<div style="width: 100%; height: 70vh">
|
|
68
|
+
<MasonEffect
|
|
69
|
+
ref="effectRef"
|
|
70
|
+
text="Hello Vue"
|
|
71
|
+
particle-color="#00ff88"
|
|
72
|
+
:max-particles="2000"
|
|
73
|
+
@ready="onReady"
|
|
74
|
+
/>
|
|
75
|
+
<button @click="handleMorph">Morph</button>
|
|
76
|
+
<button @click="handleScatter">Scatter</button>
|
|
77
|
+
</div>
|
|
78
|
+
</template>
|
|
79
|
+
|
|
80
|
+
<script setup>
|
|
81
|
+
import { ref } from 'vue';
|
|
82
|
+
import MasonEffect from 'masoneffect/vue';
|
|
83
|
+
|
|
84
|
+
const effectRef = ref(null);
|
|
85
|
+
|
|
86
|
+
const handleMorph = () => {
|
|
87
|
+
effectRef.value?.morph('New Text');
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const handleScatter = () => {
|
|
91
|
+
effectRef.value?.scatter();
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const onReady = (instance) => {
|
|
95
|
+
console.log('Ready!', instance);
|
|
96
|
+
};
|
|
97
|
+
</script>
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## API
|
|
101
|
+
|
|
102
|
+
### 옵션
|
|
103
|
+
|
|
104
|
+
| 옵션 | 타입 | 기본값 | 설명 |
|
|
105
|
+
|------|------|--------|------|
|
|
106
|
+
| `text` | `string` | `'mason effect'` | 표시할 텍스트 |
|
|
107
|
+
| `densityStep` | `number` | `2` | 파티클 샘플링 밀도 (작을수록 촘촘함) |
|
|
108
|
+
| `maxParticles` | `number` | `3200` | 최대 파티클 수 |
|
|
109
|
+
| `pointSize` | `number` | `0.5` | 파티클 점 크기 |
|
|
110
|
+
| `ease` | `number` | `0.05` | 이동 가속도 |
|
|
111
|
+
| `repelRadius` | `number` | `150` | 마우스 반발 범위 |
|
|
112
|
+
| `repelStrength` | `number` | `1` | 마우스 반발 세기 |
|
|
113
|
+
| `particleColor` | `string` | `'#fff'` | 파티클 색상 |
|
|
114
|
+
| `fontFamily` | `string` | `'Inter, system-ui, Arial'` | 폰트 패밀리 |
|
|
115
|
+
| `fontSize` | `number \| null` | `null` | 폰트 크기 (null이면 자동) |
|
|
116
|
+
| `width` | `number \| null` | `null` | 캔버스 너비 (null이면 컨테이너 크기) |
|
|
117
|
+
| `height` | `number \| null` | `null` | 캔버스 높이 (null이면 컨테이너 크기) |
|
|
118
|
+
| `devicePixelRatio` | `number \| null` | `null` | 디바이스 픽셀 비율 (null이면 자동) |
|
|
119
|
+
| `onReady` | `function` | `null` | 초기화 완료 콜백 |
|
|
120
|
+
| `onUpdate` | `function` | `null` | 업데이트 콜백 |
|
|
121
|
+
|
|
122
|
+
### 메서드
|
|
123
|
+
|
|
124
|
+
#### `morph(text?: string)`
|
|
125
|
+
텍스트 형태로 파티클을 모핑합니다. 텍스트를 전달하면 해당 텍스트로 변경됩니다.
|
|
126
|
+
|
|
127
|
+
#### `scatter()`
|
|
128
|
+
파티클을 무작위로 흩어지게 합니다.
|
|
129
|
+
|
|
130
|
+
#### `updateConfig(config: Partial<MasonEffectOptions>)`
|
|
131
|
+
설정을 업데이트합니다.
|
|
132
|
+
|
|
133
|
+
#### `destroy()`
|
|
134
|
+
인스턴스를 파괴하고 리소스를 정리합니다.
|
|
135
|
+
|
|
136
|
+
## 특징
|
|
137
|
+
|
|
138
|
+
- 🎨 텍스트를 파티클로 변환하는 모핑 효과
|
|
139
|
+
- 🖱️ 마우스 인터랙션 지원 (반발/흡입)
|
|
140
|
+
- 📱 반응형 디자인
|
|
141
|
+
- ⚡ 고성능 Canvas 렌더링
|
|
142
|
+
- 🔧 React, Vue, 바닐라 JS 모두 지원
|
|
143
|
+
- 🎯 TypeScript 타입 정의 포함
|
|
144
|
+
|
|
145
|
+
## 개발
|
|
146
|
+
|
|
147
|
+
```bash
|
|
148
|
+
# 의존성 설치
|
|
149
|
+
npm install
|
|
150
|
+
|
|
151
|
+
# 개발 모드
|
|
152
|
+
npm run dev
|
|
153
|
+
|
|
154
|
+
# 빌드
|
|
155
|
+
npm run build
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
## 라이선스
|
|
159
|
+
|
|
160
|
+
MIT
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MasonEffect - 파티클 모핑 효과 라이브러리
|
|
3
|
+
* 바닐라 JS 코어 클래스
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
class MasonEffect {
|
|
7
|
+
constructor(container, options = {}) {
|
|
8
|
+
// 컨테이너 요소
|
|
9
|
+
this.container = typeof container === 'string'
|
|
10
|
+
? document.querySelector(container)
|
|
11
|
+
: container;
|
|
12
|
+
|
|
13
|
+
if (!this.container) {
|
|
14
|
+
throw new Error('Container element not found');
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// 설정값들
|
|
18
|
+
this.config = {
|
|
19
|
+
text: options.text || 'mason crawler',
|
|
20
|
+
densityStep: options.densityStep ?? 2,
|
|
21
|
+
maxParticles: options.maxParticles ?? 3200,
|
|
22
|
+
pointSize: options.pointSize ?? 0.5,
|
|
23
|
+
ease: options.ease ?? 0.05,
|
|
24
|
+
repelRadius: options.repelRadius ?? 150,
|
|
25
|
+
repelStrength: options.repelStrength ?? 1,
|
|
26
|
+
particleColor: options.particleColor || '#fff',
|
|
27
|
+
fontFamily: options.fontFamily || 'Inter, system-ui, Arial',
|
|
28
|
+
fontSize: options.fontSize || null, // null이면 자동 계산
|
|
29
|
+
width: options.width || null, // null이면 컨테이너 크기
|
|
30
|
+
height: options.height || null, // null이면 컨테이너 크기
|
|
31
|
+
devicePixelRatio: options.devicePixelRatio ?? null, // null이면 자동
|
|
32
|
+
onReady: options.onReady || null,
|
|
33
|
+
onUpdate: options.onUpdate || null,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// 캔버스 생성
|
|
37
|
+
this.canvas = document.createElement('canvas');
|
|
38
|
+
this.ctx = this.canvas.getContext('2d');
|
|
39
|
+
this.container.appendChild(this.canvas);
|
|
40
|
+
this.canvas.style.display = 'block';
|
|
41
|
+
|
|
42
|
+
// 오프스크린 캔버스 (텍스트 렌더링용)
|
|
43
|
+
this.offCanvas = document.createElement('canvas');
|
|
44
|
+
this.offCtx = this.offCanvas.getContext('2d');
|
|
45
|
+
|
|
46
|
+
// 상태
|
|
47
|
+
this.W = 0;
|
|
48
|
+
this.H = 0;
|
|
49
|
+
this.DPR = this.config.devicePixelRatio || Math.min(window.devicePixelRatio || 1, 1.8);
|
|
50
|
+
this.particles = [];
|
|
51
|
+
this.mouse = { x: 0, y: 0, down: false };
|
|
52
|
+
this.animationId = null;
|
|
53
|
+
this.isRunning = false;
|
|
54
|
+
|
|
55
|
+
// 이벤트 핸들러 바인딩
|
|
56
|
+
this.handleResize = this.handleResize.bind(this);
|
|
57
|
+
this.handleMouseMove = this.handleMouseMove.bind(this);
|
|
58
|
+
this.handleMouseLeave = this.handleMouseLeave.bind(this);
|
|
59
|
+
this.handleMouseDown = this.handleMouseDown.bind(this);
|
|
60
|
+
this.handleMouseUp = this.handleMouseUp.bind(this);
|
|
61
|
+
|
|
62
|
+
// 초기화
|
|
63
|
+
this.init();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
init() {
|
|
67
|
+
this.resize();
|
|
68
|
+
this.setupEventListeners();
|
|
69
|
+
this.start();
|
|
70
|
+
|
|
71
|
+
if (this.config.onReady) {
|
|
72
|
+
this.config.onReady(this);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
resize() {
|
|
77
|
+
const width = this.config.width || this.container.clientWidth || window.innerWidth;
|
|
78
|
+
const height = this.config.height || this.container.clientHeight || window.innerHeight * 0.7;
|
|
79
|
+
|
|
80
|
+
this.W = Math.floor(width * this.DPR);
|
|
81
|
+
this.H = Math.floor(height * this.DPR);
|
|
82
|
+
|
|
83
|
+
this.canvas.width = this.W;
|
|
84
|
+
this.canvas.height = this.H;
|
|
85
|
+
this.canvas.style.width = width + 'px';
|
|
86
|
+
this.canvas.style.height = height + 'px';
|
|
87
|
+
|
|
88
|
+
this.buildTargets();
|
|
89
|
+
if (!this.particles.length) {
|
|
90
|
+
this.initParticles();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
buildTargets() {
|
|
95
|
+
const text = this.config.text;
|
|
96
|
+
this.offCanvas.width = this.W;
|
|
97
|
+
this.offCanvas.height = this.H;
|
|
98
|
+
this.offCtx.clearRect(0, 0, this.offCanvas.width, this.offCanvas.height);
|
|
99
|
+
|
|
100
|
+
const base = Math.min(this.W, this.H);
|
|
101
|
+
const fontSize = this.config.fontSize || Math.max(80, Math.floor(base * 0.18));
|
|
102
|
+
this.offCtx.fillStyle = '#ffffff';
|
|
103
|
+
this.offCtx.textAlign = 'center';
|
|
104
|
+
this.offCtx.textBaseline = 'middle';
|
|
105
|
+
this.offCtx.font = `400 ${fontSize}px ${this.config.fontFamily}`;
|
|
106
|
+
|
|
107
|
+
// 글자 간격 계산 및 그리기
|
|
108
|
+
const chars = text.split('');
|
|
109
|
+
const spacing = fontSize * 0.05;
|
|
110
|
+
const totalWidth = this.offCtx.measureText(text).width + spacing * (chars.length - 1);
|
|
111
|
+
let x = this.W / 2 - totalWidth / 2;
|
|
112
|
+
|
|
113
|
+
for (const ch of chars) {
|
|
114
|
+
this.offCtx.fillText(ch, x + this.offCtx.measureText(ch).width / 2, this.H / 2);
|
|
115
|
+
x += this.offCtx.measureText(ch).width + spacing;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// 픽셀 샘플링
|
|
119
|
+
const step = Math.max(2, this.config.densityStep);
|
|
120
|
+
const img = this.offCtx.getImageData(0, 0, this.W, this.H).data;
|
|
121
|
+
const targets = [];
|
|
122
|
+
|
|
123
|
+
for (let y = 0; y < this.H; y += step) {
|
|
124
|
+
for (let x = 0; x < this.W; x += step) {
|
|
125
|
+
const i = (y * this.W + x) * 4;
|
|
126
|
+
if (img[i] + img[i + 1] + img[i + 2] > 600) {
|
|
127
|
+
targets.push({ x, y });
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// 파티클 수 제한
|
|
133
|
+
while (targets.length > this.config.maxParticles) {
|
|
134
|
+
targets.splice(Math.floor(Math.random() * targets.length), 1);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// 파티클 수 조정
|
|
138
|
+
if (this.particles.length < targets.length) {
|
|
139
|
+
const need = targets.length - this.particles.length;
|
|
140
|
+
for (let i = 0; i < need; i++) {
|
|
141
|
+
this.particles.push(this.makeParticle());
|
|
142
|
+
}
|
|
143
|
+
} else if (this.particles.length > targets.length) {
|
|
144
|
+
this.particles.length = targets.length;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// 목표 좌표 할당
|
|
148
|
+
for (let i = 0; i < this.particles.length; i++) {
|
|
149
|
+
const p = this.particles[i];
|
|
150
|
+
const t = targets[i];
|
|
151
|
+
p.tx = t.x;
|
|
152
|
+
p.ty = t.y;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
makeParticle() {
|
|
157
|
+
const m = 0.12;
|
|
158
|
+
const sx = (m + Math.random() * (1 - 2 * m)) * this.W;
|
|
159
|
+
const sy = (m + Math.random() * (1 - 2 * m)) * this.H;
|
|
160
|
+
return {
|
|
161
|
+
x: sx,
|
|
162
|
+
y: sy,
|
|
163
|
+
vx: 0,
|
|
164
|
+
vy: 0,
|
|
165
|
+
tx: sx,
|
|
166
|
+
ty: sy,
|
|
167
|
+
j: Math.random() * Math.PI * 2,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
initParticles() {
|
|
172
|
+
for (const p of this.particles) {
|
|
173
|
+
p.x = (0.12 + Math.random() * 1.76) * this.W;
|
|
174
|
+
p.y = (0.12 + Math.random() * 1.76) * this.H;
|
|
175
|
+
p.vx = p.vy = 0;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
scatter() {
|
|
180
|
+
for (const p of this.particles) {
|
|
181
|
+
p.tx = (0.12 + Math.random() * 1.76) * this.W;
|
|
182
|
+
p.ty = (0.12 + Math.random() * 1.76) * this.H;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
morph(text = null) {
|
|
187
|
+
if (text) {
|
|
188
|
+
this.config.text = text;
|
|
189
|
+
}
|
|
190
|
+
this.buildTargets();
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
update() {
|
|
194
|
+
this.ctx.clearRect(0, 0, this.W, this.H);
|
|
195
|
+
|
|
196
|
+
for (const p of this.particles) {
|
|
197
|
+
// 목표 좌표로 당기는 힘
|
|
198
|
+
let ax = (p.tx - p.x) * this.config.ease;
|
|
199
|
+
let ay = (p.ty - p.y) * this.config.ease;
|
|
200
|
+
|
|
201
|
+
// 마우스 반발/흡입
|
|
202
|
+
if (this.mouse.x || this.mouse.y) {
|
|
203
|
+
const dx = p.x - this.mouse.x;
|
|
204
|
+
const dy = p.y - this.mouse.y;
|
|
205
|
+
const d2 = dx * dx + dy * dy;
|
|
206
|
+
const r = this.config.repelRadius * this.DPR;
|
|
207
|
+
if (d2 < r * r) {
|
|
208
|
+
const d = Math.sqrt(d2) + 0.0001;
|
|
209
|
+
const f = (this.mouse.down ? -1 : 1) * this.config.repelStrength * (1 - d / r);
|
|
210
|
+
ax += (dx / d) * f * 6.0;
|
|
211
|
+
ay += (dy / d) * f * 6.0;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// 진동 효과
|
|
216
|
+
p.j += 2;
|
|
217
|
+
ax += Math.cos(p.j) * 0.05;
|
|
218
|
+
ay += Math.sin(p.j * 1.3) * 0.05;
|
|
219
|
+
|
|
220
|
+
// 속도와 위치 업데이트
|
|
221
|
+
p.vx = (p.vx + ax) * Math.random();
|
|
222
|
+
p.vy = (p.vy + ay) * Math.random();
|
|
223
|
+
p.x += p.vx;
|
|
224
|
+
p.y += p.vy;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// 파티클 그리기
|
|
228
|
+
this.ctx.fillStyle = this.config.particleColor;
|
|
229
|
+
const r = this.config.pointSize * this.DPR;
|
|
230
|
+
for (const p of this.particles) {
|
|
231
|
+
this.ctx.beginPath();
|
|
232
|
+
this.ctx.arc(p.x, p.y, r, 0, Math.PI * 2);
|
|
233
|
+
this.ctx.fill();
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
if (this.config.onUpdate) {
|
|
237
|
+
this.config.onUpdate(this);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
animate() {
|
|
242
|
+
if (!this.isRunning) return;
|
|
243
|
+
this.update();
|
|
244
|
+
this.animationId = requestAnimationFrame(() => this.animate());
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
start() {
|
|
248
|
+
if (this.isRunning) return;
|
|
249
|
+
this.isRunning = true;
|
|
250
|
+
this.animate();
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
stop() {
|
|
254
|
+
this.isRunning = false;
|
|
255
|
+
if (this.animationId) {
|
|
256
|
+
cancelAnimationFrame(this.animationId);
|
|
257
|
+
this.animationId = null;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
setupEventListeners() {
|
|
262
|
+
window.addEventListener('resize', this.handleResize);
|
|
263
|
+
this.canvas.addEventListener('mousemove', this.handleMouseMove);
|
|
264
|
+
this.canvas.addEventListener('mouseleave', this.handleMouseLeave);
|
|
265
|
+
this.canvas.addEventListener('mousedown', this.handleMouseDown);
|
|
266
|
+
window.addEventListener('mouseup', this.handleMouseUp);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
removeEventListeners() {
|
|
270
|
+
window.removeEventListener('resize', this.handleResize);
|
|
271
|
+
this.canvas.removeEventListener('mousemove', this.handleMouseMove);
|
|
272
|
+
this.canvas.removeEventListener('mouseleave', this.handleMouseLeave);
|
|
273
|
+
this.canvas.removeEventListener('mousedown', this.handleMouseDown);
|
|
274
|
+
window.removeEventListener('mouseup', this.handleMouseUp);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
handleResize() {
|
|
278
|
+
this.resize();
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
handleMouseMove(e) {
|
|
282
|
+
const rect = this.canvas.getBoundingClientRect();
|
|
283
|
+
this.mouse.x = (e.clientX - rect.left) * this.DPR;
|
|
284
|
+
this.mouse.y = (e.clientY - rect.top) * this.DPR;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
handleMouseLeave() {
|
|
288
|
+
this.mouse.x = this.mouse.y = 0;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
handleMouseDown() {
|
|
292
|
+
this.mouse.down = true;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
handleMouseUp() {
|
|
296
|
+
this.mouse.down = false;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// 설정 업데이트
|
|
300
|
+
updateConfig(newConfig) {
|
|
301
|
+
this.config = { ...this.config, ...newConfig };
|
|
302
|
+
if (newConfig.text) {
|
|
303
|
+
this.buildTargets();
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// 파괴 및 정리
|
|
308
|
+
destroy() {
|
|
309
|
+
this.stop();
|
|
310
|
+
this.removeEventListeners();
|
|
311
|
+
if (this.canvas && this.canvas.parentNode) {
|
|
312
|
+
this.canvas.parentNode.removeChild(this.canvas);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
export { MasonEffect, MasonEffect as default };
|
|
318
|
+
//# sourceMappingURL=index.esm.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":["../src/core/index.js"],"sourcesContent":["/**\n * MasonEffect - 파티클 모핑 효과 라이브러리\n * 바닐라 JS 코어 클래스\n */\n\nexport class MasonEffect {\n constructor(container, options = {}) {\n // 컨테이너 요소\n this.container = typeof container === 'string' \n ? document.querySelector(container) \n : container;\n \n if (!this.container) {\n throw new Error('Container element not found');\n }\n\n // 설정값들\n this.config = {\n text: options.text || 'mason crawler',\n densityStep: options.densityStep ?? 2,\n maxParticles: options.maxParticles ?? 3200,\n pointSize: options.pointSize ?? 0.5,\n ease: options.ease ?? 0.05,\n repelRadius: options.repelRadius ?? 150,\n repelStrength: options.repelStrength ?? 1,\n particleColor: options.particleColor || '#fff',\n fontFamily: options.fontFamily || 'Inter, system-ui, Arial',\n fontSize: options.fontSize || null, // null이면 자동 계산\n width: options.width || null, // null이면 컨테이너 크기\n height: options.height || null, // null이면 컨테이너 크기\n devicePixelRatio: options.devicePixelRatio ?? null, // null이면 자동\n onReady: options.onReady || null,\n onUpdate: options.onUpdate || null,\n };\n\n // 캔버스 생성\n this.canvas = document.createElement('canvas');\n this.ctx = this.canvas.getContext('2d');\n this.container.appendChild(this.canvas);\n this.canvas.style.display = 'block';\n\n // 오프스크린 캔버스 (텍스트 렌더링용)\n this.offCanvas = document.createElement('canvas');\n this.offCtx = this.offCanvas.getContext('2d');\n\n // 상태\n this.W = 0;\n this.H = 0;\n this.DPR = this.config.devicePixelRatio || Math.min(window.devicePixelRatio || 1, 1.8);\n this.particles = [];\n this.mouse = { x: 0, y: 0, down: false };\n this.animationId = null;\n this.isRunning = false;\n\n // 이벤트 핸들러 바인딩\n this.handleResize = this.handleResize.bind(this);\n this.handleMouseMove = this.handleMouseMove.bind(this);\n this.handleMouseLeave = this.handleMouseLeave.bind(this);\n this.handleMouseDown = this.handleMouseDown.bind(this);\n this.handleMouseUp = this.handleMouseUp.bind(this);\n\n // 초기화\n this.init();\n }\n\n init() {\n this.resize();\n this.setupEventListeners();\n this.start();\n \n if (this.config.onReady) {\n this.config.onReady(this);\n }\n }\n\n resize() {\n const width = this.config.width || this.container.clientWidth || window.innerWidth;\n const height = this.config.height || this.container.clientHeight || window.innerHeight * 0.7;\n \n this.W = Math.floor(width * this.DPR);\n this.H = Math.floor(height * this.DPR);\n \n this.canvas.width = this.W;\n this.canvas.height = this.H;\n this.canvas.style.width = width + 'px';\n this.canvas.style.height = height + 'px';\n\n this.buildTargets();\n if (!this.particles.length) {\n this.initParticles();\n }\n }\n\n buildTargets() {\n const text = this.config.text;\n this.offCanvas.width = this.W;\n this.offCanvas.height = this.H;\n this.offCtx.clearRect(0, 0, this.offCanvas.width, this.offCanvas.height);\n\n const base = Math.min(this.W, this.H);\n const fontSize = this.config.fontSize || Math.max(80, Math.floor(base * 0.18));\n this.offCtx.fillStyle = '#ffffff';\n this.offCtx.textAlign = 'center';\n this.offCtx.textBaseline = 'middle';\n this.offCtx.font = `400 ${fontSize}px ${this.config.fontFamily}`;\n\n // 글자 간격 계산 및 그리기\n const chars = text.split('');\n const spacing = fontSize * 0.05;\n const totalWidth = this.offCtx.measureText(text).width + spacing * (chars.length - 1);\n let x = this.W / 2 - totalWidth / 2;\n \n for (const ch of chars) {\n this.offCtx.fillText(ch, x + this.offCtx.measureText(ch).width / 2, this.H / 2);\n x += this.offCtx.measureText(ch).width + spacing;\n }\n\n // 픽셀 샘플링\n const step = Math.max(2, this.config.densityStep);\n const img = this.offCtx.getImageData(0, 0, this.W, this.H).data;\n const targets = [];\n \n for (let y = 0; y < this.H; y += step) {\n for (let x = 0; x < this.W; x += step) {\n const i = (y * this.W + x) * 4;\n if (img[i] + img[i + 1] + img[i + 2] > 600) {\n targets.push({ x, y });\n }\n }\n }\n\n // 파티클 수 제한\n while (targets.length > this.config.maxParticles) {\n targets.splice(Math.floor(Math.random() * targets.length), 1);\n }\n\n // 파티클 수 조정\n if (this.particles.length < targets.length) {\n const need = targets.length - this.particles.length;\n for (let i = 0; i < need; i++) {\n this.particles.push(this.makeParticle());\n }\n } else if (this.particles.length > targets.length) {\n this.particles.length = targets.length;\n }\n\n // 목표 좌표 할당\n for (let i = 0; i < this.particles.length; i++) {\n const p = this.particles[i];\n const t = targets[i];\n p.tx = t.x;\n p.ty = t.y;\n }\n }\n\n makeParticle() {\n const m = 0.12;\n const sx = (m + Math.random() * (1 - 2 * m)) * this.W;\n const sy = (m + Math.random() * (1 - 2 * m)) * this.H;\n return {\n x: sx,\n y: sy,\n vx: 0,\n vy: 0,\n tx: sx,\n ty: sy,\n j: Math.random() * Math.PI * 2,\n };\n }\n\n initParticles() {\n for (const p of this.particles) {\n p.x = (0.12 + Math.random() * 1.76) * this.W;\n p.y = (0.12 + Math.random() * 1.76) * this.H;\n p.vx = p.vy = 0;\n }\n }\n\n scatter() {\n for (const p of this.particles) {\n p.tx = (0.12 + Math.random() * 1.76) * this.W;\n p.ty = (0.12 + Math.random() * 1.76) * this.H;\n }\n }\n\n morph(text = null) {\n if (text) {\n this.config.text = text;\n }\n this.buildTargets();\n }\n\n update() {\n this.ctx.clearRect(0, 0, this.W, this.H);\n\n for (const p of this.particles) {\n // 목표 좌표로 당기는 힘\n let ax = (p.tx - p.x) * this.config.ease;\n let ay = (p.ty - p.y) * this.config.ease;\n\n // 마우스 반발/흡입\n if (this.mouse.x || this.mouse.y) {\n const dx = p.x - this.mouse.x;\n const dy = p.y - this.mouse.y;\n const d2 = dx * dx + dy * dy;\n const r = this.config.repelRadius * this.DPR;\n if (d2 < r * r) {\n const d = Math.sqrt(d2) + 0.0001;\n const f = (this.mouse.down ? -1 : 1) * this.config.repelStrength * (1 - d / r);\n ax += (dx / d) * f * 6.0;\n ay += (dy / d) * f * 6.0;\n }\n }\n\n // 진동 효과\n p.j += 2;\n ax += Math.cos(p.j) * 0.05;\n ay += Math.sin(p.j * 1.3) * 0.05;\n\n // 속도와 위치 업데이트\n p.vx = (p.vx + ax) * Math.random();\n p.vy = (p.vy + ay) * Math.random();\n p.x += p.vx;\n p.y += p.vy;\n }\n\n // 파티클 그리기\n this.ctx.fillStyle = this.config.particleColor;\n const r = this.config.pointSize * this.DPR;\n for (const p of this.particles) {\n this.ctx.beginPath();\n this.ctx.arc(p.x, p.y, r, 0, Math.PI * 2);\n this.ctx.fill();\n }\n\n if (this.config.onUpdate) {\n this.config.onUpdate(this);\n }\n }\n\n animate() {\n if (!this.isRunning) return;\n this.update();\n this.animationId = requestAnimationFrame(() => this.animate());\n }\n\n start() {\n if (this.isRunning) return;\n this.isRunning = true;\n this.animate();\n }\n\n stop() {\n this.isRunning = false;\n if (this.animationId) {\n cancelAnimationFrame(this.animationId);\n this.animationId = null;\n }\n }\n\n setupEventListeners() {\n window.addEventListener('resize', this.handleResize);\n this.canvas.addEventListener('mousemove', this.handleMouseMove);\n this.canvas.addEventListener('mouseleave', this.handleMouseLeave);\n this.canvas.addEventListener('mousedown', this.handleMouseDown);\n window.addEventListener('mouseup', this.handleMouseUp);\n }\n\n removeEventListeners() {\n window.removeEventListener('resize', this.handleResize);\n this.canvas.removeEventListener('mousemove', this.handleMouseMove);\n this.canvas.removeEventListener('mouseleave', this.handleMouseLeave);\n this.canvas.removeEventListener('mousedown', this.handleMouseDown);\n window.removeEventListener('mouseup', this.handleMouseUp);\n }\n\n handleResize() {\n this.resize();\n }\n\n handleMouseMove(e) {\n const rect = this.canvas.getBoundingClientRect();\n this.mouse.x = (e.clientX - rect.left) * this.DPR;\n this.mouse.y = (e.clientY - rect.top) * this.DPR;\n }\n\n handleMouseLeave() {\n this.mouse.x = this.mouse.y = 0;\n }\n\n handleMouseDown() {\n this.mouse.down = true;\n }\n\n handleMouseUp() {\n this.mouse.down = false;\n }\n\n // 설정 업데이트\n updateConfig(newConfig) {\n this.config = { ...this.config, ...newConfig };\n if (newConfig.text) {\n this.buildTargets();\n }\n }\n\n // 파괴 및 정리\n destroy() {\n this.stop();\n this.removeEventListeners();\n if (this.canvas && this.canvas.parentNode) {\n this.canvas.parentNode.removeChild(this.canvas);\n }\n }\n}\n\n// 기본 export\nexport default MasonEffect;\n\n"],"names":[],"mappings":"AAAA;AACA;AACA;AACA;;AAEO,MAAM,WAAW,CAAC;AACzB,EAAE,WAAW,CAAC,SAAS,EAAE,OAAO,GAAG,EAAE,EAAE;AACvC;AACA,IAAI,IAAI,CAAC,SAAS,GAAG,OAAO,SAAS,KAAK,QAAQ;AAClD,QAAQ,QAAQ,CAAC,aAAa,CAAC,SAAS,CAAC;AACzC,QAAQ,SAAS;AACjB;AACA,IAAI,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;AACzB,MAAM,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC;AACpD,IAAI;;AAEJ;AACA,IAAI,IAAI,CAAC,MAAM,GAAG;AAClB,MAAM,IAAI,EAAE,OAAO,CAAC,IAAI,IAAI,eAAe;AAC3C,MAAM,WAAW,EAAE,OAAO,CAAC,WAAW,IAAI,CAAC;AAC3C,MAAM,YAAY,EAAE,OAAO,CAAC,YAAY,IAAI,IAAI;AAChD,MAAM,SAAS,EAAE,OAAO,CAAC,SAAS,IAAI,GAAG;AACzC,MAAM,IAAI,EAAE,OAAO,CAAC,IAAI,IAAI,IAAI;AAChC,MAAM,WAAW,EAAE,OAAO,CAA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AI,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE;AAC/C,MAAM,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC;AACrD,IAAI;AACJ,EAAE;AACF;;;;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
class t{constructor(t,s={}){if(this.container="string"==typeof t?document.querySelector(t):t,!this.container)throw Error("Container element not found");this.config={text:s.text||"mason crawler",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"),this.ctx=this.canvas.getContext("2d"),this.container.appendChild(this.canvas),this.canvas.style.display="block",this.offCanvas=document.createElement("canvas"),this.offCtx=this.offCanvas.getContext("2d"),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.handleResize=this.handleResize.bind(this),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.init()}init(){this.resize(),this.setupEventListeners(),this.start(),this.config.onReady&&this.config.onReady(this)}resize(){const t=this.config.width||this.container.clientWidth||window.innerWidth,s=this.config.height||this.container.clientHeight||.7*window.innerHeight;this.W=Math.floor(t*this.DPR),this.H=Math.floor(s*this.DPR),this.canvas.width=this.W,this.canvas.height=this.H,this.canvas.style.width=t+"px",this.canvas.style.height=s+"px",this.buildTargets(),this.particles.length||this.initParticles()}buildTargets(){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),l=this.offCtx.getImageData(0,0,this.W,this.H).data,r=[];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);l[i]+l[i+1]+l[i+2]>600&&r.push({x:s,y:t})}for(;r.length>this.config.maxParticles;)r.splice(Math.floor(Math.random()*r.length),1);if(this.particles.length<r.length){const t=r.length-this.particles.length;for(let s=0;t>s;s++)this.particles.push(this.makeParticle())}else this.particles.length>r.length&&(this.particles.length=r.length);for(let t=0;t<this.particles.length;t++){const s=this.particles[t],i=r[t];s.tx=i.x,s.ty=i.y}}makeParticle(){const t=(.12+.76*Math.random())*this.W,s=(.12+.76*Math.random())*this.H;return{x:t,y:s,vx:0,vy:0,tx:t,ty:s,j:Math.random()*Math.PI*2}}initParticles(){for(const t of this.particles)t.x=(.12+1.76*Math.random())*this.W,t.y=(.12+1.76*Math.random())*this.H,t.vx=t.vy=0}scatter(){for(const t of this.particles)t.tx=(.12+1.76*Math.random())*this.W,t.ty=(.12+1.76*Math.random())*this.H}morph(t=null){t&&(this.config.text=t),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(n*n>o){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.config={...this.config,...t},t.text&&this.buildTargets()}destroy(){this.stop(),this.removeEventListeners(),this.canvas&&this.canvas.parentNode&&this.canvas.parentNode.removeChild(this.canvas)}}export{t as MasonEffect,t as default};
|