
Security News
Django Joins curl in Pushing Back on AI Slop Security Reports
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.
fragment-shader
Advanced tools
A lightweight & highly performant WebGL fragment shader renderer written in TypeScript.
3kb
.Hello World
section for details on all default behaviors.npm install --save fragment-shader
Note: there are several plugins in modern IDEs (VSCode, etc.) that enable GLSL (shader language) syntax highlighting within template literals by prefacing them with
/*glsl*/
– doesn't seem to work on GitHub though.
import { Shader } from 'fragment-shader';
const glsl = /*glsl*/ `
void main () {
gl_FragColor = vec4(.8, .2, .7, 1.);
}
`;
const shader = new Shader(glsl);
The above code example instantiates an instance of the Shader
class and passes it but a single paramter: a string
containing your fragment shader code. By default, the Shader
class will instantiate a <canvas>
element and append it directly to the <body>
. The <canvas>
will then be sized to match the size of the browser window (and the display's pixel density). Given the default configuration value of fillViewport
being true
, An event listener is then created for the resize
event on the browser window, allowing the renderer and its <canvas>
to resize according to the browser window changing size or orientation. Then, after bootstrapping a webgl2
rendering context, it prepares all internals (including compiling your shader) before finally initializing an internal requestAnimationFrame
loop, syncing the rendering animation to the native refresh rate of the display. There are two methods on the shader instance for controlling rendering playback:
// Cancel the `requestAnimationFrame` loop.
shader.stop();
// Resume the `requestAnimationFrame` loop.
shader.start();
If you wish for the renderer to behave differently than its default configuration, you can do so by passing the constructor a configuration object.
import { Shader } from 'fragment-shader';
const config: ShaderConfig = {
shader: /*glsl*/ `
void main () {
gl_FragColor = vec4(.8, .2, .7, 1.);
}
`,
target: document.body,
uniforms: [],
width: 800,
height: 600,
dpr: window.devicePixelRatio,
fillViewport: false,
onSuccess: () => {},
onError: () => {},
animate: false,
debug: false,
};
const shader = new Shader(config);
Or, if you become accustomed to the shader being the first argument of the constructor, you can instantiate this way:
import { Shader } from 'fragment-shader';
const config: ShaderConfig = { ... }
const shader = new Shader(/*glsl*/ `
void main () {
gl_FragColor = vec4(.8, .2, .7, 1.);
}
`, config);
Note If you set
animate
tofalse
, the shader will render its initial frame, but from thereon out you will be responsible for calling thetick()
method on the Shader if you wish to update it – for example, within arequestAnimationFrame
loop.
import { Shader } from 'fragment-shader';
const shader = new Shader(
/*glsl*/ `
void main () {
gl_FragColor = vec4(.8, .2, .7, 1.);
}
`,
{ animate: false }
);
const tick = (now: DOMHighResTimeStamp) => {
requestAnimationFrame(tick);
shader.tick(now);
};
requestAnimationFrame(tick);
FAQs
This project owns three core features, each simplifying working with fragment shaders in the browser.
The npm package fragment-shader receives a total of 32 weekly downloads. As such, fragment-shader popularity was classified as not popular.
We found that fragment-shader demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
Django has updated its security policies to reject AI-generated vulnerability reports that include fabricated or unverifiable content.
Security News
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.