Skip to content

Commit 900d739

Browse files
authored
Merge pull request #994 from plotly/js-heatmap-webgl-doc-update
Js heatmap webgl doc update
2 parents 31aadd9 + 513c668 commit 900d739

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
name: WebGL Heatmaps
3+
plot_url: https://codepen.io/plotly/embed/rrvdXv/?height=500&theme-id=15263&default-tab=result
4+
language: plotly_js
5+
suite: heatmap-webgl
6+
order: 1
7+
sitemap: false
8+
arrangement: horizontal
9+
---
10+
function HeatmapGLfromImage() {
11+
var img = new Image();
12+
img.setAttribute(
13+
"src",
14+
processdata(
15+
"https://images.plot.ly/plotly-documentation/images/heatmap-galaxy.jpg")
16+
);
17+
}
18+
function processdata(url) {
19+
var canvas = document.getElementById("canvas");
20+
var img = new Image();
21+
img.crossOrigin = "anonymous";
22+
img.src = url;
23+
var context = canvas.getContext("2d");
24+
context.drawImage(img, 0, 0);
25+
var w = img.width; var h = img.height;
26+
var l = w * h;
27+
var arr = context.getImageData(0, 0, w, h).data;
28+
29+
var zdata = [];
30+
for (var i = 0; i < l; i++) {
31+
// get color of pixel
32+
var r = arr[i * 4]; // Red
33+
var g = arr[i * 4 + 1]; // Green
34+
var b = arr[i * 4 + 2]; // Blue
35+
var a = arr[i * 4 + 3]; // Alpha
36+
zdata.push(r + g + b + a);
37+
}
38+
var createGroupedArray = function(arr, chunkSize) {
39+
var groups = [],
40+
i;
41+
for (i = 0; i < arr.length; i += chunkSize) {
42+
groups.push(arr.slice(i, i + chunkSize));
43+
}
44+
return groups;
45+
};
46+
// Grouping zdata into 500x500
47+
var zdata = createGroupedArray(zdata, 500);
48+
49+
var data = [
50+
{
51+
z: zdata,
52+
type: "heatmapgl",
53+
colorscale: "Picnic"
54+
}
55+
];
56+
57+
Plotly.plot("myDiv", data);
58+
}
59+
60+
HeatmapGLfromImage();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
title: Javascript Heatmaps WebGL | Plotly
3+
name: WebGL Heatmaps
4+
permalink: javascript/heatmap-webgl/
5+
description: How to make webGL based heatmaps in Javascript with Plotly.
6+
layout: user-guide
7+
has_thumbnail: true
8+
thumbnail: thumbnail/heatmap-webgl.jpg
9+
language: plotly_js
10+
page_type: example_index
11+
display_as: scientific
12+
order: 3
13+
redirect_from: javascript-graphing-library/heatmap-webgl/
14+
---
15+
{% assign examples = site.posts | where:"language","plotly_js" | where:"suite","heatmap-webgl" | sort: "order" %}
16+
{% include auto_examples.html examples=examples %}

0 commit comments

Comments
 (0)