# <shader-doodle />
*A friendly web-component for writing and rendering shaders.*
[](https://hello-shader-doodle.glitch.me) [](https://codepen.io/pjkarlik/pen/abOVwaB) [](https://codepen.io/halvves/pen/xxGXdRv)
[](https://codepen.io/halvves/pen/abOwJbR) [](https://codepen.io/halvves/pen/WNbgvNx) [](https://codepen.io/pjkarlik/pen/yLNEoBK)
[](https://codepen.io/mhazani/pen/VwwQYvE) [](https://codepen.io/pjkarlik/pen/JjdVmqv) [](https://codepen.io/pjkarlik/pen/poJYLea)
[](https://codepen.io/halvves/pen/dyyvrxN) [](https://codepen.io/halvves/pen/VwwdQEN) [](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 `` element.
* `name`: specify a name for the texture uniform (will default to `u_audio{index}`)
* `loop`: (_temporarily disabled as part of an ios13 fix_) loop the audio file (_doesn't work on an existing audio tag_)
* `autoplay`: (_temporarily disabled as part of an ios13 fix_) autoplay the audio file (_doesn't work on an existing audio tag_)
* `crossorigin`: (_temporarily disabled as part of an ios13 fix_) specify cors (_doesn't work on an existing audio tag_)
Not yet implemented is a `mic` attribute that will allow using audio from a webrtc source.
#### Examples
##### File:
```html
```
##### Audio Tag:
```html
```
## Next steps (ordered by priority)
* shader precision attribute
* clearColor attribute
* mouseover 0-1 uniform
* LinearCopy/NearestCopy helpers
* lerp attribute for mouse, mouseover, and deviceorientation
* custom uniforms
* webgl2
* improvements to buffers (shared buffers etc...)
## See Also
* [shaderpen](https://github.com/shader-doodle/shaderpen/) - This library's predecessor.
* [css-doodle](https://github.com/css-doodle/css-doodle) - The inspiration behind rewriting shaderpen as a web-component.
* [The Book of Shaders](https://thebookofshaders.com/) - A gentle step-by-step guide through the world Fragment Shaders.
* [Shadertoy](https://www.shadertoy.com/) - Shader playground.