# <shader-doodle /> *A friendly web-component for writing and rendering shaders.* [![hello shader-doodle](screenshot/hello.gif)](https://hello-shader-doodle.glitch.me) [![fire rings](screenshot/fire-rings.gif)](https://codepen.io/pjkarlik/pen/abOVwaB) [![contrast bath](screenshot/contrast-bath.gif)](https://codepen.io/halvves/pen/xxGXdRv) [![audiovisualizer w/ feedback](screenshot/audio-visualizer.gif)](https://codepen.io/halvves/pen/abOwJbR) [![rainbow woozy](screenshot/rainbow-woozy.gif)](https://codepen.io/halvves/pen/WNbgvNx) [![vertigo truchet tiles](screenshot/vertigo-truchet-tiles.gif)](https://codepen.io/pjkarlik/pen/yLNEoBK) [![raymarching + clouds](screenshot/raymarching-clouds.gif)](https://codepen.io/mhazani/pen/VwwQYvE) [![audio reactive](screenshot/audio-reactive.gif)](https://codepen.io/pjkarlik/pen/JjdVmqv) [![space flame orb](screenshot/space-flame-orb.gif)](https://codepen.io/pjkarlik/pen/poJYLea) [![spooky](screenshot/spooky.gif)](https://codepen.io/halvves/pen/dyyvrxN) [![vhs webcam](screenshot/webcam.gif)](https://codepen.io/halvves/pen/VwwdQEN) [![audio + mouse](screenshot/audio-mouse.gif)](https://codepen.io/halvves/pen/PoqGvVG) NOTE: this README and branch are for the new `` alpha. To view the current stable version go to the [v0 branch](https://github.com/shader-doodle/shader-doodle/tree/v0). `` is a simple web-component loosely based on the [The Book of Shaders](https://thebookofshaders.com/)'s glsl previewer and [Shadertoy](https://www.shadertoy.com/). It sets up a flat responsive canvas on which to draw fragment shaders, and provides several built in uniforms relating to time, resolution, mouse position, etc. ## Usage ### Script Include ```html ``` ### Import `npm install shader-doodle@alpha` ```javascript import 'shader-doodle'; ``` ```html ``` ## API Right now the api is fairly basic. The default syntax is vanilla glsl and there are several built in uniforms following the conventions seen in [The Book of Shaders](https://thebookofshaders.com/). If you prefer ShaderToy's syntax you can set an attribute like so: ``. ### Pre-Defined Uniforms #### Default (``) * `uniform float u_time;`: shader playback time (in seconds) * `uniform float u_delta;`: delta time between frames (in seconds) * `uniform int u_frame;`: shader playback frame * `uniform vec4 u_date;`: year, month, day and seconds * `uniform vec2 u_resolution;`: viewport resolution (in pixels) * `uniform vec2 u_mouse;`: mouse pixel coords (x & y) * `uniform vec4 u_mousedrag`: xy: last click or current drag position, zw: starting drag position (flipped negative when not mousedown) * `uniform vec3 u_orientation;`: [device orientation api](https://developer.mozilla.org/en-US/docs/Web/API/Detecting_device_orientation) values: alpha, beta, gamma #### Shadertoy (``) * `uniform float iTime;`: shader playback time (in seconds) * `uniform float iDelta;`: delta time between frames (in seconds) * `uniform int iFrame;`: shader playback frame * `uniform vec4 iDate;`: year, month, day and seconds * `uniform vec2 iResolution;`: viewport resolution (in pixels) * `uniform vec2 iCurrentMouse;`: mouse pixel coords (x & y) * `uniform vec4 iMouse`: xy: last click or current drag position, zw: starting drag position (flipped negative when not mousedown) * `uniform vec3 iOrientation;`: [device orientation api](https://developer.mozilla.org/en-US/docs/Web/API/Detecting_device_orientation) values: alpha, beta, gamma _NOTE: the slight difference in mouse uniform naming_ ### Textures Textures can be used as a `sampler2D` in a shader by using the `` component. This will pass in two uniforms: * `uniform sampler2D {texture_name}` * `uniform vec2 {texture_name}_resolution` #### Attributes * `src`: source of an image/video or selector for a canvas * `name`: specify a name for the texture uniform (will default to `u_texture{index}`) * `webcam`: overrides `src` and tries to use webrtc webcam as texture source * `force-update`: forces a texture to always update (useful when using a canvas animation or animated gif as a texture) #### Examples ##### Image: ```html ``` ##### Video: ```html ``` ##### Canvas: ```html ``` ##### Camera: ```html ``` ### Audio Data The frequency and waveform data from an audio source can be used in a shader as a `sampler2D` by using the `` component. This will pass in one uniform: * `uniform sampler2D {audio_name}` The audio data is setup as two rows of texels. The first row is frequency data. The x/u axis corresponds to frequency (scaled to 0..1), and the value of a texel at a given x/u is the amplitude of the corresponding frequency. The second row is wave data. The x/u axis corresponds to the x axis of the waveform. (scaled to 0..1), and the value of a texel at a given x/u is the y axis of the waveform. #### Attributes * `src`: a url to an audio file or an id selector for an `