• Stars
    star
    1,753
  • Rank 26,437 (Top 0.6 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 9 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Simple tool to load GLSL shaders on HTML Canvas using WebGL

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 cached .glslCanvas elements will 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 case you need to reload the shader:

Reloading shaders from JS

You can change the content of the shader as many times as 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

More Repositories

1

thebookofshaders

Step-by-step guide through the abstract and complex universe of Fragment Shaders.
GLSL
5,989
star
2

glslViewer

Console-based GLSL Sandbox for 2D/3D shaders
C++
4,208
star
3

lygia

LYGIA, it's a granular and multi-language (GLSL, HLSL, WGSL, MSL and CUDA) shader library designed for performance and flexibility
GLSL
2,492
star
4

glslEditor

Simple WebGL Fragment Shader Editor
JavaScript
2,303
star
5

PixelSpiritDeck

GLSL
483
star
6

glslGallery

JavaScript
363
star
7

glslTexture

Blender addon to create 2D textures from GLSL. It follows the same nomenclature that glslViewer, glslCanvas, glslEditor, ofxShader and PixelSpirit
Python
341
star
8

ofxFX

Unlocking the GPU Power on openFrameworks with this add-on for that make easy to use GLSL Shaders
C++
330
star
9

OpenLiDAR

DIY 3D LiDAR Scanner
C++
207
star
10

ofxStreetView

OF scraper for GoogleStreetView panoramas and depthmap
C++
183
star
11

sublime-glslViewer

Sublime Text 2/3 plugin for live coding GLSL Shaders
Python
147
star
12

ada

A general porpose OpenGL app library
C++
120
star
13

KinectCoreVision

Kinect Implementation of CCV
C++
100
star
14

hilma

library to generate 2D/3D geometries
C++
89
star
15

MidiGyver

C++
77
star
16

ofxFluid

GPU Fluid Simulation with a collisions layer
C++
76
star
17

vera

C++/WASM GL Framework
C++
65
star
18

vPlotter

Wireless vPlotter for RaspberryPi
C++
64
star
19

ofxComposer

Set of connecting boxes that let you load and processes different type of data on the GPU by using shaders.
C++
59
star
20

hypatia

Geo-Astronomical library for artist
C++
58
star
21

ofxVoro

OpenFrameworks wrapper of Voro++ that do 3D Voronoi Tessellation
C++
55
star
22

ofxPTAMM

Parallel Tracking and Multiple Mapping (PTAMM by Robert Castle) wrapper for openFrameworks.
C++
53
star
23

cursoOF

Curso en espaΓ±ol de openFrameworks aplicado a la creaciΓ³n
C++
53
star
24

Shell-Initiation

Exoteric *nix terminal tutorial
51
star
25

ofxShader

Objective-C
50
star
26

LineOfSight

What are the satellites in your line of sight?
JavaScript
47
star
27

ss2015

C++
46
star
28

.glslScreenSaver

Make your own custom ScreenSaver in linux using GlslViewer
GLSL
46
star
29

libopz

non official API library for the OP-Z
C++
46
star
30

lygia_threejs_examples

GLSL
40
star
31

sims2014

Simulation Studio, creative coding course taught at parsons school of design, MFADT
C++
40
star
32

ofxInteractiveSurface

For Mapping ofTextures to surfaces making transformations and masking all in one process
C++
37
star
33

ofxBlobTracker

Classic and simple blobTracker implementation used on CCV ( Created by Ramsin Khoshabeh ) + Finger Tracking
C++
33
star
34

tangram-sandbox

JavaScript
32
star
35

ofxThermalPrinter

OpenFrameworks library to control mini thermal printers (https://www.adafruit.com/products/597)
C++
29
star
36

ofxBundle

Import bundle files with cameras and point clouds.
C++
29
star
37

lygia_unity_examples

ShaderLab
28
star
38

Meshes

Simple Python Module to load, construct, parse, transform, save meshes in OBJ/PLY format
Python
26
star
39

ofxBlackBox

Minimal aesthetic orientation GUI for openFrameworks with MultiTouch/TUIO compatibility
C
24
star
40

OFPlay

Simple and Easy Tool for install and manage openFrameworks libraries and projects
C++
24
star
41

ofxGame

Group of handy classes for making Interactive 2D Games. With time and work it could be a decent game engine
C++
24
star
42

sfpc_ll16

Lux Lingua introduction at SFPC
JavaScript
23
star
43

lygia_p5_examples

GLSL
22
star
44

HeNerator

script to generate HeN ipfs app exports of GLSL shaders
Python
22
star
45

ofxTuio

Old TUIO Wrapper
C
21
star
46

ofxPro

C++
19
star
47

AxiSurface

Python module to make 100% vector line compositions and export them as SVG or G-Code files
Python
18
star
48

lygia_of_examples

GLSL
18
star
49

RandomCity

JavaScript
18
star
50

patriciogv_algo2012

Algorithmic Animation Homework Parsons New School MFA DT 2012
C++
17
star
51

sNodes

Syphon Nodes EcoSystem wrote using openFrameworks for life coding of GLSL Shaders
C++
14
star
52

vscode-pickers

GLSL/HLSL Float, 2D/3D Vector and Color picker for vscode
JavaScript
14
star
53

vim-glslViewer

Vim pluging for glslViewer which allow you to live-code shaders from your RaspberryPi
Vim Script
13
star
54

ofxEliza

Wrapper for "Chatterbot Eliza 2.0" made by Gonzales Cenelia. With is a C++ implementation of this well know chatbot
C++
12
star
55

patriciogv_avsys2013

Audio/Video Systems Homework Parsons New School MFA DT 2013
C++
12
star
56

itp17

HTML
11
star
57

joyOfLight

application that provides a means for people to play with light-painting in an intuitive and colorful way.
C++
11
star
58

ofxKeyboard

openFrameworks Virtual Keyboard implementation using ofxTUIO
C
10
star
59

.dotfiles

CSS
10
star
60

ofxPiTFT

PiTFT addon for your RaspberryPi projects
Objective-C
10
star
61

ofxGlmTools

a set of tools that use glm library
C++
10
star
62

ISS

view of OpenStreetMap from the ISS
JavaScript
9
star
63

bootcamp2013_code

Processing
9
star
64

eyeo16

JavaScript
9
star
65

ofxTimelinePro

C++
9
star
66

Skylines03

City point cloud
C++
8
star
67

hilma_raymarcher

Cuda
8
star
68

ada_example3D

JavaScript
8
star
69

ofxPulseSensor

openFrameworks addon of PulseSensor ( for Arduino and RaspberryPi )
C++
7
star
70

ofxKinectStreamer

Multi-platform Kinect depth information streamer that use native openFrameworks addons
C
7
star
71

ofxMd2

Md2 viewer ported to openFrameworks byJellyFish.com and TheoWatson.com
C
7
star
72

Timeline

C++
6
star
73

ada_example

Simple example of an ADA program
HTML
6
star
74

AxiPrinter

Stand alone RaspberryPi controller for my AxiDraw A3
Python
6
star
75

wgpu_sandbox

rust/wgpu sandbox
Rust
6
star
76

opz_experiments

GLSL
4
star
77

openVis16

OpenVis 16' Talk about Maps and Shaders
JavaScript
4
star
78

Communitas

Interactive multiTouch table made using openFrameworks and TUIO protocol.
C++
4
star
79

ofxMapsPro

C++
4
star
80

oscLab

OSC client for the OP-Z (through libopz)
C
4
star
81

lygia_minecraft

Kotlin
4
star
82

ofxMtPhotoGallery

Mouse/TUIO moultiTouch PhotoGallery
C++
4
star
83

ofxKinectUsers

Kinect addon for re-programable gesture tracking
C++
4
star
84

glslBenchmark

JavaScript
4
star
85

RaspistillAPI

PHP Raspistill API
C++
4
star
86

patriciogonzalezvivo.com

Personal Portfolio
JavaScript
4
star
87

edenControl

iOS app for remote controlling EDEN software. That itΒ΄s an Interactive Ecosystem Simulation made specially for Efecto Mariposa.
Objective-C
4
star
88

ofxSubtitles

openFrameworks addon for working with .srt movie subtitles format
C++
4
star
89

lygia_examples

GLSL
4
star
90

mary

Open Source C++ Library for different telemetry sensors with Python bindings
C
3
star
91

PixelSpiritEditor

JavaScript
3
star
92

hawkeye

Refurnishing a Kodak Brownie Hawkeye with a RaspberryPi and RasbperryCam
GLSL
3
star
93

mesaDelTiempo

C++
3
star
94

ofxWiiOSC

Wiimote addon that connects to DarwiinOSC
C++
3
star
95

AlgorithmicNaturalis

3
star
96

southUp

http://southupmap.com/
HTML
3
star
97

ofxFXPro

C++
2
star
98

HardCodeAPaper

Write like you code. Write your academic paper in MarkDown and then export them to PDF in correct Chicago Style.
TeX
2
star
99

MotionRecorder

iOS Motion Recorder
Objective-C
2
star
100

OpenNI2TUIO

C++
2
star