1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
</head>
<body style="padding: 0; margin: 0;">
<div style="padding: 5px 10px; display: flex; align-items: center;">
<button onclick="createCone()" style="padding: 5px; margin-right: 20px;">
Add Cone
</button>
<button onclick="deleteActive()" style="padding: 5px; margin-right: 20px;">
Delete active
</button>
Resolution
<input
style="padding: 5px; margin-left: 20px;"
class="resolution"
type="range"
min="3" max="64" step="1"
/>
</div>
<div
class="appLists"
style="display: flex; flex-wrap: wrap; align-items: center;">
</div>
<script type="text/javascript" src="MultiCone.js">
</script>
<script type='text/javascript'>
// ----------------------------------------------------------------------
// Multi app management
// ----------------------------------------------------------------------
const coneApps = {};
let activeApp = '';
let nextAppID = 1;
// ----------------------------------------------------------------------
function getNextAppId() {
return `vtk-app-${nextAppID++}`;
}
// ----------------------------------------------------------------------
function activate(canvas) {
activeApp = '';
if (canvas) {
canvas.id = 'canvas';
canvas.style.outline = '2px solid #BF360C';
}
Object.values(coneApps).forEach(({ appId, el }) => {
if (canvas === el) {
activeApp = appId;
} else {
el.id = '';
el.style.outline = '2px solid black';;
}
});
const app = coneApps[activeApp];
updateSlider(app && app.api.getResolution());
}
/**
* Lock canvas size for VTK rendering unit tests. These settings make the canvas ignore
* resize events from the parent HTML element.
*/
function lockCanvasSize(runtime) {
try {
if (typeof runtime._setDefaultExpandVTKCanvasToContainer !== 'undefined') {
runtime._setDefaultExpandVTKCanvasToContainer(false);
}
if (typeof runtime._setDefaultInstallHTMLResizeObserver !== 'undefined') {
runtime._setDefaultInstallHTMLResizeObserver(false);
}
} catch (_e) {}
}
// ----------------------------------------------------------------------
function createCone() {
const appId = getNextAppId();
const el = document.createElement('canvas');
el.addEventListener("webglcontextlost", (e) => e.preventDefault(), false);
el.addEventListener('mouseenter', () => activate(el));
el.classList.add('vtk-app');
el.classList.add(appId);
el.style.width = '300';
el.style.height = '300';
el.style.margin = '30px';
document.querySelector('.appLists').appendChild(el);
var vtkWasmRuntime = null;
const context = {
appId,
canvas: el,
onRuntimeInitialized() {
lockCanvasSize(vtkWasmRuntime);
console.log('new cone init:', appId);
},
preRun: [(runtime) => {
vtkWasmRuntime = runtime;
}],
};
activate(el);
createMultiConeModule(context).then((runtime) => {
const api = {
stopInteractor: runtime.cwrap('stop','void',[]),
getResolution: runtime.cwrap('getConeResolution','int',[]),
setResolution: runtime.cwrap('setConeResolution','void',['int']),
};
coneApps[appId] = {
appId,
el,
api,
runtime,
}
});
}
// ----------------------------------------------------------------------
function deleteActive() {
const app = coneApps[activeApp];
if (app) {
delete coneApps[activeApp];
app.api.stopInteractor();
const el = document.querySelector(`.${activeApp}`);
document.querySelector('.appLists').removeChild(el);
}
}
// ----------------------------------------------------------------------
// Slider handling
// ----------------------------------------------------------------------
function onSliderChange(e) {
const app = coneApps[activeApp];
if (app) {
app.api.setResolution(Number(e.currentTarget.value));
}
}
// ----------------------------------------------------------------------
function updateSlider(value) {
if (value) {
document.querySelector('.resolution').value = Number(value);
}
}
// ----------------------------------------------------------------------
document.querySelector('.resolution').addEventListener('input', onSliderChange);
// ----------------------------------------------------------------------
</script>
</body>
</html>
|