Skip to content

Instantly share code, notes, and snippets.

@xeolabs
Created November 23, 2023 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeolabs/07f2f4f17b5f45b47069131b231a14a9 to your computer and use it in GitHub Desktop.
Save xeolabs/07f2f4f17b5f45b47069131b231a14a9 to your computer and use it in GitHub Desktop.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>xeokit Example</title>
<link href="../css/pageStyle.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/js/all.min.js"></script>
</head>
<body>
<input type="checkbox" id="info-button"/>
<label for="info-button" class="info-button"><i class="far fa-3x fa-question-circle"></i></label>
<canvas id="myCanvas"></canvas>
<div class="slideout-sidebar">
<h1>Snap-to-Edge</h1>
<h2>Hover over model to snap pointer to the nearest edge</h2>
<h3>Components Used</h3>
<ul>
<li>
<a href="../../docs/class/src/viewer/Viewer.js~Viewer.html"
target="_other">Viewer</a>
</li>
<li>
<a href="../../docs/class/src/plugins/XKTLoaderPlugin/XKTLoaderPlugin.js~XKTLoaderPlugin.html"
target="_other">XKTLoaderPlugin</a>
</li>
</ul>
</div>
</body>
<script type="module">
import {Viewer, XKTLoaderPlugin} from "../../dist/xeokit-sdk.es.js";
const viewer = new Viewer({
canvasId: "myCanvas",
dtxEnabled: true // Enable data texture model representation
});
viewer.camera.eye = [-3.93, 2.85, 27.01];
viewer.camera.look = [4.40, 3.72, 8.89];
viewer.camera.up = [-0.01, 0.99, 0.039];
const xktLoader = new XKTLoaderPlugin(viewer);
const sceneModel = xktLoader.load({
id: "myModel",
src: "../../assets/models/xkt/v10/glTF-Embedded/Duplex_A_20110505.glTFEmbedded.xkt",
edges: true
});
//------------------------------------------------------------------------------------------------------------------
// Mouse hover to snap to edge
//------------------------------------------------------------------------------------------------------------------
const markerDiv = document.createElement('div');
const canvas = viewer.scene.canvas.canvas;
canvas.parentNode.insertBefore(markerDiv, canvas);
markerDiv.style.background = "black";
markerDiv.style.border = "2px solid blue";
markerDiv.style.borderRadius = "20px";
markerDiv.style.width = "10px";
markerDiv.style.height = "10px";
markerDiv.style.margin = "-200px -200px";
markerDiv.style.zIndex = "100";
markerDiv.style.position = "absolute";
markerDiv.style.pointerEvents = "none";
var lastEntity = null;
sceneModel.on("loaded", ()=> {
viewer.scene.objects["1hOSvn6df7F8_7GcBWlS2V"].highlighted=true
})
function updateCursorPosition(canvasPos) {
const snapPickResult = viewer.scene.snapPick({
canvasPos,
snapRadius: 30,
snapToEdge: true, // Default is true
snapToVertex: false // Default is true
});
if (snapPickResult && snapPickResult.snappedCanvasPos) {
markerDiv.style.marginLeft = `${snapPickResult.snappedCanvasPos[0] - 10}px`;
markerDiv.style.marginTop = `${snapPickResult.snappedCanvasPos[1] - 10}px`;
markerDiv.style.background = "greenyellow";
markerDiv.style.border = "3px solid green";
// if (!lastEntity || (snapPickResult.snappedEntity && snapPickResult.snappedEntity.id !== lastEntity.id)) {
// if (lastEntity) {
// // lastEntity.highlighted = false;
// }
// lastEntity = snapPickResult.snappedEntity;
if (snapPickResult.snappedEntity) {
snapPickResult.snappedEntity.highlighted = true;
console.log(snapPickResult.snappedEntity.id)
}
//}
} else {
markerDiv.style.marginLeft = `${canvasPos[0] - 10}px`;
markerDiv.style.marginTop = `${canvasPos[1] - 10}px`;
markerDiv.style.background = "white";
markerDiv.style.border = "1px solid black";
if (lastEntity) {
// lastEntity.highlighted = false;
lastEntity = null;
}
}
}
function mouseUpdateCursorPosition(event) {
event.preventDefault();
updateCursorPosition([event.clientX, event.clientY]);
}
function touchUpdateCursorPosition(event) {
event.preventDefault();
const touch = event.touches[0];
updateCursorPosition([touch.clientX, touch.clientY]);
}
document.addEventListener("mousemove", mouseUpdateCursorPosition);
document.addEventListener("touchstart", touchUpdateCursorPosition);
document.addEventListener("touchmove", touchUpdateCursorPosition);
window.viewer = viewer;
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment