Skip to content

Instantly share code, notes, and snippets.

@L-A
Created May 30, 2020 04:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save L-A/9b5c55d73c171790c81a6e803687469a to your computer and use it in GitHub Desktop.
Save L-A/9b5c55d73c171790c81a6e803687469a to your computer and use it in GitHub Desktop.
Simple web GLSL using canvas-sketch
import canvasSketch from "canvas-sketch";
import createShader from "canvas-sketch-util/shader";
import glsl from "glslify";
// Note that you need to install `glslify` and `canvas-sketch-util`
// separately from `canvas-sketch`
// Configuration
const width = 8,
height = 6,
pixelsPerInch = 300;
const resolution = [width * pixelsPerInch, height * pixelsPerInch];
const settings = {
context: "webgl",
dimensions: [width, height],
units: "in",
animate: true,
pixelsPerInch: pixelsPerInch,
};
// glsl code
const frag = glsl`
#ifdef GL_ES
precision mediump float;
#endif
uniform float time;
uniform vec2 resolution;
void main() {
float normalizedX = gl_FragCoord.x / resolution.x;
float normalizedY = gl_FragCoord.y / resolution.y;
float xCycle = sin(normalizedX + time);
float yCycle = cos(normalizedY + time);
gl_FragColor = vec4(xCycle, yCycle, 1.0, 1.0);
}
`;
// Drawing
// Uniforms are passed to your shaders here in the `uniforms` parameter.
const sketch = ({ gl }) => {
return createShader({
gl,
frag,
uniforms: {
time: ({ time }) => time,
resolution: resolution,
},
});
};
canvasSketch(sketch, settings);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment