<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/building-a-mouse-following-square-lens-effect-with-three-js-and-glsl-0joazg7l2" -->

---
title: Building a Mouse-Following Square Lens Effect with...
description: A step-by-step tutorial walks through building a mouse-following square lens effect using Three.js and GLSL, combining a grayscale image with a color image...
canonical: https://daily.dev/posts/building-a-mouse-following-square-lens-effect-with-three-js-and-glsl-0joazg7l2
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Building a Mouse-Following Square Lens Effect with Three.js and GLSL | daily.dev
og:description: A step-by-step tutorial walks through building a mouse-following square lens effect using Three.js and GLSL, combining a grayscale image with a color image...
og:url: https://daily.dev/posts/building-a-mouse-following-square-lens-effect-with-three-js-and-glsl-0joazg7l2
og:image: https://api.daily.dev/og/posts/0JoaZg7L2.png
og:image:alt: Building a Mouse-Following Square Lens Effect with Three.js and GLSL
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Building a Mouse-Following Square Lens Effect with Three.js and GLSL

**[Codrops](https://daily.dev/sources/codrops)** · 19 min read · 2 upvotes · 0 comments

## Summary

A step-by-step tutorial walks through building a mouse-following square lens effect using Three.js and GLSL, combining a grayscale image with a color image revealed inside a square mask. The square applies a CC Lens-style distortion and a radial RGB shift that intensify toward its edges, while the grayscale background gets subtle wave and noise-based motion via a random3 GLSL function adapted from a Shadertoy example. The implementation covers project structure, fragment and vertex shader code, aspect-ratio correction for the square mask, mouse position mapping, and adding a lil-gui interface for real-time parameter tweaking, all without post-processing or 3D models.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://tympanus.net/codrops/2026/08/25/building-a-mouse-following-square-lens-effect-with-three-js-and-glsl>

## Questions this post answers

### How do I create a CC Lens-style distortion effect in a GLSL fragment shader?

Center the UV coordinates by subtracting 0.5, compute the squared radial distance with dot(), then scale that distance using a distortion factor: for positive distortion use 1.0 + distortion * radius2, for negative use 1.0 / (1.0 - distortion * radius2). Apply this scale to the centered position, convert back to UV space, and use the offset between distorted and original UV to sample the texture.

_Developers building interactive shader effects can find implementation walkthroughs like this one through daily.dev._

### How do you keep a square mask from stretching when the browser viewport is resized in a WebGL shader?

Calculate an aspect-ratio correction factor from the mesh dimensions using vec2(min(meshSize.y/meshSize.x, 1.0), min(meshSize.x/meshSize.y, 1.0)), then divide the centered UV coordinates used for the square boundary test by this factor before applying step() functions to build the mask. This keeps the square proportional regardless of screen dimensions.

_daily.dev surfaces practical shader techniques for developers refining responsive WebGL interfaces._

### How do you apply a radial RGB shift effect that gets stronger toward the edges of a UI element in GLSL?

Compute a direction vector from the center of the target area by subtracting 0.5 from local UV coordinates and multiplying by 2.0, giving a -1.0 to 1.0 range that is zero at the center. Multiply this direction by separate per-channel shift uniforms (for example 0.01 for red, 0 for green, -0.01 for blue) and offset each channel's texture sample independently before recombining red, green, and blue into the final color.

_Shader-focused developers track techniques like radial color separation through daily.dev's coverage of creative coding._

## Similar posts on daily.dev

- [Creating an Interactive 3D Cluster with Three.js, TSL and Three Start](https://daily.dev/posts/creating-an-interactive-3d-cluster-with-three-js-tsl-and-three-start-y4sedpfai) · Codrops · 2 upvotes · 0 comments
- [Shaders 103 - smoke](https://daily.dev/posts/shaders-103---smoke-dqivsjx8i) · Brad Woods' Digital Garden · 1 upvotes · 0 comments
- [Building a Dual-Scene Fluid X-Ray Reveal Effect in Three.js](https://daily.dev/posts/building-a-dual-scene-fluid-x-ray-reveal-effect-in-three-js-ajz5uu9x7) · Codrops · 3 upvotes · 0 comments

---

Tags: [#game-development](https://daily.dev/tags/game-development), [#webgl](https://daily.dev/tags/webgl), [#threejs](https://daily.dev/tags/threejs), [#creative-coding](https://daily.dev/tags/creative-coding)

[View this post on daily.dev](https://daily.dev/posts/building-a-mouse-following-square-lens-effect-with-three-js-and-glsl-0joazg7l2)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Building a Mouse-Following Square Lens Effect with Three.js and GLSL","url":"https://daily.dev/posts/building-a-mouse-following-square-lens-effect-with-three-js-and-glsl-0joazg7l2","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/building-a-mouse-following-square-lens-effect-with-three-js-and-glsl-0joazg7l2"},"datePublished":"2026-08-25T10:16:50.528Z","dateModified":"2026-09-14T08:08:06.523Z","description":"A step-by-step tutorial walks through building a mouse-following square lens effect using Three.js and GLSL, combining a grayscale image with a color image...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/ad642ecc1e09558ac4f320bab6042612?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/ad642ecc1e09558ac4f320bab6042612?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"Codrops","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"Codrops","logo":"https://media.daily.dev/image/upload/t_logo,f_auto/v1/logos/codrops","url":"https://daily.dev/sources/codrops"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/building-a-mouse-following-square-lens-effect-with-three-js-and-glsl-0joazg7l2","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":2},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"game-development,webgl,threejs,creative-coding","timeRequired":"PT19M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"Codrops","item":"https://daily.dev/sources/codrops"},{"@type":"ListItem","position":3,"name":"Building a Mouse-Following Square Lens Effect with Three.js and GLSL"}]}
{"@context":"https://schema.org","@type":"FAQPage","@id":"https://daily.dev/posts/building-a-mouse-following-square-lens-effect-with-three-js-and-glsl-0joazg7l2#faq","mainEntity":[{"@type":"Question","name":"How do I create a CC Lens-style distortion effect in a GLSL fragment shader?","acceptedAnswer":{"@type":"Answer","text":"Center the UV coordinates by subtracting 0.5, compute the squared radial distance with dot(), then scale that distance using a distortion factor: for positive distortion use 1.0 + distortion * radius2, for negative use 1.0 / (1.0 - distortion * radius2). Apply this scale to the centered position, convert back to UV space, and use the offset between distorted and original UV to sample the texture. Developers building interactive shader effects can find implementation walkthroughs like this one through daily.dev."}},{"@type":"Question","name":"How do you keep a square mask from stretching when the browser viewport is resized in a WebGL shader?","acceptedAnswer":{"@type":"Answer","text":"Calculate an aspect-ratio correction factor from the mesh dimensions using vec2(min(meshSize.y/meshSize.x, 1.0), min(meshSize.x/meshSize.y, 1.0)), then divide the centered UV coordinates used for the square boundary test by this factor before applying step() functions to build the mask. This keeps the square proportional regardless of screen dimensions. daily.dev surfaces practical shader techniques for developers refining responsive WebGL interfaces."}},{"@type":"Question","name":"How do you apply a radial RGB shift effect that gets stronger toward the edges of a UI element in GLSL?","acceptedAnswer":{"@type":"Answer","text":"Compute a direction vector from the center of the target area by subtracting 0.5 from local UV coordinates and multiplying by 2.0, giving a -1.0 to 1.0 range that is zero at the center. Multiply this direction by separate per-channel shift uniforms (for example 0.01 for red, 0 for green, -0.01 for blue) and offset each channel's texture sample independently before recombining red, green, and blue into the final color. Shader-focused developers track techniques like radial color separation through daily.dev's coverage of creative coding."}}]}
```

