-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
65 lines (55 loc) · 1.72 KB
/
Copy pathscript.js
File metadata and controls
65 lines (55 loc) · 1.72 KB
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
var winH = 0, winW = 0, viewport, pers = 700, backwall;
window.onload = function(){
winH = window.innerHeight;
winW = window.innerWidth;
viewport = document.getElementById("viewport");
backwall = document.getElementById("backwall");
repositionBackface();
};
window.onresize = function(){
winH = window.innerHeight;
winW = window.innerWidth;
repositionBackface();
};
window.onmousemove = function(e){
var x = e.clientX;
var y = e.clientY;
var finalW = (+x / +winW) * 100;
var finalH = (+y / +winH) * 100;
updatePerspective(finalW,finalH);
};
window.addEventListener("touchstart", handleTouch, false);
function handleTouch(e){
e.preventDefault();
}
window.onmousewheel = function(e){
var offset = 25;
pers = pers + ((e.wheelDelta / 120) * offset);
viewport.style.perspective = pers + "px";
viewport.style.webkitPerspective = pers + "px";
};
function repositionBackface(){
var x = 0;
if(winW < winH){
x = winW;
} else {
x = winH;
}
backwall.style.transform = "translateZ(-" + (x - 1) + "px)";
backwall.style.webkitTransform = "translateZ(-" + (x - 1) + "px)";
}
function updatePerspective(originX,originY){
viewport.style.perspectiveOrigin = originX.toFixed(4) + "% " + originY.toFixed(4) + "%";
viewport.style.webkitPerspectiveOrigin = originX.toFixed(4) + "% " + originY.toFixed(4) + "%";
}
window.addEventListener("deviceorientation", handleMotion,true);
function handleMotion(e){
var x = normalizeResults(-90,90,e.gamma); // In degrees [-180,180]
var y = normalizeResults(-180,180,e.beta); // In degrees [-90,90]
var finalW = +x * 100;
var finalH = +y * 100;
updatePerspective(finalW,finalH);
}
function normalizeResults(start,end,input){
return (input-start)/(end-start);
}