Skip to content

Commit ce79967

Browse files
authored
Examples: Convert animation to ES6. (#21596)
* Examples: Convert animation to ES6. * Examples: Clean up.
1 parent ceee7dd commit ce79967

File tree

8 files changed

+3561
-3541
lines changed

8 files changed

+3561
-3541
lines changed

examples/js/animation/AnimationClipCreator.js

Lines changed: 59 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,96 @@
11
( function () {
22

3-
var AnimationClipCreator = function () {};
3+
class AnimationClipCreator {
44

5-
AnimationClipCreator.CreateRotationAnimation = function ( period, axis ) {
5+
static CreateRotationAnimation( period, axis = 'x' ) {
66

7-
var times = [ 0, period ],
8-
values = [ 0, 360 ];
9-
axis = axis || 'x';
10-
var trackName = '.rotation[' + axis + ']';
11-
var track = new THREE.NumberKeyframeTrack( trackName, times, values );
12-
return new THREE.AnimationClip( null, period, [ track ] );
7+
const times = [ 0, period ],
8+
values = [ 0, 360 ];
9+
const trackName = '.rotation[' + axis + ']';
10+
const track = new THREE.NumberKeyframeTrack( trackName, times, values );
11+
return new THREE.AnimationClip( null, period, [ track ] );
1312

14-
};
13+
}
14+
15+
static CreateScaleAxisAnimation( period, axis = 'x' ) {
16+
17+
const times = [ 0, period ],
18+
values = [ 0, 1 ];
19+
const trackName = '.scale[' + axis + ']';
20+
const track = new THREE.NumberKeyframeTrack( trackName, times, values );
21+
return new THREE.AnimationClip( null, period, [ track ] );
1522

16-
AnimationClipCreator.CreateScaleAxisAnimation = function ( period, axis ) {
23+
}
1724

18-
var times = [ 0, period ],
19-
values = [ 0, 1 ];
20-
axis = axis || 'x';
21-
var trackName = '.scale[' + axis + ']';
22-
var track = new THREE.NumberKeyframeTrack( trackName, times, values );
23-
return new THREE.AnimationClip( null, period, [ track ] );
25+
static CreateShakeAnimation( duration, shakeScale ) {
2426

25-
};
27+
const times = [],
28+
values = [],
29+
tmp = new THREE.Vector3();
2630

27-
AnimationClipCreator.CreateShakeAnimation = function ( duration, shakeScale ) {
31+
for ( let i = 0; i < duration * 10; i ++ ) {
2832

29-
var times = [],
30-
values = [],
31-
tmp = new THREE.Vector3();
33+
times.push( i / 10 );
34+
tmp.set( Math.random() * 2.0 - 1.0, Math.random() * 2.0 - 1.0, Math.random() * 2.0 - 1.0 ).multiply( shakeScale ).toArray( values, values.length );
3235

33-
for ( var i = 0; i < duration * 10; i ++ ) {
36+
}
3437

35-
times.push( i / 10 );
36-
tmp.set( Math.random() * 2.0 - 1.0, Math.random() * 2.0 - 1.0, Math.random() * 2.0 - 1.0 ).multiply( shakeScale ).toArray( values, values.length );
38+
const trackName = '.position';
39+
const track = new THREE.VectorKeyframeTrack( trackName, times, values );
40+
return new THREE.AnimationClip( null, duration, [ track ] );
3741

3842
}
3943

40-
var trackName = '.position';
41-
var track = new THREE.VectorKeyframeTrack( trackName, times, values );
42-
return new THREE.AnimationClip( null, duration, [ track ] );
44+
static CreatePulsationAnimation( duration, pulseScale ) {
4345

44-
};
46+
const times = [],
47+
values = [],
48+
tmp = new THREE.Vector3();
4549

46-
AnimationClipCreator.CreatePulsationAnimation = function ( duration, pulseScale ) {
50+
for ( let i = 0; i < duration * 10; i ++ ) {
4751

48-
var times = [],
49-
values = [],
50-
tmp = new THREE.Vector3();
52+
times.push( i / 10 );
53+
const scaleFactor = Math.random() * pulseScale;
54+
tmp.set( scaleFactor, scaleFactor, scaleFactor ).toArray( values, values.length );
5155

52-
for ( var i = 0; i < duration * 10; i ++ ) {
56+
}
5357

54-
times.push( i / 10 );
55-
var scaleFactor = Math.random() * pulseScale;
56-
tmp.set( scaleFactor, scaleFactor, scaleFactor ).toArray( values, values.length );
58+
const trackName = '.scale';
59+
const track = new THREE.VectorKeyframeTrack( trackName, times, values );
60+
return new THREE.AnimationClip( null, duration, [ track ] );
5761

5862
}
5963

60-
var trackName = '.scale';
61-
var track = new THREE.VectorKeyframeTrack( trackName, times, values );
62-
return new THREE.AnimationClip( null, duration, [ track ] );
64+
static CreateVisibilityAnimation( duration ) {
6365

64-
};
66+
const times = [ 0, duration / 2, duration ],
67+
values = [ true, false, true ];
68+
const trackName = '.visible';
69+
const track = new THREE.BooleanKeyframeTrack( trackName, times, values );
70+
return new THREE.AnimationClip( null, duration, [ track ] );
6571

66-
AnimationClipCreator.CreateVisibilityAnimation = function ( duration ) {
72+
}
6773

68-
var times = [ 0, duration / 2, duration ],
69-
values = [ true, false, true ];
70-
var trackName = '.visible';
71-
var track = new THREE.BooleanKeyframeTrack( trackName, times, values );
72-
return new THREE.AnimationClip( null, duration, [ track ] );
74+
static CreateMaterialColorAnimation( duration, colors ) {
7375

74-
};
76+
const times = [],
77+
values = [],
78+
timeStep = duration / colors.length;
7579

76-
AnimationClipCreator.CreateMaterialColorAnimation = function ( duration, colors ) {
80+
for ( let i = 0; i <= colors.length; i ++ ) {
7781

78-
var times = [],
79-
values = [],
80-
timeStep = duration / colors.length;
82+
times.push( i * timeStep );
83+
values.push( colors[ i % colors.length ] );
8184

82-
for ( var i = 0; i <= colors.length; i ++ ) {
85+
}
8386

84-
times.push( i * timeStep );
85-
values.push( colors[ i % colors.length ] );
87+
const trackName = '.material[0].color';
88+
const track = new THREE.ColorKeyframeTrack( trackName, times, values );
89+
return new THREE.AnimationClip( null, duration, [ track ] );
8690

8791
}
8892

89-
var trackName = '.material[0].color';
90-
var track = new THREE.ColorKeyframeTrack( trackName, times, values );
91-
return new THREE.AnimationClip( null, duration, [ track ] );
92-
93-
};
93+
}
9494

9595
THREE.AnimationClipCreator = AnimationClipCreator;
9696

0 commit comments

Comments
 (0)