forked from quidmonkey/Canvas-Native-Fonts-Plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfont-manager.js
95 lines (77 loc) · 1.76 KB
/
font-manager.js
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
/*
* Font Manager Plugin
* Written by Abraham Walters
* v1.0 - May 2012
* v1.1 - July 2012
*/
ig.module(
'plugins.font-manager'
)
.requires(
'impact.impact',
'plugins.font'
)
.defines(function(){
FontManager = ig.Class.extend({
fonts: [],
init: function(){},
//adds a font to be tracked
//set lifetime = 0 or null to have font live indefinitely
add: function( lifetime, font ){
if( lifetime ){
font.lifetime = new ig.Timer( lifetime );
}
this.fonts.push( font );
},
draw: function(){
for( var i = this.fonts.length ; i--; i ){
this.fonts[i].draw();
}
},
//spawns a new font and tracks it
//set lifetime = 0 or null to have font live indefinitely
//returns the new note
spawn: function( lifetime, font, x, y, settings ){
var note = new Font( font, x, y, settings );
if( lifetime ){
note.lifetime = new ig.Timer( lifetime );
}
this.fonts.push( note );
return note;
},
update: function(){
for( var i = this.fonts.length ; i--; i ){
this.fonts[i].update();
if( this.fonts[i]._kill ) {
this.fonts.splice( i, 1 );
}
}
}
});
//inject lifetime & fading properties into Font
Font.inject({
lifetime: null, //lifetime - keep null or set to 0 to have font live indefinitely
_kill: false, //state
draw: function( text, x, y, align, color ) {
var ctx = ig.system.context;
ctx.globalAlpha = this.alpha;
this.parent( text, x, y, align, color );
ctx.globalAlpha = 1;
},
kill: function(){
this._kill = true;
},
update: function(){
this.parent();
if( !this.lifetime ){
return;
}
//kill note if its time has come
if( this.lifetime.delta() > 0 ) {
this.kill();
}
//fade
this.alpha = this.lifetime.delta().map( -this.lifetime.target, 0, 1, 0 );
}
});
});