trinText.js 2.1 KB

1
  1. function TrinText(text) { TrinText.superclass.constructor.apply(this); this.text = "" + text; this.color = "#000000"; this.style = "16px Arial"; this.filled = true; this.baseLine = "top"; this.align = "left"; this.shadow = {x: 0, y: 0, blur: 0}; this.patternImage = null; } extend(TrinText, TrinEntity); TrinText.prototype.setStyle = function(font, size, bold, color, align, baseLine) { if (font === undefined) { font = "Arial"; } if (size === undefined) { size = 16; } if (bold === undefined) { bold = false; } if (color === undefined) { color = this.color; } if (align === undefined) { align = this.align; } if (baseLine === undefined) { baseLine = this.baseLine; } this.style = ""; if (bold) { this.style += "Bold "; } this.style += size + "px "; this.style += font; this.color = color; this.align = align; this.baseLine = baseLine; }; TrinText.prototype.draw = function(context) { TrinText.superclass.draw.apply(this, [context]); var dx = this.x - this.orign.x; var dy = this.y - this.orign.y; context.font = this.style; context.textBaseline = this.baseLine; context.textAlign = this.align; if (this.shadow.x !== 0 || this.shadow.y !== 0 || this.shadow.blur !== 0) { context.shadowOffsetX = this.shadow.x; context.shadowOffsetY = this.shadow.y; context.shadowBlur = this.shadow.blur; } if (this.filled) { if (this.patternImage === null) { context.fillStyle = this.color; } else { if (this.patterns[this.patternImage] === null) { this.patterns[this.patternImage] = context.createPattern(this.patternImage, "repeat"); } context.fillStyle = this.patterns[this.patternImage]; } context.fillText(this.text, dx, dy); } else { context.strokeStyle = this.color; context.fillText(this.text, dx, dy); } }; TrinText.prototype.patterns = [];