-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathText.js
More file actions
41 lines (36 loc) · 1.18 KB
/
Text.js
File metadata and controls
41 lines (36 loc) · 1.18 KB
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
import _ from 'underscore';
import CanvasElement from '../core/CanvasElement';
import {checkOptions, RedrawProperties} from '../core/util';
import {standardFont} from '../core/defaults';
const isFirefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
@RedrawProperties([
'font', 'text'
])
class Text extends CanvasElement {
constructor(options){
super(options);
options = _.defaults(_.clone(options), {
font: standardFont
});
checkOptions(options, ['text']);
this.font = options.font;
this.text = options.text;
}
draw(){
let {style,weight,size,family,color} = this.font;
style = style || "normal";
weight = weight || "normal";
this.canvas.font = `${style} ${weight} ${size}px "${family}"`
this.canvas.fillStyle = color;
this.canvas.textAlign = 'left';
this.canvas.textBaseline = 'top';
const measurement = this.canvas.measureText(this.text);
_.defaults(measurement, {width: 0, height: 0});
if(isFirefox){
this.canvas.fillText(this.text, - measurement.width / 2, size / 4);
} else {
this.canvas.fillText(this.text, - measurement.width / 2, - measurement.height / 2);
}
}
}
export default Text;