All Projects → patriciogonzalezvivo → Glslcanvas

patriciogonzalezvivo / Glslcanvas

Licence: mit
Simple tool to load GLSL shaders on HTML Canvas using WebGL

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Glslcanvas

Shader Doodle
A friendly web-component for writing and rendering shaders.
Stars: ✭ 356 (-66.64%)
Mutual labels:  webgl, glsl, shaders, texture, canvas
Curtainsjs
curtains.js is a lightweight vanilla WebGL javascript library that turns HTML DOM elements into interactive textured planes.
Stars: ✭ 1,039 (-2.62%)
Mutual labels:  webgl, glsl, shaders, texture, canvas
Glsleditor
Simple WebGL Fragment Shader Editor
Stars: ✭ 1,345 (+26.05%)
Mutual labels:  webgl, glsl, shaders, texture
Wagner
Effects composer for three.js
Stars: ✭ 930 (-12.84%)
Mutual labels:  webgl, glsl, shaders
3d Game Shaders For Beginners
🎮 A step-by-step guide to implementing SSAO, depth of field, lighting, normal mapping, and more for your 3D game.
Stars: ✭ 11,698 (+996.34%)
Mutual labels:  webgl, glsl, shaders
Month Of Shaders
One GLSL shader for every day of the month August
Stars: ✭ 12 (-98.88%)
Mutual labels:  webgl, glsl, shaders
Glsl Godrays
This module implements a volumetric light scattering effect(godrays)
Stars: ✭ 155 (-85.47%)
Mutual labels:  webgl, glsl, shaders
React Regl
React Fiber Reconciler Renderer for Regl WebGL
Stars: ✭ 171 (-83.97%)
Mutual labels:  webgl, glsl, shaders
Fsynth
Web-based and pixels-based collaborative synthesizer
Stars: ✭ 146 (-86.32%)
Mutual labels:  webgl, glsl, shaders
Solarsys
Realistic Solar System simulation with three.js
Stars: ✭ 49 (-95.41%)
Mutual labels:  webgl, glsl, shaders
Langterm
🕹️ WebGL-based VT220 emulator, made as a learning example and frontend for a text adventure
Stars: ✭ 35 (-96.72%)
Mutual labels:  webgl, glsl, shaders
Veda
⚡VJ / Live Coding on Atom⚡
Stars: ✭ 373 (-65.04%)
Mutual labels:  webgl, glsl, shaders
Glsl Worley
Worley noise implementation for WebGL shaders
Stars: ✭ 66 (-93.81%)
Mutual labels:  webgl, glsl, shaders
Glslviewer
Console-based GLSL Sandbox for 2D/3D shaders shaders
Stars: ✭ 2,834 (+165.6%)
Mutual labels:  glsl, shaders, texture
Shadertoy React
6kB "Shadertoy" like react component letting you easily render your fragment shaders in your React web projects, without having to worry about implementing the WebGL part.
Stars: ✭ 74 (-93.06%)
Mutual labels:  glsl, shaders, canvas
Webgl Fundamentals
WebGL lessons that start with the basics
Stars: ✭ 3,315 (+210.68%)
Mutual labels:  webgl, glsl, shaders
Imogen
GPU Texture Generator
Stars: ✭ 648 (-39.27%)
Mutual labels:  glsl, shaders, texture
Pixi.js
The HTML5 Creation Engine: Create beautiful digital content with the fastest, most flexible 2D WebGL renderer.
Stars: ✭ 34,982 (+3178.54%)
Mutual labels:  webgl, glsl, canvas
Nau
Nau - OpenGL + Optix 3D Engine
Stars: ✭ 18 (-98.31%)
Mutual labels:  glsl, shaders
Leaf3d
A lightweight 3D rendering engine based on modern OpenGL
Stars: ✭ 16 (-98.5%)
Mutual labels:  glsl, shaders

GlslCanvas is JavaScript Library that helps you easily load GLSL Fragment and Vertex Shaders into an HTML canvas. I have used this in my Book of Shaders and glslEditor.

Donate

How to use it?

There are different ways to do this. But first, make sure you are loading the latest version of GlslCanvas.js on your page by adding this line to your HTML:

<script type="text/javascript" src="https://rawgit.com/patriciogonzalezvivo/glslCanvas/master/dist/GlslCanvas.js"></script>

or if you are using npm package manager on your console do:

npm install glslCanvas

The easy way

  1. Create a canvas element in your HTML.
  2. Add the class name glslCanvas to the canvas.
  3. Assign it a shader...
    • through a url using the attribute data-fragment-url
    • or directly writing your code inside the data-fragment attribute
<canvas class="glslCanvas" data-fragment-url="shader.frag" width="500" height="500"></canvas>

That's all! glslCanvas will automatically load a WebGL context in that <canvas> element, compile the shader and animate it for you.

As you can see, in this example we are loading the fragment shader by setting the attribute data-fragment-url to a url. But there are also a few other ways to load data to our glslCanvas:

  • data-fragment : load a fragment shader by providing the content of the shader as a string
  • data-fragment-url : load a fragment shader by providing a valid url
  • data-vertex : load a vertex shader by providing the content of the shader as a string
  • data-vertex-url : load a vertex shader by providing a valid url
  • data-textures: add a list of texture urls separated by commas (ex: data-textures="texture.jpg,normal_map.png,something.jpg"). Textures will be assigned in order to uniform sampler2D variables with names following this style: u_tex0, u_tex1, u_tex2, etc.

All the catched .glslCanvas element whill be stored in the windows.glslCanvases array.

The JS way

Create a <canvas> element and construct a glsCanvas() sandbox from it.

var canvas = document.createElement("canvas");
var sandbox = new GlslCanvas(canvas);

In the case you need to reload the

Reloading shaders from JS

You can change the content of the shader as many times you want. Here are some examples:

// Load only the Fragment Shader
var string_frag_code = "main(){\ngl_FragColor = vec4(1.0);\n}\n";
sandbox.load(string_frag_code);

// Load a Fragment and Vertex Shader
var string_vert_code = "attribute vec4 a_position; main(){\ggl_Position = a_position;\n}\n";
sandbox.load(string_frag_code, string_vert_code);

Default Uniforms

Some uniforms are automatically loaded for you:

  • u_time: a float representing elapsed time in seconds.
  • u_resolution: a vec2 representing the dimensions of the viewport.
  • u_mouse: a vec2 representing the position of the mouse, defined in Javascript with .setMouse({x:[value],y:[value]).
  • u_tex[number]: a sampler2D containing textures loaded with the data-textures attribute.

You can also send your custom uniforms to a shader with .setUniform([name],[...value]). GlslCanvas will parse the value you provide to determine its type. If the value is a String, GlslCanvas will parse it as the url of a texture.

// Assign .5 to "uniform float u_brightness"
sandbox.setUniform("u_brightness",.5); 

// Assign (.2,.3) to "uniform vec2 u_position"
sandbox.setUniform("u_position",.2,.3);

// Assign a red color to "uniform vec3 u_color"
sandbox.setUniform("u_color",1,0,0); 

// Load a new texture and assign it to "uniform sampler2D u_texture"
sandbox.setUniform("u_texture","data/texture.jpg");

Quick start demo

In the index.html file, you will find handy example code to start.

Demo page: patriciogonzalezvivo.github.io/glslCanvas/

Collaborate

If you'd like to contribute to this code, you need to:

git clone https://github.com/patriciogonzalezvivo/glslCanvas.git
cd glslCanvas
yarn
  • Run rollup in dev mode while you edit
yarn run dev
  • Build for production
yarn run build
  • Push to your local fork and make your pull request

Thank you

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].