reactPointLock
The Pointer Lock API (formerly called Mouse Lock API) provides input methods based on the movement of the mouse over time (i.e., deltas), not just the absolute position of the mouse cursor in the viewport. It gives you access to raw mouse movement, locks the target of mouse events to a single element, eliminates limits on how far mouse movement can go in a single direction, and removes the cursor from view. It is ideal for first person 3D games, for example.
example code using MDN Pointer Lock API
installation
npm install reactPointLock
Component API
onMouseMove(movement, event)
- movement.x : movement x point
- movement.y : movement y point
- event : mousemove event
onPointLock()
- when activate pointerlock
onExitPointLock()
- when user exit pointerlock ex) pressing ESC key, etc.
Example
;;; ; { super; thisstate = x: 50 y: 50 ; thisonMouseMove = thisonMouseMove; } { this; } { this; } { const canvas = ReactDOM; let x = thisstatex + movementx; let y = thisstatey + movementy; if x > canvasclientWidth + 20 x = 0; if y > canvasclientHeight + 20 y = 0; if x < -15 x = canvasclientWidth; if y < -15 y = canvasclientHeight; this; } { const canvas = ReactDOM; const context = canvas; contextfillStyle = 'black'; context; contextfillStyle = '#F9F903'; context; context; context; } { return <div className="app"> <h1>Pointer lock demo</h1> <p>This demo demonstrates usage of the pointer lock API Click on the canvas area and your mouse will directly control the ball inside the canvas not your mouse pointer You can press escape to return to the standard expected state</p> <h2>X Position: thisstatex </h2> <h2>Y Position: thisstatey </h2> <PointerLock onMouseMove= thisonMouseMove > <canvas width="640px" height="369px" ref="canvas"/> </PointerLock> </div> ; } ReactDOM;
Contribute
npm installnpm start
npm build
TODO
- test
- css build using babel.js
- minifly