All Projects → martinlaxenaire → Curtainsjs

martinlaxenaire / Curtainsjs

Licence: mit
curtains.js is a lightweight vanilla WebGL javascript library that turns HTML DOM elements into interactive textured planes.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Curtainsjs

Glslcanvas
Simple tool to load GLSL shaders on HTML Canvas using WebGL
Stars: ✭ 1,067 (+2.69%)
Mutual labels:  webgl, glsl, shaders, texture, canvas
Shader Doodle
A friendly web-component for writing and rendering shaders.
Stars: ✭ 356 (-65.74%)
Mutual labels:  webgl, glsl, shaders, texture, canvas
Three.js
JavaScript 3D Library.
Stars: ✭ 78,237 (+7430.03%)
Mutual labels:  3d, webgl, webgl2, html5, canvas
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 (+1025.89%)
Mutual labels:  3d, webgl, glsl, shaders, shader
Taro
A lightweight 3D game engine for the web.
Stars: ✭ 345 (-66.79%)
Mutual labels:  3d, webgl, html5, canvas
Glsl Worley
Worley noise implementation for WebGL shaders
Stars: ✭ 66 (-93.65%)
Mutual labels:  webgl, glsl, shaders, shader
Glsl Godrays
This module implements a volumetric light scattering effect(godrays)
Stars: ✭ 155 (-85.08%)
Mutual labels:  webgl, glsl, shaders, shader
Glsleditor
Simple WebGL Fragment Shader Editor
Stars: ✭ 1,345 (+29.45%)
Mutual labels:  webgl, glsl, shaders, texture
Spritejs
A cross platform high-performance graphics system.
Stars: ✭ 4,712 (+353.51%)
Mutual labels:  3d, webgl, webgl2, canvas
Wechart
Create all the [ch]arts by cax or three.js - Cax 和 three.js 创造一切图[表]
Stars: ✭ 152 (-85.37%)
Mutual labels:  3d, webgl, html5, canvas
Alien.js
Future web pattern
Stars: ✭ 141 (-86.43%)
Mutual labels:  3d, webgl, html5, canvas
Layaair discard
This is old LayaAir veriosn writetten by ActionScript 3.0 ,now LayaAir is using TypeScript as the Engine Script,Please use https://github.com/layabox/LayaAir instead.
Stars: ✭ 1,858 (+78.83%)
Mutual labels:  3d, webgl, html5, canvas
Hilo3d
Hilo3d, a WebGL Rendering Engine.
Stars: ✭ 123 (-88.16%)
Mutual labels:  3d, webgl, html5, canvas
Webgl Fundamentals
WebGL lessons that start with the basics
Stars: ✭ 3,315 (+219.06%)
Mutual labels:  3d, webgl, glsl, shaders
Melonjs
a fresh & lightweight javascript game engine
Stars: ✭ 3,721 (+258.13%)
Mutual labels:  webgl, webgl2, html5, canvas
Month Of Shaders
One GLSL shader for every day of the month August
Stars: ✭ 12 (-98.85%)
Mutual labels:  webgl, glsl, shaders
Tess Opt
Demonstration of how we can use tessellation shaders to make faster fragment shaders.
Stars: ✭ 13 (-98.75%)
Mutual labels:  glsl, shaders, shader
Webglstudio.js
A full open source 3D graphics editor in the browser, with scene editor, coding pad, graph editor, virtual file system, and many features more.
Stars: ✭ 4,508 (+333.88%)
Mutual labels:  3d, webgl, shaders
Veda
⚡VJ / Live Coding on Atom⚡
Stars: ✭ 373 (-64.1%)
Mutual labels:  webgl, glsl, shaders
Pixi.js
The HTML5 Creation Engine: Create beautiful digital content with the fastest, most flexible 2D WebGL renderer.
Stars: ✭ 34,982 (+3266.89%)
Mutual labels:  webgl, glsl, canvas

What is it ?

Shaders are the new front-end web developpment big thing, with the ability to create very powerful 3D interactions and animations. A lot of very good javascript libraries already handle WebGL but with most of them it's kind of a headache to position your meshes relative to the DOM elements of your web page.

curtains.js was created with just that issue in mind. It is a small vanilla WebGL javascript library that converts HTML elements containing images and videos into 3D WebGL textured planes, allowing you to animate them via shaders.
You can define each plane size and position via CSS, which makes it super easy to add WebGL responsive planes all over your pages.

curtains.js demo gif

Knowledge and technical requirements

It is easy to use but you will of course have to possess good basics of HTML, CSS and javascript.

If you've never heard about shaders, you may want to learn a bit more about them on The Book of Shaders for example. You will have to understand what are the vertex and fragment shaders, the use of uniforms as well as the GLSL syntax basics.

Installation and usage

You can directly download the files and start using the ES6 modules:
import {Curtains, Plane} from 'path/to/src/index.mjs';

const curtains = new Curtains({
    container: "canvas"
});

const plane = new Plane(curtains, document.querySelector("#plane"));
Or you can use npm:
npm i curtainsjs
Load ES6 modules:
import {Curtains, Plane} from 'curtainsjs';
In a browser, you can use the UMD files located in the `dist` directory:
<script src="dist/curtains.umd.min.js"></script>
const curtains = new Curtains({
    container: "canvas"
});

const plane = new Plane(curtains, document.querySelector("#plane"));

// etc

Usage with React

Note that if you are using React, you might want to try react-curtains, curtains.js official React package.

Documentation

The library is split into classes modules. Most of them are used internally by the library but there are however a few classes meant to be used directly, exported in the src/index.mjs file.

Core

  • Curtains: appends a canvas to your container and instanciates the WebGL context. Also handles a few helpers like scroll and resize events, request animation frame loop, etc.
  • Plane: creates a new Plane object bound to a HTML element.
  • Textures: creates a new Texture object.

Frame Buffer Objects

  • RenderTarget: creates a frame buffer object.
  • ShaderPass: creates a post processing pass using a RenderTarget object.

Loader

  • TextureLoader: loads HTML media elements such as images, videos or canvases and creates Texture objects using those sources.

Math

  • Vec2: creates a new Vector 2.
  • Vec3: creates a new Vector 3.
  • Mat4: creates a new Matrix 4.
  • Quat: creates a new Quaternion.

Extras

  • PingPongPlane: creates a plane that uses FBOs ping pong to read/write a texture.
  • FXAAPass: creates an antialiasing FXAA pass using a ShaderPass object.

Full documentation

Getting started
API docs
Examples

Basic example

HTML

<body>
    <!-- div that will hold our WebGL canvas -->
    <div id="canvas"></div>
    
    <!-- div used to create our plane -->
    <div class="plane">
    
        <!-- image that will be used as texture by our plane -->
        <img src="path/to/my-image.jpg" crossorigin="" />
    </div>
    
</body>

CSS

body {
    /* make the body fits our viewport */
    position: relative;
    width: 100%;
    height: 100vh;
    margin: 0;
    overflow: hidden;
}

#canvas {
    /* make the canvas wrapper fits the document */
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
}

.plane {
    /* define the size of your plane */
    width: 80%;
    height: 80vh;
    margin: 10vh auto;
}

.plane img {
    /* hide the img element */
    display: none;
}

Javascript

import {Curtains, Plane} from 'curtainsjs';

window.addEventListener("load", () => {
    // set up our WebGL context and append the canvas to our wrapper
    const curtains = new Curtains({
        container: "canvas"
    });
    
    // get our plane element
    const planeElement = document.getElementsByClassName("plane")[0];
    
    // set our initial parameters (basic uniforms)
    const params = {
        vertexShaderID: "plane-vs", // our vertex shader ID
        fragmentShaderID: "plane-fs", // our fragment shader ID
        uniforms: {
            time: {
                name: "uTime", // uniform name that will be passed to our shaders
                type: "1f", // this means our uniform is a float
                value: 0,
            },
        },
    };
    
    // create our plane using our curtains object, the bound HTML element and the parameters
    const plane = new Plane(curtains, planeElement, params);
    
    plane.onRender(() => {
        // use the onRender method of our plane fired at each requestAnimationFrame call
        plane.uniforms.time.value++; // update our time uniform value
    });
    
});

Shaders

Vertex shader

<script id="plane-vs" type="x-shader/x-vertex">
    #ifdef GL_ES
    precision mediump float;
    #endif
    
    // those are the mandatory attributes that the lib sets
    attribute vec3 aVertexPosition;
    attribute vec2 aTextureCoord;
    
    // those are mandatory uniforms that the lib sets and that contain our model view and projection matrix
    uniform mat4 uMVMatrix;
    uniform mat4 uPMatrix;
    
    // our texture matrix that will handle image cover
    uniform mat4 uTextureMatrix0;
    
    // pass your vertex and texture coords to the fragment shader
    varying vec3 vVertexPosition;
    varying vec2 vTextureCoord;
    
    void main() {       
        gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
        
        // set the varyings
        // here we use our texture matrix to calculate the accurate texture coords
        vTextureCoord = (uTextureMatrix0 * vec4(aTextureCoord, 0.0, 1.0)).xy;
        vVertexPosition = aVertexPosition;
    }
</script> 

Fragment shader

<script id="plane-fs" type="x-shader/x-fragment">
    #ifdef GL_ES
    precision mediump float;
    #endif
    
    // get our varyings
    varying vec3 vVertexPosition;
    varying vec2 vTextureCoord;
    
    // the uniform we declared inside our javascript
    uniform float uTime;
    
    // our texture sampler (default name, to use a different name please refer to the documentation)
    uniform sampler2D uSampler0;
    
    void main() {
        // get our texture coords from our varying
        vec2 textureCoord = vTextureCoord;
        
        // displace our pixels along the X axis based on our time uniform
        // textures coords are ranging from 0.0 to 1.0 on both axis
        textureCoord.x += sin(textureCoord.y * 25.0) * cos(textureCoord.x * 25.0) * (cos(uTime / 50.0)) / 25.0;
        
        // map our texture with the texture matrix coords
        gl_FragColor = texture2D(uSampler0, textureCoord);
    }
</script> 

Changelog

Complete changelog starting from version 7.1.0

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].