-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmml-document.html
145 lines (135 loc) · 4.92 KB
/
mml-document.html
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<html>
<body>
<!-- Scene -->
<m-light
type="point"
intensity="450"
x="10"
y="10"
z="10"
></m-light>
<!-- Labels -->
<m-group y="5">
<m-label
id="uptime-label"
y="2"
width="4"
height="0.5"
color="#bfdbfe"
font-color="#172554"
alignment="center"
></m-label>
<m-label
id="connected-label"
y="1"
width="4"
height="0.5"
color="#bfdbfe"
font-color="#172554"
alignment="center"
></m-label>
<m-label
id="click-label"
width="4"
height="0.5"
color="#bfdbfe"
font-color="#172554"
alignment="center"
content="Click the dice!"
></m-label>
</m-group>
<!-- Dice -->
<m-model
id="dice"
src="/assets/dice.glb"
onclick="rollDice()"
y="1"
>
<m-attr-anim id="y-up-anim" easing="easeOutSine" attr="y" duration="300" start="1" end="1" loop="false"></m-attr-anim>
<m-attr-anim id="y-down-anim" easing="easeOutBounce" attr="y" start-time="0" duration="500" start="1" end="1" loop="false"></m-attr-anim>
<m-attr-anim id="rx-anim" easing="easeInOutCubic" attr="rx" duration="500" start="0" end="0" loop="false"></m-attr-anim>
<m-attr-anim id="ry-anim" easing="easeInOutCubic" attr="ry" duration="500" start="0" end="0" loop="false"></m-attr-anim>
<m-attr-anim id="rz-anim" easing="easeInOutCubic" attr="rz" duration="500" start="0" end="0" loop="false"></m-attr-anim>
</m-model>
<script>
// Get elements
const uptimeLabelElem = document.getElementById("uptime-label");
const connectedLabelElem = document.getElementById("connected-label");
const yUpAnim = document.getElementById("y-up-anim");
const yDownAnim = document.getElementById("y-down-anim");
const rxAnim = document.getElementById("rx-anim");
const ryAnim = document.getElementById("ry-anim");
const rzAnim = document.getElementById("rz-anim");
const diceClickLabelElem = document.getElementById("click-label");
// Prepare document state
let diceResult = 1;
let connectedClientCount = 0;
let diceClickCount = 0;
let rollTime = 0;
const rollMap = {
1: [0, 0, 0],
2: [0, 0, 270],
3: [270, 0, 0],
4: [90, 0, 0],
5: [0, 0, 90],
6: [180, 0, 0],
};
function rollDice() {
diceClickCount++;
updateDiceClickCountLabel();
const t = document.timeline.currentTime + 50;
if (t < rollTime + 800) {
return;
}
rollTime = t;
const oldRotation = rollMap[diceResult];
diceResult = Math.floor(Math.random() * 6) + 1;
const targetRotation = rollMap[diceResult];
yUpAnim.setAttribute("start-time", t);
yUpAnim.setAttribute("end", 3);
yDownAnim.setAttribute("start-time", t+300);
yDownAnim.setAttribute("start", 3);
yDownAnim.setAttribute("end", 1);
rxAnim.setAttribute("start-time", t);
rxAnim.setAttribute("start", oldRotation[0].toString());
rxAnim.setAttribute("end", targetRotation[0].toString());
ryAnim.setAttribute("start-time", t);
ryAnim.setAttribute("start", oldRotation[1].toString());
ryAnim.setAttribute("end", targetRotation[1].toString());
rzAnim.setAttribute("start-time", t);
rzAnim.setAttribute("start", oldRotation[2].toString());
rzAnim.setAttribute("end", targetRotation[2].toString());
}
const updateUptimeLabel = () => {
// Get total document uptime
// NOTE: document.timeline.currentTime reports uptime in ms
const totalUptimeSeconds = Math.floor(document.timeline.currentTime / 1000);
const uptimeMinutes = Math.floor(totalUptimeSeconds / 60);
const uptimeSeconds = totalUptimeSeconds - uptimeMinutes * 60;
const uptimeLabelText =
uptimeMinutes > 0
? `${uptimeMinutes}:${String(uptimeSeconds).padStart(2, "0")}`
: `${uptimeSeconds}s`;
uptimeLabelElem.setAttribute("content", `Uptime: ${uptimeLabelText}`);
};
const updateConnectedCountLabel = () => {
connectedLabelElem.setAttribute("content", `Connected clients: ${connectedClientCount}`);
};
const updateDiceClickCountLabel = () => {
diceClickLabelElem.setAttribute("content", `Dice clicks: ${diceClickCount}`);
};
// Refresh document uptime
updateUptimeLabel();
setInterval(updateUptimeLabel, 1000);
// Client connection listeners
window.addEventListener("connected", () => {
connectedClientCount++;
updateConnectedCountLabel();
});
window.addEventListener("disconnected", () => {
connectedClientCount--;
updateConnectedCountLabel();
});
</script>
</body>
</html>