@cos-design/ripple-water 3.5.2 → 3.5.4

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/index.cjs CHANGED
@@ -1 +1,93 @@
1
- Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});require('./index.css');var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require("react");c=s(c,1);let l=require("@cos-design/shared"),u=require("react/jsx-runtime");var d={rippleWater:`cos-rippleWater-wsinI`,canvas:`cos-canvas-XOdSF`,hint:`cos-hint-0JH5D`},f=({width:e=800,height:t=500,color:n=`#38bdf8`})=>{let r=(0,c.useRef)(null),i=(0,c.useRef)([]),a=(0,c.useRef)(0),o=(0,c.useCallback)((n,a)=>{let o=r.current;if(!o)return;let s=o.getBoundingClientRect(),c=n-s.left,l=a-s.top,u=Math.max(e,t)*.45;i.current.push({x:c,y:l,radius:0,maxRadius:u,alpha:.7})},[t,e]);return(0,c.useEffect)(()=>{let e=r.current;if(!e)return;let t=e=>o(e.clientX,e.clientY),n=e=>{let t=e.touches[0];t&&o(t.clientX,t.clientY)};return e.addEventListener(`click`,t),e.addEventListener(`touchstart`,n,{passive:!0}),()=>{e.removeEventListener(`click`,t),e.removeEventListener(`touchstart`,n)}},[o]),(0,c.useEffect)(()=>{let o=r.current;if(!o)return;let s=window.devicePixelRatio||1;o.width=e*s,o.height=t*s;let c=o.getContext(`2d`);if(!c)return;c.setTransform(s,0,0,s,0,0);let u=document.hidden,d=(0,l.bindVisibilityPause)(e=>{u=e}),f=()=>{if(a.current=requestAnimationFrame(f),u)return;c.fillStyle=`#0c1929`,c.fillRect(0,0,e,t);let r=c.createLinearGradient(0,0,0,t);r.addColorStop(0,`#0f2744`),r.addColorStop(1,`#061018`),c.fillStyle=r,c.fillRect(0,0,e,t),i.current=i.current.filter(e=>(e.radius+=2.5,e.alpha-=.012,e.alpha<=0?!1:(c.beginPath(),c.arc(e.x,e.y,e.radius,0,Math.PI*2),c.strokeStyle=n,c.globalAlpha=e.alpha*(1-e.radius/e.maxRadius),c.lineWidth=2,c.stroke(),e.radius>8&&(c.beginPath(),c.arc(e.x,e.y,e.radius*.6,0,Math.PI*2),c.strokeStyle=n,c.globalAlpha=e.alpha*.4,c.lineWidth=1,c.stroke()),c.globalAlpha=1,!0)))};return f(),()=>{cancelAnimationFrame(a.current),d()}},[n,t,e]),(0,u.jsxs)(`div`,{className:d.rippleWater,style:{width:e,height:t},children:[(0,u.jsx)(`canvas`,{ref:r,className:d.canvas,style:{width:e,height:t}}),(0,u.jsx)(`span`,{className:d.hint,children:`点击水面产生涟漪`})]})};exports.RippleWater=f,exports.default=f;
1
+ Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});require('./index.css');var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));let c=require("react");c=s(c,1);let l=require("@cos-design/shared"),u=require("react/jsx-runtime");var d={rippleWater:`cos-rippleWater-wsinI`,canvas:`cos-canvas-XOdSF`,hint:`cos-hint-0JH5D`},f=`
2
+ attribute vec2 a_pos;
3
+ varying vec2 v_uv;
4
+ void main() {
5
+ v_uv = a_pos * 0.5 + 0.5;
6
+ gl_Position = vec4(a_pos, 0.0, 1.0);
7
+ }
8
+ `,p=`
9
+ precision highp float;
10
+
11
+ uniform float u_time;
12
+ uniform vec2 u_res;
13
+ uniform vec3 u_tint;
14
+ uniform vec3 u_from;
15
+ uniform vec3 u_to;
16
+ uniform sampler2D u_height;
17
+ uniform vec2 u_sim;
18
+ uniform float u_waveAmp;
19
+ uniform float u_waveSpeed;
20
+ uniform float u_shimmer;
21
+ uniform float u_reflection;
22
+
23
+ varying vec2 v_uv;
24
+
25
+ float hash(vec2 p) {
26
+ return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453);
27
+ }
28
+
29
+ float ambientH(vec2 p, float t) {
30
+ float h = 0.0;
31
+ h += sin(p.x * 3.2 + t * 1.3) * cos(p.y * 2.4 - t * 0.9) * 0.45;
32
+ h += sin(p.x * 6.8 - t * 1.7 + p.y * 1.1) * 0.22;
33
+ h += sin(p.x * 12.0 + p.y * 9.0 + t * 2.4) * 0.08;
34
+ h += sin(p.x * 1.4 + p.y * 1.8 + t * 0.55) * 0.35;
35
+ return h;
36
+ }
37
+
38
+ float sampleHeight(vec2 uv) {
39
+ float rip = texture2D(u_height, uv).r * 2.0 - 1.0;
40
+ vec2 p = uv * vec2(u_res.x / u_res.y, 1.0) * 2.8;
41
+ float amb = ambientH(p, u_time * u_waveSpeed) * 0.035 * u_waveAmp;
42
+ return rip * 0.55 + amb;
43
+ }
44
+
45
+ vec3 calcNormal(vec2 uv) {
46
+ vec2 e = vec2(1.5 / u_sim.x, 1.5 / u_sim.y);
47
+ float hL = sampleHeight(uv - vec2(e.x, 0.0));
48
+ float hR = sampleHeight(uv + vec2(e.x, 0.0));
49
+ float hD = sampleHeight(uv - vec2(0.0, e.y));
50
+ float hU = sampleHeight(uv + vec2(0.0, e.y));
51
+ return normalize(vec3((hL - hR) * 14.0, (hD - hU) * 14.0, 1.0));
52
+ }
53
+
54
+ void main() {
55
+ vec2 uv = v_uv;
56
+ vec3 n = calcNormal(uv);
57
+ float h = sampleHeight(uv);
58
+
59
+ vec3 V = normalize(vec3(0.0, 0.25, 1.0));
60
+ vec3 L = normalize(vec3(0.45, 0.75, 0.55));
61
+
62
+ float ndotl = max(dot(n, L), 0.0);
63
+ float fresnel = pow(1.0 - max(dot(n, V), 0.0), 3.2);
64
+
65
+ float g = clamp((uv.x + (1.0 - uv.y)) * 0.5, 0.0, 1.0);
66
+ vec3 water = mix(u_from, u_to, g);
67
+ water = mix(water, u_from, clamp(h * 1.8 + ndotl * 0.12, 0.0, 0.28));
68
+ water *= 0.92 + n.y * 0.1;
69
+
70
+ vec3 sky = mix(u_from * 1.15, vec3(0.85, 0.92, 1.0), 0.45);
71
+ vec3 reflectCol = mix(sky, vec3(0.9, 0.95, 1.0), pow(max(n.y, 0.0), 2.0));
72
+ water = mix(water, reflectCol, fresnel * u_reflection);
73
+
74
+ vec3 H = normalize(L + V);
75
+ float spec = pow(max(dot(n, H), 0.0), 180.0);
76
+ float glitter = pow(max(dot(n, H), 0.0), 48.0);
77
+ float sparkThresh = step(0.992, glitter) * glitter;
78
+ vec3 sparkle = u_tint * (spec * 1.8 + sparkThresh * 2.5) * u_shimmer
79
+ + vec3(1.0) * sparkThresh * 0.8 * u_shimmer;
80
+
81
+ float softSpec = pow(max(dot(n, H), 0.0), 16.0) * 0.22 * u_shimmer;
82
+ water += u_tint * softSpec + sparkle;
83
+
84
+ float crest = smoothstep(0.02, 0.12, abs(h)) * fresnel;
85
+ water += mix(u_tint, vec3(1.0), 0.4) * crest * 0.35;
86
+
87
+ float vig = smoothstep(1.15, 0.35, length((uv - 0.5) * vec2(1.1, 1.0)));
88
+ water *= 0.88 + 0.12 * vig;
89
+ water += (hash(uv * u_res + u_time) - 0.5) * 0.015;
90
+
91
+ gl_FragColor = vec4(water, 1.0);
92
+ }
93
+ `,m=e=>{let t=e.replace(`#`,``),n=t.length===3?t.split(``).map(e=>e+e).join(``):t,r=Number.parseInt(n,16);return Number.isNaN(r)?[.22,.74,.97]:[(r>>16&255)/255,(r>>8&255)/255,(r&255)/255]},h=(e,t,n)=>{let r=e.createShader(t);return r?(e.shaderSource(r,n),e.compileShader(r),e.getShaderParameter(r,e.COMPILE_STATUS)?r:(e.deleteShader(r),null)):null},g=(e,t,n)=>{let r=h(e,e.VERTEX_SHADER,t),i=h(e,e.FRAGMENT_SHADER,n);if(!r||!i)return null;let a=e.createProgram();return a?(e.attachShader(a,r),e.attachShader(a,i),e.linkProgram(a),e.deleteShader(r),e.deleteShader(i),e.getProgramParameter(a,e.LINK_STATUS)?a:(e.deleteProgram(a),null)):null},_=192,v=e=>({from:m(e.fromColor),to:m(e.toColor),tint:m(e.color),waveAmplitude:e.waveAmplitude,waveSpeed:e.waveSpeed,shimmer:e.shimmer,reflection:e.reflection,rippleStrength:e.rippleStrength,rippleRadius:e.rippleRadius,damping:e.damping,spread:e.spread,interactive:e.interactive}),y=({width:e=800,height:t=500,fromColor:n=`#52ade3`,toColor:r=`#013565`,color:i=`#a8d8f5`,waveAmplitude:a=1,waveSpeed:o=1,shimmer:s=1,reflection:m=.38,rippleStrength:h=1,rippleRadius:y=6,damping:b=.985,spread:x=.5,interactive:S=!0,showHint:C=!0,hint:w=`点击水面产生涟漪`})=>{let T=(0,c.useRef)(null),E=(0,c.useRef)(0),D=(0,c.useRef)(null),O=(0,c.useRef)(v({fromColor:n,toColor:r,color:i,waveAmplitude:a,waveSpeed:o,shimmer:s,reflection:m,rippleStrength:h,rippleRadius:y,damping:b,spread:x,interactive:S}));(0,c.useEffect)(()=>{O.current=v({fromColor:n,toColor:r,color:i,waveAmplitude:a,waveSpeed:o,shimmer:s,reflection:m,rippleStrength:h,rippleRadius:y,damping:b,spread:x,interactive:S})},[n,r,i,a,o,s,m,h,y,b,x,S]);let k=(0,c.useCallback)((e,t)=>{if(!O.current.interactive)return;let n=T.current;if(!n)return;let r=n.getBoundingClientRect();D.current={x:(e-r.left)/r.width,y:1-(t-r.top)/r.height,strength:O.current.rippleStrength}},[]);return(0,c.useEffect)(()=>{let e=T.current;if(!e)return;let t=e=>{e.pointerType===`mouse`&&e.button!==0||k(e.clientX,e.clientY)};return e.addEventListener(`pointerdown`,t),()=>e.removeEventListener(`pointerdown`,t)},[k]),(0,c.useEffect)(()=>{let n=T.current;if(!n)return;let r=Math.min(window.devicePixelRatio||1,2);n.width=Math.floor(e*r),n.height=Math.floor(t*r);let i=n.getContext(`webgl`,{alpha:!1,antialias:!1,depth:!1,stencil:!1,premultipliedAlpha:!1});if(!i)return;let a=g(i,f,p);if(!a)return;let o=i.createBuffer();i.bindBuffer(i.ARRAY_BUFFER,o),i.bufferData(i.ARRAY_BUFFER,new Float32Array([-1,-1,1,-1,-1,1,1,1]),i.STATIC_DRAW);let s=i.getAttribLocation(a,`a_pos`),c=i.getUniformLocation(a,`u_time`),u=i.getUniformLocation(a,`u_res`),d=i.getUniformLocation(a,`u_tint`),m=i.getUniformLocation(a,`u_from`),h=i.getUniformLocation(a,`u_to`),v=i.getUniformLocation(a,`u_height`),y=i.getUniformLocation(a,`u_sim`),b=i.getUniformLocation(a,`u_waveAmp`),x=i.getUniformLocation(a,`u_waveSpeed`),S=i.getUniformLocation(a,`u_shimmer`),C=i.getUniformLocation(a,`u_reflection`),w=_*_,k=new Float32Array(w),A=new Float32Array(w),j=new Float32Array(w),M=i.createTexture();i.bindTexture(i.TEXTURE_2D,M),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_MIN_FILTER,i.LINEAR),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_MAG_FILTER,i.LINEAR),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_WRAP_S,i.CLAMP_TO_EDGE),i.texParameteri(i.TEXTURE_2D,i.TEXTURE_WRAP_T,i.CLAMP_TO_EDGE);let N=new Uint8Array(w*4),P=()=>{for(let e=0;e<w;e++){let t=Math.max(0,Math.min(255,Math.floor((A[e]*.5+.5)*255))),n=e*4;N[n]=t,N[n+1]=t,N[n+2]=t,N[n+3]=255}i.bindTexture(i.TEXTURE_2D,M),i.texImage2D(i.TEXTURE_2D,0,i.RGBA,_,_,0,i.RGBA,i.UNSIGNED_BYTE,N)},F=(e,t,n,r)=>{let i=e*(_-1),a=t*(_-1),o=(0,l.clamp)(r,2,12);for(let e=-o-1;e<=o+1;e++)for(let t=-o-1;t<=o+1;t++){let r=Math.round(i+t),s=Math.round(a+e);if(r<=0||r>=_-1||s<=0||s>=_-1)continue;let c=Math.sqrt(t*t+e*e),l=Math.exp(-c*c*.55)*-.35,u=Math.exp(-((c-o*.55)**2)*.9);A[s*_+r]+=n*(l+u*1.1)}},I=(e,t)=>{let n=(0,l.clamp)(e,.9,.999),r=(0,l.clamp)(t,.3,.7);for(let e=0;e<2;e++){for(let e=1;e<_-1;e++){let t=e*_;for(let e=1;e<_-1;e++){let i=t+e,a=A[i-1]+A[i+1]+A[i-_]+A[i+_];j[i]=(a*r-k[i])*n,j[i]>1.5?j[i]=1.5:j[i]<-1.5&&(j[i]=-1.5)}}let e=k;k=A,A=j,j=e,j.fill(0)}},L=e=>{let t=O.current;i.viewport(0,0,n.width,n.height),i.useProgram(a),i.bindBuffer(i.ARRAY_BUFFER,o),i.enableVertexAttribArray(s),i.vertexAttribPointer(s,2,i.FLOAT,!1,0,0),i.activeTexture(i.TEXTURE0),i.bindTexture(i.TEXTURE_2D,M),i.uniform1i(v,0),i.uniform1f(c,e),i.uniform2f(u,n.width,n.height),i.uniform3f(d,t.tint[0],t.tint[1],t.tint[2]),i.uniform3f(m,t.from[0],t.from[1],t.from[2]),i.uniform3f(h,t.to[0],t.to[1],t.to[2]),i.uniform2f(y,_,_),i.uniform1f(b,(0,l.clamp)(t.waveAmplitude,0,2)),i.uniform1f(x,(0,l.clamp)(t.waveSpeed,0,3)),i.uniform1f(S,(0,l.clamp)(t.shimmer,0,2)),i.uniform1f(C,(0,l.clamp)(t.reflection,0,1)),i.drawArrays(i.TRIANGLE_STRIP,0,4)};if(P(),typeof window<`u`&&window.matchMedia(`(prefers-reduced-motion: reduce)`).matches)return L(0),()=>{i.deleteTexture(M),i.deleteBuffer(o),i.deleteProgram(a)};let R=document.hidden,z=(0,l.bindVisibilityPause)(e=>{R=e}),B=performance.now(),V=e=>{if(E.current=requestAnimationFrame(V),R)return;let t=O.current;if(D.current){let e=D.current,n=(0,l.clamp)(e.strength,0,3),r=t.rippleRadius;F(e.x,e.y,n,r),F(e.x+.004,e.y-.003,n*.35,r),D.current=null}I(t.damping,t.spread),P(),L((e-B)/1e3)};return E.current=requestAnimationFrame(V),()=>{cancelAnimationFrame(E.current),z(),i.deleteTexture(M),i.deleteBuffer(o),i.deleteProgram(a)}},[t,e]),(0,u.jsxs)(`div`,{className:d.rippleWater,style:{width:e,height:t},children:[(0,u.jsx)(`canvas`,{ref:T,className:d.canvas,style:{width:e,height:t,cursor:S?`pointer`:`default`},role:`img`,"aria-label":w||`涟漪水面`}),C?(0,u.jsx)(`span`,{className:d.hint,"aria-hidden":!0,children:w}):null]})};exports.RippleWater=y,exports.default=y;
package/dist/index.js CHANGED
@@ -1,74 +1,202 @@
1
1
  import { useCallback as e, useEffect as t, useRef as n } from "react";
2
- import { bindVisibilityPause as r } from "@cos-design/shared";
3
- import { jsx as i, jsxs as a } from "react/jsx-runtime";
4
- import './index.css';var o = {
2
+ import { bindVisibilityPause as r, clamp as i } from "@cos-design/shared";
3
+ import { jsx as a, jsxs as o } from "react/jsx-runtime";
4
+ import './index.css';var s = {
5
5
  rippleWater: "cos-rippleWater-wsinI",
6
6
  canvas: "cos-canvas-XOdSF",
7
7
  hint: "cos-hint-0JH5D"
8
- }, s = ({ width: s = 800, height: c = 500, color: l = "#38bdf8" }) => {
9
- let u = n(null), d = n([]), f = n(0), p = e((e, t) => {
10
- let n = u.current;
11
- if (!n) return;
12
- let r = n.getBoundingClientRect(), i = e - r.left, a = t - r.top, o = Math.max(s, c) * .45;
13
- d.current.push({
14
- x: i,
15
- y: a,
16
- radius: 0,
17
- maxRadius: o,
18
- alpha: .7
8
+ }, c = "\nattribute vec2 a_pos;\nvarying vec2 v_uv;\nvoid main() {\n v_uv = a_pos * 0.5 + 0.5;\n gl_Position = vec4(a_pos, 0.0, 1.0);\n}\n", l = "\nprecision highp float;\n\nuniform float u_time;\nuniform vec2 u_res;\nuniform vec3 u_tint;\nuniform vec3 u_from;\nuniform vec3 u_to;\nuniform sampler2D u_height;\nuniform vec2 u_sim;\nuniform float u_waveAmp;\nuniform float u_waveSpeed;\nuniform float u_shimmer;\nuniform float u_reflection;\n\nvarying vec2 v_uv;\n\nfloat hash(vec2 p) {\n return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453);\n}\n\nfloat ambientH(vec2 p, float t) {\n float h = 0.0;\n h += sin(p.x * 3.2 + t * 1.3) * cos(p.y * 2.4 - t * 0.9) * 0.45;\n h += sin(p.x * 6.8 - t * 1.7 + p.y * 1.1) * 0.22;\n h += sin(p.x * 12.0 + p.y * 9.0 + t * 2.4) * 0.08;\n h += sin(p.x * 1.4 + p.y * 1.8 + t * 0.55) * 0.35;\n return h;\n}\n\nfloat sampleHeight(vec2 uv) {\n float rip = texture2D(u_height, uv).r * 2.0 - 1.0;\n vec2 p = uv * vec2(u_res.x / u_res.y, 1.0) * 2.8;\n float amb = ambientH(p, u_time * u_waveSpeed) * 0.035 * u_waveAmp;\n return rip * 0.55 + amb;\n}\n\nvec3 calcNormal(vec2 uv) {\n vec2 e = vec2(1.5 / u_sim.x, 1.5 / u_sim.y);\n float hL = sampleHeight(uv - vec2(e.x, 0.0));\n float hR = sampleHeight(uv + vec2(e.x, 0.0));\n float hD = sampleHeight(uv - vec2(0.0, e.y));\n float hU = sampleHeight(uv + vec2(0.0, e.y));\n return normalize(vec3((hL - hR) * 14.0, (hD - hU) * 14.0, 1.0));\n}\n\nvoid main() {\n vec2 uv = v_uv;\n vec3 n = calcNormal(uv);\n float h = sampleHeight(uv);\n\n vec3 V = normalize(vec3(0.0, 0.25, 1.0));\n vec3 L = normalize(vec3(0.45, 0.75, 0.55));\n\n float ndotl = max(dot(n, L), 0.0);\n float fresnel = pow(1.0 - max(dot(n, V), 0.0), 3.2);\n\n float g = clamp((uv.x + (1.0 - uv.y)) * 0.5, 0.0, 1.0);\n vec3 water = mix(u_from, u_to, g);\n water = mix(water, u_from, clamp(h * 1.8 + ndotl * 0.12, 0.0, 0.28));\n water *= 0.92 + n.y * 0.1;\n\n vec3 sky = mix(u_from * 1.15, vec3(0.85, 0.92, 1.0), 0.45);\n vec3 reflectCol = mix(sky, vec3(0.9, 0.95, 1.0), pow(max(n.y, 0.0), 2.0));\n water = mix(water, reflectCol, fresnel * u_reflection);\n\n vec3 H = normalize(L + V);\n float spec = pow(max(dot(n, H), 0.0), 180.0);\n float glitter = pow(max(dot(n, H), 0.0), 48.0);\n float sparkThresh = step(0.992, glitter) * glitter;\n vec3 sparkle = u_tint * (spec * 1.8 + sparkThresh * 2.5) * u_shimmer\n + vec3(1.0) * sparkThresh * 0.8 * u_shimmer;\n\n float softSpec = pow(max(dot(n, H), 0.0), 16.0) * 0.22 * u_shimmer;\n water += u_tint * softSpec + sparkle;\n\n float crest = smoothstep(0.02, 0.12, abs(h)) * fresnel;\n water += mix(u_tint, vec3(1.0), 0.4) * crest * 0.35;\n\n float vig = smoothstep(1.15, 0.35, length((uv - 0.5) * vec2(1.1, 1.0)));\n water *= 0.88 + 0.12 * vig;\n water += (hash(uv * u_res + u_time) - 0.5) * 0.015;\n\n gl_FragColor = vec4(water, 1.0);\n}\n", u = (e) => {
9
+ let t = e.replace("#", ""), n = t.length === 3 ? t.split("").map((e) => e + e).join("") : t, r = Number.parseInt(n, 16);
10
+ return Number.isNaN(r) ? [
11
+ .22,
12
+ .74,
13
+ .97
14
+ ] : [
15
+ (r >> 16 & 255) / 255,
16
+ (r >> 8 & 255) / 255,
17
+ (r & 255) / 255
18
+ ];
19
+ }, d = (e, t, n) => {
20
+ let r = e.createShader(t);
21
+ return r ? (e.shaderSource(r, n), e.compileShader(r), e.getShaderParameter(r, e.COMPILE_STATUS) ? r : (e.deleteShader(r), null)) : null;
22
+ }, f = (e, t, n) => {
23
+ let r = d(e, e.VERTEX_SHADER, t), i = d(e, e.FRAGMENT_SHADER, n);
24
+ if (!r || !i) return null;
25
+ let a = e.createProgram();
26
+ return a ? (e.attachShader(a, r), e.attachShader(a, i), e.linkProgram(a), e.deleteShader(r), e.deleteShader(i), e.getProgramParameter(a, e.LINK_STATUS) ? a : (e.deleteProgram(a), null)) : null;
27
+ }, p = 192, m = (e) => ({
28
+ from: u(e.fromColor),
29
+ to: u(e.toColor),
30
+ tint: u(e.color),
31
+ waveAmplitude: e.waveAmplitude,
32
+ waveSpeed: e.waveSpeed,
33
+ shimmer: e.shimmer,
34
+ reflection: e.reflection,
35
+ rippleStrength: e.rippleStrength,
36
+ rippleRadius: e.rippleRadius,
37
+ damping: e.damping,
38
+ spread: e.spread,
39
+ interactive: e.interactive
40
+ }), h = ({ width: u = 800, height: d = 500, fromColor: h = "#52ade3", toColor: g = "#013565", color: _ = "#a8d8f5", waveAmplitude: v = 1, waveSpeed: y = 1, shimmer: b = 1, reflection: x = .38, rippleStrength: S = 1, rippleRadius: C = 6, damping: w = .985, spread: T = .5, interactive: E = !0, showHint: D = !0, hint: O = "点击水面产生涟漪" }) => {
41
+ let k = n(null), A = n(0), j = n(null), M = n(m({
42
+ fromColor: h,
43
+ toColor: g,
44
+ color: _,
45
+ waveAmplitude: v,
46
+ waveSpeed: y,
47
+ shimmer: b,
48
+ reflection: x,
49
+ rippleStrength: S,
50
+ rippleRadius: C,
51
+ damping: w,
52
+ spread: T,
53
+ interactive: E
54
+ }));
55
+ t(() => {
56
+ M.current = m({
57
+ fromColor: h,
58
+ toColor: g,
59
+ color: _,
60
+ waveAmplitude: v,
61
+ waveSpeed: y,
62
+ shimmer: b,
63
+ reflection: x,
64
+ rippleStrength: S,
65
+ rippleRadius: C,
66
+ damping: w,
67
+ spread: T,
68
+ interactive: E
19
69
  });
20
- }, [c, s]);
70
+ }, [
71
+ h,
72
+ g,
73
+ _,
74
+ v,
75
+ y,
76
+ b,
77
+ x,
78
+ S,
79
+ C,
80
+ w,
81
+ T,
82
+ E
83
+ ]);
84
+ let N = e((e, t) => {
85
+ if (!M.current.interactive) return;
86
+ let n = k.current;
87
+ if (!n) return;
88
+ let r = n.getBoundingClientRect();
89
+ j.current = {
90
+ x: (e - r.left) / r.width,
91
+ y: 1 - (t - r.top) / r.height,
92
+ strength: M.current.rippleStrength
93
+ };
94
+ }, []);
21
95
  return t(() => {
22
- let e = u.current;
96
+ let e = k.current;
23
97
  if (!e) return;
24
- let t = (e) => p(e.clientX, e.clientY), n = (e) => {
25
- let t = e.touches[0];
26
- t && p(t.clientX, t.clientY);
98
+ let t = (e) => {
99
+ e.pointerType === "mouse" && e.button !== 0 || N(e.clientX, e.clientY);
27
100
  };
28
- return e.addEventListener("click", t), e.addEventListener("touchstart", n, { passive: !0 }), () => {
29
- e.removeEventListener("click", t), e.removeEventListener("touchstart", n);
30
- };
31
- }, [p]), t(() => {
32
- let e = u.current;
101
+ return e.addEventListener("pointerdown", t), () => e.removeEventListener("pointerdown", t);
102
+ }, [N]), t(() => {
103
+ let e = k.current;
33
104
  if (!e) return;
34
- let t = window.devicePixelRatio || 1;
35
- e.width = s * t, e.height = c * t;
36
- let n = e.getContext("2d");
105
+ let t = Math.min(window.devicePixelRatio || 1, 2);
106
+ e.width = Math.floor(u * t), e.height = Math.floor(d * t);
107
+ let n = e.getContext("webgl", {
108
+ alpha: !1,
109
+ antialias: !1,
110
+ depth: !1,
111
+ stencil: !1,
112
+ premultipliedAlpha: !1
113
+ });
37
114
  if (!n) return;
38
- n.setTransform(t, 0, 0, t, 0, 0);
39
- let i = document.hidden, a = r((e) => {
40
- i = e;
41
- }), o = () => {
42
- if (f.current = requestAnimationFrame(o), i) return;
43
- n.fillStyle = "#0c1929", n.fillRect(0, 0, s, c);
44
- let e = n.createLinearGradient(0, 0, 0, c);
45
- e.addColorStop(0, "#0f2744"), e.addColorStop(1, "#061018"), n.fillStyle = e, n.fillRect(0, 0, s, c), d.current = d.current.filter((e) => (e.radius += 2.5, e.alpha -= .012, e.alpha <= 0 ? !1 : (n.beginPath(), n.arc(e.x, e.y, e.radius, 0, Math.PI * 2), n.strokeStyle = l, n.globalAlpha = e.alpha * (1 - e.radius / e.maxRadius), n.lineWidth = 2, n.stroke(), e.radius > 8 && (n.beginPath(), n.arc(e.x, e.y, e.radius * .6, 0, Math.PI * 2), n.strokeStyle = l, n.globalAlpha = e.alpha * .4, n.lineWidth = 1, n.stroke()), n.globalAlpha = 1, !0)));
115
+ let a = f(n, c, l);
116
+ if (!a) return;
117
+ let o = n.createBuffer();
118
+ n.bindBuffer(n.ARRAY_BUFFER, o), n.bufferData(n.ARRAY_BUFFER, new Float32Array([
119
+ -1,
120
+ -1,
121
+ 1,
122
+ -1,
123
+ -1,
124
+ 1,
125
+ 1,
126
+ 1
127
+ ]), n.STATIC_DRAW);
128
+ let s = n.getAttribLocation(a, "a_pos"), m = n.getUniformLocation(a, "u_time"), h = n.getUniformLocation(a, "u_res"), g = n.getUniformLocation(a, "u_tint"), _ = n.getUniformLocation(a, "u_from"), v = n.getUniformLocation(a, "u_to"), y = n.getUniformLocation(a, "u_height"), b = n.getUniformLocation(a, "u_sim"), x = n.getUniformLocation(a, "u_waveAmp"), S = n.getUniformLocation(a, "u_waveSpeed"), C = n.getUniformLocation(a, "u_shimmer"), w = n.getUniformLocation(a, "u_reflection"), T = p * p, E = new Float32Array(T), D = new Float32Array(T), O = new Float32Array(T), N = n.createTexture();
129
+ n.bindTexture(n.TEXTURE_2D, N), n.texParameteri(n.TEXTURE_2D, n.TEXTURE_MIN_FILTER, n.LINEAR), n.texParameteri(n.TEXTURE_2D, n.TEXTURE_MAG_FILTER, n.LINEAR), n.texParameteri(n.TEXTURE_2D, n.TEXTURE_WRAP_S, n.CLAMP_TO_EDGE), n.texParameteri(n.TEXTURE_2D, n.TEXTURE_WRAP_T, n.CLAMP_TO_EDGE);
130
+ let P = new Uint8Array(T * 4), F = () => {
131
+ for (let e = 0; e < T; e++) {
132
+ let t = Math.max(0, Math.min(255, Math.floor((D[e] * .5 + .5) * 255))), n = e * 4;
133
+ P[n] = t, P[n + 1] = t, P[n + 2] = t, P[n + 3] = 255;
134
+ }
135
+ n.bindTexture(n.TEXTURE_2D, N), n.texImage2D(n.TEXTURE_2D, 0, n.RGBA, p, p, 0, n.RGBA, n.UNSIGNED_BYTE, P);
136
+ }, I = (e, t, n, r) => {
137
+ let a = e * (p - 1), o = t * (p - 1), s = i(r, 2, 12);
138
+ for (let e = -s - 1; e <= s + 1; e++) for (let t = -s - 1; t <= s + 1; t++) {
139
+ let r = Math.round(a + t), i = Math.round(o + e);
140
+ if (r <= 0 || r >= p - 1 || i <= 0 || i >= p - 1) continue;
141
+ let c = Math.sqrt(t * t + e * e), l = Math.exp(-c * c * .55) * -.35, u = Math.exp(-((c - s * .55) ** 2) * .9);
142
+ D[i * p + r] += n * (l + u * 1.1);
143
+ }
144
+ }, L = (e, t) => {
145
+ let n = i(e, .9, .999), r = i(t, .3, .7);
146
+ for (let e = 0; e < 2; e++) {
147
+ for (let e = 1; e < p - 1; e++) {
148
+ let t = e * p;
149
+ for (let e = 1; e < p - 1; e++) {
150
+ let i = t + e, a = D[i - 1] + D[i + 1] + D[i - p] + D[i + p];
151
+ O[i] = (a * r - E[i]) * n, O[i] > 1.5 ? O[i] = 1.5 : O[i] < -1.5 && (O[i] = -1.5);
152
+ }
153
+ }
154
+ let e = E;
155
+ E = D, D = O, O = e, O.fill(0);
156
+ }
157
+ }, R = (t) => {
158
+ let r = M.current;
159
+ n.viewport(0, 0, e.width, e.height), n.useProgram(a), n.bindBuffer(n.ARRAY_BUFFER, o), n.enableVertexAttribArray(s), n.vertexAttribPointer(s, 2, n.FLOAT, !1, 0, 0), n.activeTexture(n.TEXTURE0), n.bindTexture(n.TEXTURE_2D, N), n.uniform1i(y, 0), n.uniform1f(m, t), n.uniform2f(h, e.width, e.height), n.uniform3f(g, r.tint[0], r.tint[1], r.tint[2]), n.uniform3f(_, r.from[0], r.from[1], r.from[2]), n.uniform3f(v, r.to[0], r.to[1], r.to[2]), n.uniform2f(b, p, p), n.uniform1f(x, i(r.waveAmplitude, 0, 2)), n.uniform1f(S, i(r.waveSpeed, 0, 3)), n.uniform1f(C, i(r.shimmer, 0, 2)), n.uniform1f(w, i(r.reflection, 0, 1)), n.drawArrays(n.TRIANGLE_STRIP, 0, 4);
46
160
  };
47
- return o(), () => {
48
- cancelAnimationFrame(f.current), a();
161
+ if (F(), typeof window < "u" && window.matchMedia("(prefers-reduced-motion: reduce)").matches) return R(0), () => {
162
+ n.deleteTexture(N), n.deleteBuffer(o), n.deleteProgram(a);
49
163
  };
50
- }, [
51
- l,
52
- c,
53
- s
54
- ]), /* @__PURE__ */ a("div", {
55
- className: o.rippleWater,
164
+ let z = document.hidden, B = r((e) => {
165
+ z = e;
166
+ }), V = performance.now(), H = (e) => {
167
+ if (A.current = requestAnimationFrame(H), z) return;
168
+ let t = M.current;
169
+ if (j.current) {
170
+ let e = j.current, n = i(e.strength, 0, 3), r = t.rippleRadius;
171
+ I(e.x, e.y, n, r), I(e.x + .004, e.y - .003, n * .35, r), j.current = null;
172
+ }
173
+ L(t.damping, t.spread), F(), R((e - V) / 1e3);
174
+ };
175
+ return A.current = requestAnimationFrame(H), () => {
176
+ cancelAnimationFrame(A.current), B(), n.deleteTexture(N), n.deleteBuffer(o), n.deleteProgram(a);
177
+ };
178
+ }, [d, u]), /* @__PURE__ */ o("div", {
179
+ className: s.rippleWater,
56
180
  style: {
57
- width: s,
58
- height: c
181
+ width: u,
182
+ height: d
59
183
  },
60
- children: [/* @__PURE__ */ i("canvas", {
61
- ref: u,
62
- className: o.canvas,
184
+ children: [/* @__PURE__ */ a("canvas", {
185
+ ref: k,
186
+ className: s.canvas,
63
187
  style: {
64
- width: s,
65
- height: c
66
- }
67
- }), /* @__PURE__ */ i("span", {
68
- className: o.hint,
69
- children: "点击水面产生涟漪"
70
- })]
188
+ width: u,
189
+ height: d,
190
+ cursor: E ? "pointer" : "default"
191
+ },
192
+ role: "img",
193
+ "aria-label": O || "涟漪水面"
194
+ }), D ? /* @__PURE__ */ a("span", {
195
+ className: s.hint,
196
+ "aria-hidden": !0,
197
+ children: O
198
+ }) : null]
71
199
  });
72
200
  };
73
201
  //#endregion
74
- export { s as RippleWater, s as default };
202
+ export { h as RippleWater, h as default };
@@ -2,8 +2,34 @@ import { default as React } from 'react';
2
2
  export interface RippleWaterProps {
3
3
  width?: number;
4
4
  height?: number;
5
- /** 涟漪颜色 */
5
+ /** 水面渐变浅端(左上) */
6
+ fromColor?: string;
7
+ /** 水面渐变深端(右下) */
8
+ toColor?: string;
9
+ /** 涟漪高光色 */
6
10
  color?: string;
11
+ /** 环境波浪强度 0~2,默认 1 */
12
+ waveAmplitude?: number;
13
+ /** 环境波浪速度 0~3,默认 1 */
14
+ waveSpeed?: number;
15
+ /** 波光闪烁强度 0~2,默认 1 */
16
+ shimmer?: number;
17
+ /** 水面反射强度 0~1,默认 0.38 */
18
+ reflection?: number;
19
+ /** 点击涟漪力度 0~3,默认 1 */
20
+ rippleStrength?: number;
21
+ /** 涟漪落点半径(仿真格点 2~12),默认 6 */
22
+ rippleRadius?: number;
23
+ /** 涟漪衰减 0.9~0.999,越大越持久,默认 0.985 */
24
+ damping?: number;
25
+ /** 涟漪传播速度 0.3~0.7,默认 0.5 */
26
+ spread?: number;
27
+ /** 是否响应点击/触摸产生涟漪,默认 true */
28
+ interactive?: boolean;
29
+ /** 是否显示底部提示,默认 true */
30
+ showHint?: boolean;
31
+ /** 底部提示文案 */
32
+ hint?: string;
7
33
  }
8
34
  declare const RippleWater: React.FC<RippleWaterProps>;
9
35
  export default RippleWater;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cos-design/ripple-water",
3
- "version": "3.5.2",
3
+ "version": "3.5.4",
4
4
  "description": "RippleWater component from cos-design",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -52,6 +52,6 @@
52
52
  "registry": "https://registry.npmjs.org"
53
53
  },
54
54
  "dependencies": {
55
- "@cos-design/shared": "3.5.2"
55
+ "@cos-design/shared": "3.5.3"
56
56
  }
57
57
  }