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

Method setText

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

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

Source from the content-addressed store, hash-verified

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

Callers 15

onResetEventMethod · 0.95
onResetEventMethod · 0.95
font.spec.jsFile · 0.45
updateMethod · 0.45
setScoreMethod · 0.45
showGameOverMethod · 0.45
setLivesMethod · 0.45
drawTextMethod · 0.45
updateMethod · 0.45
applyLabelStateFunction · 0.45
updateMethod · 0.45

Calls 11

_drawFontMethod · 0.95
nextPowerOfTwoFunction · 0.90
wordWrapMethod · 0.80
addBoundsMethod · 0.80
invalidateMethod · 0.80
getBoundsMethod · 0.65
splitMethod · 0.65
clearMethod · 0.65
toStringMethod · 0.45
measureTextMethod · 0.45
resizeMethod · 0.45

Tested by

no test coverage detected