Skip to content

Instantly share code, notes, and snippets.

@micahnyc
Last active May 12, 2022 17:20
Show Gist options
  • Save micahnyc/39f84b8d36e923a5aa03be9cc4cea1cb to your computer and use it in GitHub Desktop.
Save micahnyc/39f84b8d36e923a5aa03be9cc4cea1cb to your computer and use it in GitHub Desktop.
Openspace html/js button example
<html>
<head>
<script src="http://hub.openspaceproject.com/openspace-api.js"></script>
<title>OpenSpace javascript demo</title>
</head>
<body>
<div id="text">connecting</div>
<ul id="buttons"></ul>
<script>
//function to create buttons that send commands to openspace
function createOpenSpaceButton(buttonTitle, actionIdentifier) {
//create a new button with title
let newButton = document.createElement("button");
newButton.innerHTML = buttonTitle;
//assign click function to button
newButton.onclick = function () {
openspace.action.triggerAction(actionIdentifier);
};
//append the new button to the button container
var buttons = document.getElementById("buttons");
buttons.appendChild(newButton);
}
//create buttons
createOpenSpaceButton("Hello World", "profile.focus.earth");
createOpenSpaceButton("Toggle Zoom Friction", "os_default.toggle_zoom_friction");
createOpenSpaceButton("Goodnight", "os_default.fade_to_black");
</script>
<script>
//function to setup the openspace scripting once we are connected
//async means it will wait to connect
async function onOpenSpaceSocketConnected() {
try {
//store the scripting library for use later
openspace = await api.library();
//update the text on the webpage
document.getElementById('text').innerHTML = 'connected';
} catch (e) {
console.log('OpenSpace library could not be loaded: Error: \n', e)
return;
}
}
//create a variable to hold the openspace scripting library
var openspace = null;
//create a variable to connect the openspace socket
var api = window.openspaceApi('localhost', 4682);
//tell the socket what to do when it connects
api.onConnect(onOpenSpaceSocketConnected);
//tell the socket to connect
api.connect();
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment