MCPcopy Create free account
hub / github.com/melonjs/melonJS / updateBounds

Method updateBounds

packages/melonjs/src/renderable/text/text.js:381–425  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

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

Callers 8

centerXMethod · 0.45
centerYMethod · 0.45
updateMethod · 0.45
_applyFrameMethod · 0.45
refreshAbsoluteBoundsFunction · 0.45
container.spec.jsFile · 0.45
raycast.spec.jsFile · 0.45

Calls 5

addFrameMethod · 0.80
getBoundsMethod · 0.65
clearMethod · 0.65
getAbsolutePositionMethod · 0.65
centerOnMethod · 0.45

Tested by

no test coverage detected