npm install nosnap.js
<!-- ES Module -->
<script type="module">
import NoSnap from 'https://cdn.jsdelivr.net/npm/nosnap.js/dist/nosnap.esm.js';
</script>
<!-- UMD (Global) -->
<script src="https://cdn.jsdelivr.net/npm/nosnap.js/dist/nosnap.umd.min.js"></script>
import NoSnap from 'nosnap.js';
// Get your canvas element
const canvas = document.getElementById('myCanvas');
// Create the animation
const animation = new NoSnap(canvas, {
text: 'HELLO WORLD',
cellSize: 2,
stepMs: 32
});
// Start the animation
animation.start();
new NoSnap(canvas, options)
Creates a new animated noise text instance.
Parameters:
canvas (HTMLCanvasElement) - Required. The target canvas element where the animation will be rendered.options (Object) - Optional. Configuration options for the animation. See Configuration Options for details.Returns: NoSnap instance
Throws:
Error - If canvas is not a valid HTMLCanvasElementError - If canvas context cannot be obtainedError - If critical components fail to initializeExample:
const canvas = document.getElementById('myCanvas');
const animation = new NoSnap(canvas, {
text: 'ANIMATED TEXT',
cellSize: 3,
stepMs: 40
});
All configuration options are optional and have sensible defaults.
| Option | Type | Default | Description |
|---|---|---|---|
text |
string | 'HELLO' |
The text to display in the animation |
cellSize |
number | 2 |
Size of individual noise cells (1-10) |
circleRadius |
number | 300 |
Radius of the animated circle effect |
stepPixels |
number | 4 |
Number of pixels to move per animation step |
stepMs |
number | 32 |
Animation step interval in milliseconds |
maskBlockSize |
number | 2 |
Size of text mask blocks for pixelation effect |
fontSize |
number | null |
Font size in pixels (auto-calculated if null) |
fontWeight |
number|string | 900 |
Font weight (100-900 or ‘normal’, ‘bold’, etc.) |
fontFamily |
string | 'sans-serif' |
Font family for text rendering |
// Minimal configuration
const animation = new NoSnap(canvas, {
text: 'MINIMAL'
});
// Custom styling
const animation = new NoSnap(canvas, {
text: 'CUSTOM STYLE',
fontSize: 48,
fontWeight: 'bold',
fontFamily: 'Arial, sans-serif'
});
// Performance optimized
const animation = new NoSnap(canvas, {
text: 'FAST',
cellSize: 4,
stepMs: 50,
maskBlockSize: 4
});
// High quality
const animation = new NoSnap(canvas, {
text: 'HIGH QUALITY',
cellSize: 1,
stepMs: 16,
maskBlockSize: 1
});
Starts the animation loop.
Returns: void
Throws:
Error - If instance has been destroyedError - If required components are missingError - If canvas context is unavailableExample:
animation.start();
Stops the animation loop while preserving the instance state.
Returns: void
Example:
animation.stop();
Stops the animation and cleans up all resources including event listeners and offscreen canvases. The instance cannot be reused after calling destroy().
Returns: void
Example:
animation.destroy();
Updates the displayed text dynamically without stopping the animation.
Parameters:
text (string) - The new text to displayReturns: void
Throws:
Error - If instance has been destroyedError - If text mask generation failsExample:
animation.setText('NEW TEXT');
animation.setText(''); // Empty string is handled gracefully
animation.setText('🌟 UNICODE 🌟'); // Unicode characters supported
Updates configuration options dynamically. Only the provided options will be changed.
Parameters:
options (Object) - Configuration options to updateReturns: void
Throws:
Error - If instance has been destroyedExample:
animation.updateConfig({
cellSize: 3,
stepMs: 40
});
// Update multiple options
animation.updateConfig({
text: 'UPDATED',
fontSize: 64,
fontWeight: 'normal'
});
The library automatically handles canvas resize events and adapts the animation accordingly. No manual event handling is required.
The animation automatically:
The library includes comprehensive error handling with graceful fallbacks:
try {
const animation = new NoSnap(canvas, options);
} catch (error) {
console.error('Failed to initialize:', error.message);
// Handle initialization failure
}
try {
animation.start();
} catch (error) {
console.error('Failed to start animation:', error.message);
// Handle start failure
}
try {
animation.setText('NEW TEXT');
} catch (error) {
console.error('Failed to update text:', error.message);
// Handle text update failure
}
The library includes automatic error recovery mechanisms:
For older browser support, include these polyfills:
<!-- ResizeObserver polyfill -->
<script src="https://cdn.jsdelivr.net/npm/resize-observer-polyfill/dist/ResizeObserver.js"></script>
<!-- requestAnimationFrame polyfill -->
<script src="https://cdn.jsdelivr.net/npm/raf/index.js"></script>
// High performance (lower quality)
const fastAnimation = new NoSnap(canvas, {
cellSize: 4,
stepMs: 50,
maskBlockSize: 4
});
// Balanced performance
const balancedAnimation = new NoSnap(canvas, {
cellSize: 2,
stepMs: 32,
maskBlockSize: 2
});
// High quality (lower performance)
const qualityAnimation = new NoSnap(canvas, {
cellSize: 1,
stepMs: 16,
maskBlockSize: 1
});
The library automatically:
The library includes TypeScript declarations:
import NoSnap, { NoSnapConfig } from 'nosnap.js';
const config: NoSnapConfig = {
text: 'TYPESCRIPT',
cellSize: 2,
stepMs: 32
};
const canvas = document.getElementById('canvas') as HTMLCanvasElement;
const animation = new NoSnap(canvas, config);
Enable debug mode for development:
// Enable console warnings and error details
const animation = new NoSnap(canvas, options);
// Check for initialization errors
if (animation.initializationErrors?.length > 0) {
console.warn('Initialization warnings:', animation.initializationErrors);
}
// Monitor runtime errors
if (animation.runtimeErrors?.length > 0) {
console.warn('Runtime errors:', animation.runtimeErrors);
}