Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@paper-design/shaders-react

Package Overview
Dependencies
Maintainers
0
Versions
21
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@paper-design/shaders-react - npm Package Compare versions

Comparing version 0.0.4 to 0.0.5

dist/index.d.ts.map

1

dist/index.d.ts

@@ -5,1 +5,2 @@ export { ShaderMount } from './shader-mount';

export { getShaderColorFromString } from '@paper-design/shaders';
//# sourceMappingURL=index.d.ts.map

80

dist/index.js

@@ -1,80 +0,2 @@

import{useEffect as p,useRef as b}from"react";var A=class{canvas;gl;program=null;uniformLocations={};fragmentShader;rafId=null;lastFrameTime=0;totalAnimationTime=0;isAnimated;providedUniforms;hasBeenDisposed=!1;resolutionChanged=!0;constructor(e,t,r={},i,o=!0){this.canvas=e,this.fragmentShader=t,this.providedUniforms=r,this.isAnimated=o;let s=e.getContext("webgl",i);if(!s)throw new Error("WebGL not supported");this.gl=s,this.initWebGL(),this.setupResizeObserver(),this.isAnimated?this.startAnimating():this.render(performance.now())}initWebGL=()=>{let e=R(this.gl,_,this.fragmentShader);e&&(this.program=e,this.setupPositionAttribute(),this.setupUniforms())};setupPositionAttribute=()=>{let e=this.gl.getAttribLocation(this.program,"a_position"),t=this.gl.createBuffer();this.gl.bindBuffer(this.gl.ARRAY_BUFFER,t);let r=[-1,-1,1,-1,-1,1,-1,1,1,-1,1,1];this.gl.bufferData(this.gl.ARRAY_BUFFER,new Float32Array(r),this.gl.STATIC_DRAW),this.gl.enableVertexAttribArray(e),this.gl.vertexAttribPointer(e,2,this.gl.FLOAT,!1,0,0)};setupUniforms=()=>{this.uniformLocations={u_time:this.gl.getUniformLocation(this.program,"u_time"),u_resolution:this.gl.getUniformLocation(this.program,"u_resolution"),...Object.fromEntries(Object.keys(this.providedUniforms).map(e=>[e,this.gl.getUniformLocation(this.program,e)]))}};resizeObserver=null;setupResizeObserver=()=>{this.resizeObserver=new ResizeObserver(()=>this.handleResize()),this.resizeObserver.observe(this.canvas),this.handleResize()};handleResize=()=>{let e=this.canvas.clientWidth,t=this.canvas.clientHeight;(this.canvas.width!==e||this.canvas.height!==t)&&(this.canvas.width=e,this.canvas.height=t,this.resolutionChanged=!0,this.gl.viewport(0,0,this.gl.canvas.width,this.gl.canvas.height),this.render(performance.now()))};render=e=>{if(this.hasBeenDisposed)return;let t=e-this.lastFrameTime;this.lastFrameTime=e,this.isAnimated&&(this.totalAnimationTime+=t),this.gl.useProgram(this.program),this.gl.uniform1f(this.uniformLocations.u_time,this.totalAnimationTime*.001),this.resolutionChanged&&(this.gl.uniform2f(this.uniformLocations.u_resolution,this.gl.canvas.width,this.gl.canvas.height),this.resolutionChanged=!1),this.gl.drawArrays(this.gl.TRIANGLES,0,6),this.isAnimated?this.requestRender():this.rafId=null};requestRender=()=>{this.rafId!==null&&cancelAnimationFrame(this.rafId),this.rafId=requestAnimationFrame(this.render)};updateProvidedUniforms=()=>{this.gl.useProgram(this.program),Object.entries(this.providedUniforms).forEach(([e,t])=>{let r=this.uniformLocations[e];if(r)if(Array.isArray(t))switch(t.length){case 2:this.gl.uniform2fv(r,t);break;case 3:this.gl.uniform3fv(r,t);break;case 4:this.gl.uniform4fv(r,t);break;default:t.length===9?this.gl.uniformMatrix3fv(r,!1,t):t.length===16?this.gl.uniformMatrix4fv(r,!1,t):console.warn(`Unsupported uniform array length: ${t.length}`)}else typeof t=="number"?this.gl.uniform1f(r,t):typeof t=="boolean"?this.gl.uniform1i(r,t?1:0):console.warn(`Unsupported uniform type for ${e}: ${typeof t}`)})};startAnimating=()=>{this.isAnimated=!0,this.lastFrameTime=performance.now(),this.rafId=requestAnimationFrame(this.render)};stopAnimating=()=>{this.isAnimated=!1,this.rafId&&(cancelAnimationFrame(this.rafId),this.rafId=null)};setUniforms=e=>{this.providedUniforms={...this.providedUniforms,...e},this.updateProvidedUniforms(),this.render(performance.now())};dispose=()=>{this.hasBeenDisposed=!0,this.stopAnimating(),this.gl&&this.program&&(this.gl.deleteProgram(this.program),this.program=null,this.gl.bindBuffer(this.gl.ARRAY_BUFFER,null),this.gl.bindBuffer(this.gl.ELEMENT_ARRAY_BUFFER,null),this.gl.bindRenderbuffer(this.gl.RENDERBUFFER,null),this.gl.bindFramebuffer(this.gl.FRAMEBUFFER,null),this.gl.getError()),this.resizeObserver&&(this.resizeObserver.disconnect(),this.resizeObserver=null),this.uniformLocations={}}},_=`
attribute vec4 a_position;
void main() {
gl_Position = a_position;
}
`;function x(e,t,r){let i=e.createShader(t);return i?(e.shaderSource(i,r),e.compileShader(i),e.getShaderParameter(i,e.COMPILE_STATUS)?i:(console.error("An error occurred compiling the shaders: "+e.getShaderInfoLog(i)),e.deleteShader(i),null)):null}function R(e,t,r){let i=x(e,e.VERTEX_SHADER,t),o=x(e,e.FRAGMENT_SHADER,r);if(!i||!o)return null;let s=e.createProgram();return s?(e.attachShader(s,i),e.attachShader(s,o),e.linkProgram(s),e.getProgramParameter(s,e.LINK_STATUS)?(e.detachShader(s,i),e.detachShader(s,o),e.deleteShader(i),e.deleteShader(o),s):(console.error("Unable to initialize the shader program: "+e.getProgramInfoLog(s)),e.deleteProgram(s),e.deleteShader(i),e.deleteShader(o),null)):null}function y(e){let t,r,i;if(e.startsWith("#"))[t,r,i]=C(e);else if(e.startsWith("rgb"))[t,r,i]=F(e);else if(e.startsWith("hsl"))[t,r,i]=E(U(e));else return console.error("Unsupported color format"),[0,0,0];return[t,r,i]}function C(e){let t=parseInt(e.slice(1),16),r=(t>>16&255)/255,i=(t>>8&255)/255,o=(t&255)/255;return[r,i,o]}function F(e){let t=e.match(/^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i);return t?[parseInt(t[1]??"0")/255,parseInt(t[2]??"0")/255,parseInt(t[3]??"0")/255]:[0,0,0]}function U(e){let t=e.match(/^hsl\s*\(\s*(\d+)\s*,\s*(\d+)%\s*,\s*(\d+)%\s*\)$/i);return t?[parseInt(t[1]??"0"),parseInt(t[2]??"0"),parseInt(t[3]??"0")]:[0,0,0]}function E(e){let[t,r,i]=e,o=t/360,s=r/100,a=i/100,n,m,c;if(r===0)n=m=c=a;else{let d=(h,g,l)=>(l<0&&(l+=1),l>1&&(l-=1),l<.16666666666666666?h+(g-h)*6*l:l<.5?g:l<.6666666666666666?h+(g-h)*(.6666666666666666-l)*6:h),u=a<.5?a*(1+s):a+s-a*s,f=2*a-u;n=d(f,u,o+1/3),m=d(f,u,o),c=d(f,u,o-1/3)}return[n,m,c]}var S=`
precision highp float;
uniform vec2 u_resolution;
uniform float u_time;
uniform vec3 u_color1;
uniform vec3 u_color2;
uniform float u_noiseScale;
uniform float u_noiseSpeed;
uniform float u_grainAmount;
// Simplex 2D noise
vec3 permute(vec3 x) { return mod(((x*34.0)+1.0)*x, 289.0); }
float snoise(vec2 v) {
const vec4 C = vec4(0.211324865405187, 0.366025403784439,
-0.577350269189626, 0.024390243902439);
vec2 i = floor(v + dot(v, C.yy));
vec2 x0 = v - i + dot(i, C.xx);
vec2 i1;
i1 = (x0.x > x0.y) ? vec2(1.0, 0.0) : vec2(0.0, 1.0);
vec4 x12 = x0.xyxy + C.xxzz;
x12.xy -= i1;
i = mod(i, 289.0);
vec3 p = permute( permute( i.y + vec3(0.0, i1.y, 1.0))
+ i.x + vec3(0.0, i1.x, 1.0));
vec3 m = max(0.5 - vec3(dot(x0,x0), dot(x12.xy,x12.xy),
dot(x12.zw,x12.zw)), 0.0);
m = m*m ;
m = m*m ;
vec3 x = 2.0 * fract(p * C.www) - 1.0;
vec3 h = abs(x) - 0.5;
vec3 ox = floor(x + 0.5);
vec3 a0 = x - ox;
m *= 1.79284291400159 - 0.85373472095314 * (a0*a0 + h*h);
vec3 g;
g.x = a0.x * x0.x + h.x * x0.y;
g.yz = a0.yz * x12.xz + h.yz * x12.yw;
return 130.0 * dot(m, g);
}
void main() {
vec2 st = gl_FragCoord.xy / u_resolution.xy;
// Calculate the aspect ratio of the shader
float shaderAspect = u_resolution.x / u_resolution.y;
// Define the aspect ratio of your content (e.g., 1.0 for square)
float contentAspect = 1.0;
// Adjust st to maintain content aspect ratio
if (shaderAspect > contentAspect) {
float scale = shaderAspect / contentAspect;
st.x = (st.x - 0.5) * scale + 0.5;
} else {
float scale = contentAspect / shaderAspect;
st.y = (st.y - 0.5) * scale + 0.5;
}
// Create blobby texture
float n = snoise(st * u_noiseScale + u_time * u_noiseSpeed);
n += 0.5 * snoise(st * u_noiseScale * 2.0 - u_time * u_noiseSpeed * 0.5);
n += 0.25 * snoise(st * u_noiseScale * 4.0 + u_time * u_noiseSpeed * 0.25);
n = n * 0.5 + 0.5;
// Color interpolation
vec3 color = mix(u_color1, u_color2, n);
// Add grain
float grain = fract(sin(dot(st * 1000.0, vec2(12.9898, 78.233))) * 43758.5453);
color += (grain - 0.5) * u_grainAmount;
gl_FragColor = vec4(color, 1.0);
}
`;import{jsx as w}from"react/jsx-runtime";var v=({ref:e,fragmentShader:t,style:r,uniforms:i={},webGlContextAttributes:o,animated:s=!0})=>{let a=e??b(null),n=b(null);return p(()=>(a.current&&(n.current=new A(a.current,t,i,o,s)),()=>{n.current&&n.current.dispose()}),[t,o]),p(()=>{n.current&&n.current.setUniforms(i)},[i]),p(()=>{n.current&&(s?n.current.startAnimating():n.current.stopAnimating())},[s]),w("canvas",{ref:a,style:r})};import{jsx as P}from"react/jsx-runtime";var L=e=>P(v,{...e,fragmentShader:S});export{L as GrainClouds,v as ShaderMount,y as getShaderColorFromString};
import{useEffect as a,useRef as f}from"react";import{ShaderMount as d}from"@paper-design/shaders";import{jsx as m}from"react/jsx-runtime";var s=({ref:e,fragmentShader:u,style:l,uniforms:t={},webGlContextAttributes:i,animated:n=!0})=>{let o=e??f(null),r=f(null);return a(()=>(o.current&&(r.current=new d(o.current,u,t,i,n)),()=>{r.current&&r.current.dispose()}),[u,i]),a(()=>{r.current&&r.current.setUniforms(t)},[t]),a(()=>{r.current&&(n?r.current.startAnimating():r.current.stopAnimating())},[n]),m("canvas",{ref:o,style:l})};import{grainCloudsFragmentShader as p}from"@paper-design/shaders";import{jsx as S}from"react/jsx-runtime";var c=e=>S(s,{...e,fragmentShader:p});import{getShaderColorFromString as E}from"@paper-design/shaders";export{c as GrainClouds,s as ShaderMount,E as getShaderColorFromString};
//# sourceMappingURL=index.js.map

@@ -11,1 +11,2 @@ import React from 'react';

export declare const ShaderMount: React.FC<ShaderMountProps>;
//# sourceMappingURL=shader-mount.d.ts.map

@@ -8,1 +8,2 @@ import { type ShaderMountProps } from '../shader-mount';

export {};
//# sourceMappingURL=grain-clouds.d.ts.map
{
"name": "@paper-design/shaders-react",
"version": "0.0.4",
"version": "0.0.5",
"license": "MIT",

@@ -5,0 +5,0 @@ "type": "module",

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc