## Archive for October, 2012

### Circular stipple patterns on an HTML5 canvas

In my previous post on stipple patterns, I presented code to draw a few simple stipple patterns based on drawing single pixels at fixed locations. In this post, I’ll present something just a bit more complex: drawing circles to create a circular stipple patten, again writing a shader that makes use of the GraphicsCore and FXController classes.

`Shader.circleStippleShader = function (imageData, bufWrite, index, x, y, r, g, b, a, passNum, frameNum, maxFrames){ var alpha = 1.0; var r1 = r / 255.0; var rF = Math.floor((alpha * r1 + (1.0 - alpha)) * 255.0); var circleMaxDiam = 12; // circle at every 12th pixel, also defines max diameter of circle, var circleMaxRadius = circleMaxDiam / 2; // maximum radius of circle // figure out the x, y indices of the circle we're within // x,y need to be shifted by the circle radius b/c circleMaxDiam defined the offset of the circle center // ... e.g. going along the x-axis, we are within the next circle not at x/circleMaxDiam, but at (x+6)/circleMaxDiam var iX = Math.floor((x + circleMaxRadius) / circleMaxDiam); var iY = Math.floor((y + circleMaxRadius) / circleMaxDiam); // multiply the circle indices by the diameter to get the actual coordinates of the circle's center var targetX = iX * circleMaxDiam; var targetY = iY * circleMaxDiam; // calculate squared distance to the circle we are within var dist = (targetX - x) * (targetX - x) + (targetY - y) * (targetY - y); if (dist < 25) { GraphicsCore.setPixel(bufWrite, index, rF, 0, 0, 255); } else { GraphicsCore.setPixel(bufWrite, index, r, g, b, 255); }}Shader.circleStippleShader.numPassesRequired = 1;    ` Conceptually, we define the center of a circle at every 12th pixel (both along the x and y axis). At every pixel (x,y) we figure out which circle we are within, and calculate the distance to the center. If the distance is less than our threshold (25), we change the color of the pixel (use the red channel only).