Skip to content

Instantly share code, notes, and snippets.

@enjalot
Created February 8, 2013 04:05
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 enjalot/4736524 to your computer and use it in GitHub Desktop.
Save enjalot/4736524 to your computer and use it in GitHub Desktop.
leap setup
{"description":"leap setup","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"leap.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"hud.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"thumbnail":"http://i.imgur.com/XV1rxwx.png"}
var onoffColors = ["#B40101", "#20B420"];
var svg = d3.select("svg");
var hub = svg.append("g")
.classed("hub", true);
hub.append("rect")
.classed("indicator", true)
.attr({
width: 20,
height: 20,
x: 50,
y: 50,
stroke: "#000",
"stroke-width":2
})
.on("click", tributary.leap.toggle);
var hand1 = hub.append("g")
.classed("hand", true)
.classed("one", true);
var hand2 = hub.append("g")
.classed("hand", true)
.classed("two", true);
update();
function update() {
hub.select("rect.indicator")
.style("fill", tributary.leap.connected ? onoffColors[1] : onoffColors[0]);
}
tributary.leap.events.off("open");
tributary.leap.events.on("open", function(d) {
console.log("open");
update();
})
tributary.leap.events.off("close");
tributary.leap.events.on("close", function(d) {
console.log("close");
update();
})
tributary.leap.events.off("frame");
tributary.leap.events.on("frame", function(d) {
console.log(d);
})
tributary.leap = {
events: _.clone(Backbone.Events)
};
// Support both the WebSocket and MozWebSocket objects
if ((typeof(WebSocket) == 'undefined') &&
(typeof(MozWebSocket) != 'undefined')) {
WebSocket = MozWebSocket;
}
// Create the socket with event handlers
tributary.leap.init = function() {
if(tributary.ws) {
tributary.ws.close();
}
console.log("Starting");
//Create and open the socket
tributary.ws = new WebSocket("ws://localhost:6437/");
var ws = tributary.ws;
// On successful connection
ws.onopen = function(event) {
console.log("Open");
tributary.leap.connected = true;
tributary.leap.events.trigger("open");
};
// On message received
ws.onmessage = function(event) {
tributary.leap.events.trigger("frame", JSON.parse(event.data));
};
// On socket close
ws.onclose = function(event) {
ws = null;
tributary.leap.connected = false;
tributary.leap.events.trigger("close");
}
//On socket error
ws.onerror = function(event) {
console.error("Received error");
};
}
var socketState;
if(!tributary.ws || tributary.ws.readyState != 1) {
tributary.leap.connected = false;
} else {
tributary.leap.connected = true;
}
tributary.leap.toggle = function() {
if(tributary.ws.readyState === 1) {
tributary.ws.close();
} else {
tributary.leap.init();
}
}
tributary.leap.init();
tributary.events.on("restart", function() {
tributary.leap.init();
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment