/*
 (c) 2014, Vladimir Agafonkin
 simpleheat, a tiny JavaScript library for drawing heatmaps with Canvas
 https://github.com/mourner/simpleheat
*/

(function () { 'use strict';

function simpleheat(canvas) {
    // jshint newcap: false, validthis: true
    if (!(this instanceof simpleheat)) { return new simpleheat(canvas); }

    this._canvas = canvas = typeof canvas === 'string' ? document.getElementById(canvas) : canvas;

    this._ctx = canvas.getContext('2d');
    this._width = canvas.width;
    this._height = canvas.height;
}

simpleheat.prototype = {

    data: function (data) {
        this._data = data;
        return this;
    },

    radius: function (r, blur) {
        this._circle = document.createElement('canvas');

        blur = blur || 15;

        var ctx = this._circle.getContext('2d'),
            r2 = this._r = r + blur;

        this._circle.width = this._circle.height = r2 * 2;

        ctx.shadowOffsetX = ctx.shadowOffsetY = 1000;
        ctx.shadowBlur = blur;
        ctx.shadowColor = 'black';

        ctx.beginPath();
        ctx.arc(r2 - 1000, r2 - 1000, r, 0, Math.PI * 2, true);
        ctx.closePath();
        ctx.fill();

        return this;
    },

    gradient: function (grad) {
        var canvas = document.createElement('canvas'),
            ctx = canvas.getContext('2d');

        canvas.width = 1;
        canvas.height = 256;

        var gradient = ctx.createLinearGradient(0, 0, 0, 256);

        gradient.addColorStop(0, 'rgba(0,0,255,0)');

        for (var i in grad) {
            gradient.addColorStop(i, grad[i]);
        }

        ctx.fillStyle = gradient;
        ctx.fillRect(0, 0, 1, 256);

        this._gradient = ctx.getImageData(0, 0, 1, 256).data;

        return this;
    },

    draw: function (max, min) {
        if (!this._circle) {
            this.radius(25).gradient({0.45: 'blue', 0.55: 'cyan', 0.65: 'lime', 0.95: 'yellow', 1: 'red'});
        }

        var ctx = this._ctx;

        ctx.clearRect(0, 0, this._width, this._height);

        for (var i = 0, len = this._data.length, p; i < len; i++) {
            p = this._data[i];

            ctx.globalAlpha = Math.max(p[2], min || max * 0.05) / (max || 1);
            ctx.drawImage(this._circle, p[0] - this._r, p[1] - this._r);
        }

        var colored = ctx.getImageData(0, 0, this._width, this._height);
        this._colorize(colored.data, this._gradient);
        ctx.putImageData(colored, 0, 0);

        return this;
    },

    _colorize: function (pixels, gradient) {
        for (var i = 3, len = pixels.length, j; i < len; i += 4) {
            j = pixels[i] * 4;

            if (j) {
                pixels[i - 3] = gradient[j];
                pixels[i - 2] = gradient[j + 1];
                pixels[i - 1] = gradient[j + 2];
            }
        }
    }
};

window.simpleheat = simpleheat;

})();

@@ -673,32 +673,73 @@
  blur) %7B%0A       
+ blur = blur %7C%7C 15;%0A%0A        var circle =
  this._circle = 
@@ -770,18 +770,17 @@
 canvas')
-;%0A
+,
 %0A       
@@ -784,52 +784,18 @@
     
-blur = blur %7C%7C 15;%0A%0A        var ctx = this._
+    ctx = 
 circ
@@ -861,22 +861,16 @@
         
-this._
 circle.w
@@ -876,22 +876,16 @@
 width = 
-this._
 circle.h
@@ -949,18 +949,17 @@
 fsetY = 
-10
+2
 00;%0A    
@@ -1063,26 +1063,25 @@
 tx.arc(r2 - 
-10
+2
 00, r2 - 100
@@ -1081,10 +1081,9 @@
 2 - 
-10
+2
 00, 
@@ -1311,18 +1311,17 @@
 xt('2d')
-;%0A
+,
 %0A       
@@ -1325,117 +1325,117 @@
     
-canvas.width = 1;%0A        canvas.height = 256;%0A%0A        var gradient = ctx.createLinearGradient(0, 0, 0, 256)
+    gradient = ctx.createLinearGradient(0, 0, 0, 256);%0A%0A        canvas.width = 1;%0A        canvas.height = 256
 ;%0A%0A 
@@ -1660,28 +1660,24 @@
   this._grad
-ient
  = ctx.getIm
@@ -2383,20 +2383,16 @@
 is._grad
-ient
 );%0A     

clean up code a bit