# cesium 截取图片

44 阅读1分钟

cesium 截取图片

没啥好说的,直接贴代码吧

	toImg() {
      if (viewer) {
        viewer.scene.render();
        let canvas = viewer.scene.canvas;
        let image = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
        let link = document.createElement("a");
        let blob = this.dataURLtoBlob(image);
        let objUrl = URL.createObjectURL(blob);
        link.download = "cesium.png";
        link.href = objUrl;
        link.click();
      }
    },

    dataURLtoBlob(dataUrl) {
      let arr = dataUrl.split(','),
        mime = arr[0].match(/:(.*?);/)[1],
        bStr = atob(arr[1]),
        n = bStr.length,
        u8arr = new Uint8Array(n);
      while (n--) {
        u8arr[n] = bStr.charCodeAt(n);
      }
      return new Blob([u8arr], { type: mime });
    }

就这样,是不是觉得很水啊,水点儿就水点儿吧。