简体   繁体   English

如何在图像上渲染heatmap.js?

[英]How to render heatmap.js on an image?

I am using heatmap.js to overlay a heatmap using the GoogleMaps plugin. 我正在使用heatmap.js使用GoogleMaps插件叠加热图。 I would like to be able to download the picture as an image along with the heatmap overlay. 我希望能够将图片与热图叠加图一起下载。 I tried the method heatmapInstance.getDataURL() only returns the overlay region and not along with the image on which it is overlayed. 我尝试了HeatmapInstance.getDataURL()方法仅返回覆盖区域,而不返回其覆盖的图像。

You need to create an id which is used inside of your div and script tags 您需要创建一个在div和script标签内部使用的id

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

As you can see I got the element by id 'heatmap'. 如您所见,我通过id'heatmap'获得了元素。 So inside of your div where you want to place the picture on the page just call the id 'heatmap' 因此,在div中您想要在页面上放置图片的地方,只需调用id“ heatmap”

<div id='heatMap'></div>

Then simply place an image under the heatmap by using css 然后只需使用CSS将图像放在热图下

#heatMap {
    background-image: url(HinckleyTown.jpg);
    height: 445px;
    width: 840px;
}

I used the same id for the css, html and javascript. 我对CSS,HTML和JavaScript使用了相同的ID。

I hit this problem recently and encountered all sorts of problems getting around it. 我最近遇到了这个问题,遇到了各种各样的问题。 The heatmap was generated with heatmap.js and displayed successfully over a background image using CSS background-image: as described above. 如上所述,热图是通过heatmap.js生成的,并使用CSS background-image在背景图像上成功显示

The problem came when trying to save both the heatmap and background as a single image on my local machine. 尝试将热图和背景都保存为本地计算机上的单个图像时出现问题。 I think this is the problem the OP was referring to. 我认为这是OP所指的问题。 My understanding is that the problem arises due to how heatmap.js creates a canvas within an element such as a div 我的理解是,问题是由于heatmap.js如何在div等元素中创建画布而产生的

A detailed search didn't come up with a solution that didn't introduce a new set of problems. 详细的搜索没有提供没有引入新问题的解决方案。 I did however come across a fairly neat solution which at least met my needs. 但是,我确实遇到了一个相当简洁的解决方案,至少可以满足我的需求。

I used Steel.Liao's post ( https://stackoverflow.com/a/36077807/9438775 ) as the basis to clone the contents of the div using html2canvas.js into a new canvas . 我使用了Steel.Liao的帖子( https://stackoverflow.com/a/36077807/9438775 )作为使用html2canvas.jsdiv的内容克隆到新画布中的基础 I then set the original div to display: none . 然后,我将原始div设置为显示:none

You can then just right-click on the new canvas to save it as a PNG image file. 然后,您可以右键单击新画布以将其另存为PNG图像文件。 Both the heatmap overlay and the background are contained in the saved image. 热图叠加层和背景都包含在已保存的图像中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM