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';
// Defines geographic extent: CRS, min/max X, min/max Y
const extent = new Extent(
'EPSG:3857',
-20037508.342789244,
20037508.342789244,
-20037508.342789244,
20037508.342789244,
);
const dimensions = extent.dimensions();
// `viewerDiv` will contain Giro3D' rendering area (the canvas element)
const viewerDiv = document.getElementById('viewerDiv');
let instance = null;
let inspector = null;
let controls = null;
/** @type {Map} */
let map = null;
function init() {
// Creates a Giro3D instance
instance = new Instance(viewerDiv, {
crs: extent.crs(),
renderer: {
clearColor: 0x0a3b59,
},
});
// Creates a map that will contain the layer
map = new Map('planar', { extent, maxSubdivisionLevel: 10 });
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));
// Instanciates camera
instance.camera.camera3D.position.set(
(Math.random() - 0.5) * dimensions.x,
(Math.random() - 0.5) * dimensions.y,
25000000,
);
// Instanciates controls
controls = new MapControls(instance.camera.camera3D, instance.domElement);
instance.useTHREEControls(controls);
inspector = Inspector.attach(document.getElementById('panelDiv'), 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);
// we need to get the current state of the checkbox, as browsers remembers it
let intervalId;
const autoreloadCheckbox = document.getElementById('autoreload');
if (autoreloadCheckbox.checked) {
intervalId = setInterval(reload, 2000);
}
autoreloadCheckbox.addEventListener('change', e => {
if (intervalId) {
clearInterval(intervalId);
}
if (e.target.checked) {
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="viewerDiv" class="m-0 p-0 w-100 h-100"></div>
<div id="panelDiv" 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">
<input
class="form-check-input"
type="checkbox"
checked="true"
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.38.5"
},
"devDependencies": {
"vite": "^3.2.3"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}