three-spritetext

repository·master·Indexed 18 days ago

https://github.com/vasturiano/three-spritetext

A ThreeJS component that renders text onto a canvas, converts it into a texture, and applies it to a Sprite to ensure text always faces the camera. Version 1.10.0 provides a SpriteText class extending THREE.Sprite for creating 3D labels and UI elements with customizable font styling, borders, and backgrounds.

Tokens
1.1K
Snippets
4
Records
5
Agent score
14%

What's inside three-spritetext

  1. Install and Quick start with three-spritetext

    master

    To use three-spritetext in your ThreeJS project, you can install it via npm or include it via a CDN script tag. The component creates a Sprite that always faces the camera, with the text rendered onto a canvas texture.

    // Using npm
    import SpriteText from 'three-spritetext';
    
    // Or using a script tag
    // <script src="//cdn.jsdelivr.net/npm/three-spritetext"></script>
    
    const myText = new SpriteText('My text');
    const myScene = new THREE.Scene();
    myScene.add(myText);
  2. Configure SpriteText properties

    master

    You can customize the appearance of the text sprite using the following properties:

    Text Content & Styling

    • text: The string to display. Supports multi-line text using \n for line breaks.
    • textHeight: The height of the text (Default: 10).
    • color: The fill color of the text (Default: white).
    • fontFace: The font type (Default: Arial).
    • fontSize: The resolution of the text in vertical pixels. Higher values are sharper but cost more performance (Default: 90).
    • fontWeight: The boldness of the font (e.g., normal).
    • strokeWidth: Width of the text stroke. 0 disables it (Default: 0).
    • strokeColor: Color of the text stroke (Default: white).

    Canvas & Background

    • backgroundColor: The canvas background color. Use a falsy value for a transparent background.
    • padding: Padding between text and canvas edge. Accepts a single value or a tuple [horizontal, vertical] (Default: 0).
    • offsetX: Horizontal shift/margin (Default: 0).
    • offsetY: Vertical shift/margin (Default: 0).

    Border & Corners

    • borderWidth: Size of the border around the canvas. Accepts a single value or a tuple [horizontal, vertical] (Default: 0).
    • borderColor: Color of the border (Default: white).
    • borderRadius: Corner radius. Accepts a single value or an array of four values [top-left, top-right, bottom-right, bottom-left] (Default: 0).
  3. Clone and copy SpriteText instances

    master

    You can duplicate a SpriteText instance using the .clone() or .copy() methods.

    • clone(): Creates a new instance of the same class and copies all properties from the current instance.
    • copy(source): Copies all properties from a source instance into the current instance. This method also calls the base THREE.Sprite.prototype.copy to ensure standard Three.js properties are synchronized.
    const original = new SpriteText('Original', 10, 'white');
    original.fontSize = 150;
    
    // Create a clone
    const clone = original.clone();
    
    // Copy properties from one to another
    const another = new SpriteText();
    another.copy(original);
  4. Create a text-based sprite with SpriteText

    master

    The SpriteText class (exported as the default export) extends THREE.Sprite and allows you to render text onto a canvas texture that is applied to a sprite. This is useful for creating 3D labels, UI elements, or floating text in a Three.js scene.

    To use it, instantiate the class with the desired text, height, and color. Note that changing properties like text, textHeight, or color will automatically trigger a canvas regeneration to update the texture.

    import SpriteText from 'three-spritetext';
    
    // Create a sprite with text 'Hello World', height 10, and white color
    const spriteText = new SpriteText('Hello World', 10, 'rgba(255, 255, 255, 1)');
    
    // Add it to your Three.js scene
    scene.add(spriteText);