@andresclua/perlindom 0.0.1 → 0.0.3

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/readme.md CHANGED
@@ -1,271 +1,351 @@
1
1
  # PerlinDOM
2
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
3
+ [![npm version](https://badge.fury.io/js/@andresclua%2Fperlindom.svg)](https://badge.fury.io/js/@andresclua%2Fperlindom)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+ [![JavaScript](https://img.shields.io/badge/JavaScript-ES6+-yellow.svg)](https://developer.mozilla.org/en-US/docs/Web/JavaScript)
6
+
7
+ A lightweight JavaScript library for creating smooth, organic animations on DOM elements using Perlin noise. Create natural, fluid movements that bring your web interfaces to life.
8
+
9
+ ## 📋 Table of Contents
10
+
11
+ - [Features](#-features)
12
+ - [Installation](#-installation)
13
+ - [Quick Start](#-quick-start)
14
+ - [Configuration](#-configuration)
15
+ - [Examples](#-examples)
16
+ - [Best Practices](#-best-practices)
17
+ - [API Reference](#-api-reference)
18
+ - [Troubleshooting](#-troubleshooting)
19
+ - [Browser Compatibility](#-browser-compatibility)
20
+ - [Changelog](#-changelog)
21
+ - [License](#-license)
22
+
23
+ ## ✨ Features
24
+
25
+ - **🌊 Organic Animations**: Natural movements using Perlin noise that avoid mechanical rigidity
26
+ - **📱 Responsive Design**: Support for percentage-based values that adapt to container size
27
+ - **🎯 Smart Positioning**: Automatically works from element's current position (CSS or inline styles)
28
+ - **⚡ High Performance**: Uses `transform` and `requestAnimationFrame` for smooth 60fps animations
29
+ - **🎛️ Full Control**: Play, pause, and destroy animations with smooth transitions
30
+ - **📦 Lightweight**: Minimal footprint with zero dependencies
31
+ - **🔧 Easy Integration**: Simple API that works with any DOM element
32
+ - **🎨 Flexible**: Mix pixel and percentage values, animate on single or both axes
33
+
34
+ ## 📦 Installation
27
35
 
28
36
  ```bash
29
- npm install @andresclua/PerlinDOM
37
+ npm install @andresclua/perlindom
30
38
  ```
31
39
 
40
+ Or include directly in your HTML:
32
41
 
33
- ## Basic Usage
42
+ ```html
43
+ <script type="module">
44
+ import PerlinDOM from 'https://unpkg.com/@andresclua/perlindom/dist/perlindom.es.js';
45
+ </script>
46
+ ```
47
+
48
+ ## 🚀 Quick Start
34
49
 
35
50
  ```javascript
36
- import PerlinDOM from '@andresclua/PerlinDOM';
51
+ import PerlinDOM from '@andresclua/perlindom';
37
52
 
38
- // Create a basic animation
53
+ // Basic animation - element will animate from its current position
39
54
  const animation = new PerlinDOM({
40
55
  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
56
+ x: { min: -50, max: 50 }, // Move ±50px horizontally
57
+ y: { min: -30, max: 30 }, // Move ±30px vertically
43
58
  speed: 0.01, // Animation speed
44
- seed: 123 // Seed for the noise generator
59
+ seed: 123 // Reproducible pattern
45
60
  });
61
+
62
+ // Control the animation
63
+ animation.pause(); // Smooth pause
64
+ animation.play(); // Resume
65
+ animation.destroy(); // Clean up
46
66
  ```
47
67
 
48
- ## Configuration Options
68
+ ```css
69
+ /* Your element needs positioning for transforms to work */
70
+ .my-element {
71
+ position: relative; /* or absolute, or fixed */
72
+ /* PerlinDOM will animate from wherever this element is positioned */
73
+ }
74
+ ```
49
75
 
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) |
76
+ ## ⚙️ Configuration
58
77
 
59
- ## Examples
78
+ | Option | Type | Default | Description |
79
+ |--------|------|---------|-------------|
80
+ | `element` | `HTMLElement` | **required** | The DOM element to animate |
81
+ | `x` | `Object \| null` | `null` | Horizontal movement: `{min: number, max: number}` |
82
+ | `y` | `Object \| null` | `null` | Vertical movement: `{min: number, max: number}` |
83
+ | `speed` | `number` | `0.01` | Animation speed (lower = slower) |
84
+ | `seed` | `number` | `0` | Seed for reproducible patterns |
85
+ | `lerpSpeed` | `number` | `0.1` | Transition smoothness for pause/play |
60
86
 
61
- ### 1. Basic Animation
87
+ ### Value Types
62
88
 
63
- Create an element with smooth movement on both axes.
89
+ - **Pixels**: `{ min: -50, max: 50 }` - Fixed pixel movement
90
+ - **Percentages**: `{ min: '-10%', max: '10%' }` - Responsive to parent size
91
+ - **Mixed**: `x: { min: '-5%', max: '5%' }, y: { min: -20, max: 20 }`
92
+ - **Single Axis**: Set unused axis to `null`
64
93
 
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
- ```
94
+ ## 📚 Examples
95
+
96
+ ### 1. Basic Element Animation
74
97
 
75
- ### 2. Horizontal-Only Movement
98
+ ```html
99
+ <div class="floating-card">Content</div>
100
+ ```
76
101
 
77
- Animate an element only on the horizontal axis.
102
+ ```css
103
+ .floating-card {
104
+ position: relative;
105
+ left: 50%;
106
+ top: 50%;
107
+ transform: translate(-50%, -50%);
108
+ }
109
+ ```
78
110
 
79
111
  ```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
112
+ new PerlinDOM({
113
+ element: document.querySelector('.floating-card'),
114
+ x: { min: -20, max: 20 },
115
+ y: { min: -15, max: 15 },
116
+ speed: 0.008,
117
+ seed: 42
85
118
  });
86
119
  ```
87
120
 
88
- ### 3. Multiple Elements with Different Seeds
89
-
90
- Animate multiple elements with different patterns.
121
+ ### 2. Responsive Background Elements
91
122
 
92
123
  ```javascript
93
- document.querySelectorAll('.particle').forEach((el, index) => {
124
+ // Elements adapt to container size changes
125
+ document.querySelectorAll('.bg-particle').forEach((el, index) => {
94
126
  new PerlinDOM({
95
127
  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
128
+ x: { min: '-15%', max: '15%' }, // 15% of parent width
129
+ y: { min: '-10%', max: '10%' }, // 10% of parent height
130
+ speed: 0.003,
131
+ seed: index * 1000
100
132
  });
101
133
  });
102
134
  ```
103
135
 
104
- ### 4. Background Effect
136
+ ### 3. Interactive Hover Effects
137
+
138
+ ```javascript
139
+ const button = document.querySelector('.animated-button');
140
+ let animation;
141
+
142
+ button.addEventListener('mouseenter', () => {
143
+ animation = new PerlinDOM({
144
+ element: button.querySelector('.effect'),
145
+ x: { min: -5, max: 5 },
146
+ y: { min: -5, max: 5 },
147
+ speed: 0.02,
148
+ lerpSpeed: 0.15
149
+ });
150
+ });
105
151
 
106
- Create a dynamic background with slowly moving elements.
152
+ button.addEventListener('mouseleave', () => {
153
+ animation?.pause();
154
+ });
155
+ ```
156
+
157
+ ### 4. Multiple Elements with Synchronized Patterns
107
158
 
108
159
  ```javascript
109
- document.querySelectorAll('.bg-element').forEach(el => {
160
+ // Same seed = synchronized movement
161
+ document.querySelectorAll('.sync-element').forEach(el => {
110
162
  new PerlinDOM({
111
163
  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
164
+ x: { min: -30, max: 30 },
165
+ y: { min: -30, max: 30 },
166
+ speed: 0.01,
167
+ seed: 12345 // Same seed for all elements
116
168
  });
117
169
  });
118
170
  ```
119
171
 
120
- ### 5. Pause and Play with Mouse Events
121
-
122
- Control the animation with mouse events.
172
+ ### 5. Custom Animation Patterns
123
173
 
124
174
  ```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();
175
+ // Horizontal-only movement for sliding effects
176
+ new PerlinDOM({
177
+ element: document.querySelector('.slider'),
178
+ x: { min: -100, max: 100 },
179
+ y: null, // No vertical movement
180
+ speed: 0.005
137
181
  });
138
182
 
139
- // Play on mouse enter
140
- document.querySelector('.container').addEventListener('mouseenter', () => {
141
- pausePlayAnimation.play();
183
+ // Vertical-only for floating effects
184
+ new PerlinDOM({
185
+ element: document.querySelector('.floater'),
186
+ x: null, // No horizontal movement
187
+ y: { min: -40, max: 40 },
188
+ speed: 0.008
142
189
  });
143
190
  ```
144
191
 
145
- ### 6. Interactive Controls
146
-
147
- Allow the user to adjust animation parameters.
192
+ ### 6. Advanced: SVG Path Animation
148
193
 
149
194
  ```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 },
195
+ // Animate SVG elements or paths
196
+ const svgElement = document.querySelector('#animated-path');
197
+ const pathAnimation = new PerlinDOM({
198
+ element: null, // No direct element
199
+ x: { min: -20, max: 20 },
200
+ y: { min: -15, max: 15 },
155
201
  speed: 0.01,
156
- seed: 0
202
+ seed: 999
157
203
  });
158
204
 
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
- });
205
+ function updatePath() {
206
+ const offsetX = pathAnimation.lastX;
207
+ const offsetY = pathAnimation.lastY;
208
+
209
+ // Update SVG path or transform
210
+ svgElement.style.transform = `translate(${offsetX}px, ${offsetY}px)`;
211
+
212
+ requestAnimationFrame(updatePath);
213
+ }
214
+ updatePath();
164
215
  ```
165
216
 
166
- ### 7. Animated Button with Circles
217
+ ## 💡 Best Practices
218
+
219
+ ### ✅ Do
167
220
 
168
- Create a button with animated hover effects.
221
+ - **Use CSS for initial positioning**: Position elements with CSS classes rather than inline styles
222
+ - **Set proper positioning**: Ensure elements have `position: relative`, `absolute`, or `fixed`
223
+ - **Use percentage values for responsive designs**: Great for mobile-friendly animations
224
+ - **Choose appropriate speeds**: Start with `0.01` and adjust based on desired effect
225
+ - **Clean up animations**: Call `destroy()` when removing elements from DOM
226
+
227
+ ### ❌ Don't
228
+
229
+ - **Don't rely on inline positioning**: PerlinDOM works from any position, but CSS is cleaner
230
+ - **Don't use on static elements**: Transform won't work without proper positioning
231
+ - **Don't forget to destroy**: Memory leaks can occur if animations aren't cleaned up
232
+ - **Don't use extreme speeds**: Very high speeds can cause jittery animations
233
+
234
+ ### 🎯 Performance Tips
169
235
 
170
236
  ```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
- }
237
+ // Good: Reuse animation instances
238
+ const sharedAnimation = new PerlinDOM({...});
194
239
 
195
- // Add event listeners to the button
196
- document.querySelector('.animated-button').addEventListener('mouseenter', () => {
197
- initCircleAnimations();
198
- circleAnimations.forEach(anim => anim.play());
199
- });
240
+ // Good: Use percentage values for responsive design
241
+ x: { min: '-5%', max: '5%' }
200
242
 
201
- document.querySelector('.animated-button').addEventListener('mouseleave', () => {
202
- circleAnimations.forEach(anim => anim.pause());
203
- });
243
+ // Good: Appropriate speeds for smooth animation
244
+ speed: 0.01 // Smooth and natural
245
+
246
+ // Good: Clean up when done
247
+ animation.destroy();
204
248
  ```
205
249
 
206
- ### 8. Triangle with Animated Vertex
250
+ ## 📖 API Reference
207
251
 
208
- Use PerlinDOM to animate a vertex of an SVG polygon.
252
+ ### Constructor
209
253
 
210
254
  ```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
- });
255
+ new PerlinDOM(options)
256
+ ```
257
+
258
+ ### Methods
259
+
260
+ | Method | Description | Returns |
261
+ |--------|-------------|---------|
262
+ | `play()` | Start or resume animation | `void` |
263
+ | `pause()` | Smoothly pause animation | `void` |
264
+ | `destroy()` | Stop animation and clean up resources | `void` |
265
+ | `init(seed?)` | Reinitialize with optional new seed | `void` |
231
266
 
232
- // Animation function
233
- function updateTriangle() {
234
- // Get the current noise values
235
- const offsetX = topVertexAnimation.lastX;
236
- const offsetY = topVertexAnimation.lastY;
267
+ ### Properties
237
268
 
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);
269
+ | Property | Type | Description |
270
+ |----------|------|-------------|
271
+ | `lastX` | `number` | Current X offset value |
272
+ | `lastY` | `number` | Current Y offset value |
273
+ | `isPlaying` | `boolean` | Current animation state |
274
+ | `speed` | `number` | Animation speed (writable) |
275
+ | `lerpSpeed` | `number` | Transition speed (writable) |
241
276
 
242
- // Continue animation
243
- requestAnimationFrame(updateTriangle);
244
- }
277
+ ## 🔧 Troubleshooting
245
278
 
246
- // Start the animation
247
- updateTriangle();
279
+ ### Common Issues
280
+
281
+ **❓ Animation not visible**
282
+ ```css
283
+ /* Ensure element has positioning */
284
+ .my-element {
285
+ position: relative; /* or absolute/fixed */
248
286
  }
287
+ ```
288
+
289
+ **❓ Element jumps on initialization**
290
+ - PerlinDOM automatically detects current position
291
+ - If element has conflicting inline styles, consider moving to CSS
249
292
 
250
- // Initialize the animation
251
- animateTriangle();
293
+ **❓ Animation too fast/slow**
294
+ ```javascript
295
+ // Adjust speed value
296
+ speed: 0.001 // Very slow
297
+ speed: 0.01 // Normal
298
+ speed: 0.1 // Fast
252
299
  ```
253
300
 
254
- ## Use Cases
301
+ **❓ Percentage values not working**
302
+ - Ensure parent container has defined dimensions
303
+ - PerlinDOM calculates percentages based on parent size
304
+
305
+ **❓ Memory leaks**
306
+ ```javascript
307
+ // Always clean up when removing elements
308
+ animation.destroy();
309
+ ```
310
+
311
+ ### Browser DevTools
312
+
313
+ Monitor performance in DevTools:
314
+ - Check for smooth 60fps in Performance tab
315
+ - Verify transform properties are being animated (not layout properties)
316
+
317
+ ## 🌐 Browser Compatibility
318
+
319
+ PerlinDOM works in all modern browsers supporting:
320
+
321
+ - ✅ **ES6 Modules** (2015+)
322
+ - ✅ **requestAnimationFrame** (IE10+)
323
+ - ✅ **CSS Transforms** (IE9+)
324
+ - ✅ **getComputedStyle** (IE9+)
325
+
326
+ ### Tested Browsers
327
+
328
+ - Chrome 60+
329
+ - Firefox 55+
330
+ - Safari 12+
331
+ - Edge 79+
332
+
333
+ ## 📝 Changelog
334
+
335
+ See [CHANGELOG.md](./CHANGELOG.md) for detailed version history.
336
+
337
+ ### Recent Updates
338
+
339
+ - **v0.0.3**: Fixed positioning conflicts, improved automatic position detection
340
+ - **v0.0.2**: Added percentage support and responsive animations
341
+ - **v0.0.1**: Initial release with core Perlin noise animations
255
342
 
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
343
+ ## 📄 License
261
344
 
262
- ## Compatibility
345
+ MIT License - see [LICENSE](./LICENSE) file for details.
263
346
 
264
- PerlinDOM works in all modern browsers that support:
265
- - ES6 (ECMAScript 2015)
266
- - requestAnimationFrame
267
- - CSS transform
347
+ ---
268
348
 
269
- ## License
349
+ **Made with ❤️ by [Andres Clua](https://github.com/andresclua)**
270
350
 
271
- MIT
351
+ *PerlinDOM - Bringing organic motion to the web*