SelectionHelper.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * @author HypnosNova / https://www.threejs.org.cn/gallery
  3. */
  4. import {
  5. Vector2
  6. } from "../../../build/three.module.js";
  7. var SelectionHelper = ( function () {
  8. function SelectionHelper( selectionBox, renderer, cssClassName ) {
  9. this.element = document.createElement( 'div' );
  10. this.element.classList.add( cssClassName );
  11. this.element.style.pointerEvents = 'none';
  12. this.renderer = renderer;
  13. this.startPoint = new Vector2();
  14. this.pointTopLeft = new Vector2();
  15. this.pointBottomRight = new Vector2();
  16. this.isDown = false;
  17. this.renderer.domElement.addEventListener( 'mousedown', function ( event ) {
  18. this.isDown = true;
  19. this.onSelectStart( event );
  20. }.bind( this ), false );
  21. this.renderer.domElement.addEventListener( 'mousemove', function ( event ) {
  22. if ( this.isDown ) {
  23. this.onSelectMove( event );
  24. }
  25. }.bind( this ), false );
  26. this.renderer.domElement.addEventListener( 'mouseup', function ( event ) {
  27. this.isDown = false;
  28. this.onSelectOver( event );
  29. }.bind( this ), false );
  30. }
  31. SelectionHelper.prototype.onSelectStart = function ( event ) {
  32. this.renderer.domElement.parentElement.appendChild( this.element );
  33. this.element.style.left = event.clientX + 'px';
  34. this.element.style.top = event.clientY + 'px';
  35. this.element.style.width = '0px';
  36. this.element.style.height = '0px';
  37. this.startPoint.x = event.clientX;
  38. this.startPoint.y = event.clientY;
  39. };
  40. SelectionHelper.prototype.onSelectMove = function ( event ) {
  41. this.pointBottomRight.x = Math.max( this.startPoint.x, event.clientX );
  42. this.pointBottomRight.y = Math.max( this.startPoint.y, event.clientY );
  43. this.pointTopLeft.x = Math.min( this.startPoint.x, event.clientX );
  44. this.pointTopLeft.y = Math.min( this.startPoint.y, event.clientY );
  45. this.element.style.left = this.pointTopLeft.x + 'px';
  46. this.element.style.top = this.pointTopLeft.y + 'px';
  47. this.element.style.width = ( this.pointBottomRight.x - this.pointTopLeft.x ) + 'px';
  48. this.element.style.height = ( this.pointBottomRight.y - this.pointTopLeft.y ) + 'px';
  49. };
  50. SelectionHelper.prototype.onSelectOver = function () {
  51. this.element.parentElement.removeChild( this.element );
  52. };
  53. return SelectionHelper;
  54. } )();
  55. export { SelectionHelper };