gisthreemap
Version:
基于webGL的三维api
116 lines (110 loc) • 3.99 kB
JavaScript
/***
* 六边形扩散效果
* 参考gitee开源ts代码
* 取消import和export,整合两个类
*/
// 点效果集合 父类
class Effect {
constructor(viewer, id, duration = 1000,
maxRadius = 1000,
pointDraged =null,
leftDownFlag = false,
update_position = null) {
// todo id应随机, 还应包括dataSource
this.viewer = viewer
this.id = id
this.duration = duration
this.maxRadius = maxRadius
this.pointDraged = pointDraged
this.leftDownFlag = leftDownFlag
this.update_position = update_position
}
change_duration(d) {
this.duration = d
}
change_color(val) {
const curEntity = this.viewer.entities.getById(this.id)
curEntity._ellipse._material.color = new Cesium.Color.fromCssColorString(
val
)
}
change_position(p) {
const cartesian3 = Cesium.Cartesian3.fromDegrees(
parseFloat(p[0]),
parseFloat(p[1]),
parseFloat(p[2])
)
const curEntity = this.viewer.entities.getById(this.id)
curEntity.position = cartesian3
}
del() {
this.viewer.entities.removeById(this.id)
}
add(
position,
color,
maxRadius,
duration,
eid = `effect_${Math.random()}`,
isEdit = false
) {
const _this = this
this.duration = duration
this.maxRadius = maxRadius
this.eid = eid
if (!isEdit) {
return
}
// 实体的移动 ------ 暂不使用
function leftDownAction(e) {
_this.pointDraged = _this.viewer.scene.pick(e.position) // 选取当前的entity
if (
_this.pointDraged &&
_this.pointDraged.id &&
_this.pointDraged.id.id === _this.id
) {
_this.leftDownFlag = true
_this.viewer.scene.screenSpaceCameraController.enableRotate = false // 锁定相机
}
}
function leftUpAction(e) {
_this.leftDownFlag = false
_this.pointDraged = null
_this.viewer.scene.screenSpaceCameraController.enableRotate = true // 解锁相机
}
function mouseMoveAction(e) {
if (
_this.leftDownFlag === true &&
_this.pointDraged !== null &&
_this.pointDraged !== undefined
) {
const ray = _this.viewer.camera.getPickRay(e.endPosition)
const cartesian = _this.viewer.scene.globe.pick(ray, _this.viewer.scene)
_this.pointDraged.id.position = cartesian // 此处根据具体entity来处理,也可能是pointDraged.id.position=cartesian;
// 这里笛卡尔坐标转 经纬度
const ellipsoid = _this.viewer.scene.globe.ellipsoid
const cartographic = ellipsoid.cartesianToCartographic(cartesian)
const lat = Cesium.Math.toDegrees(cartographic.latitude)
const lng = Cesium.Math.toDegrees(cartographic.longitude)
let alt = cartographic.height
alt = alt < 0 ? 0 : alt
if (_this.update_position) {
_this.update_position([lng.toFixed(8), lat.toFixed(8), alt])
}
}
}
this.viewer.screenSpaceEventHandler.setInputAction(
leftDownAction,
Cesium.ScreenSpaceEventType.LEFT_DOWN
)
this.viewer.screenSpaceEventHandler.setInputAction(
leftUpAction,
Cesium.ScreenSpaceEventType.LEFT_UP
)
this.viewer.screenSpaceEventHandler.setInputAction(
mouseMoveAction,
Cesium.ScreenSpaceEventType.MOUSE_MOVE
)
}
}
export default Effect