* change the text to be displayed * @param {number|string|string[]} value - a string, or an array of strings * @returns {Text} this object for chaining
(value = "")
| 266 | * @returns {Text} this object for chaining |
| 267 | */ |
| 268 | setText(value = "") { |
| 269 | const bounds = this.getBounds(); |
| 270 | |
| 271 | // set the next text |
| 272 | if (this._text.toString() !== value.toString()) { |
| 273 | if (!Array.isArray(value)) { |
| 274 | this._text = ("" + value).split("\n"); |
| 275 | } else { |
| 276 | this._text = value; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | // word wrap if necessary |
| 281 | if (this._text.length > 0 && this.wordWrapWidth > 0) { |
| 282 | this._text = this.metrics.wordWrap( |
| 283 | this._text, |
| 284 | this.wordWrapWidth, |
| 285 | this.canvasTexture.context, |
| 286 | ); |
| 287 | } |
| 288 | |
| 289 | // calculcate the text size and update the bounds accordingly |
| 290 | bounds.addBounds( |
| 291 | this.metrics.measureText(this._text, this.canvasTexture.context), |
| 292 | true, |
| 293 | ); |
| 294 | |
| 295 | // round the offscreen canvas size to the next power of two |
| 296 | // (required for WebGL1, harmless for WebGL2/Canvas) |
| 297 | const width = nextPowerOfTwo(this.metrics.width); |
| 298 | const height = nextPowerOfTwo(this.metrics.height); |
| 299 | |
| 300 | // invalidate the texture |
| 301 | const renderer = this.parentApp?.renderer ?? game.renderer; |
| 302 | this.canvasTexture.invalidate(renderer); |
| 303 | |
| 304 | // resize the cache canvas if necessary |
| 305 | if ( |
| 306 | this.canvasTexture.width < width || |
| 307 | this.canvasTexture.height < height |
| 308 | ) { |
| 309 | this.canvasTexture.resize(width, height); |
| 310 | } |
| 311 | |
| 312 | this.canvasTexture.clear(); |
| 313 | this._drawFont( |
| 314 | this.canvasTexture.context, |
| 315 | this._text, |
| 316 | this.pos.x - this.metrics.x, |
| 317 | this.pos.y - this.metrics.y, |
| 318 | ); |
| 319 | |
| 320 | this.isDirty = true; |
| 321 | |
| 322 | return this; |
| 323 | } |
| 324 | |
| 325 | /** |
no test coverage detected