1.0.2 • Published 5 months ago

media-shader v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

Media Shader

A powerful web component for applying WebGL shader effects to images and videos. This component makes it easy to add real-time shader effects to your media elements with minimal setup.

šŸŽ® Live Demo & Documentation

Features

  • šŸŽØ Custom WebGL shader support
  • šŸ–¼ļø Apply shaders to images and videos
  • šŸŽ® Interactive mouse controls
  • ⚔ Efficient rendering with WebGL
  • šŸ“± Responsive design
  • šŸ”„ Real-time updates
  • šŸŽÆ Lazy loading support
  • ♿ Accessibility features

Installation

npm install media-shader

Usage

Import the Component

import "media-shader";

Basic Example

<!-- Apply default shader to an image -->
<media-shader src="path/to/your/image.jpg" width="400" height="300">
</media-shader>

<!-- Apply default shader to a video -->
<media-shader
  src="path/to/your/video.mp4"
  width="640"
  height="360"
  playing="true"
>
</media-shader>

Custom Shader Example

<media-shader
  src="path/to/your/media.jpg"
  fragment-shader="
    precision highp float;
    uniform sampler2D uTexture;
    uniform vec2 uResolution;
    uniform float uTime;
    varying vec2 vTexCoord;

    void main() {
      vec2 uv = vTexCoord;
      vec4 color = texture2D(uTexture, uv);
      // Add a simple wave effect
      color.rgb *= 0.8 + 0.2 * sin(uTime + uv.x * 10.0);
      gl_FragColor = color;
    }
  "
>
</media-shader>

Custom Uniforms Example

<media-shader
  src="path/to/your/media.jpg"
  uniforms='{"intensity": 1.0, "color": [1.0, 0.0, 0.0]}'
>
</media-shader>

API Reference

Attributes

AttributeTypeDefaultDescription
srcstringnullURL of the image or video to display
fragment-shaderstring(default shader)GLSL fragment shader code
widthnumbernullWidth of the canvas in pixels
heightnumbernullHeight of the canvas in pixels
uniformsstring (JSON){}JSON string of uniform values
playingbooleantrueControls video playback
altstringnullAlternative text for accessibility
loadingstring"lazy"Loading mode ('eager' or 'lazy')

Built-in Uniforms

The following uniforms are automatically available in your shaders:

UniformTypeDescription
uTexturesampler2DThe media texture
uResolutionvec2Canvas dimensions in pixels
uTimefloatTime in seconds since initialization
uMousevec4Mouse position and click state x, y, clickX, clickY
uHasTextureboolWhether a texture is currently loaded

Events

The component inherits standard HTMLElement events and adds:

  • play - Fired when video playback starts
  • pause - Fired when video playback pauses
  • loadeddata - Fired when media data is loaded

Performance Considerations

  • The component uses requestVideoFrameCallback (with fallback to requestAnimationFrame) for optimal video performance
  • Texture updates are synchronized with video frames
  • The component supports lazy loading for better page performance
  • WebGL context is managed efficiently with proper cleanup

Browser Support

Supports all modern browsers with WebGL2 capability. Requires the following features:

  • Custom Elements v1
  • WebGL2
  • ES6 Modules

License

MIT License - See LICENSE file for details

Author

Created by Rogie King

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago