Bug 20489 Configure illegal file characters https://bugzilla.wikimedia.org/show_bug...
[mediawiki.git] / js2 / mwEmbed / libClipEdit / pixastic-lib / actions / pointillize.js
blob59329c8a907874ec4e37167ab0b06eaf1fcc6a9f
1 /*
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]
5  */
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;
17                         var w = rect.width;
18                         var h = rect.height;
19                         var w4 = w*4;
20                         var y = h;
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");
29                         copy.width = w;
30                         copy.height = h;
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;
36                         if (transparent)
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] + ")";
56                                         ctx.beginPath();
57                                         ctx.arc(rect.left + rndX,rect.top + rndY, radius, 0, Math.PI*2, true);
58                                         ctx.closePath();
59                                         ctx.fill();
60                                 }
61                         }
63                         params.useData = false;
65                         return true;
66                 }
67         },
68         checkSupport : function() {
69                 return (Pixastic.Client.hasCanvasImageData());
70         }