* update the bounding box for this Text, accounting for textAlign and textBaseline. * @param {boolean} [absolute=true] - update in absolute coordinates * @returns {Bounds} this renderable's bounding box
(absolute = true)
| 379 | * @returns {Bounds} this renderable's bounding box |
| 380 | */ |
| 381 | updateBounds(absolute = true) { |
| 382 | if (typeof this.metrics !== "undefined" && this._text.length > 0) { |
| 383 | const bounds = this.getBounds(); |
| 384 | bounds.clear(); |
| 385 | |
| 386 | const w = this.metrics.width; |
| 387 | const h = this.metrics.height; |
| 388 | |
| 389 | // compute x offset based on textAlign |
| 390 | let ax = 0; |
| 391 | switch (this.textAlign) { |
| 392 | case "right": |
| 393 | ax = w; |
| 394 | break; |
| 395 | case "center": |
| 396 | ax = w / 2; |
| 397 | break; |
| 398 | } |
| 399 | |
| 400 | // compute y offset based on textBaseline |
| 401 | let ay = 0; |
| 402 | switch (this.textBaseline) { |
| 403 | case "middle": |
| 404 | ay = h / 2; |
| 405 | break; |
| 406 | case "ideographic": |
| 407 | case "alphabetic": |
| 408 | case "bottom": |
| 409 | ay = h; |
| 410 | break; |
| 411 | } |
| 412 | |
| 413 | bounds.addFrame(-ax, -ay, w - ax, h - ay); |
| 414 | |
| 415 | if (absolute === true) { |
| 416 | const absPos = this.getAbsolutePosition(); |
| 417 | bounds.centerOn( |
| 418 | absPos.x + bounds.x + bounds.width / 2, |
| 419 | absPos.y + bounds.y + bounds.height / 2, |
| 420 | ); |
| 421 | } |
| 422 | return bounds; |
| 423 | } |
| 424 | return super.updateBounds(absolute); |
| 425 | } |
| 426 | |
| 427 | /** |
| 428 | * measure the given text size in pixels |
no test coverage detected