blob: fe239ee69ad4916b33cf52b8d4159d9f49d4d287 (
plain)
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
|
/////////////////////////////////////////////////////////////////
//
// animation.js
//
// JavaScript for use with `3Djscript' option of \includemovie
//
// * Activates keyframe animation embedded in the u3d file.
// * Arrow keys `Down', `Up' can be used for speeding up and
// slowing down a running animation, key `Home' for reverting
// to the default speed.
//
// * Adjustable parameters:
var rate = 1; // 1 --> use original speed as default
var palindrome = true; // true --> play forth and back
//
/////////////////////////////////////////////////////////////////
//get the first animation in the scene
var myAnim = scene.animations.getByIndex(0);
myAnim.wallTime = 0;
myAnim.speed = 1;
myAnim.myLength = myAnim.endTime - myAnim.startTime;
scene.activateAnimation(myAnim);
//method to set animation speed
myAnim.setSpeed = function (speed) {
speed = Math.abs(speed);
this.wallTime /= speed/this.speed; //correct the walltime
this.speed = speed;
};
//method to change animation speed by a factor
myAnim.changeSpeed = function (mult) {
this.wallTime /= mult; //correct the walltime
this.speed *= mult;
};
//set default speed
myAnim.setSpeed(rate);
//menu items
runtime.addCustomMenuItem("faster", "Faster (Key Up)", "default", 0);
runtime.addCustomMenuItem("slower", "Slower (Key Down)", "default", 0);
runtime.addCustomMenuItem("default", "Default Speed (Key Home)", "default", 0);
//menu handler to control speed
menuEventHandler = new MenuEventHandler();
menuEventHandler.onEvent = function(e) {
if (e.menuItemName == "faster") {
myAnim.changeSpeed(1.25);
}
else if (e.menuItemName == "slower") {
myAnim.changeSpeed(1/1.25);
}
else if (e.menuItemName == "default") {
myAnim.setSpeed(rate);
}
};
runtime.addEventHandler(menuEventHandler);
//key handler to control speed
keyEventHandler = new KeyEventHandler();
keyEventHandler.onKeyDown = true;
keyEventHandler.onEvent = function(e) {
switch(e.characterCode) {
case 30: //key up
myAnim.changeSpeed(1.05);
break;
case 31: //key down
myAnim.changeSpeed(1/1.05);
break;
case 4: //key home
myAnim.setSpeed(rate);
break;
case 1: //key end
myAnim.setSpeed(rate);
break;
}
};
runtime.addEventHandler(keyEventHandler);
//run the animation using a TimeEventHandler
myTimer = new TimeEventHandler();
myTimer.onTimeChange = true;
myTimer.onEvent = function(e) {
myAnim.wallTime += e.deltaTime;
if (palindrome == true) {
myAnim.currentTime =
myAnim.startTime
+ myAnim.myLength/2
* (1 - Math.cos(Math.PI * myAnim.speed/myAnim.myLength * myAnim.wallTime));
} else {
myAnim.currentTime = myAnim.startTime
+ (myAnim.speed * myAnim.wallTime % myAnim.myLength);
}
};
runtime.addEventHandler(myTimer);
|