浏览代码

添加场景导出功能

tags/1.16.0
Caven Chen 4 年前
父节点
当前提交
9a16b5e5a0
共有 3 个文件被更改,包括 40 次插入14 次删除
  1. 1
    1
      src/core/index.js
  2. 17
    0
      src/core/utils/Util.js
  3. 22
    13
      src/core/viewer/Viewer.js

+ 1
- 1
src/core/index.js 查看文件

@@ -5,7 +5,7 @@

const install = function(DC) {
if (!DC) {
throw new Error('Missing Base SDK')
throw new Error('Missing DC Base Package')
}

DC.init(() => {

+ 17
- 0
src/core/utils/Util.js 查看文件

@@ -154,6 +154,23 @@ class Util {
}, delay)
}
}

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

export default Util

+ 22
- 13
src/core/viewer/Viewer.js 查看文件

@@ -11,7 +11,7 @@ import {
SceneEvent
} from '../event'
import { ViewerOption, CameraOption } from '../option'
import { DomUtil } from '../utils'
import { Util, DomUtil } from '../utils'
import Transform from '../transform/Transform'
import Parse from '../parse/Parse'
import createWidgets from '../widget'
@@ -490,17 +490,7 @@ class Viewer {
* @returns {Viewer}
*/
zoomToPosition(position, completeCallback) {
position = Parse.parsePosition(position)
this.camera.flyTo({
destination: Transform.transformWGS84ToCartesian(position),
orientation: {
heading: Cesium.Math.toRadians(position.heading),
pitch: Cesium.Math.toRadians(position.pitch),
roll: Cesium.Math.toRadians(position.roll)
},
complete: completeCallback,
duration: 0
})
this.flyToPosition(position, completeCallback, 0)
return this
}

@@ -552,7 +542,26 @@ class Viewer {
}

/**
* adds a plugin
* Export scene to image
* @param name
* @returns {Viewer}
*/
exportScene(name) {
let canvas = this.canvas
let image = canvas
.toDataURL('image/png')
.replace('image/png', 'image/octet-stream')
let link = document.createElement('a')
let blob = Util.dataURLtoBlob(image)
let objUrl = URL.createObjectURL(blob)
link.download = `${name || 'scene'}.png`
link.href = objUrl
link.click()
return this
}

/**
* Adds a plugin
* @param plugin
* @returns {Viewer}
*/

正在加载...
取消
保存