This tutorial demonstrates how to seamlessly integrate WebGL effects into existing <image></image>
and <video></video>
elements on a webpage. While several resources address this topic, this guide simplifies the process, focusing on the crucial connection between standard web pages and WebGL. We'll build an interactive image slider with a dynamic mouse hover effect.
The core functionality of the slider and intricate WebGL/GLSL shader details won't be covered extensively. However, the demo code includes comments and links for further learning. Note that we're using WebGL2 and GLSL 300, which are not compatible with Safari or Internet Explorer. Use Chrome or Firefox for the demos. For production use, consider loading both GLSL 100 and 300 shader versions, using GLSL 300 only when curtains.renderer._isWebGL2
is true (as shown in the demo).
Step 1: Build Your Webpage
Create your HTML, CSS, and JavaScript as usual. Our example is an image slider, but the principles apply broadly. The slider features:
- Slides spanning the full page width.
- Momentum-based dragging and snapping to the nearest slide.
- Exit and enter animations for slides.
- An interactive hover effect.
GreenSock Animation Platform (GSAP) is used for animation and dragging, offering plugins for drag interactions, momentum, and text line splitting. If you're unfamiliar with GSAP sliders, explore the demo code for guidance. These DOM elements will be synchronized with our WebGL rendering.
Step 2: WebGL Rendering with CurtainsJS
We'll render images using WebGL. This involves:
- Loading images as textures in a GLSL shader.
- Creating WebGL planes for images and applying textures.
- Positioning and scaling planes to match their DOM counterparts.
CurtainsJS simplifies this process by handling the synchronization between DOM and WebGL elements during scrolling and user interactions. Here's the essential JavaScript:
// Create a Curtains instance const curtains = new Curtains({ container: "canvas", autoRender: false }); // Single rAF for GSAP and Curtains function renderScene() { curtains.render(); } gsap.ticker.add(renderScene); // Curtains parameters const params = { vertexShaderID: "slider-planes-vs", fragmentShaderID: "slider-planes-fs", uniforms: { // ... } }; // Create planes for each slide const planeElements = document.querySelectorAll(".slide"); planeElements.forEach((planeEl, i) => { const plane = curtains.addPlane(planeEl, params); if (plane) { plane.onReady(function() { plane.htmlElement.closest(".slide").classList.add("loaded"); }); } });
The updateProgress
function needs modification to update WebGL planes:
function updateProgress() { // Update slider animation.progress(wrapVal(this.x) / wrapWidth); // Update WebGL planes planes.forEach(plane => plane.updatePosition()); }
Basic vertex and fragment shaders are needed to display textures. These can be loaded via <script></script>
tags or using backticks (as in the demo). The vertex shader positions planes, while the fragment shader processes pixels. Remember the shader variable prefixes: in
, uniform
, and out
.
Step 3: Implementing WebGL Effects
Now, let's add effects. Our inspiration video features:
- Color inversion.
- A fisheye effect around the mouse.
- Animated radius around the mouse.
- Smooth radius animation.
- Image translation based on mouse position.
Shaders lack memory between frames. To animate effects (like the radius), use JavaScript variables and pass them to each frame.
Modify shaders to invert colors outside the radius and create the fisheye effect. The focus here is the DOM-WebGL connection, so shader details are minimized, but comments are provided.
Finding pre-made WebGL shaders for websites is challenging. ShaderToy and VertexShaderArt offer impressive shaders, but not necessarily for typical website effects.
Step 4: Connecting JavaScript and Shaders
Add interactivity by passing uniforms to shaders and modifying them based on user interactions.
Declare uniforms in shaders:
// Vertex shader uniform vec2 uMouse; // Fragment shader uniform float uRadius; uniform vec2 uResolution;
Add uniform values to the Curtains parameters:
const params = { // ... uniforms: { mouse: { name: "uMouse", type: "2f", value: mouse }, radius: { name: "uRadius", type: "1f", value: radius.val }, resolution: { name: "uResolution", type: "2f", value: [innerWidth, innerHeight] } } };
Create event listeners and animations:
const radius = { val: 0.1 }; const radiusAnim = gsap.from(radius, { val: 0, duration: 0.3, paused: true, onUpdate: updateRadius }); function updateRadius() { planes.forEach((plane, i) => { plane.uniforms.radius.value = radius.val; }); } // ... (mouse event handling code)
Update updateProgress
to keep WebGL mouse synced.
GSAP's callbacks (like onComplete
) and frame-rate independence are advantageous.
Further Enhancements
Add effects like turbulence and displacement. Displacement involves moving pixels based on a gradient lightmap. Animate displacement power and intensity based on drag velocity. Curtains also offers a React version. Share your creations!
The above is the detailed content of Creating WebGL Effects with CurtainsJS. For more information, please follow other related articles on the PHP Chinese website!

@keyframesispopularduetoitsversatilityandpowerincreatingsmoothCSSanimations.Keytricksinclude:1)Definingsmoothtransitionsbetweenstates,2)Animatingmultiplepropertiessimultaneously,3)Usingvendorprefixesforbrowsercompatibility,4)CombiningwithJavaScriptfo

CSSCountersareusedtomanageautomaticnumberinginwebdesigns.1)Theycanbeusedfortablesofcontents,listitems,andcustomnumbering.2)Advancedusesincludenestednumberingsystems.3)Challengesincludebrowsercompatibilityandperformanceissues.4)Creativeusesinvolvecust

Using scroll shadows, especially for mobile devices, is a subtle bit of UX that Chris has covered before. Geoff covered a newer approach that uses the animation-timeline property. Here’s yet another way.

Let’s run through a quick refresher. Image maps date all the way back to HTML 3.2, where, first, server-side maps and then client-side maps defined clickable regions over an image using map and area elements.

The State of Devs survey is now open to participation, and unlike previous surveys it covers everything except code: career, workplace, but also health, hobbies, and more.

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
