Help to test if Giro3D correctly deallocates memory.
import StadiaMaps from "ol/source/StadiaMaps.js";
import { MapControls } from "three/examples/jsm/controls/MapControls.js";
import Extent from "@giro3d/giro3d/core/geographic/Extent.js";
import Instance from "@giro3d/giro3d/core/Instance.js";
import ColorLayer from "@giro3d/giro3d/core/layer/ColorLayer.js";
import Map from "@giro3d/giro3d/entities/Map.js";
import Inspector from "@giro3d/giro3d/gui/Inspector.js";
import TiledImageSource from "@giro3d/giro3d/sources/TiledImageSource.js";
function bindToggle(id, onChange) {
const element = document.getElementById(id);
if (!(element instanceof HTMLInputElement)) {
throw new Error(
"invalid binding element: expected HTMLButtonElement, got: " +
element.constructor.name,
);
}
element.oninput = function oninput() {
onChange(element.checked);
};
const callback = (v) => {
element.checked = v;
onChange(element.checked);
};
return [callback, element.checked, element];
}
const extent = new Extent(
"EPSG:3857",
-20037508.342789244,
20037508.342789244,
-20037508.342789244,
20037508.342789244,
);
const dimensions = extent.dimensions();
let instance = null;
let inspector = null;
let controls = null;
let map = null;
function init() {
instance = new Instance({
target: "view",
crs: extent.crs,
backgroundColor: 0x0a3b59,
});
map = new Map({ extent });
instance.add(map);
// Adds an TMS imagery layer
map
.addLayer(
new ColorLayer({
name: "osm",
source: new TiledImageSource({
source: new StadiaMaps({ layer: "stamen_watercolor", wrapX: false }),
}),
}),
)
.catch((e) => console.error(e));
instance.view.camera.position.set(
(Math.random() - 0.5) * dimensions.x,
(Math.random() - 0.5) * dimensions.y,
25000000,
);
controls = new MapControls(instance.view.camera, instance.domElement);
instance.view.setControls(controls);
inspector = Inspector.attach("inspector", instance);
}
init();
function reload() {
if (!instance) {
return;
}
map.getLayers().forEach((l) => l.dispose());
inspector.detach();
instance.dispose();
controls.dispose();
inspector = null;
instance = null;
controls = null;
init();
}
document.getElementById("load_once").addEventListener("click", reload);
let intervalId;
bindToggle("autoreload", (state) => {
clearInterval(intervalId);
if (state) {
intervalId = setInterval(reload, 2000);
}
});
<!doctype html>
<html lang="en">
<head>
<title>Dispose instance</title>
<meta charset="UTF-8" />
<meta name="name" content="instance_disposing" />
<meta
name="description"
content="Help to test if Giro3D correctly deallocates memory."
/>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" href="https://giro3d.org/images/favicon.svg" />
<link
href="https://giro3d.org/assets/bootstrap-custom.css"
rel="stylesheet"
/>
<script src="https://giro3d.org/assets/bootstrap.bundle.min.js"></script>
<link
rel="stylesheet"
type="text/css"
href="https://giro3d.org/latest/css/example.css"
/>
</head>
<body>
<div id="view" class="m-0 p-0 w-100 h-100"></div>
<div
id="inspector"
class="position-absolute top-0 start-0 mh-100 overflow-auto"
></div>
<div class="side-pane-with-status-bar">
<div class="card" id="buttons-container">
<div class="card-body">
<div class="form-check form-switch mb-3">
<input
class="form-check-input"
type="checkbox"
role="switch"
id="autoreload"
autocomplete="off"
/>
<label class="form-check-label" for="autoreload"
>Reload Giro3D every 2 seconds</label
>
</div>
<button id="load_once" class="btn btn-primary">
Dispose and reload Giro3D once
</button>
</div>
</div>
</div>
<script type="module" src="index.js"></script>
<script>
/* activate popovers */
const popoverTriggerList = [].slice.call(
document.querySelectorAll('[data-bs-toggle="popover"]'),
);
popoverTriggerList.map(
// bootstrap is used as script in the template, disable warning about undef
// eslint-disable-next-line no-undef
(popoverTriggerEl) =>
new bootstrap.Popover(popoverTriggerEl, {
trigger: "hover",
placement: "left",
content: document.getElementById(
popoverTriggerEl.getAttribute("data-bs-content"),
).innerHTML,
html: true,
}),
);
</script>
</body>
</html>
{
"name": "instance_disposing",
"dependencies": {
"@giro3d/giro3d": "0.39.0"
},
"devDependencies": {
"vite": "^3.2.3"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}