textmode.filters.js 1.0.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 +21 -0
- package/README.md +242 -0
- package/dist/textmode.filters.esm.js +216 -0
- package/dist/textmode.filters.umd.js +208 -0
- package/dist/types/index.d.ts +33 -0
- package/package.json +39 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 humanbydefinition
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
<div align="center">
|
|
2
|
+
|
|
3
|
+
# 🎨 textmode.filters.js
|
|
4
|
+
|
|
5
|
+
**GPU-accelerated image filters plugin for [textmode.js](https://code.textmode.art/)**
|
|
6
|
+
|
|
7
|
+
[](https://www.npmjs.com/package/textmode.filters.js)
|
|
8
|
+
[](https://github.com/humanbydefinition/textmode.filters.js/blob/main/LICENSE)
|
|
9
|
+
[](https://github.com/humanbydefinition/textmode.filters.js/blob/main/CONTRIBUTING.md)
|
|
10
|
+
|
|
11
|
+
[Installation](#installation) •
|
|
12
|
+
[Usage](#usage) •
|
|
13
|
+
[Filters](#filters) •
|
|
14
|
+
[Contributing](#contributing)
|
|
15
|
+
|
|
16
|
+
</div>
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
> [!NOTE]
|
|
21
|
+
> 🚧 **Work in Progress** - The required textmode.js version (0.7.0-beta.2+) is not yet publicly released.
|
|
22
|
+
|
|
23
|
+
## ✨ Features
|
|
24
|
+
|
|
25
|
+
- 🚀 **GPU-accelerated** - All filters run on the GPU via WebGL2 fragment shaders
|
|
26
|
+
- 🎛️ **Customizable parameters** - Fine-tune each filter to your needs
|
|
27
|
+
- 📦 **Lightweight** - Minimal footprint, no external dependencies
|
|
28
|
+
- 🤝 **Open to contributions** - Easy to add your own custom filters!
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
npm install textmode.filters.js
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
> **Note:** Make sure `textmode.js` is also installed in your project. This add-on declares it as a peer dependency.
|
|
37
|
+
|
|
38
|
+
You can also use it via CDN:
|
|
39
|
+
|
|
40
|
+
```html
|
|
41
|
+
<!-- ESM -->
|
|
42
|
+
<script type="module" src="https://unpkg.com/textmode.js/dist/textmode.esm.js"></script>
|
|
43
|
+
<script type="module" src="https://unpkg.com/textmode.filters.js/dist/textmode.filters.esm.js"></script>
|
|
44
|
+
|
|
45
|
+
<!-- UMD -->
|
|
46
|
+
<script src="https://unpkg.com/textmode.js/dist/textmode.umd.js"></script>
|
|
47
|
+
<script src="https://unpkg.com/textmode.filters.js/dist/textmode.filters.umd.js"></script>
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Usage
|
|
51
|
+
|
|
52
|
+
### ESM
|
|
53
|
+
|
|
54
|
+
```javascript
|
|
55
|
+
import { textmode } from 'textmode.js';
|
|
56
|
+
import { createFiltersPlugin } from 'textmode.filters.js';
|
|
57
|
+
|
|
58
|
+
const t = textmode.create({
|
|
59
|
+
width: 800,
|
|
60
|
+
height: 600,
|
|
61
|
+
plugins: [createFiltersPlugin()],
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
t.draw(() => {
|
|
65
|
+
// Apply filters to layers
|
|
66
|
+
t.layers.base.filter('brightness', 1.2);
|
|
67
|
+
t.layers.base.filter('contrast', { amount: 1.5 });
|
|
68
|
+
|
|
69
|
+
t.background(0);
|
|
70
|
+
// ... draw something ...
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### UMD
|
|
75
|
+
|
|
76
|
+
```html
|
|
77
|
+
<script src="https://unpkg.com/textmode.js/dist/textmode.umd.js"></script>
|
|
78
|
+
<script src="https://unpkg.com/textmode.filters.js/dist/textmode.filters.umd.js"></script>
|
|
79
|
+
<script>
|
|
80
|
+
const t = textmode.create({
|
|
81
|
+
plugins: [createFiltersPlugin()],
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
t.draw(() => {
|
|
85
|
+
t.layers.base.filter('brightness', 1.3);
|
|
86
|
+
});
|
|
87
|
+
</script>
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Filters
|
|
91
|
+
|
|
92
|
+
### `brightness`
|
|
93
|
+
|
|
94
|
+
Adjust image brightness *(1.0 = normal, >1 = brighter, <1 = darker)*
|
|
95
|
+
|
|
96
|
+
**Parameters:**
|
|
97
|
+
- `amount` - Brightness multiplier
|
|
98
|
+
Default: `1.0` | Range: `0.0` - `∞`
|
|
99
|
+
|
|
100
|
+
**Author:** [@humanbydefinition](https://github.com/humanbydefinition)
|
|
101
|
+
|
|
102
|
+
---
|
|
103
|
+
|
|
104
|
+
### `contrast`
|
|
105
|
+
|
|
106
|
+
Adjust image contrast *(1.0 = normal, >1 = more contrast, <1 = less)*
|
|
107
|
+
|
|
108
|
+
**Parameters:**
|
|
109
|
+
- `amount` - Contrast multiplier
|
|
110
|
+
Default: `1.0` | Range: `0.0` - `∞`
|
|
111
|
+
|
|
112
|
+
**Author:** [@humanbydefinition](https://github.com/humanbydefinition)
|
|
113
|
+
|
|
114
|
+
---
|
|
115
|
+
|
|
116
|
+
### `hueRotate`
|
|
117
|
+
|
|
118
|
+
Shift colors around the color wheel
|
|
119
|
+
|
|
120
|
+
**Parameters:**
|
|
121
|
+
- `angle` - Rotation angle in degrees
|
|
122
|
+
Default: `0.0` | Range: `0.0` - `360.0`
|
|
123
|
+
|
|
124
|
+
**Author:** [@humanbydefinition](https://github.com/humanbydefinition)
|
|
125
|
+
|
|
126
|
+
---
|
|
127
|
+
|
|
128
|
+
### `glitch`
|
|
129
|
+
|
|
130
|
+
Digital glitch effect with RGB channel separation, scanlines, and noise
|
|
131
|
+
|
|
132
|
+
**Parameters:**
|
|
133
|
+
- `amount` - Glitch intensity
|
|
134
|
+
Default: `0.0` | Range: `0.0` - `∞`
|
|
135
|
+
|
|
136
|
+
**Author:** [@humanbydefinition](https://github.com/humanbydefinition)
|
|
137
|
+
|
|
138
|
+
---
|
|
139
|
+
|
|
140
|
+
### `chromaticAberration`
|
|
141
|
+
|
|
142
|
+
RGB color channel separation effect that simulates lens distortion
|
|
143
|
+
|
|
144
|
+
**Parameters:**
|
|
145
|
+
- `amount` - Offset amount in pixels
|
|
146
|
+
Default: `5.0` | Range: `0.0` - `∞`
|
|
147
|
+
- `direction` - Direction of the color separation as `[x, y]`
|
|
148
|
+
Default: `[1.0, 0.0]` (horizontal)
|
|
149
|
+
|
|
150
|
+
**Author:** [@humanbydefinition](https://github.com/humanbydefinition)
|
|
151
|
+
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
### `pixelate`
|
|
155
|
+
|
|
156
|
+
Reduce image resolution to create a mosaic/pixelated effect
|
|
157
|
+
|
|
158
|
+
**Parameters:**
|
|
159
|
+
- `pixelSize` - Size of each pixel block in pixels
|
|
160
|
+
Default: `4.0` | Range: `1.0` - `∞`
|
|
161
|
+
|
|
162
|
+
**Author:** [@humanbydefinition](https://github.com/humanbydefinition)
|
|
163
|
+
|
|
164
|
+
---
|
|
165
|
+
|
|
166
|
+
### Usage examples
|
|
167
|
+
|
|
168
|
+
```javascript
|
|
169
|
+
// Using a single value (shorthand)
|
|
170
|
+
t.layers.base.filter('brightness', 1.5);
|
|
171
|
+
t.layers.base.filter('hueRotate', 180);
|
|
172
|
+
|
|
173
|
+
// Using an options object
|
|
174
|
+
t.layers.base.filter('contrast', { amount: 0.8 });
|
|
175
|
+
t.layers.base.filter('glitch', { amount: 2.0 });
|
|
176
|
+
|
|
177
|
+
// Chaining multiple filters in sequence
|
|
178
|
+
t.layers.base.filter('brightness', 1.2);
|
|
179
|
+
t.layers.base.filter('contrast', 1.3);
|
|
180
|
+
t.layers.base.filter('hueRotate', 90);
|
|
181
|
+
|
|
182
|
+
// Chromatic aberration with custom direction
|
|
183
|
+
t.layers.base.filter('chromaticAberration', { amount: 10, direction: [1, 1] });
|
|
184
|
+
|
|
185
|
+
// Pixelate effect
|
|
186
|
+
t.layers.base.filter('pixelate', 8);
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
## Adding custom filters
|
|
190
|
+
|
|
191
|
+
Want to create your own filter? Check out the [Contributing Guide](CONTRIBUTING.md) for detailed instructions on how to add new filters to this library!
|
|
192
|
+
|
|
193
|
+
The basic structure of a filter shader:
|
|
194
|
+
|
|
195
|
+
```glsl
|
|
196
|
+
#version 300 es
|
|
197
|
+
precision highp float;
|
|
198
|
+
|
|
199
|
+
uniform sampler2D u_texture; // Input texture
|
|
200
|
+
uniform vec2 u_resolution; // Canvas resolution
|
|
201
|
+
uniform float u_yourParam; // Your custom parameter
|
|
202
|
+
|
|
203
|
+
in vec2 v_uv;
|
|
204
|
+
out vec4 fragColor;
|
|
205
|
+
|
|
206
|
+
void main() {
|
|
207
|
+
vec4 color = texture(u_texture, v_uv);
|
|
208
|
+
|
|
209
|
+
// Your filter logic here
|
|
210
|
+
vec3 result = color.rgb; // Modify this!
|
|
211
|
+
|
|
212
|
+
fragColor = vec4(result, color.a);
|
|
213
|
+
}
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
## Contributing
|
|
217
|
+
|
|
218
|
+
Contributions are welcome and greatly appreciated! 🎉
|
|
219
|
+
|
|
220
|
+
Whether you want to:
|
|
221
|
+
- 🐛 Report a bug
|
|
222
|
+
- 💡 Suggest a new filter
|
|
223
|
+
- 🔧 Submit a pull request
|
|
224
|
+
- 📖 Improve documentation
|
|
225
|
+
|
|
226
|
+
Please read our [Contributing Guide](CONTRIBUTING.md) to get started.
|
|
227
|
+
|
|
228
|
+
## Related projects
|
|
229
|
+
|
|
230
|
+
- **[textmode.js](https://github.com/humanbydefinition/textmode.js)** - The main textmode.js library
|
|
231
|
+
- **[code.textmode.art](https://code.textmode.art/)** - Documentation & examples
|
|
232
|
+
- **[editor.textmode.art](https://editor.textmode.art/)** - textmode.js web editor
|
|
233
|
+
|
|
234
|
+
---
|
|
235
|
+
|
|
236
|
+
<div align="center">
|
|
237
|
+
|
|
238
|
+
<br />
|
|
239
|
+
|
|
240
|
+
**[↑ Back to Top](#-textmodefiltersjs)**
|
|
241
|
+
|
|
242
|
+
</div>
|
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
const n = () => ({ name: "textmode.filters", version: "1.0.0", async install(r) {
|
|
2
|
+
r.filters.register("brightness", `#version 300 es\r
|
|
3
|
+
precision highp float;\r
|
|
4
|
+
\r
|
|
5
|
+
uniform sampler2D u_texture;\r
|
|
6
|
+
uniform vec2 u_resolution;\r
|
|
7
|
+
uniform float u_amount;\r
|
|
8
|
+
\r
|
|
9
|
+
in vec2 v_uv;\r
|
|
10
|
+
out vec4 fragColor;\r
|
|
11
|
+
\r
|
|
12
|
+
void main() {\r
|
|
13
|
+
vec4 color = texture(u_texture, v_uv);\r
|
|
14
|
+
\r
|
|
15
|
+
// Multiply RGB by brightness amount\r
|
|
16
|
+
vec3 result = color.rgb * u_amount;\r
|
|
17
|
+
\r
|
|
18
|
+
fragColor = vec4(result, color.a);\r
|
|
19
|
+
}`, { u_amount: ["amount", 1] }), r.filters.register("contrast", `#version 300 es\r
|
|
20
|
+
precision highp float;\r
|
|
21
|
+
\r
|
|
22
|
+
uniform sampler2D u_texture;\r
|
|
23
|
+
uniform vec2 u_resolution;\r
|
|
24
|
+
uniform float u_amount;\r
|
|
25
|
+
\r
|
|
26
|
+
in vec2 v_uv;\r
|
|
27
|
+
out vec4 fragColor;\r
|
|
28
|
+
\r
|
|
29
|
+
void main() {\r
|
|
30
|
+
vec4 color = texture(u_texture, v_uv);\r
|
|
31
|
+
\r
|
|
32
|
+
// Adjust contrast around midpoint (0.5)\r
|
|
33
|
+
vec3 result = (color.rgb - 0.5) * u_amount + 0.5;\r
|
|
34
|
+
\r
|
|
35
|
+
fragColor = vec4(result, color.a);\r
|
|
36
|
+
}`, { u_amount: ["amount", 1] }), r.filters.register("hueRotate", `#version 300 es\r
|
|
37
|
+
precision highp float;\r
|
|
38
|
+
\r
|
|
39
|
+
uniform sampler2D u_texture;\r
|
|
40
|
+
uniform vec2 u_resolution;\r
|
|
41
|
+
uniform float u_angle;\r
|
|
42
|
+
\r
|
|
43
|
+
in vec2 v_uv;\r
|
|
44
|
+
out vec4 fragColor;\r
|
|
45
|
+
\r
|
|
46
|
+
// Helper function for HSL to RGB conversion\r
|
|
47
|
+
float hue2rgb(float p, float q, float t) {\r
|
|
48
|
+
if (t < 0.0) t += 1.0;\r
|
|
49
|
+
if (t > 1.0) t -= 1.0;\r
|
|
50
|
+
if (t < 1.0 / 6.0) return p + (q - p) * 6.0 * t;\r
|
|
51
|
+
if (t < 1.0 / 2.0) return q;\r
|
|
52
|
+
if (t < 2.0 / 3.0) return p + (q - p) * (2.0 / 3.0 - t) * 6.0;\r
|
|
53
|
+
return p;\r
|
|
54
|
+
}\r
|
|
55
|
+
\r
|
|
56
|
+
// Convert RGB to HSL\r
|
|
57
|
+
vec3 rgb2hsl(vec3 color) {\r
|
|
58
|
+
float maxC = max(max(color.r, color.g), color.b);\r
|
|
59
|
+
float minC = min(min(color.r, color.g), color.b);\r
|
|
60
|
+
float delta = maxC - minC;\r
|
|
61
|
+
\r
|
|
62
|
+
vec3 hsl = vec3(0.0);\r
|
|
63
|
+
hsl.z = (maxC + minC) / 2.0; // Lightness\r
|
|
64
|
+
\r
|
|
65
|
+
if (delta == 0.0) {\r
|
|
66
|
+
hsl.x = 0.0; // Hue\r
|
|
67
|
+
hsl.y = 0.0; // Saturation\r
|
|
68
|
+
} else {\r
|
|
69
|
+
hsl.y = hsl.z < 0.5 ? delta / (maxC + minC) : delta / (2.0 - maxC - minC); // Saturation\r
|
|
70
|
+
\r
|
|
71
|
+
if (color.r == maxC) {\r
|
|
72
|
+
hsl.x = (color.g - color.b) / delta + (color.g < color.b ? 6.0 : 0.0);\r
|
|
73
|
+
} else if (color.g == maxC) {\r
|
|
74
|
+
hsl.x = (color.b - color.r) / delta + 2.0;\r
|
|
75
|
+
} else {\r
|
|
76
|
+
hsl.x = (color.r - color.g) / delta + 4.0;\r
|
|
77
|
+
}\r
|
|
78
|
+
hsl.x /= 6.0;\r
|
|
79
|
+
}\r
|
|
80
|
+
\r
|
|
81
|
+
return hsl;\r
|
|
82
|
+
}\r
|
|
83
|
+
\r
|
|
84
|
+
// Convert HSL to RGB\r
|
|
85
|
+
vec3 hsl2rgb(vec3 hsl) {\r
|
|
86
|
+
float h = hsl.x;\r
|
|
87
|
+
float s = hsl.y;\r
|
|
88
|
+
float l = hsl.z;\r
|
|
89
|
+
\r
|
|
90
|
+
if (s == 0.0) {\r
|
|
91
|
+
return vec3(l);\r
|
|
92
|
+
}\r
|
|
93
|
+
\r
|
|
94
|
+
float q = l < 0.5 ? l * (1.0 + s) : l + s - l * s;\r
|
|
95
|
+
float p = 2.0 * l - q;\r
|
|
96
|
+
\r
|
|
97
|
+
float r = hue2rgb(p, q, h + 1.0 / 3.0);\r
|
|
98
|
+
float g = hue2rgb(p, q, h);\r
|
|
99
|
+
float b = hue2rgb(p, q, h - 1.0 / 3.0);\r
|
|
100
|
+
\r
|
|
101
|
+
return vec3(r, g, b);\r
|
|
102
|
+
}\r
|
|
103
|
+
\r
|
|
104
|
+
void main() {\r
|
|
105
|
+
vec4 color = texture(u_texture, v_uv);\r
|
|
106
|
+
\r
|
|
107
|
+
// Convert to HSL\r
|
|
108
|
+
vec3 hsl = rgb2hsl(color.rgb);\r
|
|
109
|
+
\r
|
|
110
|
+
// Rotate hue (angle is in degrees, convert to 0-1 range)\r
|
|
111
|
+
hsl.x = fract(hsl.x + u_angle / 360.0);\r
|
|
112
|
+
\r
|
|
113
|
+
// Convert back to RGB\r
|
|
114
|
+
vec3 result = hsl2rgb(hsl);\r
|
|
115
|
+
\r
|
|
116
|
+
fragColor = vec4(result, color.a);\r
|
|
117
|
+
}\r
|
|
118
|
+
`, { u_angle: ["angle", 0] }), r.filters.register("glitch", `#version 300 es\r
|
|
119
|
+
precision highp float;\r
|
|
120
|
+
\r
|
|
121
|
+
uniform sampler2D u_texture;\r
|
|
122
|
+
uniform vec2 u_resolution;\r
|
|
123
|
+
uniform float u_amount;\r
|
|
124
|
+
\r
|
|
125
|
+
in vec2 v_uv;\r
|
|
126
|
+
out vec4 fragColor;\r
|
|
127
|
+
\r
|
|
128
|
+
// Random function\r
|
|
129
|
+
float random(vec2 co) {\r
|
|
130
|
+
return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453);\r
|
|
131
|
+
}\r
|
|
132
|
+
\r
|
|
133
|
+
void main() {\r
|
|
134
|
+
vec2 uv = v_uv;\r
|
|
135
|
+
\r
|
|
136
|
+
// Create glitch effect based on amount\r
|
|
137
|
+
float glitchStrength = u_amount * 0.1;\r
|
|
138
|
+
\r
|
|
139
|
+
// Horizontal displacement\r
|
|
140
|
+
float block = floor(uv.y * 20.0);\r
|
|
141
|
+
float noise = random(vec2(block, floor(u_amount * 100.0)));\r
|
|
142
|
+
\r
|
|
143
|
+
if (noise > 0.7) {\r
|
|
144
|
+
uv.x += (noise - 0.7) * glitchStrength * 2.0;\r
|
|
145
|
+
}\r
|
|
146
|
+
\r
|
|
147
|
+
// RGB channel separation (chromatic aberration)\r
|
|
148
|
+
float separation = glitchStrength * 0.02;\r
|
|
149
|
+
vec2 uvR = uv + vec2(separation, 0.0);\r
|
|
150
|
+
vec2 uvG = uv;\r
|
|
151
|
+
vec2 uvB = uv - vec2(separation, 0.0);\r
|
|
152
|
+
\r
|
|
153
|
+
float r = texture(u_texture, uvR).r;\r
|
|
154
|
+
float g = texture(u_texture, uvG).g;\r
|
|
155
|
+
float b = texture(u_texture, uvB).b;\r
|
|
156
|
+
float a = texture(u_texture, uv).a;\r
|
|
157
|
+
\r
|
|
158
|
+
vec3 color = vec3(r, g, b);\r
|
|
159
|
+
\r
|
|
160
|
+
// Add scanlines\r
|
|
161
|
+
float scanline = sin(uv.y * u_resolution.y * 2.0) * 0.1 * u_amount;\r
|
|
162
|
+
color -= scanline;\r
|
|
163
|
+
\r
|
|
164
|
+
// Add random noise\r
|
|
165
|
+
float noiseAmount = random(uv + fract(u_amount)) * 0.1 * u_amount;\r
|
|
166
|
+
color += noiseAmount;\r
|
|
167
|
+
\r
|
|
168
|
+
fragColor = vec4(color, a);\r
|
|
169
|
+
}\r
|
|
170
|
+
`, { u_amount: ["amount", 0] }), r.filters.register("chromaticAberration", `#version 300 es\r
|
|
171
|
+
precision highp float;\r
|
|
172
|
+
\r
|
|
173
|
+
uniform sampler2D u_texture;\r
|
|
174
|
+
uniform vec2 u_resolution;\r
|
|
175
|
+
uniform float u_amount;\r
|
|
176
|
+
uniform vec2 u_direction;\r
|
|
177
|
+
\r
|
|
178
|
+
in vec2 v_uv;\r
|
|
179
|
+
out vec4 fragColor;\r
|
|
180
|
+
\r
|
|
181
|
+
void main() {\r
|
|
182
|
+
vec2 texelSize = 1.0 / u_resolution;\r
|
|
183
|
+
vec2 offset = u_direction * u_amount * texelSize;\r
|
|
184
|
+
\r
|
|
185
|
+
// Sample each color channel with different offsets\r
|
|
186
|
+
float r = texture(u_texture, v_uv + offset).r;\r
|
|
187
|
+
float g = texture(u_texture, v_uv).g;\r
|
|
188
|
+
float b = texture(u_texture, v_uv - offset).b;\r
|
|
189
|
+
float a = texture(u_texture, v_uv).a;\r
|
|
190
|
+
\r
|
|
191
|
+
fragColor = vec4(r, g, b, a);\r
|
|
192
|
+
}\r
|
|
193
|
+
`, { u_amount: ["amount", 5], u_direction: ["direction", [1, 0]] }), r.filters.register("pixelate", `#version 300 es\r
|
|
194
|
+
precision highp float;\r
|
|
195
|
+
\r
|
|
196
|
+
uniform sampler2D u_texture;\r
|
|
197
|
+
uniform vec2 u_resolution;\r
|
|
198
|
+
uniform float u_pixelSize;\r
|
|
199
|
+
\r
|
|
200
|
+
in vec2 v_uv;\r
|
|
201
|
+
out vec4 fragColor;\r
|
|
202
|
+
\r
|
|
203
|
+
void main() {\r
|
|
204
|
+
vec2 pixelSize = vec2(u_pixelSize) / u_resolution;\r
|
|
205
|
+
vec2 pixelatedUV = floor(v_uv / pixelSize) * pixelSize + pixelSize * 0.5;\r
|
|
206
|
+
\r
|
|
207
|
+
fragColor = texture(u_texture, pixelatedUV);\r
|
|
208
|
+
}\r
|
|
209
|
+
`, { u_pixelSize: ["pixelSize", 4] });
|
|
210
|
+
}, async uninstall(r) {
|
|
211
|
+
r.filters.unregister("brightness"), r.filters.unregister("contrast"), r.filters.unregister("hueRotate"), r.filters.unregister("glitch"), r.filters.unregister("chromaticAberration"), r.filters.unregister("pixelate");
|
|
212
|
+
} });
|
|
213
|
+
typeof window < "u" && (window.createFiltersPlugin = n);
|
|
214
|
+
export {
|
|
215
|
+
n as createFiltersPlugin
|
|
216
|
+
};
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
var r,n;r=this,n=function(t){const o=()=>({name:"textmode.filters",version:"1.0.0",async install(e){e.filters.register("brightness",`#version 300 es\r
|
|
2
|
+
precision highp float;\r
|
|
3
|
+
\r
|
|
4
|
+
uniform sampler2D u_texture;\r
|
|
5
|
+
uniform vec2 u_resolution;\r
|
|
6
|
+
uniform float u_amount;\r
|
|
7
|
+
\r
|
|
8
|
+
in vec2 v_uv;\r
|
|
9
|
+
out vec4 fragColor;\r
|
|
10
|
+
\r
|
|
11
|
+
void main() {\r
|
|
12
|
+
vec4 color = texture(u_texture, v_uv);\r
|
|
13
|
+
\r
|
|
14
|
+
// Multiply RGB by brightness amount\r
|
|
15
|
+
vec3 result = color.rgb * u_amount;\r
|
|
16
|
+
\r
|
|
17
|
+
fragColor = vec4(result, color.a);\r
|
|
18
|
+
}`,{u_amount:["amount",1]}),e.filters.register("contrast",`#version 300 es\r
|
|
19
|
+
precision highp float;\r
|
|
20
|
+
\r
|
|
21
|
+
uniform sampler2D u_texture;\r
|
|
22
|
+
uniform vec2 u_resolution;\r
|
|
23
|
+
uniform float u_amount;\r
|
|
24
|
+
\r
|
|
25
|
+
in vec2 v_uv;\r
|
|
26
|
+
out vec4 fragColor;\r
|
|
27
|
+
\r
|
|
28
|
+
void main() {\r
|
|
29
|
+
vec4 color = texture(u_texture, v_uv);\r
|
|
30
|
+
\r
|
|
31
|
+
// Adjust contrast around midpoint (0.5)\r
|
|
32
|
+
vec3 result = (color.rgb - 0.5) * u_amount + 0.5;\r
|
|
33
|
+
\r
|
|
34
|
+
fragColor = vec4(result, color.a);\r
|
|
35
|
+
}`,{u_amount:["amount",1]}),e.filters.register("hueRotate",`#version 300 es\r
|
|
36
|
+
precision highp float;\r
|
|
37
|
+
\r
|
|
38
|
+
uniform sampler2D u_texture;\r
|
|
39
|
+
uniform vec2 u_resolution;\r
|
|
40
|
+
uniform float u_angle;\r
|
|
41
|
+
\r
|
|
42
|
+
in vec2 v_uv;\r
|
|
43
|
+
out vec4 fragColor;\r
|
|
44
|
+
\r
|
|
45
|
+
// Helper function for HSL to RGB conversion\r
|
|
46
|
+
float hue2rgb(float p, float q, float t) {\r
|
|
47
|
+
if (t < 0.0) t += 1.0;\r
|
|
48
|
+
if (t > 1.0) t -= 1.0;\r
|
|
49
|
+
if (t < 1.0 / 6.0) return p + (q - p) * 6.0 * t;\r
|
|
50
|
+
if (t < 1.0 / 2.0) return q;\r
|
|
51
|
+
if (t < 2.0 / 3.0) return p + (q - p) * (2.0 / 3.0 - t) * 6.0;\r
|
|
52
|
+
return p;\r
|
|
53
|
+
}\r
|
|
54
|
+
\r
|
|
55
|
+
// Convert RGB to HSL\r
|
|
56
|
+
vec3 rgb2hsl(vec3 color) {\r
|
|
57
|
+
float maxC = max(max(color.r, color.g), color.b);\r
|
|
58
|
+
float minC = min(min(color.r, color.g), color.b);\r
|
|
59
|
+
float delta = maxC - minC;\r
|
|
60
|
+
\r
|
|
61
|
+
vec3 hsl = vec3(0.0);\r
|
|
62
|
+
hsl.z = (maxC + minC) / 2.0; // Lightness\r
|
|
63
|
+
\r
|
|
64
|
+
if (delta == 0.0) {\r
|
|
65
|
+
hsl.x = 0.0; // Hue\r
|
|
66
|
+
hsl.y = 0.0; // Saturation\r
|
|
67
|
+
} else {\r
|
|
68
|
+
hsl.y = hsl.z < 0.5 ? delta / (maxC + minC) : delta / (2.0 - maxC - minC); // Saturation\r
|
|
69
|
+
\r
|
|
70
|
+
if (color.r == maxC) {\r
|
|
71
|
+
hsl.x = (color.g - color.b) / delta + (color.g < color.b ? 6.0 : 0.0);\r
|
|
72
|
+
} else if (color.g == maxC) {\r
|
|
73
|
+
hsl.x = (color.b - color.r) / delta + 2.0;\r
|
|
74
|
+
} else {\r
|
|
75
|
+
hsl.x = (color.r - color.g) / delta + 4.0;\r
|
|
76
|
+
}\r
|
|
77
|
+
hsl.x /= 6.0;\r
|
|
78
|
+
}\r
|
|
79
|
+
\r
|
|
80
|
+
return hsl;\r
|
|
81
|
+
}\r
|
|
82
|
+
\r
|
|
83
|
+
// Convert HSL to RGB\r
|
|
84
|
+
vec3 hsl2rgb(vec3 hsl) {\r
|
|
85
|
+
float h = hsl.x;\r
|
|
86
|
+
float s = hsl.y;\r
|
|
87
|
+
float l = hsl.z;\r
|
|
88
|
+
\r
|
|
89
|
+
if (s == 0.0) {\r
|
|
90
|
+
return vec3(l);\r
|
|
91
|
+
}\r
|
|
92
|
+
\r
|
|
93
|
+
float q = l < 0.5 ? l * (1.0 + s) : l + s - l * s;\r
|
|
94
|
+
float p = 2.0 * l - q;\r
|
|
95
|
+
\r
|
|
96
|
+
float r = hue2rgb(p, q, h + 1.0 / 3.0);\r
|
|
97
|
+
float g = hue2rgb(p, q, h);\r
|
|
98
|
+
float b = hue2rgb(p, q, h - 1.0 / 3.0);\r
|
|
99
|
+
\r
|
|
100
|
+
return vec3(r, g, b);\r
|
|
101
|
+
}\r
|
|
102
|
+
\r
|
|
103
|
+
void main() {\r
|
|
104
|
+
vec4 color = texture(u_texture, v_uv);\r
|
|
105
|
+
\r
|
|
106
|
+
// Convert to HSL\r
|
|
107
|
+
vec3 hsl = rgb2hsl(color.rgb);\r
|
|
108
|
+
\r
|
|
109
|
+
// Rotate hue (angle is in degrees, convert to 0-1 range)\r
|
|
110
|
+
hsl.x = fract(hsl.x + u_angle / 360.0);\r
|
|
111
|
+
\r
|
|
112
|
+
// Convert back to RGB\r
|
|
113
|
+
vec3 result = hsl2rgb(hsl);\r
|
|
114
|
+
\r
|
|
115
|
+
fragColor = vec4(result, color.a);\r
|
|
116
|
+
}\r
|
|
117
|
+
`,{u_angle:["angle",0]}),e.filters.register("glitch",`#version 300 es\r
|
|
118
|
+
precision highp float;\r
|
|
119
|
+
\r
|
|
120
|
+
uniform sampler2D u_texture;\r
|
|
121
|
+
uniform vec2 u_resolution;\r
|
|
122
|
+
uniform float u_amount;\r
|
|
123
|
+
\r
|
|
124
|
+
in vec2 v_uv;\r
|
|
125
|
+
out vec4 fragColor;\r
|
|
126
|
+
\r
|
|
127
|
+
// Random function\r
|
|
128
|
+
float random(vec2 co) {\r
|
|
129
|
+
return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453);\r
|
|
130
|
+
}\r
|
|
131
|
+
\r
|
|
132
|
+
void main() {\r
|
|
133
|
+
vec2 uv = v_uv;\r
|
|
134
|
+
\r
|
|
135
|
+
// Create glitch effect based on amount\r
|
|
136
|
+
float glitchStrength = u_amount * 0.1;\r
|
|
137
|
+
\r
|
|
138
|
+
// Horizontal displacement\r
|
|
139
|
+
float block = floor(uv.y * 20.0);\r
|
|
140
|
+
float noise = random(vec2(block, floor(u_amount * 100.0)));\r
|
|
141
|
+
\r
|
|
142
|
+
if (noise > 0.7) {\r
|
|
143
|
+
uv.x += (noise - 0.7) * glitchStrength * 2.0;\r
|
|
144
|
+
}\r
|
|
145
|
+
\r
|
|
146
|
+
// RGB channel separation (chromatic aberration)\r
|
|
147
|
+
float separation = glitchStrength * 0.02;\r
|
|
148
|
+
vec2 uvR = uv + vec2(separation, 0.0);\r
|
|
149
|
+
vec2 uvG = uv;\r
|
|
150
|
+
vec2 uvB = uv - vec2(separation, 0.0);\r
|
|
151
|
+
\r
|
|
152
|
+
float r = texture(u_texture, uvR).r;\r
|
|
153
|
+
float g = texture(u_texture, uvG).g;\r
|
|
154
|
+
float b = texture(u_texture, uvB).b;\r
|
|
155
|
+
float a = texture(u_texture, uv).a;\r
|
|
156
|
+
\r
|
|
157
|
+
vec3 color = vec3(r, g, b);\r
|
|
158
|
+
\r
|
|
159
|
+
// Add scanlines\r
|
|
160
|
+
float scanline = sin(uv.y * u_resolution.y * 2.0) * 0.1 * u_amount;\r
|
|
161
|
+
color -= scanline;\r
|
|
162
|
+
\r
|
|
163
|
+
// Add random noise\r
|
|
164
|
+
float noiseAmount = random(uv + fract(u_amount)) * 0.1 * u_amount;\r
|
|
165
|
+
color += noiseAmount;\r
|
|
166
|
+
\r
|
|
167
|
+
fragColor = vec4(color, a);\r
|
|
168
|
+
}\r
|
|
169
|
+
`,{u_amount:["amount",0]}),e.filters.register("chromaticAberration",`#version 300 es\r
|
|
170
|
+
precision highp float;\r
|
|
171
|
+
\r
|
|
172
|
+
uniform sampler2D u_texture;\r
|
|
173
|
+
uniform vec2 u_resolution;\r
|
|
174
|
+
uniform float u_amount;\r
|
|
175
|
+
uniform vec2 u_direction;\r
|
|
176
|
+
\r
|
|
177
|
+
in vec2 v_uv;\r
|
|
178
|
+
out vec4 fragColor;\r
|
|
179
|
+
\r
|
|
180
|
+
void main() {\r
|
|
181
|
+
vec2 texelSize = 1.0 / u_resolution;\r
|
|
182
|
+
vec2 offset = u_direction * u_amount * texelSize;\r
|
|
183
|
+
\r
|
|
184
|
+
// Sample each color channel with different offsets\r
|
|
185
|
+
float r = texture(u_texture, v_uv + offset).r;\r
|
|
186
|
+
float g = texture(u_texture, v_uv).g;\r
|
|
187
|
+
float b = texture(u_texture, v_uv - offset).b;\r
|
|
188
|
+
float a = texture(u_texture, v_uv).a;\r
|
|
189
|
+
\r
|
|
190
|
+
fragColor = vec4(r, g, b, a);\r
|
|
191
|
+
}\r
|
|
192
|
+
`,{u_amount:["amount",5],u_direction:["direction",[1,0]]}),e.filters.register("pixelate",`#version 300 es\r
|
|
193
|
+
precision highp float;\r
|
|
194
|
+
\r
|
|
195
|
+
uniform sampler2D u_texture;\r
|
|
196
|
+
uniform vec2 u_resolution;\r
|
|
197
|
+
uniform float u_pixelSize;\r
|
|
198
|
+
\r
|
|
199
|
+
in vec2 v_uv;\r
|
|
200
|
+
out vec4 fragColor;\r
|
|
201
|
+
\r
|
|
202
|
+
void main() {\r
|
|
203
|
+
vec2 pixelSize = vec2(u_pixelSize) / u_resolution;\r
|
|
204
|
+
vec2 pixelatedUV = floor(v_uv / pixelSize) * pixelSize + pixelSize * 0.5;\r
|
|
205
|
+
\r
|
|
206
|
+
fragColor = texture(u_texture, pixelatedUV);\r
|
|
207
|
+
}\r
|
|
208
|
+
`,{u_pixelSize:["pixelSize",4]})},async uninstall(e){e.filters.unregister("brightness"),e.filters.unregister("contrast"),e.filters.unregister("hueRotate"),e.filters.unregister("glitch"),e.filters.unregister("chromaticAberration"),e.filters.unregister("pixelate")}});typeof window<"u"&&(window.createFiltersPlugin=o),t.createFiltersPlugin=o,Object.defineProperty(t,Symbol.toStringTag,{value:"Module"})},typeof exports=="object"&&typeof module<"u"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):n((r=typeof globalThis<"u"?globalThis:r||self).textmodeFilters={});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import type { TextmodePlugin } from 'textmode.js';
|
|
2
|
+
/**
|
|
3
|
+
* Creates the `textmode.filters.js` plugin for textmode.js.
|
|
4
|
+
*
|
|
5
|
+
* Included filters:
|
|
6
|
+
* - `brightness` - Adjust image brightness (amount: 1.0 = normal, >1 = brighter, <1 = darker)
|
|
7
|
+
* - `contrast` - Adjust image contrast (amount: 1.0 = normal, >1 = more contrast, <1 = less)
|
|
8
|
+
* - `hueRotate` - Rotate colors around the hue wheel (angle: 0-360 degrees)
|
|
9
|
+
* - `glitch` - Digital glitch effect (amount: 0.0 = none, higher values = more intense)
|
|
10
|
+
* - `chromaticAberration` - RGB channel separation effect (amount: offset in pixels, direction: vec2 for offset direction)
|
|
11
|
+
* - `pixelate` - Pixelation effect (pixelSize: size of pixels in pixels)
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```javascript
|
|
15
|
+
* import { textmode } from 'textmode.js';
|
|
16
|
+
* import { createFiltersPlugin } from 'textmode.filters.js';
|
|
17
|
+
*
|
|
18
|
+
* const t = textmode.create({
|
|
19
|
+
* plugins: [createFiltersPlugin()]
|
|
20
|
+
* });
|
|
21
|
+
*
|
|
22
|
+
* t.draw(() => {
|
|
23
|
+
* t.layers.base.filter('brightness', 1.2);
|
|
24
|
+
* t.layers.base.filter('contrast', { amount: 1.5 });
|
|
25
|
+
*
|
|
26
|
+
* t.background(0);
|
|
27
|
+
* // ... draw something ...
|
|
28
|
+
* });
|
|
29
|
+
* ```
|
|
30
|
+
*
|
|
31
|
+
* @returns A textmode.js plugin instance.
|
|
32
|
+
*/
|
|
33
|
+
export declare const createFiltersPlugin: () => TextmodePlugin;
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "textmode.filters.js",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Image filters plugin for textmode.js",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/textmode.filters.umd.js",
|
|
7
|
+
"module": "./dist/textmode.filters.esm.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/textmode.filters.esm.js",
|
|
13
|
+
"require": "./dist/textmode.filters.umd.js"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"build": "vite build && tsc -p tsconfig.json",
|
|
21
|
+
"dev": "vite --port 5179 --host"
|
|
22
|
+
},
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"textmode.js": "^0.7.0"
|
|
25
|
+
},
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@rollup/plugin-terser": "^0.4.4",
|
|
28
|
+
"@types/node": "^24.10.1",
|
|
29
|
+
"tslib": "^2.8.1",
|
|
30
|
+
"typescript": "^5.9.2",
|
|
31
|
+
"vite": "^6.3.4"
|
|
32
|
+
},
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"author": "humanbydefinition",
|
|
35
|
+
"repository": {
|
|
36
|
+
"type": "git",
|
|
37
|
+
"url": "https://github.com/humanbydefinition/textmode.filters.js"
|
|
38
|
+
}
|
|
39
|
+
}
|