From a7597199d252276e0b43bd834630e01f5e0ea35e Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 7 Apr 2022 16:19:46 -0400 Subject: [PATCH 1/2] Update simpleheat.js clear canvas --- simpleheat.js | 1 + 1 file changed, 1 insertion(+) diff --git a/simpleheat.js b/simpleheat.js index dc06eb9..42a91a6 100644 --- a/simpleheat.js +++ b/simpleheat.js @@ -43,6 +43,7 @@ simpleheat.prototype = { }, clear: function () { + this._ctx.clearRect(0, 0, this._width, this._height); this._data = []; return this; }, From 1e64cd3d9996c5a3e2ef00ae471e86c0be205c8a Mon Sep 17 00:00:00 2001 From: YGC Date: Wed, 13 Apr 2022 16:00:04 -0400 Subject: [PATCH 2/2] add alpha support --- package.json | 2 +- simpleheat.js | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 006858c..4931a3f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "simpleheat", - "version": "0.4.0", + "version": "0.4.1", "description": "A tiny JavaScript library for drawing heatmaps with Canvas", "homepage": "https://github.com/mourner/simpleheat", "keywords": [ diff --git a/simpleheat.js b/simpleheat.js index 42a91a6..63d6b18 100644 --- a/simpleheat.js +++ b/simpleheat.js @@ -96,7 +96,7 @@ simpleheat.prototype = { return this; }, - draw: function (minOpacity) { + draw: function (minOpacity, alpha) { if (!this._circle) this.radius(this.defaultRadius); if (!this._grad) this.gradient(this.defaultGradient); @@ -113,13 +113,13 @@ simpleheat.prototype = { // 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, alpha); ctx.putImageData(colored, 0, 0); return this; }, - _colorize: function (pixels, gradient) { + _colorize: function (pixels, gradient, alpha) { for (var i = 0, len = pixels.length, j; i < len; i += 4) { j = pixels[i + 3] * 4; // get gradient color from opacity value @@ -127,6 +127,10 @@ simpleheat.prototype = { pixels[i] = gradient[j]; pixels[i + 1] = gradient[j + 1]; pixels[i + 2] = gradient[j + 2]; + if (alpha) + { + pixels[i + 3] = pixels[i + 3] * alpha; + } } } },