Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created August 26, 2012 04:24
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 Raynos/3474040 to your computer and use it in GitHub Desktop.
Save Raynos/3474040 to your computer and use it in GitHub Desktop.
Crazy Streams
var through = require("through")
, KEYS = {
"37": "left"
, "38": "up"
, "39": "right"
, "40": "down"
}
module.exports = Input
function Input() {
var stream = through()
window.addEventListener("keydown", listenOnKeys)
return stream
function listenOnKeys(event) {
var key = KEYS[event.which]
if (key) {
stream.write(key)
}
}
}
module.exports = PositionChangeStream
function PositionChangeStream(speed) {
var id = uuid()
return through(translateInput)
function translateInput(type) {
var changes = {}
if (type === "left") {
changes.x = -speed
} else if (type === "right") {
changes.x = +speed
} else if (type === "up") {
changes.y = -speed
} else if (type === "down") {
changes.y = +speed
}
this.emit("data", [changes, Date.now(), id])
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment