forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectionHelper.js
83 lines (51 loc) · 2.14 KB
/
SelectionHelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* @author HypnosNova / https://www.threejs.org.cn/gallery
*/
THREE.SelectionHelper = ( function () {
function SelectionHelper( selectionBox, renderer, cssClassName ) {
this.element = document.createElement( 'div' );
this.element.classList.add( cssClassName );
this.element.style.pointerEvents = 'none';
this.renderer = renderer;
this.startPoint = new THREE.Vector2();
this.pointTopLeft = new THREE.Vector2();
this.pointBottomRight = new THREE.Vector2();
this.isDown = false;
this.renderer.domElement.addEventListener( 'mousedown', function ( event ) {
this.isDown = true;
this.onSelectStart( event );
}.bind( this ), false );
this.renderer.domElement.addEventListener( 'mousemove', function ( event ) {
if ( this.isDown ) {
this.onSelectMove( event );
}
}.bind( this ), false );
this.renderer.domElement.addEventListener( 'mouseup', function ( event ) {
this.isDown = false;
this.onSelectOver( event );
}.bind( this ), false );
}
SelectionHelper.prototype.onSelectStart = function ( event ) {
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;
};
SelectionHelper.prototype.onSelectMove = function ( event ) {
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';
};
SelectionHelper.prototype.onSelectOver = function () {
this.element.parentElement.removeChild( this.element );
};
return SelectionHelper;
} )();