cheryglsljs 1.0.0 → 1.0.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/dist/cherry.js CHANGED
@@ -1 +1 @@
1
- !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define("chery",[],t):"object"==typeof exports?exports.chery=t():e.chery=t()}(self,(()=>(()=>{const e=new THREE.Scene,t=new THREE.PerspectiveCamera(75,window.innerWidth/window.innerHeight,.1,1e3),n=document.querySelector("canvas");n.width=400,n.height=400;const o=new THREE.WebGLRenderer({canvas:n});o.setSize(400,400);const r=new THREE.BoxGeometry,i=new THREE.MeshBasicMaterial({color:65280}),c=new THREE.Mesh(r,i);return e.add(c),t.position.z=2,function n(){requestAnimationFrame(n),gsap.to(c.rotation,{y:"+=0.01",duration:.1,overwrite:"auto"}),o.render(e,t)}(),{}})()));
1
+ !function(e,n){"object"==typeof exports&&"object"==typeof module?module.exports=n():"function"==typeof define&&define.amd?define("chery",[],n):"object"==typeof exports?exports.chery=n():e.chery=n()}(self,(()=>(()=>{"use strict";var e={d:(n,t)=>{for(var r in t)e.o(t,r)&&!e.o(n,r)&&Object.defineProperty(n,r,{enumerable:!0,get:t[r]})},o:(e,n)=>Object.prototype.hasOwnProperty.call(e,n),r:e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})}},n={};e.r(n),e.d(n,{default:()=>r});function t(e=400,n=400){const t=new THREE.Scene,r=new THREE.PerspectiveCamera(75,window.innerWidth/window.innerHeight,.1,1e3),o=new THREE.WebGLRenderer({alpha:!0});return o.setSize(e,n),{Scene:t,camera:r,renderer:o}}const r={CherryWave:({container:e,image:n,speed:r=.05,strength:o=8,hover:i=!1})=>{if(!n)return void console.log("No image provided");(new THREE.TextureLoader).load(n.src,(a=>{const u={uImage:{value:a},uTime:{value:0},uStrength:{value:i?0:o}},c=t(e.clientWidth,e.clientHeight);let d;c.camera.position.z=190;new THREE.Raycaster;const s=new THREE.Vector2;function v(){const t=e.clientWidth,r=e.clientHeight,o=t/r,i=n.naturalWidth/n.naturalHeight;let a,s;o<i?(a=t,s=t/i):(s=r,a=r*i),d&&(c.Scene.remove(d),d.geometry.dispose(),d.material.dispose()),d=function(e,n){const t=new THREE.PlaneGeometry(e,n,100,100),r=new THREE.ShaderMaterial({vertexShader:"\n varying vec2 vUv;\n varying vec3 pos;\n uniform float uTime;\n uniform float uStrength;\n\n void main() {\n \n vUv = uv;\n \n vec3 newPosition = position;\n\n // Enhanced wave effect: move vertices in all directions\n float waveX = uStrength * sin(10.0 * uv.y + uTime * 2.0);\n float waveY = uStrength * cos(12.0 * uv.x + uTime * 1.5);\n \n float waveZ = uStrength * sin(10.0 * uv.x + uTime * 2.5);\n\n newPosition.x += waveX;\n newPosition.y += waveY;\n newPosition.z += waveZ;\n\n pos = (modelViewMatrix * vec4(newPosition, 1.0)).xyz;\n gl_Position = projectionMatrix * modelViewMatrix * vec4(newPosition, 1.0);\n }\n",fragmentShader:"\nvarying vec2 vUv;\nuniform sampler2D uImage;\nuniform float uTime;\nuniform float uStrength;\nvoid main() {\n // Create a wave effect by modifying the y coordinate\n vec2 uv = vUv;\n \n \n uv.y += uStrength/400.0 * sin(10.0 * uv.x + uTime * 2.0);\n uv.x += uStrength/400.0 * sin(10.0 * uv.y + uTime * 2.0);\n \n vec4 color = texture2D(uImage, uv);\n gl_FragColor = color;\n}\n",uniforms:u});return new THREE.Mesh(t,r)}(a,s),c.mesh=d,c.Scene.add(d)}return i&&(e.addEventListener("mousemove",(function(n){const t=e.getBoundingClientRect();s.x=(n.clientX-t.left)/t.width*2-1,s.y=-(n.clientY-t.top)/t.height*2+1})),e.addEventListener("mouseenter",(()=>{gsap.to(u.uStrength,{value:o,duration:.5,ease:"power2.out"})})),e.addEventListener("mouseleave",(()=>{gsap.to(u.uStrength,{value:0,duration:.5,ease:"power2.out"})}))),e.innerHTML="",e.appendChild(c.renderer.domElement),window.addEventListener("resize",(function(){const n=e.clientWidth,t=e.clientHeight;c.renderer.setSize(n,t),c.renderer.setPixelRatio(window.devicePixelRatio),c.camera.aspect=n/t,c.camera.updateProjectionMatrix(),v()})),v(),function e(){u.uTime.value+=r,c.renderer.render(c.Scene,c.camera),requestAnimationFrame(e)}(),{mesh:c.mesh,geometry:c.geometry,renderer:c.renderer,Scene:c.Scene}}))}};return n})()));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cheryglsljs",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "\"It is threejs,glsl and gsap based js library\"",
5
5
  "main": "src/cherry.js",
6
6
  "scripts": {
@@ -24,7 +24,7 @@
24
24
  },
25
25
  "files": [
26
26
  "dist/",
27
- "src/cherry.js"
27
+ "src/"
28
28
  ],
29
29
  "devDependencies": {
30
30
  "css-loader": "^6.8.1",
@@ -0,0 +1,124 @@
1
+ import createWaveAssets from '../../utils/init.js';
2
+ import { fragmentShader } from './FragementShader.glsl.js';
3
+ import vertexShader from './Vertexshader.glsl.js';
4
+
5
+ const CherryWave = ({ container, image, speed = 0.05, strength = 8, hover = false }) => {
6
+ if (!image) {
7
+ console.log("No image provided");
8
+ return;
9
+ }
10
+
11
+ const textureLoader = new THREE.TextureLoader();
12
+ textureLoader.load(image.src, (texture) => {
13
+ const uniforms = {
14
+ uImage: { value: texture },
15
+ uTime: { value: 0.0 },
16
+ uStrength: { value: hover ? 0 : strength }
17
+ };
18
+
19
+ const waveassets = createWaveAssets(container.clientWidth, container.clientHeight);
20
+ waveassets.camera.position.z = 190;
21
+
22
+ let mesh;
23
+ const raycaster = new THREE.Raycaster();
24
+ const mouse = new THREE.Vector2();
25
+ let isHovering = false;
26
+
27
+ function createMesh(width, height) {
28
+ const geometry = new THREE.PlaneGeometry(width, height, 100, 100);
29
+ const material = new THREE.ShaderMaterial({
30
+ vertexShader,
31
+ fragmentShader,
32
+ uniforms
33
+ });
34
+ return new THREE.Mesh(geometry, material);
35
+ }
36
+
37
+ function addMesh() {
38
+ const containerWidth = container.clientWidth;
39
+ const containerHeight = container.clientHeight;
40
+ const containerAspect = containerWidth / containerHeight;
41
+ const imageAspect = image.naturalWidth / image.naturalHeight;
42
+
43
+ let scaleWidth, scaleHeight;
44
+ if (containerAspect < imageAspect) {
45
+ scaleWidth = containerWidth;
46
+ scaleHeight = containerWidth / imageAspect;
47
+ } else {
48
+ scaleHeight = containerHeight;
49
+ scaleWidth = containerHeight * imageAspect;
50
+ }
51
+
52
+ if (mesh) {
53
+ waveassets.Scene.remove(mesh);
54
+ mesh.geometry.dispose();
55
+ mesh.material.dispose();
56
+ }
57
+
58
+ mesh = createMesh(scaleWidth, scaleHeight);
59
+ waveassets.mesh = mesh;
60
+ waveassets.Scene.add(mesh);
61
+ }
62
+
63
+ function onResize() {
64
+ const width = container.clientWidth;
65
+ const height = container.clientHeight;
66
+
67
+ waveassets.renderer.setSize(width, height);
68
+ waveassets.renderer.setPixelRatio(window.devicePixelRatio);
69
+
70
+ waveassets.camera.aspect = width / height;
71
+ waveassets.camera.updateProjectionMatrix();
72
+
73
+ addMesh();
74
+ }
75
+
76
+ function onMouseMove(event) {
77
+ const bounds = container.getBoundingClientRect();
78
+ mouse.x = ((event.clientX - bounds.left) / bounds.width) * 2 - 1;
79
+ mouse.y = -((event.clientY - bounds.top) / bounds.height) * 2 + 1;
80
+ }
81
+
82
+ if (hover) {
83
+ container.addEventListener('mousemove', onMouseMove);
84
+
85
+ container.addEventListener('mouseenter', () => {
86
+ gsap.to(uniforms.uStrength, {
87
+ value: strength,
88
+ duration: 0.5,
89
+ ease: 'power2.out'
90
+ });
91
+ });
92
+
93
+ container.addEventListener('mouseleave', () => {
94
+ gsap.to(uniforms.uStrength, {
95
+ value: 0,
96
+ duration: 0.5,
97
+ ease: 'power2.out'
98
+ });
99
+ });
100
+ }
101
+
102
+ container.innerHTML = '';
103
+ container.appendChild(waveassets.renderer.domElement);
104
+ window.addEventListener('resize', onResize);
105
+ addMesh();
106
+
107
+ function animate() {
108
+ uniforms.uTime.value += speed;
109
+ waveassets.renderer.render(waveassets.Scene, waveassets.camera);
110
+ requestAnimationFrame(animate);
111
+ }
112
+
113
+ animate();
114
+
115
+ return {
116
+ mesh: waveassets.mesh,
117
+ geometry: waveassets.geometry,
118
+ renderer: waveassets.renderer,
119
+ Scene: waveassets.Scene
120
+ };
121
+ });
122
+ };
123
+
124
+ export default CherryWave;
@@ -0,0 +1,17 @@
1
+ export const fragmentShader = `
2
+ varying vec2 vUv;
3
+ uniform sampler2D uImage;
4
+ uniform float uTime;
5
+ uniform float uStrength;
6
+ void main() {
7
+ // Create a wave effect by modifying the y coordinate
8
+ vec2 uv = vUv;
9
+
10
+
11
+ uv.y += uStrength/400.0 * sin(10.0 * uv.x + uTime * 2.0);
12
+ uv.x += uStrength/400.0 * sin(10.0 * uv.y + uTime * 2.0);
13
+
14
+ vec4 color = texture2D(uImage, uv);
15
+ gl_FragColor = color;
16
+ }
17
+ `;
@@ -0,0 +1,28 @@
1
+ const vertexShader = `
2
+ varying vec2 vUv;
3
+ varying vec3 pos;
4
+ uniform float uTime;
5
+ uniform float uStrength;
6
+
7
+ void main() {
8
+
9
+ vUv = uv;
10
+
11
+ vec3 newPosition = position;
12
+
13
+ // Enhanced wave effect: move vertices in all directions
14
+ float waveX = uStrength * sin(10.0 * uv.y + uTime * 2.0);
15
+ float waveY = uStrength * cos(12.0 * uv.x + uTime * 1.5);
16
+
17
+ float waveZ = uStrength * sin(10.0 * uv.x + uTime * 2.5);
18
+
19
+ newPosition.x += waveX;
20
+ newPosition.y += waveY;
21
+ newPosition.z += waveZ;
22
+
23
+ pos = (modelViewMatrix * vec4(newPosition, 1.0)).xyz;
24
+ gl_Position = projectionMatrix * modelViewMatrix * vec4(newPosition, 1.0);
25
+ }
26
+ `;
27
+
28
+ export default vertexShader;
@@ -0,0 +1,21 @@
1
+ // src/utils/init.js
2
+ const Scene = () => new THREE.Scene();
3
+ const camera = () => new THREE.PerspectiveCamera(
4
+ 75,
5
+ window.innerWidth / window.innerHeight,
6
+ 0.1,
7
+ 1000
8
+ );
9
+ const renderer = () => new THREE.WebGLRenderer({alpha:true});
10
+
11
+ export default function createWaveAssets(width = 400, height = 400) {
12
+ const SceneInstance = Scene();
13
+ const cameraInstance = camera();
14
+ const rendererInstance = renderer();
15
+ rendererInstance.setSize(width, height);
16
+ return {
17
+ Scene: SceneInstance,
18
+ camera: cameraInstance,
19
+ renderer: rendererInstance,
20
+ };
21
+ }