# shader-web-background
_Displays GLSL fragment shaders as a website background. Supports Shadertoy shaders,
multipass - ping-pong offscreen buffers, feedback loops, floating-point textures.
Either with WebGL 1 or 2, will try to run wherever it's technically possible._
**Website/Demo:** :fireworks: https://xemantic.github.io/shader-web-background :sparkler:
:grey_question::rainbow::angel::construction_worker: To ask questions go to [xemantic discord server](https://discord.gg/vQktqqN2Vn) :coffee::tea::strawberry::space_invader:

I designed this library to use complex fragment shaders as part of my web design and development
process. This is the tool which finally lets me embrace the web browser as a creative coding
environment. If you are familiar with GLSL, then it might help you publish your work on
web as well. If you are coming from a web development background,
then you might want to learn a bit more about shaders first, for example from
[The Book of Shaders](https://thebookofshaders.com/). I hope that examples presented
in this documentation are self-explanatory. If you find it useful, then
[:heart: Sponsor xemantic on GitHub](https://github.com/sponsors/xemantic) or

https://www.buymeacoffee.com/kazik
_Kazik (morisil) Pogoda_
https://xemantic.com/
---
**Table of Contents**
- [Features](#features)
- [Adding shader-web-background to your projects](#adding-shader-web-background-to-your-projects)
- [Step 1 - Add library to your project](#step-1---add-library-to-your-project)
- [Option A - Embedded minified library directly in HTML](#option-a---embedded-minified-library-directly-in-html)
- [Option B - Reference the minified library](#option-b---reference-the-minified-library)
- [Option C - Download distribution](#option-c---download-distribution)
- [Step 2 - Add your fragment shaders](#step-2---add-your-fragment-shaders)
- [Step 3 - Start shading](#step-3---start-shading)
- [Step 4 - Specify fallback styles](#step-4---specify-fallback-styles)
- [shader-web-background API](#shader-web-background-api)
- [Configuring shading](#configuring-shading)
- [Adding shader uniforms](#adding-shader-uniforms)
- [About uniforms](#about-uniforms)
- [Textures as uniforms](#textures-as-uniforms)
- [Initializing shader texture](#initializing-shader-texture)
- [Complex config example](#complex-config-example)
- [Handling errors](#handling-errors)
- [Shader GLSL version](#shader-glsl-version)
- [Adding mouse support](#adding-mouse-support)
- [Adding textures](#adding-textures)
- [Shadertoy support](#shadertoy-support)
- [What to do with Shadertoy "Common" tab?](#what-to-do-with-shadertoy-common-tab)
- [What to do with `texture` function?](#what-to-do-with-texture-function)
- [Handling Shadertoy texture parameters](#handling-shadertoy-texture-parameters)
- [How to handle "Multipass" Shadertoy shaders?](#how-to-handle-multipass-shadertoy-shaders)
- [Own vertex shader](#own-vertex-shader)
- [General tips](#general-tips)
- [Building](#building)
- [Contributing](#contributing)
- [Code conventions](#code-conventions)
- [Adding your project to the list of project using this library](#adding-your-project-to-the-list-of-project-using-this-library)
- [Tools and dependencies](#tools-and-dependencies)
- [TODO](#todo)
## Features
* **simplicity**: it is just rendering canvas background as fragment shader.
* **speed**: designed to be embedded in HTML and start rendering before other page resources
are downloaded.
* **extensibility**: adding own interaction and controls is trivial.
* **convenience**: straightforward [API](API.md), specific errors will inform you about mistakes
which are otherwise hard to debug.
* **minimal footprint**: transpiled from JavaScript to JavaScript with
[Google Closure Compiler].
* **pixel feedback loops**: preserving movement in time on offscreen buffers with floating–point precision.
* **[Shadertoy support](#shadertoy-support)**: including multipass shaders
* **cross browser / cross device**: on Chrome, Safari, Firefox or Edge, either with WebGL 1 or 2,
on Linux, Windows, Mac, iPhone or Samsung phone — it will use optimal strategy to squeeze out what's possible from the browser and the hardware.
## Adding shader-web-background to your projects
**TL;DR:**
```html
Minimal shader
shader-web-background minimal example
```
:information_source: If you prefer to learn by example, here is the list of demos displayed
with their highlighted source code:
https://xemantic.github.io/shader-web-background/#demo
There are several ways of adjusting this library to your needs:
### Step 1 - Add library to your project
#### Option A - Embedded minified library directly in HTML
If you want your shaders to start rendering before any other resources are loaded,
then go for this method. Just take the contents of:
https://xemantic.github.io/shader-web-background/dist/shader-web-background.min.js
and put it as `
```
#### Option C - Download distribution
In the future I will publish `shader-web-background` to npm. For now you can just
download the latest minified distribution together with source map and sources.
### Step 2 - Add your fragment shaders
You will need at least one fragment shader defined like this:
```html
```
Put it in the `` of your HTML. The `type` should be `x-shader/x-fragment` and
the `id` attribute is arbitrary.
:warning: Note: Remember to give unique `id` to each of your shaders if you are
defining more of them.
### Step 3 - Start shading
```javascript
```
:warning: Note: the shader name `image` should match the one defined as
shader source `id` attribute.
### Step 4 - Specify fallback styles
:information_source: This step is not necessary, however adding it will improve
the experience for the small amount of users who still cannot
run shaders on their devices.
Define fallback CSS style, for example a static screenshot of your shader frame:
```html
```
The `shader-web-background-fallback` CSS class is applied to HTML document root and
the canvas.
:warning: Note that in case of any errors the default canvas will not be attached
to HTML document at all. In case of shading a canvas which is already attached
to HTML, it might be tempting to provide a fallback canvas background based on the
`shader-web-background-fallback` CSS class, however it might not work on some browsers.
Custom error handler might be needed for cross compatibility.
See [Handling errors](#handling-errors) section for details.
## shader-web-background API
See the full [shader-web-background API](API.md)
## Configuring shading
The [configuration object](API.md#config) passed to the
[shaderWebBackground.shade(config)](API.md#shaderwebbackgroundshadeconfig)
call in the example above will result in a minimal rendering pipeline consisting of one fragment
shader named `image`. A new static
`