plasma-foil 0.0.0-stage → 0.1.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 +74 -2
- package/dist/HoloBadge.d.ts +17 -0
- package/dist/HoloBadge.js +170 -0
- package/package.json +49 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Mehmed Dourmouch
|
|
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
CHANGED
|
@@ -1,3 +1,75 @@
|
|
|
1
|
-
#
|
|
1
|
+
# plasma-foil
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Holographic plasma hover effect for badge images, as a React component.
|
|
4
|
+
Point it at any transparent badge (certifications, achievements, stickers) and it
|
|
5
|
+
tilts toward the pointer, runs a 90s demoscene sine plasma recoloured as rainbow
|
|
6
|
+
foil, and adds a glare spot, all clipped to the badge's own shape.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install plasma-foil
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
Requires React 19+. No CSS import or bundler config: the component injects its
|
|
15
|
+
own styles once per page. Works in Next.js App Router (ships with `"use client"`).
|
|
16
|
+
|
|
17
|
+
## Usage
|
|
18
|
+
|
|
19
|
+
```tsx
|
|
20
|
+
import { HoloBadge } from "plasma-foil";
|
|
21
|
+
|
|
22
|
+
<HoloBadge src="/badges/my-cert.png" alt="My Certification" size={200} />
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### Props
|
|
26
|
+
|
|
27
|
+
| Prop | Type | Default | Description |
|
|
28
|
+
| ----------- | ------------------ | ------- | -------------------------------------------------------- |
|
|
29
|
+
| `src` | `string` | – | Badge image URL (see requirements below). |
|
|
30
|
+
| `alt` | `string` | – | Alt text for the image. |
|
|
31
|
+
| `size` | `number \| string` | `240` | Width; numbers are px. The badge box is square. |
|
|
32
|
+
| `tilt` | `number` | `14` | Max tilt in degrees. |
|
|
33
|
+
| `intensity` | `number` | `0.65` | Plasma opacity while hovered, 0–1. |
|
|
34
|
+
| `speed` | `number` | `0.6` | Plasma drift per second while hovered. |
|
|
35
|
+
| `follow` | `number` | `4` | How fast plasma and glare chase the pointer; lower = lazier. |
|
|
36
|
+
| `className` | `string` | – | Extra class on the outer element, for layout. |
|
|
37
|
+
|
|
38
|
+
### Image requirements
|
|
39
|
+
|
|
40
|
+
- **Transparent background** (PNG with alpha, or SVG). The effect is masked to
|
|
41
|
+
the image's alpha, so an opaque rectangle gets the effect on the whole rectangle.
|
|
42
|
+
- **Same origin, or served with CORS headers.** CSS masks fetch images in CORS
|
|
43
|
+
mode, so a hotlinked image from another host (e.g. Credly) shows the badge but
|
|
44
|
+
no effect. Download the image into your own `public/` folder instead.
|
|
45
|
+
- Square-ish images look best; non-square ones are letterboxed with `contain`.
|
|
46
|
+
|
|
47
|
+
## How it works
|
|
48
|
+
|
|
49
|
+
Three stacked layers inside a 3D-tilting wrapper: the image, a WebGL canvas
|
|
50
|
+
running the plasma shader (`mix-blend-mode: color-dodge`), and a radial glare
|
|
51
|
+
(`mix-blend-mode: overlay`). The effect layers use `mask: url(src)` so they
|
|
52
|
+
only paint on the badge. Rendering runs only while hovered.
|
|
53
|
+
`prefers-reduced-motion` disables tilt and the automatic drift.
|
|
54
|
+
Without WebGL, tilt and glare still work.
|
|
55
|
+
|
|
56
|
+
Each badge uses one WebGL context; browsers allow roughly 16 per page.
|
|
57
|
+
|
|
58
|
+
## Demo / development
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
npm install
|
|
62
|
+
npm run dev # demo page at http://localhost:3000
|
|
63
|
+
npm run build:lib # builds dist/ for npm
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
The demo lists its badges in `app/page.tsx`. To try your own, drop images into
|
|
67
|
+
`public/badges/` and add them to that list.
|
|
68
|
+
|
|
69
|
+
## License
|
|
70
|
+
|
|
71
|
+
Code: [MIT](LICENSE).
|
|
72
|
+
|
|
73
|
+
The AWS Certification badge images in `public/badges/` are trademarks of
|
|
74
|
+
Amazon Web Services, Inc. They are included for demo purposes only and are
|
|
75
|
+
**not** covered by the MIT license.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type HoloBadgeProps = {
|
|
2
|
+
/** Badge image URL. Transparent PNG/SVG; must be same-origin or served with CORS headers (CSS mask). */
|
|
3
|
+
src: string;
|
|
4
|
+
alt: string;
|
|
5
|
+
/** Width; number = px. Badge is square. Default 240. */
|
|
6
|
+
size?: number | string;
|
|
7
|
+
/** Max tilt in degrees. Default 14. */
|
|
8
|
+
tilt?: number;
|
|
9
|
+
/** Plasma layer opacity while hovered, 0..1. Default 0.65. */
|
|
10
|
+
intensity?: number;
|
|
11
|
+
/** Plasma drift per second while hovered. Default 0.6. */
|
|
12
|
+
speed?: number;
|
|
13
|
+
/** How fast plasma + glare catch up with the pointer (1/s); lower = lazier. Default 4. */
|
|
14
|
+
follow?: number;
|
|
15
|
+
className?: string;
|
|
16
|
+
};
|
|
17
|
+
export declare function HoloBadge({ src, alt, size, tilt, intensity, speed, follow, className, }: HoloBadgeProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { useEffect, useRef } from "react";
|
|
4
|
+
const SCRUB = 8; // extra plasma time per unit of pointer travel (badge widths)
|
|
5
|
+
const DPR_CAP = 1.5; // canvas resolution cap, plasma is soft anyway
|
|
6
|
+
// Classic demoscene sine plasma, recoloured as a holographic foil.
|
|
7
|
+
// The pointer moves one ripple centre and slides the rainbow bands (fake viewing angle).
|
|
8
|
+
const FRAG = `
|
|
9
|
+
precision mediump float;
|
|
10
|
+
uniform vec2 uRes;
|
|
11
|
+
uniform float uTime;
|
|
12
|
+
uniform vec2 uPtr; // 0..1, y down
|
|
13
|
+
#define SCALE 5.0 // plasma zoom; lower = bigger blobs
|
|
14
|
+
#define BANDS 8.0 // posterised colour steps per rainbow cycle; 0 = smooth
|
|
15
|
+
|
|
16
|
+
void main() {
|
|
17
|
+
vec2 uv = gl_FragCoord.xy / uRes;
|
|
18
|
+
uv.y = 1.0 - uv.y;
|
|
19
|
+
vec2 p = (uv - 0.5) * SCALE;
|
|
20
|
+
float t = uTime;
|
|
21
|
+
|
|
22
|
+
float v = sin(p.x + t)
|
|
23
|
+
+ sin((p.y + t) * 0.5)
|
|
24
|
+
+ sin((p.x + p.y + t) * 0.5);
|
|
25
|
+
vec2 c = p - (uPtr - 0.5) * SCALE + vec2(sin(t * 0.33), cos(t * 0.5)) * 1.5;
|
|
26
|
+
v += sin(length(c) * 1.6 - t * 1.5);
|
|
27
|
+
v *= 0.25; // ~ -1..1
|
|
28
|
+
|
|
29
|
+
// ~1 rainbow cycle across the badge, so each colour band is wide
|
|
30
|
+
float angle = (uPtr.x - 0.5) + (uPtr.y - 0.5);
|
|
31
|
+
float hue = v * 0.45 + (uv.x + uv.y) * 0.35 + angle * 0.8;
|
|
32
|
+
if (BANDS > 0.0) hue = floor(hue * BANDS) / BANDS;
|
|
33
|
+
vec3 col = 0.5 + 0.5 * cos(6.28318 * (hue + vec3(0.0, 0.33, 0.67)));
|
|
34
|
+
|
|
35
|
+
// soft metallic contour lines along the plasma field
|
|
36
|
+
col += pow(1.0 - abs(sin(v * 4.0)), 12.0) * 0.35;
|
|
37
|
+
|
|
38
|
+
// saturation boost, same luma weights as CSS saturate(1.3) but without a per-frame filter pass
|
|
39
|
+
col = clamp(col, 0.0, 1.0);
|
|
40
|
+
col = mix(vec3(dot(col, vec3(0.2126, 0.7152, 0.0722))), col, 1.3);
|
|
41
|
+
|
|
42
|
+
gl_FragColor = vec4(col, 1.0);
|
|
43
|
+
}`;
|
|
44
|
+
const VERT = `attribute vec2 a; void main() { gl_Position = vec4(a, 0.0, 1.0); }`;
|
|
45
|
+
// Plain CSS, injected once per page by React 19 (<style href precedence> is deduped and hoisted).
|
|
46
|
+
const CSS = `
|
|
47
|
+
.pf-card { --mx: 50%; --my: 50%; --rx: 0deg; --ry: 0deg; aspect-ratio: 1; perspective: 700px; }
|
|
48
|
+
.pf-tilt {
|
|
49
|
+
position: relative; height: 100%;
|
|
50
|
+
transform: rotateY(var(--rx)) rotateX(var(--ry)) scale(var(--s, 1));
|
|
51
|
+
transition: transform 0.7s cubic-bezier(0.2, 0.8, 0.2, 1);
|
|
52
|
+
will-change: transform;
|
|
53
|
+
}
|
|
54
|
+
.pf-card[data-active] { --s: 1.06; }
|
|
55
|
+
.pf-card[data-active] .pf-tilt { transition-duration: 0.12s; }
|
|
56
|
+
.pf-img, .pf-layer { position: absolute; inset: 0; width: 100%; height: 100%; }
|
|
57
|
+
/* Shadow on the static image, not the wrapper: wrapping the animated canvas would re-blur every frame. */
|
|
58
|
+
.pf-img { object-fit: contain; user-select: none; filter: drop-shadow(0 10px 18px rgb(0 0 0 / 0.45)); }
|
|
59
|
+
.pf-card[data-active] .pf-img {
|
|
60
|
+
filter: drop-shadow(0 18px 28px rgb(0 0 0 / 0.55)) drop-shadow(0 0 18px rgb(140 120 255 / 0.35));
|
|
61
|
+
}
|
|
62
|
+
/* Each layer is clipped to the badge's alpha and blends with the image below.
|
|
63
|
+
Mask + blend live on each layer, not a wrapper: a wrapper would isolate the blending. */
|
|
64
|
+
.pf-layer { mask: var(--mask) center / contain no-repeat; opacity: 0; transition: opacity 0.6s; pointer-events: none; }
|
|
65
|
+
.pf-plasma { mix-blend-mode: color-dodge; }
|
|
66
|
+
.pf-glare { background: radial-gradient(circle at var(--mx) var(--my), rgb(255 255 255 / 0.85), transparent 55%); mix-blend-mode: overlay; }
|
|
67
|
+
.pf-card[data-active] .pf-plasma { opacity: var(--pf-intensity, 0.65); }
|
|
68
|
+
.pf-card[data-active] .pf-glare { opacity: 1; }
|
|
69
|
+
@media (prefers-reduced-motion: reduce) { .pf-tilt { transform: none !important; } }
|
|
70
|
+
`;
|
|
71
|
+
function initGL(cv) {
|
|
72
|
+
const gl = cv.getContext("webgl", { alpha: false, antialias: false });
|
|
73
|
+
if (!gl)
|
|
74
|
+
return null;
|
|
75
|
+
const prog = gl.createProgram();
|
|
76
|
+
for (const [type, src] of [[gl.VERTEX_SHADER, VERT], [gl.FRAGMENT_SHADER, FRAG]]) {
|
|
77
|
+
const sh = gl.createShader(type);
|
|
78
|
+
gl.shaderSource(sh, src);
|
|
79
|
+
gl.compileShader(sh);
|
|
80
|
+
if (!gl.getShaderParameter(sh, gl.COMPILE_STATUS)) {
|
|
81
|
+
console.error(gl.getShaderInfoLog(sh));
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
gl.attachShader(prog, sh);
|
|
85
|
+
}
|
|
86
|
+
gl.linkProgram(prog);
|
|
87
|
+
gl.useProgram(prog);
|
|
88
|
+
gl.bindBuffer(gl.ARRAY_BUFFER, gl.createBuffer());
|
|
89
|
+
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);
|
|
90
|
+
gl.enableVertexAttribArray(0);
|
|
91
|
+
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
|
|
92
|
+
const u = (n) => gl.getUniformLocation(prog, n);
|
|
93
|
+
return { gl, uRes: u("uRes"), uTime: u("uTime"), uPtr: u("uPtr") };
|
|
94
|
+
}
|
|
95
|
+
export function HoloBadge({ src, alt, size = 240, tilt = 14, intensity = 0.65, speed = 0.6, follow = 4, className, }) {
|
|
96
|
+
const card = useRef(null);
|
|
97
|
+
const canvas = useRef(null);
|
|
98
|
+
const opts = useRef({ tilt, speed, follow });
|
|
99
|
+
opts.current = { tilt, speed, follow }; // read live by the loop, so prop changes don't re-init WebGL
|
|
100
|
+
useEffect(() => {
|
|
101
|
+
const el = card.current;
|
|
102
|
+
const cv = canvas.current;
|
|
103
|
+
// ponytail: one WebGL context per badge; browsers cap ~16, share one offscreen context if you show more
|
|
104
|
+
const g = initGL(cv);
|
|
105
|
+
const still = matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
106
|
+
const ptr = { x: 0.5, y: 0.5 }; // raw pointer, drives tilt
|
|
107
|
+
const sp = { ...ptr }; // eased pointer, drives plasma, scrub and glare
|
|
108
|
+
let t = Math.random() * 100;
|
|
109
|
+
let raf = 0;
|
|
110
|
+
let last = 0;
|
|
111
|
+
let stopAt = Infinity;
|
|
112
|
+
const frame = (now) => {
|
|
113
|
+
const dt = Math.min((now - last) / 1000, 0.05);
|
|
114
|
+
last = now;
|
|
115
|
+
const k = 1 - Math.exp(-dt * opts.current.follow); // frame-rate independent easing
|
|
116
|
+
const dx = (ptr.x - sp.x) * k;
|
|
117
|
+
const dy = (ptr.y - sp.y) * k;
|
|
118
|
+
sp.x += dx;
|
|
119
|
+
sp.y += dy;
|
|
120
|
+
t += (still ? 0 : dt * opts.current.speed) + Math.hypot(dx, dy) * SCRUB;
|
|
121
|
+
el.style.setProperty("--mx", `${sp.x * 100}%`);
|
|
122
|
+
el.style.setProperty("--my", `${sp.y * 100}%`);
|
|
123
|
+
if (g) {
|
|
124
|
+
g.gl.uniform2f(g.uRes, cv.width, cv.height);
|
|
125
|
+
g.gl.uniform1f(g.uTime, t);
|
|
126
|
+
g.gl.uniform2f(g.uPtr, sp.x, sp.y);
|
|
127
|
+
g.gl.drawArrays(g.gl.TRIANGLES, 0, 3);
|
|
128
|
+
}
|
|
129
|
+
raf = now < stopAt ? requestAnimationFrame(frame) : 0;
|
|
130
|
+
};
|
|
131
|
+
const move = (e) => {
|
|
132
|
+
const r = el.getBoundingClientRect();
|
|
133
|
+
const max = opts.current.tilt;
|
|
134
|
+
ptr.x = Math.min(Math.max((e.clientX - r.left) / r.width, 0), 1);
|
|
135
|
+
ptr.y = Math.min(Math.max((e.clientY - r.top) / r.height, 0), 1);
|
|
136
|
+
el.style.setProperty("--rx", `${(ptr.x - 0.5) * 2 * max}deg`);
|
|
137
|
+
el.style.setProperty("--ry", `${(0.5 - ptr.y) * 2 * max}deg`);
|
|
138
|
+
el.dataset.active = "";
|
|
139
|
+
stopAt = Infinity;
|
|
140
|
+
if (!raf) {
|
|
141
|
+
const dpr = Math.min(devicePixelRatio, DPR_CAP);
|
|
142
|
+
const w = Math.round(r.width * dpr);
|
|
143
|
+
const h = Math.round(r.height * dpr);
|
|
144
|
+
if (cv.width !== w || cv.height !== h) {
|
|
145
|
+
// assigning width/height reallocates the drawing buffer, so only do it when the size changed
|
|
146
|
+
cv.width = w;
|
|
147
|
+
cv.height = h;
|
|
148
|
+
g?.gl.viewport(0, 0, w, h);
|
|
149
|
+
}
|
|
150
|
+
Object.assign(sp, ptr); // start where the pointer enters, no sweep from last position
|
|
151
|
+
last = performance.now();
|
|
152
|
+
raf = requestAnimationFrame(frame);
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
const leave = () => {
|
|
156
|
+
delete el.dataset.active;
|
|
157
|
+
el.style.setProperty("--rx", "0deg");
|
|
158
|
+
el.style.setProperty("--ry", "0deg");
|
|
159
|
+
stopAt = performance.now() + 700; // keep drawing while the fade-out transition runs
|
|
160
|
+
};
|
|
161
|
+
el.addEventListener("pointermove", move);
|
|
162
|
+
el.addEventListener("pointerleave", leave);
|
|
163
|
+
return () => {
|
|
164
|
+
el.removeEventListener("pointermove", move);
|
|
165
|
+
el.removeEventListener("pointerleave", leave);
|
|
166
|
+
cancelAnimationFrame(raf);
|
|
167
|
+
};
|
|
168
|
+
}, []);
|
|
169
|
+
return (_jsxs("div", { ref: card, className: className ? `pf-card ${className}` : "pf-card", style: { width: size, "--pf-intensity": intensity }, children: [_jsx("style", { href: "plasma-foil", precedence: "default", children: CSS }), _jsxs("div", { className: "pf-tilt", style: { "--mask": `url("${src.replace(/"/g, "%22")}")` }, children: [_jsx("img", { className: "pf-img", src: src, alt: alt, draggable: false }), _jsx("canvas", { ref: canvas, className: "pf-layer pf-plasma", "aria-hidden": true }), _jsx("div", { className: "pf-layer pf-glare", "aria-hidden": true })] })] }));
|
|
170
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "plasma-foil",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Holographic plasma hover effect for badge images (React)",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"react",
|
|
8
|
+
"holographic",
|
|
9
|
+
"plasma",
|
|
10
|
+
"badge",
|
|
11
|
+
"webgl",
|
|
12
|
+
"hover-effect"
|
|
13
|
+
],
|
|
14
|
+
"type": "module",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/HoloBadge.d.ts",
|
|
18
|
+
"default": "./dist/HoloBadge.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"scripts": {
|
|
26
|
+
"dev": "next dev",
|
|
27
|
+
"build": "next build",
|
|
28
|
+
"start": "next start",
|
|
29
|
+
"build:lib": "tsc -p tsconfig.lib.json",
|
|
30
|
+
"prepublishOnly": "npm run build:lib"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"react": ">=19"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/node": "^26.6.11",
|
|
37
|
+
"@types/react": "^19.3.3",
|
|
38
|
+
"next": "^16.4.6",
|
|
39
|
+
"react": "^19.3.1",
|
|
40
|
+
"react-dom": "^19.3.1",
|
|
41
|
+
"typescript": "^7.0.2"
|
|
42
|
+
},
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "git",
|
|
45
|
+
"url": "git+https://github.com/mdourmouch/plasma-foil.git"
|
|
46
|
+
},
|
|
47
|
+
"homepage": "https://github.com/mdourmouch/plasma-foil#readme",
|
|
48
|
+
"bugs": {
|
|
49
|
+
"url": "https://github.com/mdourmouch/plasma-foil/issues"
|
|
50
|
+
}
|
|
51
|
+
}
|