Clock.js 1.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/**
 * @author alteredq / http://alteredqualia.com/
 */

THREE.Clock = function ( autoStart ) {

	this.autoStart = ( autoStart !== undefined ) ? autoStart : true;

	this.startTime = 0;
	this.oldTime = 0;
	this.elapsedTime = 0;

	this.running = false;

};

17
THREE.extend( THREE.Clock.prototype, {
18

19
	start: function () {
20

21 22 23
		this.startTime = window.performance !== undefined && window.performance.now !== undefined
					? window.performance.now()
					: Date.now();
24

25 26 27
		this.oldTime = this.startTime;
		this.running = true;
	},
28

29
	stop: function () {
30

31 32
		this.getElapsedTime();
		this.running = false;
33

34
	},
35

36
	getElapsedTime: function () {
37

38 39
		this.getDelta();
		return this.elapsedTime;
40

41
	},
42

43
	getDelta: function () {
44

45
		var diff = 0;
46

47
		if ( this.autoStart && ! this.running ) {
48

49
			this.start();
50

51
		}
52

53
		if ( this.running ) {
54

55 56 57
			var newTime = window.performance !== undefined && window.performance.now !== undefined
					? window.performance.now()
					: Date.now();
58

59 60
			diff = 0.001 * ( newTime - this.oldTime );
			this.oldTime = newTime;
61

62
			this.elapsedTime += diff;
63

64
		}
65

66
		return diff;
67 68 69

	}

70
} );