@andresclua/perlindom 0.0.1
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/PerlinDOM.es.js +98 -0
- package/dist/PerlinDOM.umd.js +1 -0
- package/docs/examples.html +385 -0
- package/package.json +33 -0
- package/public/vite.svg +1 -0
- package/readme.md +271 -0
- package/src/examples.html +515 -0
- package/src/index.html +655 -0
- package/src/js/PerlinDom.js +215 -0
- package/src/js/PerlinSVG.js +190 -0
- package/src/js/animated-button.js +40 -0
- package/src/js/basic-examples.js +43 -0
- package/src/js/index.js +13 -0
- package/src/js/interactive-controls.js +43 -0
- package/src/js/pause-play.js +33 -0
- package/src/js/triangle-animation.js +48 -0
- package/src/scss/style.scss +20 -0
- package/vite.config.js +36 -0
package/readme.md
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
# PerlinDOM
|
|
2
|
+
|
|
3
|
+
A lightweight JavaScript library for creating organic and fluid animations on DOM elements using Perlin noise.
|
|
4
|
+
|
|
5
|
+
## Description
|
|
6
|
+
|
|
7
|
+
PerlinDOM allows you to animate HTML elements with natural and organic movements based on Perlin noise. This technique creates movements that appear random but are smooth and predictable, perfect for subtle and attractive visual effects.
|
|
8
|
+
|
|
9
|
+
## Methods
|
|
10
|
+
|
|
11
|
+
| Method | Description |
|
|
12
|
+
|--------|-------------|
|
|
13
|
+
| `play()` | Starts or resumes the animation |
|
|
14
|
+
| `pause()` | Pauses the animation |
|
|
15
|
+
| `destroy()` | Stops the animation and cleans up resources |
|
|
16
|
+
| `init(seed)` | Reinitializes the animation with an optional new seed |
|
|
17
|
+
|
|
18
|
+
## Features
|
|
19
|
+
|
|
20
|
+
- **Organic animations**: Natural movements that avoid the rigidity of traditional animations
|
|
21
|
+
- **Customizable**: Control over movement ranges, speed, and noise seed
|
|
22
|
+
- **Lightweight**: Minimal implementation with no external dependencies
|
|
23
|
+
- **Easy to use**: Simple API to implement complex effects
|
|
24
|
+
- **Optimized performance**: Uses requestAnimationFrame for smooth animations
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm install @andresclua/PerlinDOM
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
## Basic Usage
|
|
34
|
+
|
|
35
|
+
```javascript
|
|
36
|
+
import PerlinDOM from '@andresclua/PerlinDOM';
|
|
37
|
+
|
|
38
|
+
// Create a basic animation
|
|
39
|
+
const animation = new PerlinDOM({
|
|
40
|
+
element: document.querySelector('.my-element'),
|
|
41
|
+
x: { min: -50, max: 50 }, // Horizontal movement range in pixels
|
|
42
|
+
y: { min: -50, max: 50 }, // Vertical movement range in pixels
|
|
43
|
+
speed: 0.01, // Animation speed
|
|
44
|
+
seed: 123 // Seed for the noise generator
|
|
45
|
+
});
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Configuration Options
|
|
49
|
+
|
|
50
|
+
| Option | Type | Description |
|
|
51
|
+
|--------|------|-------------|
|
|
52
|
+
| `element` | HTMLElement | The DOM element to animate |
|
|
53
|
+
| `x` | Object or null | Horizontal movement configuration: `{min: value, max: value}`. Use `null` to disable horizontal movement |
|
|
54
|
+
| `y` | Object or null | Vertical movement configuration: `{min: value, max: value}`. Use `null` to disable vertical movement |
|
|
55
|
+
| `speed` | Number | Animation speed (smaller values = slower movement) |
|
|
56
|
+
| `seed` | Number | Seed for the Perlin noise generator |
|
|
57
|
+
| `lerpSpeed` | Number | Interpolation speed for smooth transitions (default: 0.1) |
|
|
58
|
+
|
|
59
|
+
## Examples
|
|
60
|
+
|
|
61
|
+
### 1. Basic Animation
|
|
62
|
+
|
|
63
|
+
Create an element with smooth movement on both axes.
|
|
64
|
+
|
|
65
|
+
```javascript
|
|
66
|
+
const basicAnimation = new PerlinDOM({
|
|
67
|
+
element: document.querySelector('.element'),
|
|
68
|
+
x: { min: -50, max: 50 },
|
|
69
|
+
y: { min: -50, max: 50 },
|
|
70
|
+
speed: 0.01,
|
|
71
|
+
seed: 123
|
|
72
|
+
});
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### 2. Horizontal-Only Movement
|
|
76
|
+
|
|
77
|
+
Animate an element only on the horizontal axis.
|
|
78
|
+
|
|
79
|
+
```javascript
|
|
80
|
+
const horizontalAnimation = new PerlinDOM({
|
|
81
|
+
element: document.querySelector('.horizontal-element'),
|
|
82
|
+
x: { min: -100, max: 100 },
|
|
83
|
+
y: null, // No vertical movement
|
|
84
|
+
speed: 0.02
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### 3. Multiple Elements with Different Seeds
|
|
89
|
+
|
|
90
|
+
Animate multiple elements with different patterns.
|
|
91
|
+
|
|
92
|
+
```javascript
|
|
93
|
+
document.querySelectorAll('.particle').forEach((el, index) => {
|
|
94
|
+
new PerlinDOM({
|
|
95
|
+
element: el,
|
|
96
|
+
x: { min: -100, max: 100 },
|
|
97
|
+
y: { min: -100, max: 100 },
|
|
98
|
+
speed: 0.01,
|
|
99
|
+
seed: index * 100 // Different seed for each element
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### 4. Background Effect
|
|
105
|
+
|
|
106
|
+
Create a dynamic background with slowly moving elements.
|
|
107
|
+
|
|
108
|
+
```javascript
|
|
109
|
+
document.querySelectorAll('.bg-element').forEach(el => {
|
|
110
|
+
new PerlinDOM({
|
|
111
|
+
element: el,
|
|
112
|
+
x: { min: -10, max: 10 },
|
|
113
|
+
y: { min: -10, max: 10 },
|
|
114
|
+
speed: 0.002, // Very slow movement
|
|
115
|
+
seed: Math.random() * 2000
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### 5. Pause and Play with Mouse Events
|
|
121
|
+
|
|
122
|
+
Control the animation with mouse events.
|
|
123
|
+
|
|
124
|
+
```javascript
|
|
125
|
+
const pausePlayAnimation = new PerlinDOM({
|
|
126
|
+
element: document.querySelector('.pause-element'),
|
|
127
|
+
x: { min: -80, max: 80 },
|
|
128
|
+
y: { min: -80, max: 80 },
|
|
129
|
+
speed: 0.01,
|
|
130
|
+
seed: 42,
|
|
131
|
+
lerpSpeed: 0.05 // Controls how smooth the transition is
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
// Pause on mouse leave
|
|
135
|
+
document.querySelector('.container').addEventListener('mouseleave', () => {
|
|
136
|
+
pausePlayAnimation.pause();
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
// Play on mouse enter
|
|
140
|
+
document.querySelector('.container').addEventListener('mouseenter', () => {
|
|
141
|
+
pausePlayAnimation.play();
|
|
142
|
+
});
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### 6. Interactive Controls
|
|
146
|
+
|
|
147
|
+
Allow the user to adjust animation parameters.
|
|
148
|
+
|
|
149
|
+
```javascript
|
|
150
|
+
// Create the animation with initial settings
|
|
151
|
+
const interactiveAnimation = new PerlinDOM({
|
|
152
|
+
element: document.querySelector('.interactive-element'),
|
|
153
|
+
x: { min: -50, max: 50 },
|
|
154
|
+
y: { min: -50, max: 50 },
|
|
155
|
+
speed: 0.01,
|
|
156
|
+
seed: 0
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// Update settings based on user input
|
|
160
|
+
speedControl.addEventListener('input', () => {
|
|
161
|
+
interactiveAnimation.speed = parseFloat(speedControl.value);
|
|
162
|
+
interactiveAnimation.init(); // Restart with new settings
|
|
163
|
+
});
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### 7. Animated Button with Circles
|
|
167
|
+
|
|
168
|
+
Create a button with animated hover effects.
|
|
169
|
+
|
|
170
|
+
```javascript
|
|
171
|
+
// Get all the circle elements inside the button
|
|
172
|
+
const circleElements = document.querySelectorAll('.animated-button .circle');
|
|
173
|
+
let circleAnimations = [];
|
|
174
|
+
|
|
175
|
+
// Function to initialize animations
|
|
176
|
+
function initCircleAnimations() {
|
|
177
|
+
// Clear existing animations
|
|
178
|
+
circleAnimations.forEach(anim => anim.destroy());
|
|
179
|
+
circleAnimations = [];
|
|
180
|
+
|
|
181
|
+
// Create new animations for each circle
|
|
182
|
+
circleElements.forEach((el, index) => {
|
|
183
|
+
const animation = new PerlinDOM({
|
|
184
|
+
element: el,
|
|
185
|
+
x: { min: -15, max: 15 },
|
|
186
|
+
y: { min: -15, max: 15 },
|
|
187
|
+
speed: 0.03,
|
|
188
|
+
seed: index * 1000,
|
|
189
|
+
lerpSpeed: 0.1
|
|
190
|
+
});
|
|
191
|
+
circleAnimations.push(animation);
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Add event listeners to the button
|
|
196
|
+
document.querySelector('.animated-button').addEventListener('mouseenter', () => {
|
|
197
|
+
initCircleAnimations();
|
|
198
|
+
circleAnimations.forEach(anim => anim.play());
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
document.querySelector('.animated-button').addEventListener('mouseleave', () => {
|
|
202
|
+
circleAnimations.forEach(anim => anim.pause());
|
|
203
|
+
});
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### 8. Triangle with Animated Vertex
|
|
207
|
+
|
|
208
|
+
Use PerlinDOM to animate a vertex of an SVG polygon.
|
|
209
|
+
|
|
210
|
+
```javascript
|
|
211
|
+
// Get the polygon element
|
|
212
|
+
const trianglePath = document.getElementById('trianglePath');
|
|
213
|
+
|
|
214
|
+
// Store the original points
|
|
215
|
+
const originalPoints = {
|
|
216
|
+
x1: 150, y1: 50, // Top vertex
|
|
217
|
+
x2: 250, y2: 250, // Bottom right
|
|
218
|
+
x3: 50, y3: 250 // Bottom left
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
// Create a custom animation function
|
|
222
|
+
function animateTriangle() {
|
|
223
|
+
// Create a PerlinDOM instance for the top vertex
|
|
224
|
+
const topVertexAnimation = new PerlinDOM({
|
|
225
|
+
element: null, // We're not moving a DOM element directly
|
|
226
|
+
x: { min: -30, max: 30 },
|
|
227
|
+
y: { min: -20, max: 20 },
|
|
228
|
+
speed: 0.005,
|
|
229
|
+
seed: 42
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
// Animation function
|
|
233
|
+
function updateTriangle() {
|
|
234
|
+
// Get the current noise values
|
|
235
|
+
const offsetX = topVertexAnimation.lastX;
|
|
236
|
+
const offsetY = topVertexAnimation.lastY;
|
|
237
|
+
|
|
238
|
+
// Update the polygon points
|
|
239
|
+
const newPoints = `${originalPoints.x1 + offsetX},${originalPoints.y1 + offsetY} ${originalPoints.x2},${originalPoints.y2} ${originalPoints.x3},${originalPoints.y3}`;
|
|
240
|
+
trianglePath.setAttribute('points', newPoints);
|
|
241
|
+
|
|
242
|
+
// Continue animation
|
|
243
|
+
requestAnimationFrame(updateTriangle);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// Start the animation
|
|
247
|
+
updateTriangle();
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Initialize the animation
|
|
251
|
+
animateTriangle();
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
## Use Cases
|
|
255
|
+
|
|
256
|
+
- **Background elements**: Create dynamic and organic backgrounds
|
|
257
|
+
- **Hover effects**: Enhance the interactivity of buttons and links
|
|
258
|
+
- **Data visualizations**: Add subtle movement to charts and visualizations
|
|
259
|
+
- **Decorative elements**: Bring decorative elements to life on your website
|
|
260
|
+
- **User interfaces**: Create more dynamic and attractive interfaces
|
|
261
|
+
|
|
262
|
+
## Compatibility
|
|
263
|
+
|
|
264
|
+
PerlinDOM works in all modern browsers that support:
|
|
265
|
+
- ES6 (ECMAScript 2015)
|
|
266
|
+
- requestAnimationFrame
|
|
267
|
+
- CSS transform
|
|
268
|
+
|
|
269
|
+
## License
|
|
270
|
+
|
|
271
|
+
MIT
|