|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang=en-us> |
| 3 | +<head> |
| 4 | +<meta charset=utf-8><meta content="text/html; charset=utf-8" http-equiv=Content-Type> |
| 5 | +<link rel="shortcut icon" href="lamp.ico" type="image/x-icon" /> |
| 6 | +<title>OCCT WebGL Viewer Sample</title> |
| 7 | +</head> |
| 8 | +<body> |
| 9 | + |
| 10 | +<h2>OCCT WebGL Viewer Sample</h2> |
| 11 | +<div> |
| 12 | + <canvas id=occViewerCanvas oncontextmenu=event.preventDefault() tabindex=-1 style="border:0 none;background-color:#000" width="409" height="409"></canvas> |
| 13 | + <img id=occlogo src="OCC_logo.png" style="position: absolute; left: 20px; top: 0px; z-index: 2;" /> |
| 14 | +</div> |
| 15 | + |
| 16 | +<div> |
| 17 | + <label for="fileInput">Choose BREP file to upload: </label><input type="file" id="fileInput" accept=".brep"> |
| 18 | + <input type="button" value="Clear All" onclick="OccViewerModule.removeAllObjects()"> |
| 19 | + <input type="button" value="Fit All" onclick="OccViewerModule.fitAllObjects(true)"> |
| 20 | +</div> |
| 21 | +<p>Repository: <a href="https://github.com/gkv311/occt-webgl">https://github.com/gkv311/occt-webgl</a></p> |
| 22 | +<h4>Console output:</h4> |
| 23 | +<p id="output"></p> |
| 24 | +<script> |
| 25 | +//! Resize canvas to fit into window. |
| 26 | +function updateCanvasSize() |
| 27 | +{ |
| 28 | + // size of canvas in logical (density-independent) units |
| 29 | + var aSizeX = Math.min (window.innerWidth, window.screen.availWidth); |
| 30 | + var aSizeY = Math.min (window.innerHeight, window.screen.availHeight); |
| 31 | + aSizeX = Math.max (300, aSizeX - 30); |
| 32 | + aSizeY = Math.max (300, aSizeY / 2); |
| 33 | + occViewerCanvas.style.width = aSizeX + "px"; |
| 34 | + occViewerCanvas.style.height = aSizeY + "px"; |
| 35 | + |
| 36 | + // drawing buffer size (aka backing store) |
| 37 | + var aDevicePixelRatio = window.devicePixelRatio || 1; |
| 38 | + occViewerCanvas.width = aSizeX * aDevicePixelRatio; |
| 39 | + occViewerCanvas.height = aSizeY * aDevicePixelRatio; |
| 40 | + |
| 41 | + occlogo.style.top = (aSizeY - 30) + "px"; |
| 42 | +} |
| 43 | +window.onresize = updateCanvasSize; |
| 44 | +updateCanvasSize(); |
| 45 | + |
| 46 | +//! Check browser support. |
| 47 | +function isWasmSupported() |
| 48 | +{ |
| 49 | + try { |
| 50 | + if (typeof WebAssembly === "object" |
| 51 | + && typeof WebAssembly.instantiate === "function") { |
| 52 | + const aDummyModule = new WebAssembly.Module (Uint8Array.of (0x0, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00)); |
| 53 | + if (aDummyModule instanceof WebAssembly.Module) |
| 54 | + { |
| 55 | + return new WebAssembly.Instance(aDummyModule) instanceof WebAssembly.Instance; |
| 56 | + } |
| 57 | + } |
| 58 | + } catch (e) {} |
| 59 | + return false; |
| 60 | +} |
| 61 | +if (!isWasmSupported()) |
| 62 | +{ |
| 63 | + var anElement = document.getElementById('output'); |
| 64 | + anElement.innerHTML += "Browser is too old - WebAssembly support is missing!<br>Please check updates or install a modern browser.<br>"; |
| 65 | +} |
| 66 | + |
| 67 | +//! Handle file uploading. |
| 68 | +fileInput.onchange = function() |
| 69 | +{ |
| 70 | + if (fileInput.files.length == 0) { return; } |
| 71 | + // Warning! Entire file is pre-loaded into memory. |
| 72 | + var aFile = fileInput.files[0]; |
| 73 | + var aReader = new FileReader(); |
| 74 | + aReader.onload = function() |
| 75 | + { |
| 76 | + var aDataArray = new Uint8Array (aReader.result); |
| 77 | + const aDataBuffer = OccViewerModule._malloc (aDataArray.length); |
| 78 | + OccViewerModule.HEAPU8.set (aDataArray, aDataBuffer); |
| 79 | + OccViewerModule.openFromMemory (aFile.name, aDataBuffer, aDataArray.length, true); |
| 80 | + //OccViewerModule._free (aDataBuffer); will be freed by called method |
| 81 | + OccViewerModule.displayGround (true); |
| 82 | + }; |
| 83 | + aReader.readAsArrayBuffer(aFile); |
| 84 | +}; |
| 85 | +</script> |
| 86 | +<script type="text/javascript" src="occt-webgl-sample.js" charset="utf-8"></script> |
| 87 | +</body> |
| 88 | +</html> |
0 commit comments