@bbki.ng/site 1.7.0 → 1.7.1
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/CHANGELOG.md +2 -0
- package/package.json +1 -1
- package/src/components/effect-layer/EffectLayer.tsx +17 -12
- package/src/components/effect-layer/effects/grain.frag +42 -0
- package/src/components/effect-layer/hooks/useRender.ts +19 -0
- package/src/components/effect-layer/main.frag +27 -0
- package/src/components/effect-layer/shapes/circle.frag +32 -0
- package/src/components/effect-layer/shapes/heart.frag +11 -0
- package/src/components/effect-layer/uniforms.ts +19 -1
- package/src/hooks/use_mouse_position.ts +7 -4
- package/src/components/effect-layer/attributes.ts +0 -15
- package/src/components/effect-layer/shader.frag +0 -37
- package/src/components/effect-layer/useRenderHandler.ts +0 -6
package/CHANGELOG.md
CHANGED
package/package.json
CHANGED
|
@@ -1,27 +1,32 @@
|
|
|
1
|
-
import React from "react";
|
|
1
|
+
import React, { useCallback, useEffect } from "react";
|
|
2
2
|
import { Canvas } from "@bbki.ng/components";
|
|
3
|
-
import frag from "./
|
|
3
|
+
import frag from "./main.frag";
|
|
4
4
|
import vert from "./shader.vert";
|
|
5
5
|
import cls from "classnames";
|
|
6
6
|
import uniforms from "./uniforms";
|
|
7
|
-
|
|
8
|
-
const canvasDefaultCls = cls(
|
|
9
|
-
"fixed",
|
|
10
|
-
"top-0",
|
|
11
|
-
"left-0",
|
|
12
|
-
"h-full",
|
|
13
|
-
"pointer-events-none",
|
|
14
|
-
"w-full",
|
|
15
|
-
"z-[999]"
|
|
16
|
-
);
|
|
7
|
+
import { useRender } from "@/components/effect-layer/hooks/useRender";
|
|
17
8
|
|
|
18
9
|
export const EffectLayer = () => {
|
|
10
|
+
const canvasDefaultCls = cls(
|
|
11
|
+
"fixed",
|
|
12
|
+
"top-0",
|
|
13
|
+
"left-0",
|
|
14
|
+
"h-full",
|
|
15
|
+
"pointer-events-none",
|
|
16
|
+
"w-full",
|
|
17
|
+
"z-[999]"
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
const { onRender } = useRender();
|
|
21
|
+
|
|
19
22
|
return (
|
|
20
23
|
<Canvas
|
|
24
|
+
style={{ opacity: 0.1 }}
|
|
21
25
|
className={canvasDefaultCls}
|
|
22
26
|
uniforms={uniforms}
|
|
23
27
|
fragment={frag}
|
|
24
28
|
vertex={vert}
|
|
29
|
+
onRender={onRender}
|
|
25
30
|
/>
|
|
26
31
|
);
|
|
27
32
|
};
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
float rand(vec2 co){
|
|
2
|
+
return fract(
|
|
3
|
+
sin(
|
|
4
|
+
dot(
|
|
5
|
+
co.xy,
|
|
6
|
+
vec2(12.9898, 78.233)
|
|
7
|
+
)
|
|
8
|
+
) * 43758.5453
|
|
9
|
+
);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
vec4 grain(vec4 fragColor, vec2 uv){
|
|
13
|
+
vec4 color = fragColor;
|
|
14
|
+
float diff = (rand(uv) - 0.0) * 0.09;
|
|
15
|
+
color.r += diff;
|
|
16
|
+
color.g += diff;
|
|
17
|
+
color.b += diff;
|
|
18
|
+
return color;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
vec4 randGrain(vec2 uv) {
|
|
22
|
+
vec4 color = vec4(
|
|
23
|
+
rand(uv * abs(sin(uProgress))) * 0.09,
|
|
24
|
+
rand(uv * abs(sin(uProgress))) * 0.09,
|
|
25
|
+
rand(uv * abs(sin(uProgress))) * 0.09,
|
|
26
|
+
rand(uv * abs(sin(uProgress)))
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
vec4 result = grain(color, uv);
|
|
30
|
+
result.a *= 0.8;
|
|
31
|
+
|
|
32
|
+
return result;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
void drawGrainOnNav(vec2 uv) {
|
|
36
|
+
float navHeight = 64. * uDevicePixelRatio / uResolution.y;
|
|
37
|
+
|
|
38
|
+
if (uv.y <= navHeight) {
|
|
39
|
+
gl_FragColor = randGrain(uv);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import {useCallback, useState} from "react";
|
|
2
|
+
import {useMousePosition} from "@/hooks/use_mouse_position";
|
|
3
|
+
|
|
4
|
+
export const useRender = () => {
|
|
5
|
+
const [inst, setInst] = useState<any>(null)
|
|
6
|
+
const pos = useMousePosition();
|
|
7
|
+
|
|
8
|
+
const onRender = useCallback((p: any) => {
|
|
9
|
+
if (inst === null) {
|
|
10
|
+
setInst(p)
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
inst.uniforms.uMouse.value[0] = pos.current.x;
|
|
15
|
+
inst.uniforms.uMouse.value[1] = pos.current.y;
|
|
16
|
+
}, [inst]);
|
|
17
|
+
|
|
18
|
+
return { inst, onRender }
|
|
19
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
precision mediump float;
|
|
2
|
+
|
|
3
|
+
uniform vec2 uResolution;
|
|
4
|
+
uniform float uProgress;
|
|
5
|
+
uniform float uDevicePixelRatio;
|
|
6
|
+
uniform vec2 uCenter;
|
|
7
|
+
uniform vec2 uOffset;
|
|
8
|
+
|
|
9
|
+
#define DefaultColor vec4(0.0, 0.0, 0.0, 0.0)
|
|
10
|
+
#define CenterPos uCenter * uDevicePixelRatio / uResolution
|
|
11
|
+
|
|
12
|
+
#include "effects/grain.frag"
|
|
13
|
+
//#include "shapes/circle.frag"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
void main() {
|
|
17
|
+
vec2 uv = gl_FragCoord.xy / uResolution;
|
|
18
|
+
|
|
19
|
+
// set default color
|
|
20
|
+
gl_FragColor = DefaultColor;
|
|
21
|
+
|
|
22
|
+
// draw circle on mouse
|
|
23
|
+
// drawCircleUnderMouse(uv, 0.05);
|
|
24
|
+
|
|
25
|
+
// draw grain on nav
|
|
26
|
+
drawGrainOnNav(uv);
|
|
27
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
uniform vec2 uMouse;
|
|
2
|
+
|
|
3
|
+
vec4 sdfCircle(vec2 uv, float r, vec2 center) {
|
|
4
|
+
float x = uv.x;
|
|
5
|
+
float y = uv.y;
|
|
6
|
+
|
|
7
|
+
float d = distance(vec2(x, y), center) - r;
|
|
8
|
+
|
|
9
|
+
return d > 0. ? DefaultColor : vec4(1.0, 0.0, 1.0, 1.0);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
vec4 sdfSquare(vec2 uv, vec4 rect) {
|
|
13
|
+
float x = uv.x;
|
|
14
|
+
float y = uv.y;
|
|
15
|
+
|
|
16
|
+
float dx = max(abs(x - rect.x) - rect.z, 0.);
|
|
17
|
+
float dy = max(abs(y - rect.y) - rect.w, 0.);
|
|
18
|
+
|
|
19
|
+
float d = max(dx, dy);
|
|
20
|
+
|
|
21
|
+
return d > 0. ? DefaultColor : vec4(1.0, 0.0, 1.0, 1.0);
|
|
22
|
+
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
void drawCircleUnderMouse(vec2 uv, float r) {
|
|
26
|
+
// flip uMouse vertically
|
|
27
|
+
vec2 normalizedMouse = vec2(uMouse.x, uResolution.y - uMouse.y) * uDevicePixelRatio / uResolution;
|
|
28
|
+
|
|
29
|
+
// sdfCircle transition to sdfSquare
|
|
30
|
+
|
|
31
|
+
// gl_FragColor = sdfSquare(uv, vec4(normalizedMouse, 0.1, 0.1));
|
|
32
|
+
}
|
|
@@ -1,14 +1,32 @@
|
|
|
1
1
|
export default {
|
|
2
2
|
uResolution: {
|
|
3
3
|
type: "vec2",
|
|
4
|
-
value: [
|
|
4
|
+
value: [
|
|
5
|
+
Math.min(innerWidth, innerHeight),
|
|
6
|
+
Math.min(innerWidth, innerHeight),
|
|
7
|
+
],
|
|
5
8
|
},
|
|
6
9
|
uDevicePixelRatio: {
|
|
7
10
|
type: "float",
|
|
8
11
|
value: [devicePixelRatio],
|
|
9
12
|
},
|
|
13
|
+
uOffset: {
|
|
14
|
+
type: "vec2",
|
|
15
|
+
value: [
|
|
16
|
+
(innerWidth / innerHeight) > 1 ? (innerWidth - innerHeight) / 2 : 0,
|
|
17
|
+
(innerWidth / innerHeight) < 1 ? (innerHeight - innerWidth) / 2 : 0,
|
|
18
|
+
],
|
|
19
|
+
},
|
|
10
20
|
pi: {
|
|
11
21
|
type: "float",
|
|
12
22
|
value: [Math.PI],
|
|
13
23
|
},
|
|
24
|
+
uCenter: {
|
|
25
|
+
type: "vec2",
|
|
26
|
+
value: [innerWidth / 2, innerHeight / 2],
|
|
27
|
+
},
|
|
28
|
+
uMouse: {
|
|
29
|
+
type: "vec2",
|
|
30
|
+
value: [0, 0],
|
|
31
|
+
}
|
|
14
32
|
}
|
|
@@ -1,14 +1,17 @@
|
|
|
1
|
-
import {useEffect,
|
|
1
|
+
import {useEffect, useRef} from "react";
|
|
2
2
|
|
|
3
3
|
export const useMousePosition = () => {
|
|
4
|
-
const
|
|
4
|
+
const posRef = useRef<{x: number, y:number}>({x: 0, y: 0});
|
|
5
5
|
useEffect(() => {
|
|
6
6
|
const updateMousePosition = (e: MouseEvent) => {
|
|
7
|
-
|
|
7
|
+
posRef.current = {
|
|
8
|
+
x: e.clientX,
|
|
9
|
+
y: e.clientY,
|
|
10
|
+
};
|
|
8
11
|
};
|
|
9
12
|
window.addEventListener("mousemove", updateMousePosition);
|
|
10
13
|
return () => window.removeEventListener("mousemove", updateMousePosition);
|
|
11
14
|
}, []);
|
|
12
15
|
|
|
13
|
-
return
|
|
16
|
+
return posRef;
|
|
14
17
|
}
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
precision mediump float;
|
|
2
|
-
|
|
3
|
-
uniform vec2 uResolution;
|
|
4
|
-
uniform float uProgress;
|
|
5
|
-
uniform float uDevicePixelRatio;
|
|
6
|
-
|
|
7
|
-
float rand(vec2 co){
|
|
8
|
-
return fract(sin(dot(co.xy, vec2(12.9898, 78.233))) * 43758.5453);
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
vec4 grain(vec4 fragColor, vec2 uv){
|
|
12
|
-
vec4 color = fragColor;
|
|
13
|
-
float diff = (rand(uv) - 0.0) * 0.09;
|
|
14
|
-
color.r += diff;
|
|
15
|
-
color.g += diff;
|
|
16
|
-
color.b += diff;
|
|
17
|
-
return color;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
vec4 randGrain(vec2 uv) {
|
|
21
|
-
vec4 color = vec4(0.0, 0.0, 0.0, 0.1);
|
|
22
|
-
color.a = rand(uv + uProgress) * 0.1;
|
|
23
|
-
return grain(color, uv);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
void main() {
|
|
27
|
-
vec2 uv = gl_FragCoord.xy/uResolution;
|
|
28
|
-
|
|
29
|
-
float navHeight = 64. * uDevicePixelRatio / uResolution.y;
|
|
30
|
-
|
|
31
|
-
if (uv.y <= navHeight) {
|
|
32
|
-
gl_FragColor = randGrain(uv);
|
|
33
|
-
} else {
|
|
34
|
-
gl_FragColor = vec4(0., 0., 0., 0.);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
}
|