Skip to content

Commit

Permalink
merged master code
Browse files Browse the repository at this point in the history
  • Loading branch information
nswamy14 committed Jan 7, 2024
2 parents 737278a + 720095e commit 02c2bd3
Show file tree
Hide file tree
Showing 13 changed files with 210 additions and 265 deletions.
1 change: 0 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module.exports = {
},
globals: {
'Image': true,
'Path2D': true,
'Canvas': true
},
rules: {
Expand Down
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules/
server/
.DS_Store
npm-debug.log
test/
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2019, Narayana Swamy
Copyright (c) 2019, Narayana Swamy([email protected])
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
37 changes: 18 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# Visual-Heatmap Js [![npm](https://img.shields.io/npm/v/visual-heatmap.svg)](https://www.npmjs.com/package/visual-heatmap) [![Downloads](https://img.shields.io/npm/dm/visual-heatmap.svg)](https://www.npmjs.com/package/visual-heatmap)
Open source javascript module for rendering high performance, large scale heatmap.

Visual Heatmap is based on advanced graphical rendering context - WebGL/Shaders. It can render 500,000+ data points with a good framerate.
Visual Heatmap, an open-source JavaScript module, emerges as a powerful tool designed to render large-scale heatmaps with exceptional performance. This framework is based on advanced graphical rendering - WebGL/Shaders. It can render 500,000+ data points with a good framerate.

### Examples:
<p align="center">
<a href="https://nswamy14.github.io/visual-heatmap/demo/heatmap3.html"> <label>Click me<label><img src="https://nswamy14.github.io/visual-heatmap/snaps/snap3.png" width=1200> </a>
<a href="https://nswamy14.github.io/visual-heatmap/demo/heatmap1.html"> <label>Click me<label><img src="https://nswamy14.github.io/visual-heatmap/snaps/snap1.png" width=1200></a>
Expand All @@ -24,7 +23,7 @@ Download source code from below links

Visual-Heatmap is written in ES6 Modules. To import use below syntax

Importing everthing into namespace
Importing everything into namespace
```
import Heatmap from 'visual-heatmap'
```
Expand All @@ -40,9 +39,9 @@ let instance = Heatmap('#containerId', {
min: 0, // if not set, will be derived from data
intensity: 1.0,
background: {
url: "path",
width: 100, // if not set, naturalWidth of the image will be considered
height: 100, // if not set, naturalWidth of the image will be considered
url: "urlPath",
width: 100, // if not set, viewport width of the image will be considered
height: 100, // if not set, viewport height of the image will be considered
x: 0,
y: 0
},
Expand Down Expand Up @@ -70,16 +69,16 @@ let instance = Heatmap('#containerId', {
Object with config properties.
```
{
size : Radius of the data point, in pixels.
max : Max data Value for relative gradient computation.
min : Min data Value for relative gradient computation.
intensity : intensity factor.
opacity : Opacity factor.
rotationAngle : Rotation angle.
translate : translate vector [x, y].
zoom : Zoom Factor.
gradient : Color Gradient, array of objects with color value and offset.
background: To set background of the heatMap
size : Radius of the data point, in pixels.
max : Max data Value for relative gradient computation.
min : Min data Value for relative gradient computation.
intensity : intensity factor.
opacity : Opacity factor.
rotationAngle : Rotation angle.
translate : translate vector [x, y].
zoom : Zoom Factor.
gradient : Color Gradient, array of objects with color value and offset.
background: To set background of the heatMap
}
```

Expand Down Expand Up @@ -123,11 +122,11 @@ Try [Example](https://nswamy14.github.io/visual-heatmap/demo/heatmap3.html)

### instance.setBackgroundImage({ url: , x: , y: , height: , width: })
Api to set Background image. Accepts Object with { Url, height, width, x, and y} properties as input
Try [Example](https://nswamy14.github.io/visual-heatmap/demo/heatmap_withImage.html)
Try [Example](https://nswamy14.github.io/visual-heatmap/demo/heatmap3.html)

### instance.projection({x: , y: })
Api to get projected co-ordinates relative to the heatmap layer.
Try [Example](https://nswamy14.github.io/visual-heatmap/demo/heatmap3.html)
Try [Example](https://nswamy14.github.io/visual-heatmap/demo/heatmapWithLabels.html)

### instance.clear()
Api to clear canvas.
Expand Down
6 changes: 3 additions & 3 deletions demo/heatmap1.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

var gui = new dat.GUI();
var Params = function() {
this.points = 20000;
this.points = 10000;
this.size = 15;
this.opacity = 1.0;
this.intensity = 1.0;
Expand All @@ -56,7 +56,7 @@
};

var params = new Params();
gui.add( params, 'points', 1, 1000000 ).onChange( function () {
gui.add( params, 'points', 1, 500000 ).onChange( function () {
data = generateData(params.points);
instance.renderData(data);
} );
Expand All @@ -82,7 +82,7 @@
instance.setRotationAngle(params.rotationAngle);
} );

data = generateData(20000);
data = generateData(10000);
instance.renderData(data);

function generateData (count) {
Expand Down
10 changes: 5 additions & 5 deletions demo/heatmap2.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

let data = [];
let instance = Heatmap('#canvas', {
size: 15.0,
size: 25.0,
max: 100,
backgroundImage: {
url: "./world_map_PNG14.png",
Expand All @@ -43,8 +43,8 @@

var gui = new dat.GUI();
var ParamsCon = function() {
this.points = 100000;
this.size = 15;
this.points = 10000;
this.size = 25;
this.opacity = 1.0;
this.intensity = 1.0;
this.transalteX = 0;
Expand All @@ -56,7 +56,7 @@
};

var params = new ParamsCon();
gui.add( params, 'size', 10, 100 ).onChange( function () {
gui.add( params, 'size', 5, 100 ).onChange( function () {
instance.setSize(params.size);
} );
gui.add( params, 'opacity', 0, 1 ).onChange( function () {
Expand Down Expand Up @@ -93,7 +93,7 @@
}
return data;
}
data = generateData(100000);
data = generateData(10000);
call();

function call () {
Expand Down
2 changes: 1 addition & 1 deletion demo/heatmap3.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
};

var params = new ParamsCon();
gui.add( params, 'size', 1, 100 ).onChange( function () {
gui.add( params, 'size', 1, 200 ).onChange( function () {
instance.setSize(params.size);
} );
gui.add( params, 'opacity', 0, 1 ).onChange( function () {
Expand Down
8 changes: 1 addition & 7 deletions demo/heatmapWithLabels.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@

var gui = new dat.GUI();
var ParamsCon = function() {
this.points = 100;
this.size = 50;
this.opacity = 1.0;
this.intensity = 1.0;
Expand All @@ -59,11 +58,6 @@
};

var params = new ParamsCon();
gui.add( params, 'points', 1, 1000000 ).onChange( function () {
data = generateData(params.points);
instance.renderData(data);
params.updateLabels();
} );
gui.add( params, 'size', 50, 100 ).onChange( function () {
instance.setSize(params.size);
params.updateLabels();
Expand Down Expand Up @@ -93,7 +87,7 @@
params.updateLabels();
} );

data = generateData(100);
data = generateData(200);
instance.renderData(data);


Expand Down
134 changes: 59 additions & 75 deletions dist/visualHeatmap.esm.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*!
* Heatmap v1.0.5
* (c) 2023 Narayana Swamy ([email protected])
* (c) 2024 Narayana Swamy ([email protected])
* @license BSD-3-Clause
*/
function Heatmap (context, config = {}) {
Expand Down Expand Up @@ -568,15 +568,16 @@ function getPixlRatio (ctx) {
}

var GradShaders = {
vertex: `attribute vec2 a_position;
attribute float a_intensity;
vertex: `#version 300 es
in vec2 a_position;
in float a_intensity;
uniform float u_size;
uniform vec2 u_resolution;
uniform vec2 u_translate;
uniform float u_zoom;
uniform float u_angle;
uniform float u_density;
varying float v_i;
out float v_i;
vec2 rotation(vec2 v, float a, float aspect) {
float s = sin(a); float c = cos(a); mat2 m = mat2(c, -s, s, c);
Expand All @@ -600,11 +601,13 @@ var GradShaders = {
gl_PointSize = u_size * u_density;
v_i = a_intensity;
}`,
fragment: `precision mediump float;
fragment: `#version 300 es
precision mediump float;
uniform float u_max;
uniform float u_min;
uniform float u_intensity;
varying float v_i;
in float v_i;
out vec4 fragColor;
void main() {
float r = 0.0;
vec2 cxy = 2.0 * gl_PointCoord - 1.0;
Expand All @@ -614,83 +617,64 @@ var GradShaders = {
deno = 1.0;
}
if(r <= 1.0) {
gl_FragColor = vec4(0, 0, 0, ((v_i - u_min) / (deno)) * u_intensity * (1.0 - sqrt(r)));
fragColor = vec4(0, 0, 0, ((v_i - u_min) / (deno)) * u_intensity * (1.0 - sqrt(r)));
}
}`
};

var ColorShader = {
vertex: `#version 300 es
precision highp float;
in vec2 a_texCoord;
out vec2 v_texCoord;
void main() {
vec2 clipSpace = a_texCoord * 2.0 - 1.0;
gl_Position = vec4(clipSpace * vec2(1, -1), 0, 1);
v_texCoord = a_texCoord;
}`,
precision highp float;
in vec2 a_texCoord;
out vec2 v_texCoord;
void main() {
vec2 clipSpace = a_texCoord * 2.0 - 1.0;
gl_Position = vec4(clipSpace * vec2(1, -1), 0, 1);
v_texCoord = a_texCoord;
}
`,
fragment: `#version 300 es
precision mediump float;
in vec2 v_texCoord;
out vec4 fragColor;
uniform sampler2D u_framebuffer;
uniform vec4 u_colorArr[11];
uniform float u_colorCount;
uniform float u_opacity;
uniform float u_offset[11];
float remap ( float minval, float maxval, float curval ) {
return ( curval - minval ) / ( maxval - minval );
}
precision mediump float;
in vec2 v_texCoord;
out vec4 fragColor;
uniform sampler2D u_framebuffer;
uniform vec4 u_colorArr[20];
uniform float u_colorCount;
uniform float u_opacity;
uniform float u_offset[20];
float remap ( float minval, float maxval, float curval ) {
return ( curval - minval ) / ( maxval - minval );
}
void main() {
float alpha = texture(u_framebuffer, v_texCoord.xy).a;
if (alpha > 0.0 && alpha <= 1.0) {
vec4 color_;
if (alpha <= u_offset[0]) {
color_ = u_colorArr[0];
} else if (alpha <= u_offset[1]) {
color_ = mix( u_colorArr[0], u_colorArr[1], remap( u_offset[0], u_offset[1], alpha ) );
color_ = color_ * mix( u_colorArr[0][3], u_colorArr[1][3], remap( u_offset[0], u_offset[1], alpha ));
} else if (alpha <= u_offset[2]) {
color_ = mix( u_colorArr[1], u_colorArr[2], remap( u_offset[1], u_offset[2], alpha ) );
color_ = color_ * mix( u_colorArr[1][3], u_colorArr[2][3], remap( u_offset[1], u_offset[2], alpha ));
} else if (alpha <= u_offset[3]) {
color_ = mix( u_colorArr[2], u_colorArr[3], remap( u_offset[2], u_offset[3], alpha ) );
color_ = color_ * mix( u_colorArr[2][3], u_colorArr[3][3], remap( u_offset[2], u_offset[3], alpha ));
} else if (alpha <= u_offset[4]) {
color_ = mix( u_colorArr[3], u_colorArr[4], remap( u_offset[3], u_offset[4], alpha ) );
color_ = color_ * mix( u_colorArr[3][3], u_colorArr[4][3], remap( u_offset[3], u_offset[4], alpha ));
} else if (alpha <= u_offset[5]) {
color_ = mix( u_colorArr[4], u_colorArr[5], remap( u_offset[4], u_offset[5], alpha ) );
color_ = color_ * mix( u_colorArr[4][3], u_colorArr[5][3], remap( u_offset[4], u_offset[5], alpha ));
} else if (alpha <= u_offset[6]) {
color_ = mix( u_colorArr[5], u_colorArr[6], remap( u_offset[5], u_offset[6], alpha ) );
color_ = color_ * mix( u_colorArr[5][3], u_colorArr[6][3], remap( u_offset[5], u_offset[6], alpha ));
} else if (alpha <= u_offset[7]) {
color_ = mix( u_colorArr[6], u_colorArr[7], remap( u_offset[6], u_offset[7], alpha ) );
color_ = color_ * mix( u_colorArr[6][3], u_colorArr[7][3], remap( u_offset[6], u_offset[7], alpha ));
} else if (alpha <= u_offset[8]) {
color_ = mix( u_colorArr[7], u_colorArr[8], remap( u_offset[7], u_offset[8], alpha ) );
color_ = color_ * mix( u_colorArr[7][3], u_colorArr[8][3], remap( u_offset[7], u_offset[8], alpha ));
} else if (alpha <= u_offset[9]) {
color_ = mix( u_colorArr[8], u_colorArr[9], remap( u_offset[8], u_offset[9], alpha ) );
color_ = color_ * mix( u_colorArr[8][3], u_colorArr[9][3], remap( u_offset[8], u_offset[9], alpha ));
} else if (alpha <= u_offset[10]) {
color_ = mix( u_colorArr[9], u_colorArr[10], remap( u_offset[9], u_offset[10], alpha ) );
color_ = color_ * mix( u_colorArr[9][3], u_colorArr[10][3], remap( u_offset[9], u_offset[10], alpha ));
} else {
color_ = vec4(0.0, 0.0, 0.0, 0.0);
}
color_ = color_ * u_opacity;
if (color_.a < 0.0) {
color_.a = 0.0;
}
fragColor = color_;
} else {
fragColor = vec4(0.0, 0.0, 0.0, 0.0);
}
}`
void main() {
float alpha = texture(u_framebuffer, v_texCoord.xy).a;
if (alpha > 0.0 && alpha <= 1.0) {
vec4 color_;
if (alpha <= u_offset[0]) {
color_ = u_colorArr[0];
} else {
for (int i = 1; i <= 10; ++i) {
if (alpha <= u_offset[i]) {
color_ = mix( u_colorArr[i - 1], u_colorArr[i], remap( u_offset[i - 1], u_offset[i], alpha ) );
color_ = color_ * mix( u_colorArr[i - 1][3], u_colorArr[i][3], remap( u_offset[i - 1], u_offset[i], alpha ));
break;
}
}
}
color_ = color_ * u_opacity;
if (color_.a < 0.0) {
color_.a = 0.0;
}
fragColor = color_;
} else {
fragColor = vec4(0.0, 0.0, 0.0, 0.0);
}
}
`
};

var imageShaders = {
Expand Down
4 changes: 2 additions & 2 deletions dist/visualHeatmap.esm.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 02c2bd3

Please sign in to comment.