Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including pointer-lock with all npm packages installed. Try it out:

var pointerLock = require("pointer-lock")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

pointer-lock v0.0.4

pointer lock polyfill that presents an eventemitter / stream api


pointer-lock api exposed as an event emitter that emits readable streams of mouse movement data. automatically handles adding appropriate mousedown/up event listeners on the targeted element. tries to reuse objects instead of creating new objects, to keep garbage generation low. may sing you songs, if you're nice enough to it.

var lock = require('pointer-lock')
  , my_element = document.getElementById('element')

  return alert('not available!')

// my_element can be any element currently attached to
// the document -- but not the document itself (document.body on down
// is fine.)
var pointer = lock(my_element)

pointer.on('attain', function(movements) {
  var initial = {x: movements.x, y: movements: y}

  // movements is a readable stream
  movements.on('data', function(move) {
    // be sure to copy the data *out of* move,
    // as the move object is reused.
    initial.x += move.dx
    initial.y += move.dy
    initial.t += move.dt

  movements.on('close', function() {
    // no more movements from this pointer-lock session.

pointer.on('release', function() {
  // pointer has been released

pointer.on('error', function() {
  // user denied pointer lock OR it's not available

pointer.on('needs-fullscreen', function() {
  // some browsers require you to be in fullscreen mode
  // for pointer lock.
  // this lets you catch that case and request it after
  // you've requested fullscreen.
  var fullscreen = require('fullscreen')
    , fs = fullscreen(my_element)

  fs.once('attain', function() {
    // manually re-request pointer lock

  // request fullscreen!

// request pointer lock: warning, may require being called from a mouse event listener

// releases the pointer lock session, if any.

// current pointer lock element, if any.

// remove the mouse event listeners added by lock




RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free