cheryglsljs 1.1.14 → 1.1.15
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 +104 -22
- package/dist/cherry.js +1 -1
- package/package.json +2 -2
- package/src/cherry.js +3 -1
- package/src/effects/TransitionEffect/Effect.js +159 -0
- package/src/effects/TransitionEffect/FragementsShader.glsl.js +17 -0
- package/src/effects/TransitionEffect/VertexShader.glsl.js +9 -0
- package/src/effects/waveEffect/Effect.js +1 -1
- package/src/utils/init.js +2 -2
package/package.json
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
{
|
2
2
|
"name": "cheryglsljs",
|
3
|
-
"version": "1.1.
|
3
|
+
"version": "1.1.15",
|
4
4
|
"description": "A Three.js + GLSL-based wave effect library.",
|
5
5
|
"main": "dist/cherry.js",
|
6
|
-
"module": "dist/cherry.js",
|
6
|
+
"module": "dist/cherry.js",
|
7
7
|
"scripts": {
|
8
8
|
"build": "webpack",
|
9
9
|
"start": "webpack serve"
|
package/src/cherry.js
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
|
2
2
|
import CherryWave from "./effects/waveEffect/Effect";
|
3
3
|
import { ImageTransition1 } from "./effects/TransitionEffect1/Effect";
|
4
|
+
import { ImageTransition2 } from "./effects/TransitionEffect/Effect";
|
4
5
|
export default {
|
5
6
|
CherryWave,
|
6
|
-
ImageTransition1
|
7
|
+
ImageTransition1,
|
8
|
+
ImageTransition2
|
7
9
|
};
|
@@ -0,0 +1,159 @@
|
|
1
|
+
import createWaveAssets from "../../utils/init.js";
|
2
|
+
import fragmentShader from "./FragementsShader.glsl.js";
|
3
|
+
import vertexShader from "./VertexShader.glsl.js";
|
4
|
+
import gsap from "gsap";
|
5
|
+
import * as THREE from 'three';
|
6
|
+
export function ImageTransition2(container, { x = -1, y = 1,time = false,speed = 1.2,ttime =4.0,p =0.0,hover =false,target = null ,ttype = 0} = {}) {
|
7
|
+
|
8
|
+
if(!container) return console.error("No container provided");
|
9
|
+
const image = Array.from(container.querySelectorAll("img"));
|
10
|
+
if(!(image.length > 1)) return console.error("No images provided, at least two images are required for transition");
|
11
|
+
|
12
|
+
const ImageTransitionassets = createWaveAssets(
|
13
|
+
container.clientWidth,
|
14
|
+
container.clientHeight
|
15
|
+
);
|
16
|
+
|
17
|
+
const uniforms = {
|
18
|
+
uImage: { value: null },
|
19
|
+
uImage2: { value: null },
|
20
|
+
uTime: { value: 0.0 },
|
21
|
+
uStrength: { value: 0.02 },
|
22
|
+
uMouse: { value: new THREE.Vector2(-10, -10) },
|
23
|
+
uX:{ value: x },
|
24
|
+
uY:{ value: y }
|
25
|
+
};
|
26
|
+
|
27
|
+
const textureLoader = new THREE.TextureLoader();
|
28
|
+
let textures = [];
|
29
|
+
let index;
|
30
|
+
|
31
|
+
image.map((img, i) => {
|
32
|
+
textureLoader.load(img.src, (texture) => {
|
33
|
+
index = 0;
|
34
|
+
if (i == 1) uniforms.uImage2.value = texture;
|
35
|
+
if (i == 0) uniforms.uImage.value = texture;
|
36
|
+
textures.push(texture);
|
37
|
+
});
|
38
|
+
});
|
39
|
+
ImageTransitionassets.camera.fov = 2 * Math.atan(container.clientHeight / 2 / 600) * (180 / Math.PI);
|
40
|
+
ImageTransitionassets.camera.position.z = 2.7 -p;
|
41
|
+
|
42
|
+
let mesh;
|
43
|
+
|
44
|
+
function createMesh() {
|
45
|
+
const width = container.clientWidth;
|
46
|
+
const height = container.clientHeight;
|
47
|
+
|
48
|
+
const aspect = width / height;
|
49
|
+
const worldHeight = 2.3;
|
50
|
+
const worldWidth = worldHeight * aspect;
|
51
|
+
|
52
|
+
const geometry = new THREE.PlaneGeometry(worldWidth, worldHeight, 100, 100);
|
53
|
+
const material = new THREE.ShaderMaterial({
|
54
|
+
vertexShader: vertexShader,
|
55
|
+
fragmentShader: fragmentShader,
|
56
|
+
uniforms,
|
57
|
+
transparent: true,
|
58
|
+
});
|
59
|
+
|
60
|
+
return new THREE.Mesh(geometry, material);
|
61
|
+
}
|
62
|
+
|
63
|
+
function addMesh() {
|
64
|
+
if (mesh) {
|
65
|
+
ImageTransitionassets.Scene.remove(mesh);
|
66
|
+
mesh.geometry.dispose();
|
67
|
+
mesh.material.dispose();
|
68
|
+
}
|
69
|
+
|
70
|
+
mesh = createMesh();
|
71
|
+
ImageTransitionassets.Scene.add(mesh);
|
72
|
+
}
|
73
|
+
|
74
|
+
function onResize() {
|
75
|
+
const width = container.clientWidth;
|
76
|
+
const height = container.clientHeight;
|
77
|
+
|
78
|
+
ImageTransitionassets.renderer.setSize(width, height);
|
79
|
+
ImageTransitionassets.renderer.setPixelRatio(window.devicePixelRatio);
|
80
|
+
|
81
|
+
ImageTransitionassets.camera.aspect = width / height;
|
82
|
+
ImageTransitionassets.camera.updateProjectionMatrix();
|
83
|
+
|
84
|
+
addMesh();
|
85
|
+
}
|
86
|
+
onResize();
|
87
|
+
window.addEventListener("resize", onResize, false);
|
88
|
+
container.innerHTML = "";
|
89
|
+
container.appendChild(ImageTransitionassets.renderer.domElement);
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
|
94
|
+
if (time) {
|
95
|
+
gsap.to(uniforms.uTime, {
|
96
|
+
value: 1.0,
|
97
|
+
duration: 3,
|
98
|
+
repeat: -1,
|
99
|
+
delay: ttime,
|
100
|
+
repeatDelay: 4.0,
|
101
|
+
onRepeat: () => {
|
102
|
+
console.log("Transition completed");
|
103
|
+
uniforms.uTime.value = 0.0;
|
104
|
+
index = (index + 1) % textures.length;
|
105
|
+
uniforms.uImage.value = textures[index];
|
106
|
+
uniforms.uImage2.value = textures[(index + 1) % textures.length];
|
107
|
+
}
|
108
|
+
});
|
109
|
+
} else if (hover) {
|
110
|
+
let isAnimating = false;
|
111
|
+
container.addEventListener("mouseenter", () => {
|
112
|
+
if (isAnimating) return;
|
113
|
+
isAnimating = true;
|
114
|
+
gsap.to(uniforms.uTime, {
|
115
|
+
value: 1.0,
|
116
|
+
duration: 1.2,
|
117
|
+
onComplete: () => {
|
118
|
+
uniforms.uTime.value = 0.0;
|
119
|
+
index = (index + 1) % textures.length;
|
120
|
+
uniforms.uImage.value = textures[index];
|
121
|
+
uniforms.uImage2.value = textures[(index + 1) % textures.length];
|
122
|
+
isAnimating = false;
|
123
|
+
}
|
124
|
+
});
|
125
|
+
});
|
126
|
+
} else if (target && Array.isArray(target) && target.length === image.length) {
|
127
|
+
target.forEach((el, i) => {
|
128
|
+
let eventType;
|
129
|
+
if(ttype) eventType = "click";
|
130
|
+
else eventType = "mouseenter";
|
131
|
+
el.addEventListener(eventType, () => {
|
132
|
+
console.log(eventType);
|
133
|
+
if (index === i) return;
|
134
|
+
gsap.to(uniforms.uTime, {
|
135
|
+
value: 1.0,
|
136
|
+
duration: 1.2,
|
137
|
+
onComplete: () => {
|
138
|
+
uniforms.uTime.value = 0.0;
|
139
|
+
index = i;
|
140
|
+
uniforms.uImage.value = textures[index];
|
141
|
+
uniforms.uImage2.value = textures[(index + 1) % textures.length];
|
142
|
+
}
|
143
|
+
});
|
144
|
+
});
|
145
|
+
});
|
146
|
+
}
|
147
|
+
function animate() {
|
148
|
+
ImageTransitionassets.renderer.render(ImageTransitionassets.Scene, ImageTransitionassets.camera);
|
149
|
+
requestAnimationFrame(animate);
|
150
|
+
}
|
151
|
+
animate();
|
152
|
+
|
153
|
+
return {
|
154
|
+
assets: ImageTransitionassets,
|
155
|
+
mesh,
|
156
|
+
updateMesh: addMesh,
|
157
|
+
};
|
158
|
+
|
159
|
+
}
|
@@ -0,0 +1,17 @@
|
|
1
|
+
export default /*glsl*/`
|
2
|
+
varying vec2 vUv;
|
3
|
+
uniform float uTime;
|
4
|
+
uniform sampler2D uImage;
|
5
|
+
uniform sampler2D uImage2;
|
6
|
+
uniform float uX;
|
7
|
+
uniform float uY;
|
8
|
+
void main(){
|
9
|
+
vec2 uv = vUv;
|
10
|
+
float uvScale=uv.x+uY*uv.y;
|
11
|
+
vec4 color1 = texture2D(uImage, uv);
|
12
|
+
vec4 color2 = texture2D(uImage2, uv);
|
13
|
+
uvScale/= 2.0;
|
14
|
+
uv.x=step(uTime,fract(uX*1.0*uvScale) );
|
15
|
+
gl_FragColor = mix(color2, color1, uv.x);
|
16
|
+
}
|
17
|
+
`
|
@@ -27,7 +27,7 @@ const CherryWave = ({ container, image, speed = 0.05, strength = 0.02, hover = f
|
|
27
27
|
|
28
28
|
// Set camera to frame a 2x2 unit square
|
29
29
|
waveassets.camera.fov = 2 * Math.atan(container.clientHeight / 2 / 600) * (180 / Math.PI);
|
30
|
-
waveassets.camera.position.z = 2.8
|
30
|
+
waveassets.camera.position.z = 2.8 -p;
|
31
31
|
|
32
32
|
let mesh;
|
33
33
|
|
package/src/utils/init.js
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
import *as THREE from 'three';
|
1
|
+
// import *as THREE from 'three';
|
2
2
|
const Scene = () => new THREE.Scene();
|
3
3
|
const camera = () => new THREE.PerspectiveCamera(
|
4
4
|
75,
|
@@ -7,7 +7,7 @@ const camera = () => new THREE.PerspectiveCamera(
|
|
7
7
|
1000
|
8
8
|
);
|
9
9
|
|
10
|
-
const renderer = () => new THREE.WebGLRenderer({alpha:
|
10
|
+
const renderer = () => new THREE.WebGLRenderer({alpha:true});
|
11
11
|
|
12
12
|
export default function createWaveAssets(width = 400, height = 400) {
|
13
13
|
const SceneInstance = Scene();
|