2 * Pixastic Lib - Pointillize filter - v0.1.0
3 * Copyright (c) 2008 Jacob Seidelin, jseidelin@nihilogic.dk, http://blog.nihilogic.dk/
4 * MIT License [http://www.opensource.org/licenses/mit-license.php]
7 Pixastic.Actions.pointillize = {
9 process : function(params) {
10 var radius = Math.max(1,parseInt(params.options.radius,10));
11 var density = Math.min(5,Math.max(0,parseFloat(params.options.density)||0));
12 var noise = Math.max(0,parseFloat(params.options.noise)||0);
13 var transparent = !!params.options.transparent;
15 if (Pixastic.Client.hasCanvasImageData()) {
16 var rect = params.options.rect;
22 var ctx = params.canvas.getContext("2d");
24 var pixel = document.createElement("canvas");
25 pixel.width = pixel.height = 1;
26 var pixelCtx = pixel.getContext("2d");
28 var copy = document.createElement("canvas");
31 var copyCtx = copy.getContext("2d");
32 copyCtx.drawImage(params.canvas,rect.left,rect.top,w,h, 0,0,w,h);
34 var diameter = radius * 2;
37 ctx.clearRect(rect.left, rect.top, rect.width, rect.height);
39 var noiseRadius = radius * noise;
41 var dist = 1 / density;
43 for (var y=0;y<h+radius;y+=diameter*dist) {
44 for (var x=0;x<w+radius;x+=diameter*dist) {
45 rndX = noise ? (x+((Math.random()*2-1) * noiseRadius))>>0 : x;
46 rndY = noise ? (y+((Math.random()*2-1) * noiseRadius))>>0 : y;
48 var pixX = rndX - radius;
49 var pixY = rndY - radius;
50 if (pixX < 0) pixX = 0;
51 if (pixY < 0) pixY = 0;
53 pixelCtx.drawImage(copy, pixX, pixY, diameter, diameter, 0, 0, 1, 1);
54 var data = pixelCtx.getImageData(0,0,1,1).data;
55 ctx.fillStyle = "rgb(" + data[0] + "," + data[1] + "," + data[2] + ")";
57 ctx.arc(rect.left + rndX,rect.top + rndY, radius, 0, Math.PI*2, true);
63 params.useData = false;
68 checkSupport : function() {
69 return (Pixastic.Client.hasCanvasImageData());