-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjsorbit.html
More file actions
114 lines (99 loc) · 3.13 KB
/
jsorbit.html
File metadata and controls
114 lines (99 loc) · 3.13 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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Model: JS-Orbit test</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<h1>Model: JS-Orbit test</h1>
<hr />
<p>
A custom JavaScript-based demonstration of a behavior similar to the
expectation for <code>stagemode="orbit"</code> provided by the UA.
</p>
<h2>Result</h2>
<model alt="A model of a teapot">
<source src="./teapot.usdz" type="model/vnd.usdz+zip" />
<source src="./teapot.glb" type="model/gltf-binary" />
<img
src="./teapot-fallback-black.jpg"
alt="An image of a model of a teapot" />
</model>
<div id="log"></div>
</div>
<script type="module">
let startDragX, startDragY, omegaX, omegaY;
const VERTICAL_DRAG_FACTOR = 7;
const HORIZONTAL_DRAG_FACTOR = 5;
const VERTICAL_RELEASE_DECAY = 0.9;
const HORIZONTAL_RELEASE_DECAY = 0.94;
const INPUT_SMOOTH_FACTOR = 0.85;
let dx = 0;
let dy = 0;
let smoothDX = 0;
let smoothDY = 0;
let prevX = 0;
let curX = 0;
let vX = 0;
let errors = {};
let pointerDown = false;
let initialXfo;
function tick() {
requestAnimationFrame(tick);
try {
if (!pointerDown) {
dy *= VERTICAL_RELEASE_DECAY;
vX *= HORIZONTAL_RELEASE_DECAY;
dx += vX;
}
smoothDX =
(1 - INPUT_SMOOTH_FACTOR) * dx + INPUT_SMOOTH_FACTOR * smoothDX;
smoothDY =
(1 - INPUT_SMOOTH_FACTOR) * dy + INPUT_SMOOTH_FACTOR * smoothDY;
model.entityTransform = initialXfo
.rotate(smoothDY / VERTICAL_DRAG_FACTOR, 0, 0)
.rotate(0, smoothDX / HORIZONTAL_DRAG_FACTOR, 0);
} catch (err) {
errors[err.message] = errors[err.message] || 0;
errors[err.message]++;
log.innerText =
'errors:' +
Object.keys(errors)
.map((type) => type + ':' + errors[type])
.join('<br>');
}
}
const model = document.querySelector('model');
await model.ready;
initialXfo = model.entityTransform;
model.addEventListener('touchstart', (e) => {
e.preventDefault();
e.stopImmediatePropagation();
document.body.classList.add('noscroll');
});
model.addEventListener('pointerdown', (e) => {
pointerDown = true;
startDragX = e.clientX - dx;
startDragY = e.clientY;
vX = 0;
});
window.addEventListener('pointermove', (e) => {
if (pointerDown) {
dx = e.clientX - startDragX;
dy = e.clientY - startDragY;
prevX = curX;
curX = e.clientX;
}
});
window.addEventListener('pointerup', (e) => {
pointerDown = false;
vX = curX - prevX;
});
requestAnimationFrame(tick);
</script>
</body>
</html>