Skip to content

Commit dd6b7eb

Browse files
committed
Add minOpacity configuration option to heatmap.js and to heatmap-leaflet.js
1 parent 0d925f8 commit dd6b7eb

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ At the configuration object you can specify the following properties in order to
1515
- **visible** (optional) Boolean. Whether the heatmap is visible or not. Default is true
1616
- **gradient** (optional) Object. An object which contains colorstops from 0 to 1. Default is the standard heatmap gradient.
1717
- **opacity** (optional) Number [0-100]. Opacity of the heatmap measured in percent.
18+
- **minOpacity** (optional) Number [0-100]. Minimum opacity of the heatmap measured in percent. Default is 0 (transparent).
1819

1920
Here is an example instanciation:
2021

src/heatmap-leaflet.js

+1
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@
191191
"element": tile,
192192
"visible": true,
193193
"opacity": 100, // we use leaflet's opacity for tiles
194+
"minOpacity": options.minOpacity * 100,
194195
"gradient": options.gradient,
195196
"debug": options.debug
196197
});

src/heatmap.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@
305305
max : false,
306306
gradient : false,
307307
opacity: 180,
308+
minOpacity: 0,
308309
premultiplyAlpha: false,
309310
bounds: {
310311
l: 1000,
@@ -342,6 +343,7 @@
342343
me.set("max", config.max || false);
343344
me.set("gradient", config.gradient || { 0.45: "rgb(0,0,255)", 0.55: "rgb(0,255,255)", 0.65: "rgb(0,255,0)", 0.95: "yellow", 1.0: "rgb(255,0,0)"}); // default is the common blue to red gradient
344345
me.set("opacity", parseInt(255/(100/config.opacity), 10) || 180);
346+
me.set("minOpacity", parseInt(255/(100/config.minOpacity), 10) || 0);
345347
me.set("width", config.width || 0);
346348
me.set("height", config.height || 0);
347349
me.set("debug", config.debug);
@@ -464,6 +466,7 @@
464466
premultiplyAlpha = me.get("premultiplyAlpha"),
465467
palette = me.get("gradient"),
466468
opacity = me.get("opacity"),
469+
minOpacity = me.get("minOpacity"),
467470
bounds = me.get("bounds"),
468471
left, top, bottom, right,
469472
image, imageData, length, alpha, offset, finalAlpha;
@@ -524,7 +527,14 @@
524527

525528
// we ve started with i=3
526529
// set the new r, g and b values
527-
finalAlpha = (alpha < opacity)?alpha:opacity;
530+
if(alpha < minOpacity){
531+
finalAlpha = minOpacity;
532+
}else if(alpha > opacity){
533+
finalAlpha = opacity;
534+
}else{
535+
finalAlpha = alpha;
536+
}
537+
528538
imageData[i-3]=palette[offset];
529539
imageData[i-2]=palette[offset+1];
530540
imageData[i-1]=palette[offset+2];
@@ -537,7 +547,7 @@
537547
imageData[i-1] /= 255/finalAlpha;
538548
}
539549

540-
// we want the heatmap to have a gradient from transparent to the colors
550+
// we want the heatmap to have a gradient from transparent (or minOpacity, if specified) to the colors.
541551
// as long as alpha is lower than the defined opacity (maximum), we'll use the alpha value
542552
imageData[i] = finalAlpha;
543553
}
@@ -650,4 +660,4 @@
650660
};
651661
})();
652662
w.h337 = w.heatmapFactory = heatmapFactory;
653-
})(window);
663+
})(window);

0 commit comments

Comments
 (0)