Socket
Socket
Sign inDemoInstall

@seregpie/three.text-texture

Package Overview
Dependencies
1
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @seregpie/three.text-texture

A texture with the drawn text.


Version published
Maintainers
1
Install size
9.80 kB
Created

Readme

Source

THREE.TextTexture

class THREE.TextTexture extends THREE.Texture

An instance of TextTexture is a texture with the drawn text.

setup

npm

npm i @seregpie/three.text-texture

ES module

import TextTexture from '@seregpie/three.text-texture';

browser

<script src="https://unpkg.com/three"></script>
<script src="https://unpkg.com/@seregpie/three.text-texture"></script>

The class is globally available as THREE.TextTexture.

usage

let texture = new THREE.TextTexture({
  fillStyle: '#24ff00',
  fontFamily: '"Times New Roman", Times, serif',
  fontSize: 32,
  fontStyle: 'italic',
  text: [
    'Twinkle, twinkle, little star,',
    'How I wonder what you are!',
    'Up above the world so high,',
    'Like a diamond in the sky.',
  ].join('\n'),
});
let material = new THREE.SpriteMaterial({map: texture});
let sprite = new THREE.Sprite(material);
let spriteScale = 10;
await texture.redraw();
sprite.scale
  .set(texture.image.width / texture.image.height, 1, 1)
  .multiplyScalar(spriteScale);
scene.add(sprite);

Update the texture.

texture.fontFamily = 'Arial, Helvetica, sans-serif';
texture.text = [
  'When this blazing sun is gone,',
  'When he nothing shines upon,',
  'Then you show your little light,',
  'Twinkle, twinkle, through the night.',
].join('\n');
await texture.redraw();
sprite.scale
  .set(texture.image.width / texture.image.height, 1, 1)
  .multiplyScalar(spriteScale);

members

constructor

new THREE.TextTexture({
  align: 'center',
  createCanvas() { /*...*/ },
  fillStyle: '#fff',
  fontFamily: 'sans-serif',
  fontSize: 16,
  fontStyle: 'normal',
  fontVariant: 'normal',
  fontWeight: 'normal',
  lineGap: 0.15,
  loadFontFace(family, style, variant, weight) { /*...*/ },
  padding: 0.25,
  strokeStyle: '#000',
  strokeWidth: 0,
  text: '',
})
argumentdescription
alignThe horizontal text alignment. Possible values are 'center', 'left' and 'right'.
createCanvasA function to create a new Canvas instance.
fillStyleThe fill color or style.
fontFamilyThe font family.
fontSizeThe font size.
fontStyleThe font style.
fontVariantThe font variant.
fontWeightThe font weight.
lineGapThe vertical distance between the text lines. The value is relative to the font size.
loadFontFaceA function to load a font face.
paddingThe space around the text. The value is relative to the font size.
strokeStyleThe stroke color or style.
strokeWidthThe stroke width. The value is relative to the font size.
textThe text.

Provide a custom loadFontFace function to support the older browsers.

loadFontFace(family, style, variant, weight) {
  return (new FontFaceObserver(family, {style, weight})).load();
}

properties

.isTextTexture = true

Used to check whether this is an instance of TextTexture.


.text

.fontFamily

.fontSize

.fontWeight

.fontVariant

.fontStyle

.fillStyle

.strokeWidth

.strokeStyle

.align

.lineGap

.padding

.createCanvas

.loadFontFace


.lines

read-only

The text splitted by the newline character.


.height

read-only

The image height. The value is relative to the font size.

methods

.redraw()

Waits until the image is redrawn. The method is throttled and is called when the dependent properties are changed.

Returns a Promise that resolves when the image is redrawn.


.computeOptimalFontSize(object, renderer, camera)

Computes the optimal font size depending on the distance of the object to the camera and the size of the renderer DOM element.

argumentdescription
objectAn instance of THREE.Object3D.
rendererAn instance with a domElement property.
cameraAn instance of THREE.Camera.

Returns the computed font size.

see also

  • THREE.TextSprite

Keywords

FAQs

Last updated on 14 Oct 2019

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc