
Security News
vlt Launches "reproduce": A New Tool Challenging the Limits of Package Provenance
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
@mediamonks/image-effect-renderer
Advanced tools
The image-effect-renderer is a lightweight package that allows you to run WebGL fragment shaders in your website using WebGL. It can be used to apply effects to HTML images or video sources. Zero dependencies.
The image-effect-renderer is a lightweight package that allows you to run fragment shaders in your website using WebGL. It can be used to apply effects to HTML images or video sources. Zero dependencies.
The ImageEffectRenderer supports the most common variables used in Shadertoy and the syntax of fragments shaders from Shadertoy and OneShader. This makes it easy to prototype different effects using Shadertoy or OneShader.
This is a build from the repository's example/ directory.
Add @mediamonks/image-effect-renderer
to your project:
npm i @mediamonks/image-effect-renderer
Simple shader rendering on canvas.
import { ImageEffectRenderer } from '@mediamonks/image-effect-renderer';
import shader from './shader.glsl';
const options = { loop: true };
// Creating an RendererInstance
const renderer = ImageEffectRenderer.createTemporary(wrapperElement, shader, options);
...
// Clean up the renderer when it's no longer needed
ImageEffectRenderer.releaseTemporary(renderer);
You can set the following options when creating a renderer:
false
.true
.window.devicePixelRatio
.false
.true
.ImageEffectRenderers can share one WebGLContext (see useSharedContext
). This is needed if multiple ImageEffectRenderers are active at a time.
If you have only one ImageEffectRenderer on a page or if you create a large ImageEffectRenderer (i.e. fullscreen), the ImageEffectRenderer will run faster if you create it having its own WebGLContext:
This library allows adding images into up to eight slots, which can be utilized in the shader (as sampler2D iChannel0 to iChannel7). Ensure images are fully loaded before adding them.
slotIndex
(number) - Index of the slot where the image will be set.image
(HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | RendererBuffer) - The image data you want to use in the shader.options
(Partial) - Custom configuration for image handling. This is optional.import { ImageEffectRenderer } from '@mediamonks/image-effect-renderer';
import shader from './shader.glsl';
const renderer = ImageEffectRenderer.createTemporary(wrapperElement, shader, { loop: false });
const imageOptions = {};
renderer.setImage(0, image, imageOptions);
renderer.play();
When setting an image, you can pass an object with the following options:
true
.true
.false
.true
.true
.true
.true
.Commences or resumes the rendering loop.
renderer.play();
Pauses the rendering loop.
renderer.stop();
Creates or replaces a render buffer at a specified index.
i
(number) - The index of the buffer to create/replace.shader
(string) - The shader used for the buffer rendering.options
(Partial) - Custom configuration for buffer creation. This is optional.Renderer
- The newly created or replaced buffer object.let newBuffer = renderer.createBuffer(index, shader, options);
Registers a tick function to be called on every frame update.
tick
(Function) - The function to be called. It accepts a single parameter dt
representing the delta time.renderer.tick(dt => {
// Operations to be performed every tick
});
Registers a ready function to be called when the renderer instance is ready.
ready
(Function) - The function to be called.renderer.ready(() => {
// Operations to be performed when renderer is ready
});
Draws a frame manually.
time
(number) - Time of the frame to draw. Defaults to 0 if not specified.renderer.drawFrame(time);
You can set uniforms for each RendererInstance (created by calling ImageEffectRenderer.createTemporary
or by creating a buffer using renderer.createBuffer
).
Sets a float uniform in the shader program.
name
(string) - Name of the uniform.value
(number) - Float value.renderer.setUniformFloat('uniformName', 0.5);
Sets an integer uniform in the shader program.
name
(string) - Name of the uniform.value
(number) - Integer value.renderer.setUniformInt('uniformName', 4);
Sets a vec2 uniform in the shader program.
name
(string) - Name of the uniform.x
(number) - X value.y
(number) - Y value.renderer.setUniformVec2('uniformName', 0.5, 1.5);
Sets a vec3 uniform in the shader program.
name
(string) - Name of the uniform.x
(number) - X value.y
(number) - Y value.z
(number) - Z value.renderer.setUniformVec3('uniformName', 0.5, 1.5, 2.5);
Sets a vec4 uniform in the shader program.
name
(string) - Name of the uniform.x
(number) - X value.y
(number) - Y value.z
(number) - Z value.w
(number) - W value.renderer.setUniformVec4('uniformName', 0.5, 1.5, 2.5, 3.5);
Sets a matrix uniform in the shader program.
name
(string) - Name of the uniform.matrix
(Float32Array) - 4X4 matrix.let matrix = new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]);
renderer.setUniformMatrix('uniformName', matrix);
It is possible to create multiple ping-pong buffers, each functioning with its shader, which functions analogously to adding extra buffer tabs in Shadertoy.
renderer.createBuffer(0, shader);
You can assign a buffer to an image slot:
renderer.buffers[0].setImage(0, this.renderer.buffers[0]); // ping-pong
// and
renderer.setImage(0, renderer.buffers[0]);
A buffer will render in the exact resolution as the output canvas. To see more examples, please take a look at the examples directory.
To build image-effect-renderer, ensure that you have Git and Node.js installed.
Clone a copy of the repo:
git clone https://github.com/mediamonks/image-effect-renderer.git
Change to the image-effect-renderer directory:
cd image-effect-renderer
Install dev dependencies:
npm i
Build package:
npm run build
FAQs
The image-effect-renderer is a lightweight package that allows you to run WebGL fragment shaders in your website using WebGL. It can be used to apply effects to HTML images or video sources. Zero dependencies.
We found that @mediamonks/image-effect-renderer demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 10 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
vlt's new "reproduce" tool verifies npm packages against their source code, outperforming traditional provenance adoption in the JavaScript ecosystem.
Research
Security News
Socket researchers uncovered a malicious PyPI package exploiting Deezer’s API to enable coordinated music piracy through API abuse and C2 server control.
Research
The Socket Research Team discovered a malicious npm package, '@ton-wallet/create', stealing cryptocurrency wallet keys from developers and users in the TON ecosystem.