SelectionHelper.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. console.warn( "THREE.SelectionHelper: As part of the transition to ES6 Modules, the files in 'examples/js' have been deprecated in r117 (May 2020) and will be deleted in r124 (December 2020). You can find more information about developing using ES6 Modules in https://threejs.org/docs/index.html#manual/en/introduction/Import-via-modules." );
  2. /**
  3. * @author HypnosNova / https://www.threejs.org.cn/gallery
  4. */
  5. THREE.SelectionHelper = ( function () {
  6. function SelectionHelper( selectionBox, renderer, cssClassName ) {
  7. this.element = document.createElement( 'div' );
  8. this.element.classList.add( cssClassName );
  9. this.element.style.pointerEvents = 'none';
  10. this.renderer = renderer;
  11. this.startPoint = new THREE.Vector2();
  12. this.pointTopLeft = new THREE.Vector2();
  13. this.pointBottomRight = new THREE.Vector2();
  14. this.isDown = false;
  15. this.renderer.domElement.addEventListener( 'mousedown', function ( event ) {
  16. this.isDown = true;
  17. this.onSelectStart( event );
  18. }.bind( this ), false );
  19. this.renderer.domElement.addEventListener( 'mousemove', function ( event ) {
  20. if ( this.isDown ) {
  21. this.onSelectMove( event );
  22. }
  23. }.bind( this ), false );
  24. this.renderer.domElement.addEventListener( 'mouseup', function ( event ) {
  25. this.isDown = false;
  26. this.onSelectOver( event );
  27. }.bind( this ), false );
  28. }
  29. SelectionHelper.prototype.onSelectStart = function ( event ) {
  30. this.renderer.domElement.parentElement.appendChild( this.element );
  31. this.element.style.left = event.clientX + 'px';
  32. this.element.style.top = event.clientY + 'px';
  33. this.element.style.width = '0px';
  34. this.element.style.height = '0px';
  35. this.startPoint.x = event.clientX;
  36. this.startPoint.y = event.clientY;
  37. };
  38. SelectionHelper.prototype.onSelectMove = function ( event ) {
  39. this.pointBottomRight.x = Math.max( this.startPoint.x, event.clientX );
  40. this.pointBottomRight.y = Math.max( this.startPoint.y, event.clientY );
  41. this.pointTopLeft.x = Math.min( this.startPoint.x, event.clientX );
  42. this.pointTopLeft.y = Math.min( this.startPoint.y, event.clientY );
  43. this.element.style.left = this.pointTopLeft.x + 'px';
  44. this.element.style.top = this.pointTopLeft.y + 'px';
  45. this.element.style.width = ( this.pointBottomRight.x - this.pointTopLeft.x ) + 'px';
  46. this.element.style.height = ( this.pointBottomRight.y - this.pointTopLeft.y ) + 'px';
  47. };
  48. SelectionHelper.prototype.onSelectOver = function () {
  49. this.element.parentElement.removeChild( this.element );
  50. };
  51. return SelectionHelper;
  52. } )();