要保存cesium場景圖片,很簡單把cesium所在的canvas保存為一張圖片并下載就行了万牺。完全不用cesium的代碼API(震驚Σ(☉▽☉"a)罗珍。
// 保存為圖片并下載
function savePic(){
var canvas =viewer.scene.canvas;
var image = canvas.toDataURL("image/png").replace("image/png", "image/octet-stream");
var link = document.createElement("a");
var strDataURI = image.substr(22, image.length);
var blob = dataURLtoBlob(image);
var objurl = URL.createObjectURL(blob);
link.download = "pic.png";
link.href = objurl;
link.click();
function dataURLtoBlob(dataurl) {
var 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});
}
};
唯一要注意的是canvas.toDataURL不能直接保存image和png這是由于保護(hù)機(jī)制,所以一定要跟replace后面的流轉(zhuǎn)換脚粟。
如果要獲取當(dāng)前場景的快照覆旱,類似于縮略圖的話
var canvas = viewer.scene.canvas;
var genimg = Canvas2Image.convertToImage(canvas, 400, 400 * canvas.height / canvas.width, 'png');
var image = document.getElementById('image');
image.src = genimg.src;
通過cesium的canvas轉(zhuǎn)換成image在創(chuàng)建一個canvas展現(xiàn)出來,并繪制到新的div上核无。
鷹眼更簡單扣唱,再次創(chuàng)建一個球,保持視角一樣就行了
let viewer2 = new Cesium.Viewer('eye',{
imageryProvider: GoogleMap,
animation: false,
baseLayerPicker: false,
fullscreenButton: false,
geocoder: false,
homeButton: false,
sceneModePicker: false,
selectionIndicator: false,
timeline: false,
navigationHelpButton: false,
infoBox: false,
navigationInstructionsInitiallyVisible: false
});
let control = viewer2.scene.screenSpaceCameraController;
control.enableRotate = false;
control.enableTranslate = false;
control.enableZoom = false;
control.enableTilt = false;
control.enableLook = false;
let syncViewer = function(){
viewer2.camera.flyTo({
destination : viewer.camera.position,
orientation : {
heading : viewer.camera.heading,
pitch : viewer.camera.pitch,
roll : viewer.camera.roll
},
duration: 0.0
});
}
viewer.entities.add({
position: Cesium.Cartesian3.fromDegrees(0, 0),
label: {
text: new Cesium.CallbackProperty(function(){
syncViewer();
return "";
}, true)
}
});