-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
194 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import { Vector2 } from 'three'; | ||
|
||
export class PointerTracker { | ||
|
||
constructor() { | ||
|
||
this.pointerOrder = []; | ||
this.pointerPositions = {}; | ||
|
||
} | ||
|
||
addPointer( e ) { | ||
|
||
const id = e.pointerId; | ||
const position = new Vector2( e.clientX, e.clientY ); | ||
this.pointerOrder.push( id ); | ||
this.pointerPositions[ id ] = position; | ||
|
||
} | ||
|
||
updatePointer( e ) { | ||
|
||
const id = e.pointerId; | ||
if ( ! ( id in this.pointerPositions ) ) { | ||
|
||
return false; | ||
|
||
} | ||
|
||
this.pointerPositions[ id ].set( e.clientX, e.clientY ); | ||
return true; | ||
|
||
} | ||
|
||
deletePointer( e ) { | ||
|
||
const id = e.pointerId; | ||
const pointerOrder = this.pointerOrder; | ||
pointerOrder.splice( pointerOrder.indexOf( id ), 1 ); | ||
delete this.pointerPositions; | ||
|
||
} | ||
|
||
getPointerCount() { | ||
|
||
return this.pointerOrder.length; | ||
|
||
} | ||
|
||
getCenterPoint( target ) { | ||
|
||
const pointerOrder = this.pointerOrder; | ||
const pointerPositions = this.pointerPositions; | ||
if ( this.getPointerCount() === 1 ) { | ||
|
||
const id = pointerOrder[ 0 ]; | ||
target.copy( pointerPositions[ id ] ); | ||
return target; | ||
|
||
} else if ( this.getPointerCount() === 2 ) { | ||
|
||
const id0 = this.pointerOrder[ 0 ]; | ||
const id1 = this.pointerOrder[ 1 ]; | ||
|
||
const p0 = this.pointerPositions[ id0 ]; | ||
const p1 = this.pointerPositions[ id1 ]; | ||
|
||
target.addVectors( p0, p1 ).multiplyScalar( 0.5 ); | ||
return target; | ||
|
||
} | ||
|
||
return null; | ||
|
||
} | ||
|
||
getPointerDistance() { | ||
|
||
if ( this.getPointerCount() <= 1 ) { | ||
|
||
return 0; | ||
|
||
} | ||
|
||
const id0 = this.pointerOrder[ 0 ]; | ||
const id1 = this.pointerOrder[ 1 ]; | ||
|
||
const p0 = this.pointerPositions[ id0 ]; | ||
const p1 = this.pointerPositions[ id1 ]; | ||
|
||
return p0.distanceTo( p1 ); | ||
|
||
} | ||
|
||
} |