Skip to content

Commit

Permalink
refactor(#10): moved init function to main
Browse files Browse the repository at this point in the history
  • Loading branch information
kleinc committed Oct 6, 2025
1 parent fd23c74 commit cbe01f8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
3 changes: 1 addition & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
href="/libs/jstree/themes/mixed/style.css"
/>
<link rel="stylesheet" type="text/css" href="src/style.css" />
<link rel="stylesheet" type="text/css" href="src/coordinateShowing.css" />
<link rel="stylesheet" type="text/css" href="src/coordinateShowing/coordinateShowing.css" />
</head>

<body>
Expand Down Expand Up @@ -97,6 +97,5 @@
})
</script>
<script type="module" src="src/main.js"></script>
<script type="module" src="src/coordinateShowing.js"></script>
</body>
</html>
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@ const wgs84 = "+proj=longlat +datum=WGS84 +no_defs"; // EPSG:4326 (geogr
const posCanvas = document.getElementById('posCanvas'); // lat/lon
const elevationCanvas = document.getElementById('elevationCanvas');

let posCtx;
let elevationCtx;
export let posCtx;
export let elevationCtx;

/**
* Initializes the canvases and their contexts.
*/
export function initCanvases() {
posCtx = resizeCanvas(posCanvas);
elevationCtx = resizeCanvas(elevationCanvas);
}

/**
* Resizes the canvas and its context to account for device pixel ratio.
Expand Down Expand Up @@ -41,7 +49,7 @@ function drawText(ctx, text, canvas) {
/**
* Updates the lat/lon position.
*/
function updatePosition() {
export function updatePosition() {
const cam = window.viewer.scene.view.position;
const [lon, lat] = proj4(ecef, wgs84, [cam.x, cam.y, cam.z]);
drawText(posCtx, `lat = ${lat.toFixed(2)}˚ lon = ${lon.toFixed(2)}˚`, posCanvas);
Expand All @@ -50,7 +58,7 @@ function updatePosition() {
/**
* Shows target elevations if camera is in orbit mode.
*/
function updateTargetElevation() {
export function updateTargetElevation() {
const pivot = window.viewer.scene.view.getPivot();
const controls = window.viewer.getControls();
const height = proj4(ecef, wgs84, [pivot.x, pivot.y, pivot.z])[2];
Expand All @@ -64,20 +72,7 @@ function updateTargetElevation() {
}


function init() {
posCtx = resizeCanvas(posCanvas);
elevationCtx = resizeCanvas(elevationCanvas);

viewer.addEventListener("update", updatePosition);
viewer.addEventListener("update", updateTargetElevation);

window.addEventListener('resize', () => {
posCtx = resizeCanvas(posCanvas);
elevationCtx = resizeCanvas(elevationCanvas);
});
}

init();



12 changes: 12 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
import { initCanvases, updatePosition, updateTargetElevation } from "./coordinateShowing/coordinateShowing.js";

function init() {
initCanvases();

viewer.addEventListener("update", updatePosition);
viewer.addEventListener("update", updateTargetElevation);

window.addEventListener('resize', initCanvases);
}

init();

0 comments on commit cbe01f8

Please sign in to comment.