-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmouse-rotate.js
90 lines (74 loc) · 4.32 KB
/
mouse-rotate.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
84
85
86
87
88
89
90
AFRAME.registerComponent('mouse-rotate',{
schema : { speed : {default:1}},
init : function(){
//█> initialize variables for mouse Delta animation
this.xPos = 0;
this.yPos = 0;
this.dX = 0;
this.dY = 0;
//█> screen width
var width = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;
//█> screen height
var height = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;
//█> set centerpoint point to half screen w/h
this.x_cord = (width/2);
this.y_cord = (height/2);
document.addEventListener('mousemove',this.OnDocumentMouseMove.bind(this));
var counter = 0;
},
// ═════ Mouse Tick / position of mouse
tick: function () {
if (this.mouseX)
{
// TEMP_# center point (-) mouse position - distance from screen center
this.temp_x = this.x_cord-this.mouseX;
this.temp_y = this.y_cord-this.mouseY;
// distance between
this.dX = this.temp_x - this.xPos;
this.dY = this.temp_y - this.yPos;
this.xPos += (this.dX / 30);
this.yPos += (this.dY / 30);
let q = new THREE.Quaternion();
let quaternion = this.el.object3D.getWorldQuaternion(q);
let eulerOrder = 'XYZ';
var rotation = new THREE.Euler().setFromQuaternion( quaternion, eulerOrder );
var z = rotation.z;
this.el.object3D.rotateY(this.dX*this.data.speed/-10000);
this.el.object3D.rotateX(this.dY*this.data.speed/-10000);
this.el.object3D.rotateZ(-z);
// const mesh = new THREE.Mesh();
let vec = new THREE.Vector3();
var position = this.el.object3D.getWorldPosition(vec);
var z = position.z;
var y = position.y;
this.el.object3D.translateX(this.dX*this.data.speed/-5000);
this.el.object3D.translateY(this.dY*this.data.speed/5000);
// this.el.object3D.rotateZ(-z);
this.counter += 1;
// ████████████████████Mouse overlay████████
document.getElementById('msg').innerHTML = 'mouseX: '+ this.mouseX + '<br>' +
'mouseY: '+ this.mouseY + '<br>' +
'dX: ' + this.dX + '<br>' +
'dY: ' + this.dY + '<br>' +
'temp_x: ' + this.temp_x + '<br>' +
'temp_y: ' + this.temp_y + '<br>' +
'xPos: ' + this.xPos + '<br>' +
'yPos: ' + this.yPos + '<br>' +
'this counter' + this.counter + '<br>' +
'object: ' + window.objectIntersection;
var d = document.getElementById('msg');
d.style.position = "absolute";
d.style.left = this.mouseX+'px';
d.style.top = this.mouseY+'px';}},
//████████████████████████ Mouse Move >- on mouse move capture mouse x,y and save to mouseX,mouseY
OnDocumentMouseMove : function(event)
{
this.mouseX = event.clientX;
this.mouseY = event.clientY;
this.counter = 0;
}
});