asciify-engine 1.0.40 → 1.0.41
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 +171 -118
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -4,201 +4,254 @@
|
|
|
4
4
|
<a href="https://www.npmjs.com/package/asciify-engine"><img src="https://img.shields.io/npm/v/asciify-engine?color=d4ff00&labelColor=0a0a0a&style=flat-square" alt="npm version" /></a>
|
|
5
5
|
<a href="https://www.npmjs.com/package/asciify-engine"><img src="https://img.shields.io/npm/dm/asciify-engine?color=d4ff00&labelColor=0a0a0a&style=flat-square" alt="downloads" /></a>
|
|
6
6
|
<a href="https://github.com/ayangabryl/asciify-engine/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-d4ff00?labelColor=0a0a0a&style=flat-square" alt="MIT license" /></a>
|
|
7
|
-
<a href="https://www.buymeacoffee.com/asciify"><img src="https://img.shields.io/badge/buy_me_a_coffee
|
|
7
|
+
<a href="https://www.buymeacoffee.com/asciify"><img src="https://img.shields.io/badge/buy_me_a_coffee-%E2%98%95-d4ff00?labelColor=0a0a0a&style=flat-square" alt="Buy Me A Coffee" /></a>
|
|
8
8
|
</p>
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
A framework-agnostic ASCII art rendering engine for the browser. Convert images, animated GIFs, and video into character-based art rendered onto an HTML canvas — with full color support, animated backgrounds, interactive hover effects, and embed generation. Zero runtime dependencies.
|
|
11
11
|
|
|
12
|
-
**[
|
|
12
|
+
**[▶ Live Playground](https://asciify.org) · [npm](https://www.npmjs.com/package/asciify-engine)**
|
|
13
13
|
|
|
14
14
|
---
|
|
15
15
|
|
|
16
|
-
##
|
|
16
|
+
## Overview
|
|
17
|
+
|
|
18
|
+
asciify-engine works in two stages:
|
|
19
|
+
|
|
20
|
+
1. **Convert** — a source (image, GIF buffer, video element) is sampled and converted into an `AsciiFrame`: a 2D array of character cells, each carrying a character and RGBA color data.
|
|
21
|
+
2. **Render** — the frame is drawn onto a `<canvas>` element via a 2D context, with full support for color modes, font sizes, hover effects, and time-based animations.
|
|
22
|
+
|
|
23
|
+
This separation means you can pre-compute frames once and render them at any frame rate, making it efficient for both static images and smooth animations.
|
|
24
|
+
|
|
25
|
+
---
|
|
26
|
+
|
|
27
|
+
## Installation
|
|
17
28
|
|
|
18
29
|
```bash
|
|
19
30
|
npm install asciify-engine
|
|
20
31
|
```
|
|
21
32
|
|
|
33
|
+
Works with any modern bundler (Vite, webpack, esbuild, Rollup) and any framework — React, Vue, Svelte, Angular, Next.js, or vanilla JS.
|
|
34
|
+
|
|
22
35
|
---
|
|
23
36
|
|
|
24
|
-
##
|
|
37
|
+
## Converting Media to ASCII
|
|
38
|
+
|
|
39
|
+
### Images
|
|
40
|
+
|
|
41
|
+
`imageToAsciiFrame` accepts any `HTMLImageElement`, `HTMLVideoElement`, or `HTMLCanvasElement` and returns a single ASCII frame.
|
|
25
42
|
|
|
26
43
|
```ts
|
|
27
44
|
import { imageToAsciiFrame, renderFrameToCanvas, DEFAULT_OPTIONS } from 'asciify-engine';
|
|
28
45
|
|
|
29
46
|
const img = new Image();
|
|
30
|
-
img.
|
|
47
|
+
img.crossOrigin = 'anonymous';
|
|
48
|
+
img.src = 'photo.jpg';
|
|
49
|
+
|
|
31
50
|
img.onload = () => {
|
|
32
51
|
const canvas = document.getElementById('ascii') as HTMLCanvasElement;
|
|
33
|
-
const
|
|
34
|
-
|
|
52
|
+
const ctx = canvas.getContext('2d')!;
|
|
53
|
+
const opts = { ...DEFAULT_OPTIONS, fontSize: 10, colorMode: 'fullcolor' as const };
|
|
54
|
+
|
|
55
|
+
const { frame } = imageToAsciiFrame(img, opts, canvas.width, canvas.height);
|
|
56
|
+
renderFrameToCanvas(ctx, frame, opts, canvas.width, canvas.height);
|
|
35
57
|
};
|
|
36
58
|
```
|
|
37
59
|
|
|
38
|
-
|
|
60
|
+
### Animated GIFs
|
|
39
61
|
|
|
40
|
-
|
|
62
|
+
`gifToAsciiFrames` parses a GIF `ArrayBuffer` and returns one `AsciiFrame` per GIF frame, preserving the original frame rate.
|
|
41
63
|
|
|
42
|
-
|
|
64
|
+
```ts
|
|
65
|
+
import { gifToAsciiFrames, renderFrameToCanvas, DEFAULT_OPTIONS } from 'asciify-engine';
|
|
43
66
|
|
|
44
|
-
|
|
67
|
+
const buffer = await fetch('animation.gif').then(r => r.arrayBuffer());
|
|
68
|
+
const canvas = document.getElementById('ascii') as HTMLCanvasElement;
|
|
69
|
+
const opts = { ...DEFAULT_OPTIONS, fontSize: 8 };
|
|
45
70
|
|
|
46
|
-
|
|
47
|
-
import { asciiBackground } from 'asciify-engine';
|
|
71
|
+
const { frames, fps } = await gifToAsciiFrames(buffer, opts, canvas.width, canvas.height);
|
|
48
72
|
|
|
49
|
-
|
|
73
|
+
let frameIndex = 0;
|
|
74
|
+
setInterval(() => {
|
|
75
|
+
renderFrameToCanvas(canvas.getContext('2d')!, frames[frameIndex], opts, canvas.width, canvas.height);
|
|
76
|
+
frameIndex = (frameIndex + 1) % frames.length;
|
|
77
|
+
}, 1000 / fps);
|
|
50
78
|
```
|
|
51
79
|
|
|
52
|
-
|
|
80
|
+
### Video
|
|
81
|
+
|
|
82
|
+
`videoToAsciiFrames` extracts frames from an `HTMLVideoElement` at a given frame rate and returns the full frame sequence.
|
|
53
83
|
|
|
54
84
|
```ts
|
|
55
|
-
|
|
56
|
-
// later…
|
|
57
|
-
stop();
|
|
58
|
-
```
|
|
85
|
+
import { videoToAsciiFrames, renderFrameToCanvas, DEFAULT_OPTIONS } from 'asciify-engine';
|
|
59
86
|
|
|
60
|
-
|
|
87
|
+
const video = document.createElement('video');
|
|
88
|
+
video.src = '/clip.mp4';
|
|
89
|
+
video.muted = true;
|
|
90
|
+
await new Promise(r => (video.onloadeddata = r));
|
|
91
|
+
|
|
92
|
+
const canvas = document.getElementById('ascii') as HTMLCanvasElement;
|
|
93
|
+
const opts = { ...DEFAULT_OPTIONS, fontSize: 8 };
|
|
61
94
|
|
|
62
|
-
|
|
95
|
+
// videoToAsciiFrames(video, options, width, height, fps?, maxDurationSeconds?)
|
|
96
|
+
const { frames, fps } = await videoToAsciiFrames(video, opts, canvas.width, canvas.height, 12, 10);
|
|
63
97
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
speed: 1.2, // animation speed multiplier
|
|
70
|
-
density: 0.6, // how many cells are active (0–1)
|
|
71
|
-
accentColor: '#d4ff00' // highlight colour
|
|
72
|
-
});
|
|
98
|
+
let frameIndex = 0;
|
|
99
|
+
setInterval(() => {
|
|
100
|
+
renderFrameToCanvas(canvas.getContext('2d')!, frames[frameIndex], opts, canvas.width, canvas.height);
|
|
101
|
+
frameIndex = (frameIndex + 1) % frames.length;
|
|
102
|
+
}, 1000 / fps);
|
|
73
103
|
```
|
|
74
104
|
|
|
75
105
|
---
|
|
76
106
|
|
|
77
|
-
##
|
|
107
|
+
## Rendering Options
|
|
78
108
|
|
|
79
|
-
|
|
109
|
+
All conversion and render functions accept an `AsciiOptions` object. Spread `DEFAULT_OPTIONS` as a base and override what you need.
|
|
80
110
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
111
|
+
| Option | Type | Default | Description |
|
|
112
|
+
|---|---|---|---|
|
|
113
|
+
| `fontSize` | `number` | `10` | Character cell size in pixels. Smaller values increase density and detail. |
|
|
114
|
+
| `colorMode` | `'grayscale' \| 'fullcolor' \| 'matrix' \| 'accent'` | `'grayscale'` | Determines how pixel color is mapped to character color. |
|
|
115
|
+
| `charset` | `string` | Standard ramp | Characters ordered from dense to sparse, representing brightness levels. |
|
|
116
|
+
| `brightness` | `number` | `0` | Brightness adjustment from `-1` (darker) to `1` (lighter). |
|
|
117
|
+
| `contrast` | `number` | `1` | Contrast multiplier applied before character mapping. |
|
|
118
|
+
| `invert` | `boolean` | `false` | Inverts the luminance mapping — light areas become dense, dark areas sparse. |
|
|
119
|
+
| `renderMode` | `'ascii' \| 'dots'` | `'ascii'` | Render as text characters or circular dot particles. |
|
|
120
|
+
| `hoverEffect` | `string` | `'none'` | Interactive effect driven by cursor position. See hover effects below. |
|
|
121
|
+
| `hoverStrength` | `number` | `0.8` | Effect intensity (0–1). |
|
|
122
|
+
| `hoverRadius` | `number` | `0.3` | Effect radius relative to canvas size (0–1). |
|
|
84
123
|
|
|
85
|
-
|
|
86
|
-
const ref = useRef<HTMLCanvasElement>(null);
|
|
124
|
+
### Color Modes
|
|
87
125
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
const opts = { ...DEFAULT_OPTIONS, fontSize: 10, colorMode: 'fullcolor' as const };
|
|
95
|
-
const { frame } = imageToAsciiFrame(img, opts, canvas.width, canvas.height);
|
|
96
|
-
renderFrameToCanvas(canvas.getContext('2d')!, frame, opts, canvas.width, canvas.height);
|
|
97
|
-
};
|
|
98
|
-
}, [src]);
|
|
126
|
+
| Mode | Description |
|
|
127
|
+
|---|---|
|
|
128
|
+
| `grayscale` | Classic monochrome ASCII. Character brightness maps to source luminance. |
|
|
129
|
+
| `fullcolor` | Each character inherits the original pixel color from the source. |
|
|
130
|
+
| `matrix` | Monochrome green — inspired by classic terminal aesthetics. |
|
|
131
|
+
| `accent` | Single accent color applied uniformly across all characters. |
|
|
99
132
|
|
|
100
|
-
|
|
101
|
-
}
|
|
102
|
-
```
|
|
133
|
+
### Hover Effects
|
|
103
134
|
|
|
104
|
-
|
|
135
|
+
Interactive effects that respond to cursor movement. Pass the effect name to `hoverEffect` and supply the cursor position to `renderFrameToCanvas` at render time.
|
|
105
136
|
|
|
106
|
-
|
|
107
|
-
import { gifToAsciiFrames, renderFrameToCanvas, DEFAULT_OPTIONS } from 'asciify-engine';
|
|
137
|
+
Available effects: `spotlight` · `flashlight` · `magnifier` · `force-field` · `neon` · `fire` · `ice` · `gravity` · `shatter` · `ghost`
|
|
108
138
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
const
|
|
139
|
+
```ts
|
|
140
|
+
canvas.addEventListener('mousemove', (e) => {
|
|
141
|
+
const rect = canvas.getBoundingClientRect();
|
|
142
|
+
renderFrameToCanvas(ctx, frame, opts, canvas.width, canvas.height, Date.now() / 1000, {
|
|
143
|
+
x: e.clientX - rect.left,
|
|
144
|
+
y: e.clientY - rect.top,
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
```
|
|
112
148
|
|
|
113
|
-
|
|
149
|
+
---
|
|
114
150
|
|
|
115
|
-
|
|
116
|
-
setInterval(() => {
|
|
117
|
-
renderFrameToCanvas(canvas.getContext('2d')!, frames[i], opts, canvas.width, canvas.height);
|
|
118
|
-
i = (i + 1) % frames.length;
|
|
119
|
-
}, 1000 / fps);
|
|
120
|
-
```
|
|
151
|
+
## Animated Backgrounds
|
|
121
152
|
|
|
122
|
-
|
|
153
|
+
`asciiBackground` mounts a self-animating ASCII renderer onto any DOM element — ideal for hero sections, banners, or full-page backgrounds. It manages its own canvas, animation loop, and resize handling internally.
|
|
123
154
|
|
|
124
155
|
```ts
|
|
125
|
-
import {
|
|
156
|
+
import { asciiBackground } from 'asciify-engine';
|
|
126
157
|
|
|
127
|
-
const
|
|
128
|
-
|
|
129
|
-
|
|
158
|
+
const stop = asciiBackground('#hero', {
|
|
159
|
+
type: 'rain',
|
|
160
|
+
colorScheme: 'auto', // follows OS dark/light mode
|
|
161
|
+
speed: 1.0,
|
|
162
|
+
density: 0.55,
|
|
163
|
+
accentColor: '#d4ff00',
|
|
164
|
+
});
|
|
130
165
|
|
|
131
|
-
|
|
132
|
-
|
|
166
|
+
// Stop and clean up when no longer needed
|
|
167
|
+
stop();
|
|
168
|
+
```
|
|
133
169
|
|
|
134
|
-
|
|
170
|
+
### Available Background Types
|
|
135
171
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
172
|
+
| Type | Description |
|
|
173
|
+
|---|---|
|
|
174
|
+
| `wave` | Flowing sine-wave field with layered noise turbulence |
|
|
175
|
+
| `rain` | Vertical column rain with a glowing leading character and fading trail |
|
|
176
|
+
| `stars` | Parallax star field that reacts to cursor position |
|
|
177
|
+
| `pulse` | Concentric ripple bursts emanating from the cursor |
|
|
178
|
+
| `noise` | Smooth value-noise field with organic, fluid motion |
|
|
179
|
+
| `grid` | Geometric grid that warps and brightens near the cursor |
|
|
180
|
+
| `aurora` | Sweeping borealis-style color bands drifting across the field |
|
|
181
|
+
| `silk` | Fluid swirl simulation following cursor movement |
|
|
182
|
+
| `void` | Gravitational singularity — characters spiral inward toward the cursor |
|
|
183
|
+
| `morph` | Characters morph between shapes over time, driven by noise |
|
|
184
|
+
|
|
185
|
+
### Background Options
|
|
186
|
+
|
|
187
|
+
| Option | Type | Default | Description |
|
|
188
|
+
|---|---|---|---|
|
|
189
|
+
| `type` | `string` | `'wave'` | Which background renderer to use. |
|
|
190
|
+
| `colorScheme` | `'auto' \| 'light' \| 'dark'` | `'dark'` | `'auto'` reacts to OS theme changes in real time. |
|
|
191
|
+
| `fontSize` | `number` | `13` | Character size in pixels. |
|
|
192
|
+
| `speed` | `number` | `1` | Global animation speed multiplier. |
|
|
193
|
+
| `density` | `number` | `0.55` | Fraction of grid cells that are active (0–1). |
|
|
194
|
+
| `accentColor` | `string` | varies | Highlight or leading-character color (any CSS color string). |
|
|
195
|
+
| `color` | `string` | — | Override the body character color. |
|
|
142
196
|
|
|
143
197
|
---
|
|
144
198
|
|
|
145
|
-
##
|
|
199
|
+
## React Integration
|
|
146
200
|
|
|
147
|
-
|
|
201
|
+
```tsx
|
|
202
|
+
import { useEffect, useRef } from 'react';
|
|
203
|
+
import { imageToAsciiFrame, renderFrameToCanvas, DEFAULT_OPTIONS } from 'asciify-engine';
|
|
148
204
|
|
|
149
|
-
|
|
150
|
-
const
|
|
151
|
-
...DEFAULT_OPTIONS,
|
|
152
|
-
fontSize: 8, // smaller = more detail
|
|
153
|
-
colorMode: 'matrix', // 'grayscale' | 'fullcolor' | 'matrix' | 'accent'
|
|
154
|
-
charset: '@#S%?*+;:,. ', // custom brightness ramp (dense → light)
|
|
155
|
-
brightness: 0.1, // -1 to 1
|
|
156
|
-
contrast: 1.2,
|
|
157
|
-
invert: false,
|
|
158
|
-
hoverEffect: 'spotlight', // interactive effect on cursor move
|
|
159
|
-
};
|
|
160
|
-
```
|
|
205
|
+
export function AsciiImage({ src }: { src: string }) {
|
|
206
|
+
const canvasRef = useRef<HTMLCanvasElement>(null);
|
|
161
207
|
|
|
162
|
-
|
|
208
|
+
useEffect(() => {
|
|
209
|
+
const canvas = canvasRef.current;
|
|
210
|
+
if (!canvas) return;
|
|
163
211
|
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
212
|
+
const img = new Image();
|
|
213
|
+
img.crossOrigin = 'anonymous';
|
|
214
|
+
img.src = src;
|
|
215
|
+
img.onload = () => {
|
|
216
|
+
const opts = { ...DEFAULT_OPTIONS, fontSize: 10, colorMode: 'fullcolor' as const };
|
|
217
|
+
const { frame } = imageToAsciiFrame(img, opts, canvas.width, canvas.height);
|
|
218
|
+
renderFrameToCanvas(canvas.getContext('2d')!, frame, opts, canvas.width, canvas.height);
|
|
219
|
+
};
|
|
220
|
+
}, [src]);
|
|
170
221
|
|
|
171
|
-
|
|
222
|
+
return <canvas ref={canvasRef} width={800} height={600} />;
|
|
223
|
+
}
|
|
224
|
+
```
|
|
172
225
|
|
|
173
226
|
---
|
|
174
227
|
|
|
175
|
-
## Embed
|
|
228
|
+
## Embed Generation
|
|
176
229
|
|
|
177
|
-
|
|
230
|
+
Generate self-contained HTML that can be hosted anywhere or dropped directly into a page — no runtime dependency required.
|
|
178
231
|
|
|
179
232
|
```ts
|
|
180
233
|
import { generateEmbedCode, generateAnimatedEmbedCode } from 'asciify-engine';
|
|
181
234
|
|
|
182
|
-
// Static
|
|
183
|
-
const
|
|
235
|
+
// Static — produces a single-file HTML with the ASCII art baked in
|
|
236
|
+
const staticHtml = generateEmbedCode(frame, options);
|
|
184
237
|
|
|
185
|
-
// Animated
|
|
186
|
-
const
|
|
238
|
+
// Animated — produces a self-running HTML animation
|
|
239
|
+
const animatedHtml = generateAnimatedEmbedCode(frames, options, fps);
|
|
187
240
|
```
|
|
188
241
|
|
|
189
242
|
---
|
|
190
243
|
|
|
191
|
-
## API
|
|
192
|
-
|
|
193
|
-
| Function |
|
|
194
|
-
|
|
195
|
-
| `imageToAsciiFrame(
|
|
196
|
-
| `renderFrameToCanvas(ctx, frame,
|
|
197
|
-
| `gifToAsciiFrames(buffer,
|
|
198
|
-
| `videoToAsciiFrames(video,
|
|
199
|
-
| `asciiBackground(selector,
|
|
200
|
-
| `generateEmbedCode(frame,
|
|
201
|
-
| `generateAnimatedEmbedCode(frames,
|
|
244
|
+
## API Reference
|
|
245
|
+
|
|
246
|
+
| Function | Signature | Returns |
|
|
247
|
+
|---|---|---|
|
|
248
|
+
| `imageToAsciiFrame` | `(source, options, w?, h?)` | `{ frame, cols, rows }` |
|
|
249
|
+
| `renderFrameToCanvas` | `(ctx, frame, options, w, h, time?, hoverPos?)` | `void` |
|
|
250
|
+
| `gifToAsciiFrames` | `(buffer, options, w, h, onProgress?)` | `Promise<{ frames, cols, rows, fps }>` |
|
|
251
|
+
| `videoToAsciiFrames` | `(video, options, w, h, fps?, maxSec?, onProgress?)` | `Promise<{ frames, cols, rows, fps }>` |
|
|
252
|
+
| `asciiBackground` | `(selector, options)` | `() => void` (cleanup) |
|
|
253
|
+
| `generateEmbedCode` | `(frame, options)` | `string` |
|
|
254
|
+
| `generateAnimatedEmbedCode` | `(frames, options, fps)` | `string` |
|
|
202
255
|
|
|
203
256
|
---
|
|
204
257
|
|
package/package.json
CHANGED