<html>
  <head>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="initial-scale=1,maximum-scale=1,user-scalable=no"
    />
    <!--
  ArcGIS API for JavaScript, https://js.arcgis.com
  For more information about the intro-mapview sample, read the original sample description at developers.arcgis.com.
  https://developers.arcgis.com/javascript/latest/sample-code/intro-mapview/index.html
  -->
<title>Intro to MapView - Create a 2D map - 4.15</title>
    <style>
      html,
      body,
      #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
      }
    </style>

    <link
      rel="stylesheet"
      href="https://js.arcgis.com/4.15/esri/themes/light/main.css"
    />
    <script src="https://js.arcgis.com/4.15/"></script>

    <script>
      require(["esri/Map", "esri/views/MapView"], function(Map, MapView) {
        var map = new Map({
          basemap: "streets"
        });

        var view = new MapView({
          container: "viewDiv",
          map: map,
          zoom: 4,
          center: [15, 65] // longitude, latitude
        });


      view.when(function() {
        view.goTo({
          center:[-86, 35]
        }).then(function(){
          //build in 3 second delay to allow zoom to finish
        setTimeout(function() {
        //take screenshot
        view.takeScreenshot().then(function(screenshot){

          //build canvas
          const imageData=screenshot.data;
          const canvas =document.createElement("canvas");
          const context = canvas.getContext("2d");
          canvas.height = imageData.height;
          canvas.width = imageData.width;

          //add screenshot to canvas
          context.putImageData(imageData,0,0);
          context.font = "12px Arial";
          context.fillStyle="#fff";
          context.fillRect(canvas.width-300,0,300,80);
          context.fillStyle = "#000";
          context.fillText("Test", imageData.width-280,15);

          const dataUrl = canvas.toDataURL();

          //download image
          if (!window.navigator.msSaveOrOpenBlob) {
          // in browsers that support the download attribute
          // a link is created and a programmatic click will trigger the download
          const element = document.createElement("a");
          element.setAttribute("href", dataUrl);
          element.setAttribute("download", filename);
          element.style.display = "none";
          document.body.appendChild(element);
          element.click();
          document.body.removeChild(element);
          } else {
          // for MS browsers convert dataUrl to Blob
          const byteString = atob(dataUrl.split(",")[1]);
          const mimeString = dataUrl
            .split(",")[0]
            .split(":")[1]
            .split(";")[0];
          const ab = new ArrayBuffer(byteString.length);
          const ia = new Uint8Array(ab);
          for (let i = 0; i < byteString.length; i++) {
            ia[i] = byteString.charCodeAt(i);
          }
          const blob = new Blob([ab], { type: mimeString });

          //set file name
          //assumes event time is in ISO format (eg YYYY-MM-DDTHH:MM:SSZ);

          var filename = "test.png";

          //download file
          //console.log("filename: " + filename);
          window.navigator.msSaveOrOpenBlob(blob, filename);
          }
        });

        },5000);


      });

    });
          });

    </script>
  </head>

  <body>
    <div id="viewDiv"></div>
  </body>
</html>