JS热图画布下载(显示热图的空白画布)

时间:2018-06-11 13:06:26

标签: javascript html canvas heatmap

我想下载我使用自己的setData创建的JS热图。正在显示heatMap,但是当我下载画布时,我得到一张空白图片。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Heatmap Test Code</title>
<script src='http://www.patrick- 
wied.at/static/heatmapjs/assets/js/heatmap.min.js'></script>
</head>
<body>
  <div id="heatMap" style="height:740px">
        <canvas id="myCanvas" width="1024" height="540" 
style="position:absolute; left: 0; top: 0"></canvas>
  </div>
    <a id="download" download="triangle.jpeg"><button type="button" 
onClick="download()">Download</button></a>
    <img id="myImage">

</body>
<script> 
  var heatmapInstance = h337.create({
    container: document.getElementById('heatMap')
  });

  var testData = {
        min: 0,
        max: 100,
        data: [{x: 807, y: 583, value: 500},{x: 770, y: 583, value: 500},{x: 
750, y: 583, value: 500},{x: 750, y: 583, value: 500},{x: 200, y: 583, 
value: 500},{x: 750, y: 583, value: 500}, {x: 597, y: 285, value: 51}, {x: 
217, y: 449, value: 73}, {x: 377, y: 656, value: 58}, {x: 467, y: 509, 
value: 47}, {x: 487, y: 164, value: 46}, {x: 247, y: 194, value: 35}]
  };
  heatmapInstance.setData(testData);

    function download(){
        var download = document.getElementById("download");
        var image = 
document.getElementById("myCanvas").toDataURL("image/png")
                .replace("image/png", "image/octet-stream");
          download.setAttribute("href", image);
}
</script>
</html>

3 个答案:

答案 0 :(得分:0)

您已关闭,将Href设置为图像,然后触发下载:

link.href = document.getElementById("myCanvas").toDataURL();
link.download = 'image_name.png';

https://jsfiddle.net/zx23ep0g/15/

答案 1 :(得分:0)

我明白了,H337没有使用你的画布。你正在提供容器,它正在该div中创建一个画布。

var heatmapInstance = h337.create({
  container: document.getElementById('heatMap')
});

如果您运行页面并检查热图,您会看到该div中有两个canvas元素。删除你的并按类别选择热图:

http://jsfiddle.net/79abc2sx/5/

答案 2 :(得分:0)

这是我的完全调整(上面添加了offwhite评论)和工作代码,根据一组点及其各自的值绘制A JS热图,如果有人需要工作代码,则将该地图下载为PNG。

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Heatmap Test Code</title>
<script src='http://www.patrick- 
wied.at/static/heatmapjs/assets/js/heatmap.min.js'></script>

<script>
function load(){
  renderCanvas();

  // add listener to the download button
  document.getElementById('download').addEventListener(
  'click',
  function() {download(this)},
  false
 );
}

// draw your heatmap here
function renderCanvas(){
 var heatmapInstance = h337.create({
    container: document.getElementById('heatMap')
  });

  var testData = {
        min: 0,
        max: 100,
        data: [{x: 807, y: 583, value: 500},{x: 770, y: 583, value: 500},{x: 
750, y: 583, value: 500},{x: 750, y: 583, value: 500},{x: 200, y: 583, 
value: 500},{x: 750, y: 583, value: 500}, {x: 597, y: 285, value: 51}, {x: 
217, y: 449, value: 73}, {x: 377, y: 656, value: 58}, {x: 467, y: 509, 
value: 47}, {x: 487, y: 164, value: 46}, {x: 247, y: 194, value: 35}]
  };
  heatmapInstance.setData(testData);
}

function download(link){

  link.href = 
(document.getElementById("heatMap").childNodes[1]).toDataURL();
//    console.log(document.getElementById("heatMap").childNodes[1]);
  link.download = 'image_name.png';
 }

</script>
</head>
<body onload="load()">
  <div id="heatMap" style="height:740px" >
  </div>
  <a id="download" href="#">Download as image</a>

</body>
</html>