Giter Club home page Giter Club logo

pointer-lock's Introduction

pointer-lock

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')

if(!lock.available())
  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
    pointer.request()
  })

  // request fullscreen!
  fs.request()
})

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

// releases the pointer lock session, if any.
pointer.release()

// current pointer lock element, if any.
pointer.target()

// remove the mouse event listeners added by lock
pointer.destroy()

license

MIT

pointer-lock's People

Contributors

chrisdickinson avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.