Pointilizer
Canvas reads the edges of an image and Raphaël redraws them as a cloud of animated SVG dots that react to the pointer.
What you are seeing
On a dark grey page, an eight-pointed star and an exclamation mark are drawn from about a thousand small orange, gold, pink and near-black dots. At the start the dots are large translucent blobs that bounce and shrink into the shape. A dot under the pointer swells into a large pale disc.
How it works
The source image is drawn to a hidden canvas. Random pixels are tested with a Sobel filter and kept only if they sit on an edge, until there are 1000 points.
Each point stores its position, colour and a size based on edge strength.
Raphaël creates one SVG circle per point and animates it from a large transparent disc down to a small solid dot. Hovering a dot swells it, and leaving lets it settle back.
History
Published on August 22, 2010.
Source
/pointilizer/animated-points.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Pointilizer | SVG Wow!</title>
<meta name="description" content="A canvas reads the edges of an image, then Raphaël draws them as a cloud of animated SVG circles that react to the pointer." />
<style>
html,body { margin: 0; padding: 0; width: 100%; height: 100%; background: #222; color: #ccc; font: 14px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif }
body { display: flex; flex-direction: column; align-items: center }
#result { flex: 1; min-height: 0; width: 100%; text-align: center }
#result svg { display: block; margin: 0 auto; width: 100%; height: 100%; max-width: 600px; max-height: 600px }
p { margin: 0; padding: 6px 16px 10px; text-align: center }
a { color: #ffc14d }
</style>
</head>
<body>
<div id="result" role="img" aria-label="An eight-pointed star and an exclamation mark drawn entirely from small coloured dots."></div>
<p>Erik Dahlström, 2010, based on textorizer by Max Froumentin. Hover the dots. Press R to replay. <a href="/gallery/pointilizer/">Gallery page</a></p>
<script src="../res/3.0/scripts/tools/pointilizer.js"></script>
<script src="../res/3.0/scripts/raphael/raphael-min.js"></script>
<script>// <![CDATA[
(function() {
var size = 400;
var reduce = window.matchMedia && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
var paper = Raphael("result", size*1.5, size*1.5);
// Scale with the window. Raphael 1.5 sets fixed pixel sizes.
paper.canvas.setAttribute("viewBox", "0 0 " + size*1.5 + " " + size*1.5);
paper.canvas.removeAttribute("width");
paper.canvas.removeAttribute("height");
paper.canvas.style.width = "";
paper.canvas.style.height = "";
function draw(r) {
paper.clear();
var len = r.data.length;
var data = r.data;
var first = null;
for(var i = 0; i < len; i++)
{
(function(d, i) {
var c = paper.circle(d.x+size/4, d.y+size/4, d.r*d.scale*Math.random()*2).attr(
{ fill: "rgb(" + d.r + "," + d.g + "," + d.b + ")", "fill-opacity" : 0, stroke: "none"});
if(reduce)
c.attr({r: d.scale*10, "fill-opacity": 1});
else if(first)
c.animateWith(first, {r: d.scale*10, "fill-opacity": 1}, i*10, "bounce");
else
{
first = c;
c.animate({r: d.scale, "fill-opacity": 1}, 3000, ">");
}
// hover handler
var overScope = paper, outScope = paper;
c.hover(function (event) {
c.animate({r: 50, "fill-opacity": 0.5}, reduce ? 1 : 1000, "bounce");
}, function (event) {
c.animate({r: d.scale*10, "fill-opacity": 1}, reduce ? 1 : 1000, "bounce");
}, overScope, outScope);
})(data[i], i);
}
}
function run() {
sw.tools.pointilize({
url: "source.png",
output: "points",
points: 1000,
threshold: 0,
width: size,
height: size
}, draw);
}
run();
document.addEventListener("keydown", function(evt) {
if(evt.key === "r" || evt.key === "R")
run();
});
})();
// ]]>
</script>
</body>
</html>
/res/3.0/scripts/tools/pointilizer.js
/** =======================================================================
* Based on textorizer by Max Froumentin
* Textorizer: Copyright 2006 Max Froumentin. Distributed under the
* W3C Software Notice and License:
* https://www.w3.org/Consortium/Legal/2002/copyright-software-20021231
* Project: https://www.w3.org/People/maxf/textorizer/
* Ported to javascript by Erik Dahlstrom
* Port's original license statement: CC0, for Erik Dahlstrom's own contribution
* http://creativecommons.org/publicdomain/zero/1.0/
* ======================================================================= */
(function () {
window.sw = window.sw ? window.sw : {};
window.sw.tools = window.sw.tools ? window.sw.tools : {};
/**
* Pointilize takes an image, filters it and then outputs an svg image with the given parameters.
*
* The callback function will receive one argument, an associative array of the output as either a DOM tree "doc", or as text "text".
*
* @param callback A function to be called when the operation is completed
* @param options An associative array with options for this function, valid options are:
* { "url" : the url of the raster image to textorize
* "output": what to return, can be 'svg' or 'points'
* "points" : how many points to use
* "threshold" : a threshold value for the filter
* "width" : image width of the textorizer output svg
* "height" : image height of the textorizer output svg
* "absolute_size" : set the width and height as attributes on the generated svg
* "bg_image" : include the source image in the background
* "bg_image_opacity" : the opacity of the background image
* }
*/
sw.tools.pointilize = function(options, callback)
{
var defaults = {
"url" : "../res/2.0/images/favicon135x135.png",
"output" : "svg",
"points" : 5000,
"threshold" : 3,
"width" : 800,
"height" : 600,
"absolute_size" : true,
"bg_image" : true,
"bg_image_opacity" : 0.5
};
if (!options)
options = {}
for (var def in defaults)
{
if (typeof options[def] == "undefined")
options[def] = defaults[def]
}
var canvas = document.createElementNS("http://www.w3.org/1999/xhtml", "canvas");
var image = new Image();
image.onload = function() {
if (canvas)
{
canvas.width = options.width;
canvas.height = options.height;
var ctx = canvas.getContext("2d");
if (ctx)
{
ctx.clearRect(0,0,options.width,options.height);
ctx.drawImage(image, 0, 0, options.width, options.height);
var output = make_svg(ctx.getImageData(0, 0, options.width, options.height), options);
callback(output);
}
}
};
image.src = options.url;
};
/**
* Sobel convolution filter
* http://en.wikipedia.org/wiki/Sobel
*/
var Sx = [
[ -1, 0, 1 ],
[ -2, 0, 2 ],
[ -1, 0, 1 ]
];
var Sy =
[
[ -1, -2, -1 ],
[ 0, 0, 0 ],
[ 1, 2, 1 ]
];
function colorAverageAt(imagedata, x, y)
{
if (x < 0)
x = 0;
if (x >= imagedata.width)
x = imagedata.width - 1;
if (y < 0)
y = 0;
if (y >= imagedata.height)
y = imagedata.height - 1;
var index= 4*(x + y*imagedata.width);
var pixels = imagedata.data;
return Math.round((pixels[index] + pixels[index+1] + pixels[index+2]) / 3);
}
function make_svg(imagedata, options)
{
var randomX;
var randomY;
var h, i, j, x, y, v;
var dir, dx, dy;
var word;
var pixels = imagedata.data;
var width = imagedata.width;
var height = imagedata.height;
var dmag2;
var B;
var scale;
var width = options.width;
var height = options.height;
var output;
if (options.output == "svg")
{
output = "<svg xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'";
if (options.absolute_size)
output += " width='" + width + "' height='" + height + "' ";
output += " viewBox='0 0 " + width + " " + height + "'>\n";
if (options.bg_image)
{
output += " <image xlink:href='" + options.url + "' width='100%' height='100%' preserveAspectRatio='none' opacity='" +
options.bg_image_opacity + "'/>";
}
}
else
output = [];
for (h = 0; h < options.points;)
{
x = Math.floor(Math.random() * width);
y = Math.floor(Math.random() * height);
v = colorAverageAt(imagedata, x, y);
if (v != 255)
{
/* compute pixel derivative */
dx = dy = 0;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
{
var vnear = colorAverageAt(imagedata, x + i - 1, y + j - 1);
dx += Sx[j][i] * vnear;
dy += Sy[j][i] * vnear;
}
dx /= 8;
dy /= 8;
dmag2 = dx * dx + dy * dy;
/*
* arbitrary threshold on gradient
*/
if (dmag2 > options.threshold)
{
h++;
B = 2 * (width + height) / 5000.;
scale = B + B * Math.sqrt(dmag2) / 100;
if (dx == 0)
dir = Math.PI / 2;
else if (dx > 0)
dir = Math.atan(dy / dx);
else if (dy == 0)
dir = 0;
else if (dy > 0)
dir = Math.atan(-dx / dy) + Math.PI / 2;
else
dir = Math.atan(dy / dx) + Math.PI;
var index=(y*4)*imagedata.width+(x*4);
var r = pixels[index];
var g = pixels[index+1];
var b = pixels[index+2];
if (options.output == "svg")
{
output += "<circle cx='" + x + "' cy='" + y + "' r='" + Math.random()*scale*10 + "'" +
" fill='rgb(" + r + "," + g + "," + b + ")'/>\n";
}
else
{
output[output.length] = { x: x, y: y, scale: scale, r: r, g: g, b: b };
}
}
}
}
if (options.output == "svg")
{
output += "</svg>\n";
var doc = null;
try
{
if (window.DOMParser)
{
parser=new DOMParser();
doc=parser.parseFromString(output,"text/xml");
}
}
catch(e)
{
}
}
return {"data": output, "doc": doc};
}
})();