-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Expand file tree
/
Copy pathroam.js
More file actions
281 lines (247 loc) · 11.1 KB
/
roam.js
File metadata and controls
281 lines (247 loc) · 11.1 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* Roam Plugin
*
* This plugin allows the user to change the view freely.
*
* Copyright 2021 kdxcxs ([email protected])
* Released under the MIT license.
* ------------------------------------------------------
* author: kdxcxs
* version: 0.1.0
*/
( function( document, window ) {
"use strict";
var api = window.impress();
var gc = api.lib.gc;
// Stores the active state of impress.
// It will be set to `true` when impress inits,
// and set to `false` when `impress().tear()` is called.
var impressActive;
// Seconds per frame, used to calculate the value move or rotate each time.
var spf;
var spfNow = 0;
var spfBefore = Date.now();
// Object that stores roaming state.
var roams = {};
// Keys controls roaming.
var roamKeys = "wsadqeikjluo";
function multiplyQuaternions ( qa, qb ) {
const qaw = qa[ 0 ], qax = qa[ 1 ], qay = qa[ 2 ], qaz = qa[ 3 ];
const qbw = qb[ 0 ], qbx = qb[ 1 ], qby = qb[ 2 ], qbz = qb[ 3 ];
return [
qaw * qbw - qax * qbx - qay * qby - qaz * qbz,
qax * qbw + qaw * qbx + qay * qbz - qaz * qby,
qay * qbw + qaw * qby + qaz * qbx - qax * qbz,
qaz * qbw + qaw * qbz + qax * qby - qay * qbx
];
}
// Called every time before the browser renders the frame to change the view by moving, rotating or both of them.
function roamAnimationFrame( timestamp ) {
if ( !impressActive ) {
// Stop requesting animation frame and do nothing for the coming frame if impress is no longer active.
return;
}
// Request to be called before next time the frame renders as long as impress is active.
window.requestAnimationFrame( roamAnimationFrame );
// The value of `spfNow` is the timestamp when the browser rendered last frame,
// so `Date.now() - spfBefore` is the seconds-per-frame( or, accuratly, milliseconds ).
spfNow = Date.now();
spf = spfNow - spfBefore;
spfBefore = spfNow;
// If no key's pressed down, do nothing for the comming frame.
if ( roams.keys.length === 0 ) {
return;
}
var presentStep = document.getElementsByClassName( "step active present" )[ 0 ];
// Here is a interesting one.
//
// The `getVectorQuaternion()` function here converts a vector( which contains x, y and z coordinates )
// in the step( which might be rotated ) coordinate system to the world coordinate system
// so that could be used to move the step in the world coordinate system.
//
// For those who missed the computer graphics lessons, you may say, "But whay quaternion?".
// That's because CSS rotation uses Euler angles which is not easy to calculate,
// here converts the Euler angles to quaternion for calculation.
// So function bellow is pure math.
function getVectorQuaternion( ...dirVec ) {
// Convert direction vector to quaternion for further calculation.
var vectorQuaternion = [
0,
...dirVec
];
// Since js trigonometric functions use the radian system, here we need to convert the angle to radian.
var stepRotates = [
( parseFloat( presentStep.dataset.rotateX ) || 0 ) * ( Math.PI / 180 ),
( parseFloat( presentStep.dataset.rotateY ) || 0 ) * ( Math.PI / 180 ),
( parseFloat( presentStep.dataset.rotateZ ) || 0 ) * ( Math.PI / 180 )
];
const sin = Math.sin;
const cos = Math.cos;
const c1 = cos( stepRotates[ 0 ] / 2 );
const c2 = cos( stepRotates[ 1 ] / 2 );
const c3 = cos( stepRotates[ 2 ] / 2 );
const s1 = sin( stepRotates[ 0 ] / 2 );
const s2 = sin( stepRotates[ 1 ] / 2 );
const s3 = sin( stepRotates[ 2 ] / 2 );
// And here comes math.
var rotateQuaternion;
switch ( ( presentStep.dataset.rotateOrder || "xyz" ).toUpperCase() ) {
case "XYZ":
rotateQuaternion = [
c1 * c2 * c3 - s1 * s2 * s3,
s1 * c2 * c3 + c1 * s2 * s3,
c1 * s2 * c3 - s1 * c2 * s3,
c1 * c2 * s3 + s1 * s2 * c3
];
break;
case "YXZ":
rotateQuaternion = [
c1 * c2 * c3 + s1 * s2 * s3,
s1 * c2 * c3 + c1 * s2 * s3,
c1 * s2 * c3 - s1 * c2 * s3,
c1 * c2 * s3 - s1 * s2 * c3
];
break;
case "ZXY":
rotateQuaternion = [
c1 * c2 * c3 - s1 * s2 * s3,
s1 * c2 * c3 - c1 * s2 * s3,
c1 * s2 * c3 + s1 * c2 * s3,
c1 * c2 * s3 + s1 * s2 * c3
];
break;
case "ZYX":
rotateQuaternion = [
c1 * c2 * c3 + s1 * s2 * s3,
s1 * c2 * c3 - c1 * s2 * s3,
c1 * s2 * c3 + s1 * c2 * s3,
c1 * c2 * s3 - s1 * s2 * c3
];
break;
case "YZX":
rotateQuaternion = [
c1 * c2 * c3 - s1 * s2 * s3,
s1 * c2 * c3 + c1 * s2 * s3,
c1 * s2 * c3 + s1 * c2 * s3,
c1 * c2 * s3 - s1 * s2 * c3
];
break;
case "XZY":
rotateQuaternion = [
c1 * c2 * c3 + s1 * s2 * s3,
s1 * c2 * c3 - c1 * s2 * s3,
c1 * s2 * c3 - s1 * c2 * s3,
c1 * c2 * s3 + s1 * s2 * c3
];
break;
}
const rotateConjugate = [
rotateQuaternion[ 0 ],
rotateQuaternion[ 1 ] * -1,
rotateQuaternion[ 2 ] * -1,
rotateQuaternion[ 3 ] * -1
];
return multiplyQuaternions( multiplyQuaternions( rotateQuaternion, vectorQuaternion ), rotateConjugate );
}
// All this plugin is built on top of the `impress().goto()` function, which "allows to edit step attributes
// dynamically, such as change their coordinates, or even remove or add steps, and have that change apply
// when goto() is called."
//
// What a wise decision!
//
// So `roamMove()` function here receives a direction vector which points the direction of required movement,
// and apply changes to dataset of present step. Ready to update "canvas".
function roamMove( ...direction ) {
var vector = getVectorQuaternion( ...direction );
presentStep.dataset.x = ( parseFloat( presentStep.dataset.x ) || 0 ) + vector[ 1 ] * 500 / spf;
presentStep.dataset.y = ( parseFloat( presentStep.dataset.y ) || 0 ) + vector[ 2 ] * 500 / spf;
presentStep.dataset.z = ( parseFloat( presentStep.dataset.z ) || 0 ) + vector[ 3 ] * 500 / spf;
}
// Respond every roam-controlling key one after another.
Array.from( roams.keys ).forEach( ( key ) => {
if ( roams[ key ].start === -1 ) {
roams[ key ].start = timestamp;
}
switch ( key ) {
// Moving part.
case "w": // forward
roamMove( 0, 0, -1 );
break;
case "s": // back
roamMove( 0, -0, 1 );
break;
case "a": // left
roamMove( -1, 0, 0 );
break;
case "d": // right
roamMove( 1, 0, 0 );
break;
case "q": // up
roamMove( 0, -1, 0 );
break;
case "e": // down
roamMove( 0, 1, 0 );
break;
// Rotating part.
case "i": // pitch-up
presentStep.dataset.rotateX = ( parseFloat( presentStep.dataset.rotateX ) || 0 ) + 30 / spf;
break;
case "k": // pitch-down
presentStep.dataset.rotateX = ( parseFloat( presentStep.dataset.rotateX ) || 0 ) - 30 / spf;
break;
case "j": // roll-left
presentStep.dataset.rotateZ = ( parseFloat( presentStep.dataset.rotateZ ) || 0 ) - 30 / spf;
break;
case "l": // roll-right
presentStep.dataset.rotateZ = ( parseFloat( presentStep.dataset.rotateZ ) || 0 ) + 30 / spf;
break;
case "u": // yaw-left
presentStep.dataset.rotateY = ( parseFloat( presentStep.dataset.rotateY ) || 0 ) + 30 / spf;
break;
case "o": // yaw-right
presentStep.dataset.rotateY = ( parseFloat( presentStep.dataset.rotateY ) || 0 ) - 30 / spf;
break;
default:
break;
}
} );
// Everything's done.
// Just leave the impress to update the "canvas" and we've finished the work for one frame!
// As there no more frame between present one and the comming one, duration should be 0.
api.goto( presentStep.id, 0 );
}
function eventHandler( event ) {
if ( event.type === "keydown" && roamKeys.includes( event.key ) && !roams.keys.includes( event.key ) ) {
roams[ event.key ].start = -1;
roams.keys += event.key;
} else if ( event.type === "keyup" && roams.keys.includes( event.key ) ) {
roams.keys = roams.keys.replaceAll( event.key, "" );
}
}
document.addEventListener( "impress:init", function() {
// Update `api` and `gc` to current ones.
api = window.impress();
gc = api.lib.gc;
// Stop requesting animation frame when `impress().tear()` is called.
api.tear = new Proxy( api.tear, {
apply ( target, thisArg, argumentsList ) {
impressActive = false;
target.apply( thisArg, argumentsList );
}
} );
// Init `roams.keys` with `""`, which means no key's pressed down.
roams.keys = "";
Array.from( roamKeys ).forEach( ( key ) => {
roams[ key ] = {
start: undefined
};
} );
// Make the keys controlling.
gc.addEventListener( document, "keydown", eventHandler );
gc.addEventListener( document, "keyup", eventHandler );
impressActive = true;
// Request to be called before next frame renders.
// And every single frame after is going to be called in the `roamAnimationFrame()` function.
window.requestAnimationFrame( roamAnimationFrame );
} );
} )( document, window );