Skip to content

Commit

Permalink
升级Cesium
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjing committed Oct 16, 2024
1 parent e14675a commit 6bd8944
Show file tree
Hide file tree
Showing 11 changed files with 102 additions and 104 deletions.
83 changes: 40 additions & 43 deletions examples/widget/tooltip.html
Original file line number Diff line number Diff line change
@@ -1,49 +1,46 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<title>dc-example</title>
<script src="/libs/dc-sdk/dc.min.js"></script>
<script src="../dat.gui.min.js"></script>
<link href="/libs/dc-sdk/dc.min.css" type="text/css" rel="stylesheet" />
<link href="../index.css" type="text/css" rel="stylesheet" />
</head>

<body>
<div id="viewer-container" class="viewer-container"></div>
<script>
let viewer = undefined

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>dc-example</title>
<script src='/libs/dc-sdk/dc.min.js'></script>
<script src="../dat.gui.min.js"></script>
<link href='/libs/dc-sdk/dc.min.css' type='text/css' rel='stylesheet'>
<link href='../index.css' type='text/css' rel='stylesheet'>
</head>
function initViewer() {
viewer = new DC.Viewer('viewer-container')
let baseLayer = DC.ImageryLayerFactory.createAMapImageryLayer({
style: 'img',
crs: 'WGS84',
})
viewer.addBaseLayer(baseLayer)
viewer.tooltip.enable = true
viewer.on(DC.MouseEventType.MOUSE_MOVE, (e) => {
viewer.tooltip.showAt(e.windowPosition, '我是鼠标提示')
})
addGuiController() // add controller
}

<body>

<div id="viewer-container" class="viewer-container"></div>
<script>
let viewer = undefined

function initViewer() {
viewer = new DC.Viewer('viewer-container')
let baseLayer = DC.ImageryLayerFactory.createAMapImageryLayer({
style: 'img',
crs:'WGS84'
})
viewer.addBaseLayer( baseLayer)
viewer.tooltip.enable = true
viewer.on(DC.MouseEventType.MOUSE_MOVE, e => {
viewer.tooltip.showAt(e.windowPosition, '我是鼠标提示')
})
addGuiController() // add controller
}

function addGuiController(){
let controls = {
enable :true,
}
let gui = new dat.GUI();
gui.add(controls,'enable',).onChange(value=>{
viewer.tooltip.enable = value
})
}
DC.ready({
baseUrl:'../libs/dc-sdk/resources/'
}).then(initViewer)
</script>
</body>
function addGuiController() {
let controls = {
enable: true,
}
let gui = new dat.GUI()
gui.add(controls, 'enable').onChange((value) => {
viewer.tooltip.enable = value
})
}
DC.ready({
baseUrl: '../libs/dc-sdk/resources/',
}).then(initViewer)
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@
"dist"
],
"dependencies": {
"@dvgis/dc-common": "^3.0.0"
"@dvgis/dc-common": "^3.5.0"
}
}
13 changes: 6 additions & 7 deletions src/modules/event/type/MouseEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class MouseEvent extends Event {
// 返回转换后的坐标。
return new Cesium.Cartesian2(x, y)
}

/**
*
* Gets the mouse information for the mouse event
Expand All @@ -84,15 +84,14 @@ class MouseEvent extends Event {
*
*/
_getMouseInfo(position) {
let adjustedPosition = this._adjustPosition(position)
let scene = this._viewer.scene
let target = scene.pick(adjustedPosition)
let target = scene.pick(position)
let cartesian = undefined
let surfaceCartesian = undefined
let wgs84Position = undefined
let wgs84SurfacePosition = undefined
if (scene.pickPositionSupported) {
cartesian = scene.pickPosition(adjustedPosition)
cartesian = scene.pickPosition(position)
}
if (cartesian) {
let c = Cesium.Ellipsoid.WGS84.cartesianToCartographic(cartesian)
Expand All @@ -108,11 +107,11 @@ class MouseEvent extends Event {
scene.mode === Cesium.SceneMode.SCENE3D &&
!(this._viewer.terrainProvider instanceof Cesium.EllipsoidTerrainProvider)
) {
let ray = scene.camera.getPickRay(adjustedPosition)
let ray = scene.camera.getPickRay(position)
surfaceCartesian = scene.globe.pick(ray, scene)
} else {
surfaceCartesian = scene.camera.pickEllipsoid(
adjustedPosition,
position,
Cesium.Ellipsoid.WGS84
)
}
Expand All @@ -129,7 +128,7 @@ class MouseEvent extends Event {

return {
target: target,
windowPosition: adjustedPosition,
windowPosition: position,
position: cartesian,
wgs84Position: wgs84Position,
surfacePosition: surfaceCartesian,
Expand Down
8 changes: 4 additions & 4 deletions src/modules/layer/type/HtmlLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,22 @@ class HtmlLayer extends Layer {
this._renderRemoveCallback = scene.postRender.addEventListener(() => {
let cp = this._viewer.camera.positionWC
let cd = this._viewer.camera.direction
const offset = this._viewer.getOffset();
const offset = this._viewer.getOffset()
this.eachOverlay((item) => {
if (item && item.position) {
let position = Transform.transformWGS84ToCartesian(item.position)
let up = scene.globe.ellipsoid.geodeticSurfaceNormal(
position,
new Cesium.Cartesian3()
)
let windowCoord = Cesium.SceneTransforms.wgs84ToWindowCoordinates(
let windowCoord = Cesium.SceneTransforms.worldToWindowCoordinates(
scene,
position
)

if (windowCoord) {
windowCoord.x += offset.x;
windowCoord.y += offset.y;
windowCoord.x += offset.x
windowCoord.y += offset.y
}
item._updateStyle(
windowCoord,
Expand Down
2 changes: 1 addition & 1 deletion src/modules/transform/Transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class Transform {
*/
static transformWGS84ToWindow(position, viewer) {
let scene = viewer.scene
return Cesium.SceneTransforms.wgs84ToWindowCoordinates(
return Cesium.SceneTransforms.worldToWindowCoordinates(
scene,
this.transformWGS84ToCartesian(position)
)
Expand Down
32 changes: 18 additions & 14 deletions src/modules/viewer/Viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ class Viewer {
}
this._delegate = Cesium.Viewer
? new Cesium.Viewer(id, {
...DEF_OPTS,
...options,
})
...DEF_OPTS,
...options,
})
: new CesiumViewer(id, {
...DEF_OPTS,
...options,
}) // Initialize the viewer
...DEF_OPTS,
...options,
}) // Initialize the viewer

/**
* Registers events
Expand Down Expand Up @@ -657,20 +657,24 @@ class Viewer {
return this
}

/**
*
* @returns
*/
getOffset() {
const offset = { x: 0, y: 0 };
const container = this._delegate?.container;
const offset = { x: 0, y: 0 }
const container = this._delegate?.container
if (container) {
if (container.getBoundingClientRect) {
const rect = container.getBoundingClientRect();
offset.x = rect.left;
offset.y = rect.top;
const rect = container.getBoundingClientRect()
offset.x = rect.left
offset.y = rect.top
} else {
offset.x = container.offsetLeft;
offset.y = container.offsetTop;
offset.x = container.offsetLeft
offset.y = container.offsetTop
}
}
return offset;
return offset
}
}

Expand Down
28 changes: 16 additions & 12 deletions src/modules/widget/Widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ class Widget {
* mount content
* @private
*/
_mountContent() { }
_mountContent() {}

/**
* binds event
* @private
*/
_bindEvent() { }
_bindEvent() {}

/**
* Unbinds event
* @private
*/
_unbindEvent() { }
_unbindEvent() {}

/**
* When enable modifies the hook executed, the subclass copies it as required
Expand All @@ -70,13 +70,24 @@ class Widget {
* @param windowCoord
* @private
*/
_updateWindowCoord(windowCoord) { }
_updateWindowCoord(windowCoord) {}

/**
* Hook for installed
* @private
*/
_installHook() { }
_installHook() {}

/**
*
* @returns
*/
_getViewerOffset() {
if (!this._viewer) {
return { x: 0, y: 0 }
}
return this._viewer.getOffset()
}

/**
* Installs to viewer
Expand Down Expand Up @@ -127,13 +138,6 @@ class Widget {
`)
}

getViewerOffset() {
if (!this._viewer) {
return { x: 0, y: 0 };
}
return this._viewer.getOffset();
}

/**
* Registers type
* @param type
Expand Down
17 changes: 6 additions & 11 deletions src/modules/widget/type/ContextMenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,20 +233,15 @@ class ContextMenu extends Widget {
* @private
*/
_updateWindowCoord(windowCoord) {
let visibility = this._ulEl.hasChildNodes() ? 'visible' : 'hidden';
let { x, y } = windowCoord;

const offset = this.getViewerOffset();

x += offset.x;
y += offset.y;

let visibility = this._ulEl.hasChildNodes() ? 'visible' : 'hidden'
let { x, y } = windowCoord
let offset = this._getViewerOffset()
x += offset.x
y += offset.y
this._wrapper.style.cssText = `
visibility:${visibility};
z-index:1;
transform:translate3d(${Math.round(x)}px,${Math.round(
y
)}px, 0);
transform:translate3d(${Math.round(x)}px,${Math.round(y)}px, 0);
`
}

Expand Down
9 changes: 5 additions & 4 deletions src/modules/widget/type/Popup.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Popup extends Widget {
self._updateWindowCoord &&
self._wrapper.style.visibility === 'visible'
) {
let windowCoord = Cesium.SceneTransforms.wgs84ToWindowCoordinates(
let windowCoord = Cesium.SceneTransforms.worldToWindowCoordinates(
scene,
self._position
)
Expand Down Expand Up @@ -95,9 +95,10 @@ class Popup extends Widget {
x = windowCoord.x
y = windowCoord.y
}
const offset = this.getViewerOffset();
x += offset.x;
y += offset.y;

let offset = this._getViewerOffset()
x += offset.x
y += offset.y

this._wrapper.style.cssText = `
visibility:visible;
Expand Down
10 changes: 4 additions & 6 deletions src/modules/widget/type/Tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,10 @@ class Tooltip extends Widget {
*/
_updateWindowCoord(windowCoord) {
let x = windowCoord.x + 10
let y = windowCoord.y - this._wrapper.offsetHeight / 2;
const offset = this.getViewerOffset();

x += offset.x;
y += offset.y;

let y = windowCoord.y - this._wrapper.offsetHeight / 2
let offset = this._getViewerOffset()
x += offset.x
y += offset.y
this._wrapper.style.cssText = `
visibility:visible;
z-index:1;
Expand Down
2 changes: 1 addition & 1 deletion src/modules/wind/WindLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class WindLayer extends Layer {

this._delegate.project = (coordinate) => {
let position = Cesium.Cartesian3.fromDegrees(coordinate[0], coordinate[1])
let coord = Cesium.SceneTransforms.wgs84ToWindowCoordinates(
let coord = Cesium.SceneTransforms.worldToWindowCoordinates(
scene,
position
)
Expand Down

0 comments on commit 6bd8944

Please sign in to comment.