How to take screenshot in Javascript for arcgis basemap with layers

9162
20
03-16-2018 01:09 PM
VenkataDunaka
New Contributor

Hi All,

How to capture the screenshot of map( basemap with additional layers) using Javascript?

Can anyone help on this? Thanks!

0 Kudos
20 Replies
VenkataSrikanth_Dasari
Occasional Contributor

Please have a look into screenshot widget for the proxy.php in the custom widgets list. Meanwhile, I would like to frame the code properly and will upload the working screenshot widget.

Cheers,

Srikanth Dasari

0 Kudos
FlavieMORAUX1
Occasional Contributor

I try webapp of demo (ArcGIS Web Application) and with widget from GitHub (GitHub - kevinsagis/ScreenshotDemo: Screenshot and SaveAs demo)... same error like me (screenshot does not work on webapp content).

Screenshot of webmap does not work because canvas conversion throw error on each layer :

On your side does it work?

Regards,

Flavie

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Flavie,

   This is the reason why I stick with the PrintTask as you do not have the issue of certain browsers and versions not supporting the Canvas route.

0 Kudos
FlavieMORAUX1
Occasional Contributor

Hy Robert,

ArcGIS Printing does not work in my case (not WAB but developpement from scratch).

I display secure content and go through esri proxy for automatic authentification. Esri proxy is problematic with printing, it does not add token access. Result print task fails.

That's the reason why I am studying screenshot solution.

Flavie

0 Kudos
VenkataSrikanth_Dasari
Occasional Contributor

Hi,

Here is the code for taking the screenshot of the map. I think you have to use your own hosted layers to get the layers in the screenshot. 

With this code you can download the image with high resolution. If you want to decrease resolution then you have to the below statements. 

canvas.toDataURL("image/jpg", 0.5);

I can't attach the supported file here so you can download the below mentioned script files from the internet

1. html2canvas.js and html2canvas.svg.js 

2. html2proxy.php from the screenshot widget

3. filesaver also from the screenshot widget.

<!DOCTYPE html>
<html>
<head>
<title>Test screen shot</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
<link rel="stylesheet" href="https://js.arcgis.com/3.23/esri/css/esri.css">
<style>
#tools {
top: 20px;
color: #444;
height: auto;
font-family: arial;
right: 20px;
margin: 5px;
padding: 10px;
position: absolute;
width: 115px;
z-index: 40;
border: solid 2px #666;
border-radius: 4px;
background-color: #fff;
}
html, body, #mapDiv {
padding:0;
margin:0;
height:100%;
}
button {
display: block;
}
#mapDiv{
height: 100%;
}
</style>

<script type="text/javascript" src="html2canvas.js"></script>
<script type="text/javascript" src="html2canvas.svg.js"></script>
<script type="text/javascript" src="CanvasToBlob.js"></script>
<script type="text/javascript" src="fileSaver.js"></script>


<script src="https://js.arcgis.com/3.23/"></script>
<script>
var map;

require([
"esri/map", "esri/layers/ArcGISDynamicMapServiceLayer",
"dojo/dom", "dojo/on", "dojo/domReady!"
], function(
Map, ArcGISDynamicMapServiceLayer,
dom, on
) {
map = new Map("mapDiv", {
basemap: "streets",
center: [-25.312, 34.307],
zoom: 3
});
map.on("load", init);

var demographicsLayer = new ArcGISDynamicMapServiceLayer("https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer");
map.addLayer(demographicsLayer);

function init() {
on(dom.byId("screenShotTool"), "click", function(evt) {
screenShot();
});
}


function screenShot(){
// html2canvas(document.getElementById('mapDiv')).then(function(canvas){
// document.body.appendChild(canvas);
//});
html2canvas(document.getElementById("mapDiv_container"), {
"logging": true, //Enable log (use Web Console for get Errors and Warnings)
"useCORS": true,
//"allowTaint": true,
"proxy": "html2canvasproxy.php",
// "timeout": "0",
onrendered: function (canvas) {
//document.body.appendChild(canvas);
//this.screenshotDataURI = canvas.toDataURL("image/jpg", 0.5);
canvas.toBlob(function (blob) {
saveAs(blob, "Screenshot.png");
});

}
});
}
});
</script>
</head>

<body>
<div id="tools">
<button id="screenShotTool">Screen shot</button>

</div>

<div id="mapDiv"></div>

</body>
</html>

Please let me know if you have any questions.

Cheers,

Srikanth Dasari

FlavieMORAUX1
Occasional Contributor

Great, it's ok now, thanks a lot

I undesrstand why map was always empty, html2canvas need parameter "useCORS" to access to services.

Regarding parameter "proxy" I found a javascript version (GitHub - niklasvh/html2canvas-proxy-nodejs: Express middleware proxy for html2canvas)... and it works to.

Best regards,

Flavie

0 Kudos
JavierPablos1
New Contributor III

Hello there,

Is there a webapp demo to see the screenshot widget? All the links provided seem to not be working.

Regards.

0 Kudos
VenkataSrikanth_Dasari
Occasional Contributor

Hi Javier Pablos,

Please go through the above code and create a html page and follow the instructions to work and let me know if you have any issues....

Cheers,

Srikanth Dasari

0 Kudos
FabianMolina
New Contributor

Hi srikanth dasari venkata

have you tested with the 4.10 api version?  

0 Kudos
VenkataSrikanth_Dasari
Occasional Contributor

Nope.

0 Kudos