Displaying text
You can display text with both three.js Text nodes and various HTML nodes (such as a H1 or div).
To update a three.js Text node, you can use the following code in a behavior attached to it:
this.instance.text = "Hello world!";To update a HTML text node, you can use the following code in a behavior attached to it:
this.instance.element.innerText = "Hello world!";You may also use
innerHTMLif you would like to customize your displayed HTML text. You can learn more about this Web API here.
These step-by-step tutorials will walk you through adding a Custom Behavior to a button, which will display some text in a separate node.
Custom display text Behavior
Section titled “Custom display text Behavior”This tutorial assumes that your button (an image or a HTML node for example) and text supported node has already been added to the Hierarchy.
1. Click on your button in the Hierarchy and then find the Behaviors Panel. Click on the plus (+) icon in the Behaviors Panel and then + New Custom Behavior.

2. Give your custom behavior a name and then click on Create.

If you want your custom behavior to take effect in the Mattercraft editor, check the Run at Design Time box.
3. Head to the Left Menu and double click on your custom behavior to open it in the Mattercraft scripting environment.

You can also open the script by right clicking on it in the Left Menu and going to Open to the Side
4. Add a method with the @zRegister decorator to handle click events and update the text:
@zRegister('onClick')private _handleClick() { // For a three.js Text node: this.zcomponent.nodes.myThreejsTextNode.text = "Hello world!";
// Or for a HTML node: // this.zcomponent.nodes.myHtmlNode.innerText = "Hello world!";}Taking care to replace myThreejsTextNode or myHtmlNode with your actual node name.
You may need to use
'onPointerDown'instead of'onClick', depending on the node you are using as a button.
this.instancewill target only the node(s) this behavior is attached to, whilstthis.zcomponent.nodeswill search through your project to target a node which may or may not have this behavior attached to it.
This method leverages the
innerTextWeb API; which you can learn more about here.
5. Your full behavior should look something like this:
import { Behavior, zBehavior, zRegister } from '@zcomponent/core';import { Button } from '@zcomponent/html/lib/button';import Scene from './Scene.zcomp';
@zBehavior({ icon: 'text_fields' })export class DisplayTextBehavior extends Behavior<Button> { protected zcomponent = this.getZComponentInstance(Scene);
@zRegister('onClick') private _handleClick() { // Updating a three.js Text node this.zcomponent.nodes.myThreejsTextNode.text = "Hello world!"; }}