shaded-image
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

shaded-image

Apply a stack of post-processing style shaders to a static image

Note When using React, I recommend using the React component wrapper shaded-image-react

NPM JavaScript Style Guide

Install

npm install shaded-image

Usage

Inbuilt Components

You can use one of the inbuilt effect components

<head>
  <script type="module">
    import "shaded-image";
  </script>
</head>
<body>
  <div style="display: flex">
    <si-greyscale-image image="./image.svg" class="logo" />
  </div>
</body>
<head>
  <script type="module">
    import "shaded-image";
  </script>
</head>
<body>
  <div style="display: flex">
    <si-anim-test-image image="./image.svg" class="logo" />
  </div>
</body>
`;

Custom Effects

You can create your own custom effects by defining a glsl fragment and vertex shader. Uniform and Varying values can be set using initFunction and updateFunction

GrayscaleImage.js

<head>
  <script type="module">
    import "shaded-image";
    const fragmentShader = `
      varying highp vec2 vTextureCoord;
      uniform sampler2D uSampler;

      void main(void) {
      	lowp vec4 tex = texture2D(uSampler, vTextureCoord);
      	lowp float col = (0.2126 * tex.r + 0.7152 * tex.g + 0.0722*tex.b);
      	gl_FragColor = vec4(col, col, col, tex.a);
      }

      `;
    const vertexShader = `
      attribute vec3 aVertexPosition;
      attribute vec2 aTextureCoord;

      varying highp vec2 vTextureCoord;

      void main(void) {
      	gl_Position =  vec4(aVertexPosition.xy, 0.0, 1.0);
      	vTextureCoord = aTextureCoord;
      }
    `;
    const shaders = [
      {
        vertexShader: vertexShader,
        fragmentShader: fragmentShader,
        initFunction: (args) => {},
        updateFunction: (args) => {},
      },
    ];

    const greyImage = document.querySelector("#example");
    greyImage.shaders = shaders;
  </script>
</head>
<body>
  <div style="display: flex">
    <si-shaded-image id="example" image="./image.svg" class="logo" />
  </div>
</body>

License

MIT © benjaminARowe

Readme

Keywords

none

Package Sidebar

Install

npm i shaded-image

Weekly Downloads

0

Version

2.0.0

License

MIT

Unpacked Size

115 kB

Total Files

37

Last publish

Collaborators

  • benjamin_alan_rowe