|
| 1 | +import { Calibration } from 'reality-mixer'; |
| 2 | + |
| 3 | +const exampleCalibrationJSON = ` |
| 4 | +{ |
| 5 | + "schemaVersion": 1, |
| 6 | + "camera": { |
| 7 | + "width": 1920, |
| 8 | + "height": 1080, |
| 9 | + "fov": 38, |
| 10 | + "position": [0, 1.5, 0], |
| 11 | + "orientation": [0, 0, 0, 1] |
| 12 | + }, |
| 13 | + "chromaKey": { |
| 14 | + "color": [0, 1, 0], |
| 15 | + "similarity": 0.25, |
| 16 | + "smoothness": 0 |
| 17 | + }, |
| 18 | + "delay": 4 |
| 19 | +} |
| 20 | +` |
| 21 | + |
| 22 | +function CalibrationInput( |
| 23 | + onCompleted, |
| 24 | + initialCalibrationJSON = exampleCalibrationJSON, |
| 25 | +) { |
| 26 | + let popupDiv = document.createElement("div"); |
| 27 | + |
| 28 | + popupDiv.style = ` |
| 29 | + position: fixed; |
| 30 | + top: 50%; |
| 31 | + left: 50%; |
| 32 | + transform: translate(-50%, -50%); |
| 33 | + background-color: #303841; |
| 34 | + border-radius: 4px; |
| 35 | + font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace; |
| 36 | + color: #EFEFEF; |
| 37 | + overflow: hidden; |
| 38 | + ` |
| 39 | + |
| 40 | + let titleDiv = document.createElement("div"); |
| 41 | + |
| 42 | + titleDiv.style = "background-color: #50565E; padding: 8px"; |
| 43 | + titleDiv.innerText = "Paste or drag and drop your Mixed Reality calibration below:" |
| 44 | + |
| 45 | + let resultDiv = document.createElement("div"); |
| 46 | + |
| 47 | + resultDiv.style = "padding: 8px; white-space: pre-wrap; background-color: green;"; |
| 48 | + |
| 49 | + let editor = document.createElement("textarea"); |
| 50 | + |
| 51 | + editor.style = ` |
| 52 | + font-family: SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace; |
| 53 | + border: none; |
| 54 | + box-shadow: none; |
| 55 | + outline: none; |
| 56 | + background-color: #303841; |
| 57 | + color: #EFEFEF; |
| 58 | + resize: none; |
| 59 | + margin: 0px; |
| 60 | + ` |
| 61 | + |
| 62 | + editor.rows = 20; |
| 63 | + editor.cols = 80; |
| 64 | + editor.spellcheck = false; |
| 65 | + editor.value = initialCalibrationJSON; |
| 66 | + |
| 67 | + function validateCalibration() { |
| 68 | + const maybeJson = editor.value; |
| 69 | + |
| 70 | + try { |
| 71 | + const json = JSON.parse(maybeJson); |
| 72 | + const calibration = Calibration.fromData(json); |
| 73 | + |
| 74 | + resultDiv.style.backgroundColor = "green"; |
| 75 | + resultDiv.innerHTML = ""; |
| 76 | + |
| 77 | + let link = document.createElement("a"); |
| 78 | + link.innerText = "Click here to continue..."; |
| 79 | + link.href = "#"; |
| 80 | + link.style.color = "#EFEFEF"; |
| 81 | + link.onclick = function() { |
| 82 | + onCompleted(popupDiv, calibration); |
| 83 | + return false; |
| 84 | + } |
| 85 | + |
| 86 | + resultDiv.appendChild(link); |
| 87 | + } catch (error) { |
| 88 | + resultDiv.innerText = error; |
| 89 | + resultDiv.style.backgroundColor = "red"; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + editor.oninput = validateCalibration; |
| 94 | + validateCalibration(); |
| 95 | + |
| 96 | + popupDiv.appendChild(titleDiv); |
| 97 | + popupDiv.appendChild(editor); |
| 98 | + popupDiv.appendChild(resultDiv); |
| 99 | + |
| 100 | + popupDiv.ondrop = function(e) { |
| 101 | + e.preventDefault(); |
| 102 | + |
| 103 | + let file = e.dataTransfer.files[0], |
| 104 | + reader = new FileReader(); |
| 105 | + |
| 106 | + reader.onload = function(event) { |
| 107 | + editor.value = event.target.result; |
| 108 | + validateCalibration(); |
| 109 | + }; |
| 110 | + |
| 111 | + reader.readAsText(file); |
| 112 | + return false; |
| 113 | + } |
| 114 | + |
| 115 | + return popupDiv; |
| 116 | +} |
| 117 | + |
| 118 | +export { CalibrationInput }; |
0 commit comments