Skip to content

Commit 2e38162

Browse files
committed
add methods for enabling better animation; edit some demos to make use of them
1 parent 7ace2ca commit 2e38162

File tree

12 files changed

+149
-208
lines changed

12 files changed

+149
-208
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,14 @@ Examples
7676
scene.addShape(shape);
7777
// Set the starting rotation
7878
var rotation=[0,0,0];
79+
// Create a timer
80+
var timer={};
7981
// Set up the render loop
80-
GLUtil.renderLoop(function(){
81-
rotation[0]+=.5; // Adjust x-rotation by .5 degree
82-
rotation[1]+=1.0; // Adjust y-rotation by 1 degree
82+
GLUtil.renderLoop(function(time){
83+
// Adjust x-rotation
84+
rotation[0]=360*GLUtil.getTimePosition(timer,time,6000);
85+
// Adjust y-rotation
86+
rotation[1]=360*GLUtil.getTimePosition(timer,time,12000);
8387
// Update the shape's rotation
8488
shape.setQuaternion(GLMath.quatFromTaitBryan(rotation));
8589
// Render the scene

demos/compositeMesh.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,11 @@
7979
scene.addShape(shape1);
8080
// Set the starting rotation
8181
var rotation=[0,0,0];
82+
var timer={};
8283
// Set up the render loop
83-
GLUtil.renderLoop(function(){
84-
rotation[0]+=.5; // Adjust x-rotation by .5 degree
85-
rotation[1]+=1.0; // Adjust y-rotation by 1 degree
84+
GLUtil.renderLoop(function(time){
85+
rotation[0]=360*GLUtil.getTimePosition(timer,time,6000);
86+
rotation[1]=360*GLUtil.getTimePosition(timer,time,12000);
8687
// Update the shape's rotation
8788
var q=GLMath.quatFromTaitBryan(rotation);
8889
scene.shapes[0].setQuaternion(q);

demos/demoutil.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
window.addEventListener("load",function(){
32
var a=document.createElement("a")
43
a.setAttribute("style","margin-left:2px;margin-top:2px;margin-bottom:2px;position:absolute;left:80%;top:0;"+
@@ -21,7 +20,7 @@ window.addEventListener("load",function(){
2120
} else {
2221
viewed=true;
2322
a.innerHTML="Hide Source"
24-
e.style.display="block"
23+
e.style.display="block"
2524
}
2625
})
27-
})
26+
})

demos/gradient.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@
106106
var shape=scene.makeShape(mesh).setTexture(tex)
107107
scene.addShape(shape);
108108
var rotation=[0,0,0];
109-
GLUtil.renderLoop(function(){
109+
var timer={};
110+
GLUtil.renderLoop(function(time){
110111
fc.update();
111-
rotation[0]+=0.5;
112-
rotation[1]+=1;
112+
rotation[0]=360*GLUtil.getTimePosition(timer,time,6000);
113+
rotation[1]=360*GLUtil.getTimePosition(timer,time,12000);
113114
var q=GLMath.quatFromTaitBryan(rotation);
114115
shape.setQuaternion(q);
115116
scene.render();

demos/platonic.html

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,15 @@
131131
scene.addShape(
132132
scene.makeShape(mesh).setMaterial(shiny("yellow")).setPosition(-15,-5,0))
133133
var rotation=[0,0,0];
134-
GLUtil.renderLoop(function(){
134+
var timer={};
135+
GLUtil.renderLoop(function(time){
135136
scene.setOrthoAspect(-20,20,-10,10,-20,20);
136137
for(var i=0;i<scene.shapes.length;i++){
137138
scene.shapes[i].setQuaternion(
138139
GLMath.quatFromTaitBryan(rotation));
139140
}
140-
rotation[0]+=0.5;
141-
rotation[1]+=1;
141+
rotation[0]=360*GLUtil.getTimePosition(timer,time,6000);
142+
rotation[1]=360*GLUtil.getTimePosition(timer,time,12000);
142143
scene.render();
143144
});
144145
//-->

demos/simple.html

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,14 @@
3535
scene.addShape(shape);
3636
// Set the starting rotation
3737
var rotation=[0,0,0];
38+
// Create a timer
39+
var timer={};
3840
// Set up the render loop
39-
GLUtil.renderLoop(function(){
40-
rotation[0]+=.5; // Adjust x-rotation by .5 degree
41-
rotation[1]+=1.0; // Adjust y-rotation by 1 degree
41+
GLUtil.renderLoop(function(time){
42+
// Adjust x-rotation
43+
rotation[0]=360*GLUtil.getTimePosition(timer,time,6000);
44+
// Adjust y-rotation
45+
rotation[1]=360*GLUtil.getTimePosition(timer,time,12000);
4246
// Update the shape's rotation
4347
var q=GLMath.quatFromTaitBryan(rotation);
4448
shape.setQuaternion(q);

demos/squares.html

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -390,17 +390,16 @@
390390
linknone();
391391
link2a();
392392
var rotation=[0,0,0];
393+
var timer={};
393394
var c=[1,2,3,4,5,6,7,6,5,4,3,2]
394-
var time=0
395395
var rot=0
396-
GLUtil.renderLoop(function(){
396+
GLUtil.renderLoop(function(time){
397397
if(currentFilter){
398398
currentFilter.setUniforms({
399-
"coarseness":c[(Math.floor(time/5))%c.length],
400-
"time":Math.floor(time/3)
399+
"coarseness":c[Math.floor(c.length*GLUtil.getTimePosition(timer,time,1500))],
400+
"time":100*GLUtil.getTimePosition(timer,time,5000)
401401
})
402402
}
403-
time+=1
404403
for(var i=0;i<blinds.shapes.length;i++){
405404
blinds.shapes[i].setQuaternion(
406405
GLMath.quatFromAxisAngle(rot,0,1,0));
@@ -409,10 +408,9 @@
409408
platonic.shapes[i].setQuaternion(
410409
GLMath.quatFromTaitBryan(rotation));
411410
}
412-
rot+=5
413-
rot%=360
414-
rotation[0]+=0.5;
415-
rotation[1]+=1;
411+
rot=360*GLUtil.getTimePosition(timer,time,2000);
412+
rotation[0]=360*GLUtil.getTimePosition(timer,time,6000);
413+
rotation[1]=360*GLUtil.getTimePosition(timer,time,12000);
416414
scene.render();
417415
fc.update();
418416
});

demos/starfield.html

Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -59,88 +59,17 @@
5959
}
6060
}
6161

62-
63-
/**
64-
* Stores time values to help enable frame-rate independent
65-
* animation.
66-
*/
67-
var Timer=function(){
68-
this.time=null;
69-
this.lastTime=null;
70-
}
71-
/**
72-
* Gets the position of a time value within an interval.
73-
* This is useful in animation cycles lasting a certain number
74-
* of seconds, such as rotating a shape in a 5-second cycle.
75-
* @param {number} timeInMs A time value, in milliseconds.
76-
* This could be the parameter received in a
77-
* <code>requestAnimationFrame()</code> callback method.
78-
* </code>.
79-
* @param {number} interval The length of the interval
80-
* (animation cycle), in milliseconds.
81-
* @return {number} A value in the range [0, 1), where closer
82-
* to 0 means closer to the start, and closer to 1 means closer
83-
* to the end of the interval. If an initial time wasn't set
84-
* by this timer, returns 0.
85-
* @example <caption>The following code sets an angle of
86-
* rotation, in degrees, such that an object rotated with the
87-
* angle does a 360-degree turn in 5 seconds (5000 milliseconds).
88-
* The variable <code>time</code> is assumed to be a time
89-
* value in milliseconds, such as the parameter of a
90-
* <code>requestAnimationFrame()</code> callback method.
91-
* </code>
92-
* var angle = 360 * timer.getTimePosition(time, 5000);
93-
*/
94-
Timer.prototype.getTimePosition=function(timeInMs,intervalInMs){
95-
if(this.time==null) {
96-
this.time=timeInMs;
97-
this.lastTime=timeInMs;
98-
return 0;
99-
} else {
100-
if(this.lastTime==null)this.lastTime=timeInMs;
101-
return ((timeInMs-this.time)%intervalInMs)/intervalInMs;
102-
}
103-
}
104-
/**
105-
* Returns the number of frames that occurred since
106-
* last time, where a frame is 1/60 of a second.
107-
* @param {number} timeInMs A time value, in milliseconds.
108-
* This could be the parameter received in a
109-
* <code>requestAnimationFrame()</code> callback method.
110-
* </code>.
111-
* @return {number} The number of frames relative to
112-
* the last time value passed to the newFrames() or
113-
* getTimePosition() method. The number can include
114-
* fractional frames. If an
115-
* initial time or last time wasn't set by this timer, returns 0.
116-
*/
117-
Timer.prototype.newFrames=function(timeInMs){
118-
if(this.time==null) {
119-
this.time=timeInMs;
120-
this.lastTime=timeInMs;
121-
return 0;
122-
} else if(this.lastTime==null){
123-
this.lastTime=timeInMs;
124-
return 0;
125-
} else {
126-
var diff=(timeInMs-this.lastTime);
127-
this.lastTime=timeInMs;
128-
return diff*60.0/1000.0;
129-
}
130-
}
131-
13262
// Create the 3D scene; find the HTML canvas and pass it
13363
// to Scene3D.
13464
var scene=new Scene3D(document.getElementById("canvas"))
13565
.cullFace(Scene3D.BACK);
13666
var fc=new FrameCounterDiv(scene)
137-
var timer=new Timer();
67+
var timer={};
13868
scene.setPerspective(45,scene.getClientAspect(),5,1000).setLookAt([0,0,500]);
13969
var group=starField(scene,1000)
14070
scene.addShape(group);
141-
timer.newFrames();
14271
GLUtil.renderLoop(function(time){
143-
moveStarField(group,1000,timer.newFrames(time))
72+
moveStarField(group,1000,GLUtil.newFrames(timer,time))
14473
fc.update()
14574
scene.render();
14675
})

demos/textured.html

Lines changed: 3 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -22,93 +22,23 @@
2222
at: http://upokecenter.dreamhosters.com/articles/donate-now-2/
2323
*/
2424

25-
26-
/**
27-
* Stores time values to help enable frame-rate independent
28-
* animation.
29-
*/
30-
var Timer=function(){
31-
this.time=null;
32-
this.lastTime=null;
33-
}
34-
/**
35-
* Gets the position of a time value within an interval.
36-
* This is useful in animation cycles lasting a certain number
37-
* of seconds, such as rotating a shape in a 5-second cycle.
38-
* @param {number} timeInMs A time value, in milliseconds.
39-
* This could be the parameter received in a
40-
* <code>requestAnimationFrame()</code> callback method.
41-
* </code>.
42-
* @param {number} interval The length of the interval
43-
* (animation cycle), in milliseconds.
44-
* @return {number} A value in the range [0, 1), where closer
45-
* to 0 means closer to the start, and closer to 1 means closer
46-
* to the end of the interval. If an initial time wasn't set
47-
* by this timer, returns 0.
48-
* @example <caption>The following code sets an angle of
49-
* rotation, in degrees, such that an object rotated with the
50-
* angle does a 360-degree turn in 5 seconds (5000 milliseconds).
51-
* The variable <code>time</code> is assumed to be a time
52-
* value in milliseconds, such as the parameter of a
53-
* <code>requestAnimationFrame()</code> callback method.
54-
* </code>
55-
* var angle = 360 * timer.getTimePosition(time, 5000);
56-
*/
57-
Timer.prototype.getTimePosition=function(timeInMs,intervalInMs){
58-
if(this.time==null) {
59-
this.time=timeInMs;
60-
this.lastTime=timeInMs;
61-
return 0;
62-
} else {
63-
if(this.lastTime==null)this.lastTime=timeInMs;
64-
return ((timeInMs-this.time)%intervalInMs)/intervalInMs;
65-
}
66-
}
67-
/**
68-
* Returns the number of frames that occurred since
69-
* last time, where a frame is 1/60 of a second.
70-
* @param {number} timeInMs A time value, in milliseconds.
71-
* This could be the parameter received in a
72-
* <code>requestAnimationFrame()</code> callback method.
73-
* </code>.
74-
* @return {number} The number of frames relative to
75-
* the last time value passed to the newFrames() or
76-
* getTimePosition() method. The number can include
77-
* fractional frames. If an
78-
* initial time or last time wasn't set by this timer, returns 0.
79-
*/
80-
Timer.prototype.newFrames=function(timeInMs){
81-
if(this.time==null) {
82-
this.time=timeInMs;
83-
this.lastTime=timeInMs;
84-
return 0;
85-
} else if(this.lastTime==null){
86-
this.lastTime=timeInMs;
87-
return 0;
88-
} else {
89-
var diff=(timeInMs-this.lastTime);
90-
this.lastTime=timeInMs;
91-
return diff*60.0/1000.0;
92-
}
93-
}
94-
9525
// Create the 3D scene; find the HTML canvas and pass it
9626
// to Scene3D.
9727
var scene=new Scene3D(document.getElementById("canvas"))
9828
.cullFace(Scene3D.BACK);
9929
var fc=new FrameCounterDiv(scene)
10030
scene.setPerspective(45,scene.getClientAspect(),0.1,100);
10131
scene.setLookAt([0,0,40]);
102-
var timer=new Timer()
32+
var timer={}
10333
scene.loadAndMapTexture("461223191.jpg").then(function(tex){
10434
var mesh=Meshes.createBox(10,20,20);
10535
var shape=scene.makeShape(mesh).setTexture(tex)
10636
scene.addShape(shape);
10737
var rotation=[0,0,0];
10838
GLUtil.renderLoop(function(time){
10939
fc.update();
110-
rotation[0]=360*timer.getTimePosition(time,12000);
111-
rotation[1]=360*timer.getTimePosition(time,6000);
40+
rotation[0]=360*GLUtil.getTimePosition(timer,time,12000);
41+
rotation[1]=360*GLUtil.getTimePosition(timer,time,6000);
11242
var q=GLMath.quatFromTaitBryan(rotation);
11343
shape.setQuaternion(q);
11444
scene.render();

demos/tris.html

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
this.group=new ShapeGroup();
2626
this.width=scene.getWidth();
2727
this.scene=scene;
28+
this.timer={};
2829
this.height=scene.getHeight();
2930
for(var i=0;i<count;i++){
3031
var tri=TriangleParticles.generateTri(this.width,this.height)
@@ -34,18 +35,19 @@
3435
}
3536
this.scene.addShape(this.group);
3637
}
37-
TriangleParticles.prototype.update=function(){
38+
TriangleParticles.prototype.update=function(time){
39+
var frames=GLUtil.newFrames(this.timer,time);
3840
for(var i=0;i<this.tris.length;i++){
39-
this.tris[i].life--;
41+
this.tris[i].life-=frames;
4042
if(this.tris[i].life<=0){
4143
var tri=TriangleParticles.generateTri(this.width,this.height)
4244
this.tris[i]=tri;
4345
this.group.shapes[i]=
4446
TriangleParticles.makeShapeFromTri(tri,this.scene);
4547
} else {
4648
var trans=this.group.shapes[i].getTransform()
47-
trans.movePosition(this.tris[i].movement)
48-
trans.multOrientation(this.tris[i].rotation,0,0,1)
49+
trans.movePosition(GLMath.vec3scale(this.tris[i].movement,frames))
50+
trans.multOrientation(this.tris[i].rotation*frames,0,0,1)
4951
}
5052
}
5153
}
@@ -114,8 +116,8 @@
114116
scene.setOrtho(0,scene.getWidth(),scene.getHeight(),0,-2,2);
115117
var tris=new TriangleParticles(scene,200);
116118
// Set up the render loop
117-
GLUtil.renderLoop(function(){
118-
tris.update();
119+
GLUtil.renderLoop(function(time){
120+
tris.update(time);
119121
// Render the scene
120122
scene.render();
121123
});

0 commit comments

Comments
 (0)