123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- import { Vector2 } from 'three';
- /**
- * A helper for {@link SelectionBox}.
- *
- * It visualizes the current selection box with a `div` container element.
- *
- * @three_import import { SelectionHelper } from 'three/addons/interactive/SelectionHelper.js';
- */
- class SelectionHelper {
- /**
- * Constructs a new selection helper.
- *
- * @param {(WebGPURenderer|WebGLRenderer)} renderer - The renderer.
- * @param {string} cssClassName - The CSS class name of the `div`.
- */
- constructor( renderer, cssClassName ) {
- /**
- * The visualization of the selection box.
- *
- * @type {HTMLDivElement}
- */
- this.element = document.createElement( 'div' );
- this.element.classList.add( cssClassName );
- this.element.style.pointerEvents = 'none';
- /**
- * A reference to the renderer.
- *
- * @type {(WebGPURenderer|WebGLRenderer)}
- */
- this.renderer = renderer;
- /**
- * Whether the mouse or pointer is pressed down.
- *
- * @type {boolean}
- * @default false
- */
- this.isDown = false;
- /**
- * Whether helper is enabled or not.
- *
- * @type {boolean}
- * @default true
- */
- this.enabled = true;
- // private
- this._startPoint = new Vector2();
- this._pointTopLeft = new Vector2();
- this._pointBottomRight = new Vector2();
- this._onPointerDown = function ( event ) {
- if ( this.enabled === false ) return;
- this.isDown = true;
- this._onSelectStart( event );
- }.bind( this );
- this._onPointerMove = function ( event ) {
- if ( this.enabled === false ) return;
- if ( this.isDown ) {
- this._onSelectMove( event );
- }
- }.bind( this );
- this._onPointerUp = function ( ) {
- if ( this.enabled === false ) return;
- this.isDown = false;
- this._onSelectOver();
- }.bind( this );
- this.renderer.domElement.addEventListener( 'pointerdown', this._onPointerDown );
- this.renderer.domElement.addEventListener( 'pointermove', this._onPointerMove );
- this.renderer.domElement.addEventListener( 'pointerup', this._onPointerUp );
- }
- /**
- * Call this method if you no longer want use to the controls. It frees all internal
- * resources and removes all event listeners.
- */
- dispose() {
- this.renderer.domElement.removeEventListener( 'pointerdown', this._onPointerDown );
- this.renderer.domElement.removeEventListener( 'pointermove', this._onPointerMove );
- this.renderer.domElement.removeEventListener( 'pointerup', this._onPointerUp );
- this.element.remove(); // in case disposal happens while dragging
- }
- // private
- _onSelectStart( event ) {
- this.element.style.display = 'none';
- this.renderer.domElement.parentElement.appendChild( this.element );
- this.element.style.left = event.clientX + 'px';
- this.element.style.top = event.clientY + 'px';
- this.element.style.width = '0px';
- this.element.style.height = '0px';
- this._startPoint.x = event.clientX;
- this._startPoint.y = event.clientY;
- }
- _onSelectMove( event ) {
- this.element.style.display = 'block';
- this._pointBottomRight.x = Math.max( this._startPoint.x, event.clientX );
- this._pointBottomRight.y = Math.max( this._startPoint.y, event.clientY );
- this._pointTopLeft.x = Math.min( this._startPoint.x, event.clientX );
- this._pointTopLeft.y = Math.min( this._startPoint.y, event.clientY );
- this.element.style.left = this._pointTopLeft.x + 'px';
- this.element.style.top = this._pointTopLeft.y + 'px';
- this.element.style.width = ( this._pointBottomRight.x - this._pointTopLeft.x ) + 'px';
- this.element.style.height = ( this._pointBottomRight.y - this._pointTopLeft.y ) + 'px';
- }
- _onSelectOver() {
- this.element.remove();
- }
- }
- export { SelectionHelper };
|