Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
<div class="container">
<div class="options">
<label>Radius </label><input type="range" id="radius" value="25" min="10" max="50" /><br />
<label>Blur </label><input type="range" id="blur" value="15" min="10" max="50" />
<label>Blur </label><input type="range" id="blur" value="15" min="10" max="50" /><br />
<label>Opacity </label><input type="range" id="minOpacityPct" value="5" min="0" max="100" />
</div>
<canvas id="canvas" width="1000" height="600"></canvas>
</div>
Expand All @@ -40,11 +41,12 @@
}

var heat = simpleheat('canvas').data(data).max(18),
minOpacity = 0,
frame;

function draw() {
console.time('draw');
heat.draw();
heat.draw(minOpacity);
console.timeEnd('draw');
frame = null;
}
Expand All @@ -58,10 +60,12 @@

var radius = get('radius'),
blur = get('blur'),
minOpacityPct = get('minOpacityPct'),
changeType = 'oninput' in radius ? 'oninput' : 'onchange';

radius[changeType] = blur[changeType] = function (e) {
radius[changeType] = blur[changeType] = minOpacityPct[changeType] = function (e) {
heat.radius(+radius.value, +blur.value);
minOpacity = +minOpacityPct.value / 100;
frame = frame || window.requestAnimationFrame(draw);
};

Expand Down
8 changes: 5 additions & 3 deletions simpleheat.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,26 +106,28 @@ simpleheat.prototype = {
// draw a grayscale heatmap by putting a blurred circle at each data point
for (var i = 0, len = this._data.length, p; i < len; i++) {
p = this._data[i];
ctx.globalAlpha = Math.min(Math.max(p[2] / this._max, minOpacity === undefined ? 0.05 : minOpacity), 1);
ctx.globalAlpha = Math.min(p[2] / this._max, 1);
ctx.drawImage(this._circle, p[0] - this._r, p[1] - this._r);
}

// colorize the heatmap, using opacity value of each pixel to get the right color from our gradient
var colored = ctx.getImageData(0, 0, this._width, this._height);
this._colorize(colored.data, this._grad);
this._colorize(colored.data, this._grad, minOpacity);
ctx.putImageData(colored, 0, 0);

return this;
},

_colorize: function (pixels, gradient) {
_colorize: function (pixels, gradient, minOpacity) {
var minAlpha = 255 * (minOpacity || 0);
for (var i = 0, len = pixels.length, j; i < len; i += 4) {
j = pixels[i + 3] * 4; // get gradient color from opacity value

if (j) {
pixels[i] = gradient[j];
pixels[i + 1] = gradient[j + 1];
pixels[i + 2] = gradient[j + 2];
pixels[i + 3] = Math.max(pixels[i + 3], minAlpha);
}
}
},
Expand Down