Any image, as sand.
sand-portrait is the dot effect from my homepage, turned into a small open-source library. Dots rise from a pile of sand into a portrait, then scatter from your cursor and spring back.
Try it.
Hover to push the dots, click to send a ripple, or drop your own photo on the stage. Cut-out photos look best.
- Role
- Designed and built by me
- Stack
- Vanilla JavaScript, Canvas 2D
- Licence
- MIT, free to use
- Code
- GitHub ↗
- Origin
- The dot portrait and wordmark on this site
How it
works
Four steps on one canvas, with no libraries underneath.
Sample
The image is drawn small, one pixel per dot. Each pixel's brightness sets its dot's size, so the portrait stays a positive image in light or dark.
Halftone gridPile
Every dot starts on the floor in a low mound of sand, denser in the middle, like a pile that was just poured.
IntroRise
Dots rise in a staggered arc, lowest rows first, and land exactly on their spot in the image.
Easing · staggerReact
A spring pulls each dot home, so the cursor can push them aside and they settle back. A click sends a ripple.
Spring physics
Built for
real pages
The details that make it safe to put on a production site.
People who ask for less motion get the finished image straight away: no intro, no interaction.
It stops drawing when nothing moves, and pauses when it is off screen or the tab is hidden.
Size the canvas with CSS. The dot grid rebuilds when the size changes.
A studio or wall background is detected from the corners, so only the subject turns to sand.
SandPortrait.text turns any word into a source, with even dots for type.
Use it
A script tag, or import it as a module. Size the canvas with CSS and pass an image.
<!-- script tag -->
<canvas id="me" style="width:400px;height:500px"></canvas>
<script src="https://cdn.jsdelivr.net/gh/hellosaifuli/sand-portrait@v1.0.0/sand-portrait.js"></script>
<script>
SandPortrait(document.getElementById('me'), { src: 'me.png' });
</script>
// or as a module
import SandPortrait from 'https://cdn.jsdelivr.net/gh/hellosaifuli/sand-portrait@v1.0.0/sand-portrait.mjs';
const sand = SandPortrait(canvas, { src: 'me.png', on: 'dark', color: '#fff' });
sand.replay(); Keep
exploring
The code, how it was built, and where it runs.