The Three.js Typeable Text Library v0.1.6
The intention of this library is to make creation and integration of typeable text elements seamless with threejs.
Support me here https://ko-fi.com/joshfield Follow me here https://twitter.com/HexaField
Design:
This is designed to be as simple as creating the ThreeTypeableText object, adding it to the scene, and optionally updating it to show the cursor. There is some basic functionality that should integrate with most project designs easily. You can find what is planned for this at the bottom.
Example:
A live demo can be found here
Usage:
{ // ... initialise threejs var textField = camera: camera font: font string: 'Hello text!' ; scene} { // ... textField // only used for displaying the cursor, not necessary for functionality // ...}
Parameters:
camera A THREE.Camera (only needed if useDocumentListeners is true)
font The THREE.Font to use (always needed)
string The text to display
useDocumentListeners: Use built-in document listeners (default: true)
material A THREE.Material to use (default: new THREE.MeshBasicMaterial({ side: THREE.DoubleSide })
)
align Shifts the text. (options: 'left', 'center', 'right', default: 'center')
fontScale Scales the geometry (default: 1)
onChange A callback that is fired when the text changes internally
onFocus A callback that is fired when the focus changes internally (true if gaining focus, false otherwise)
maxEditHistory The number of copies of the string to put in edit history (default: 32)
API:
Setting useDocumentListeners to false will require you to use the following functions to update the text manually
// Change the texttextField; // Returns the texttextField; // returns 'New text!' // To access the text as an object usetextFieldposition // Move the cursor 3 letters to the righttextField; // Returns the character index of the cursortextField; // returns 3 // Text will now display 'Ne text!'textField; // Text will now display 'Netext!'textField; // Move the cursor 1 letter to the lefttextField; // Text will now display 'N8etext!'textField; // Will now display 'Ne text!'textField; // Will return 'Netext!'textField; // Will stop all internal document listeners & hide the cursortextField; // Get the text heighttextField; // depends on fontScale and the font itself // Check to see if the user has clicked// This should run on your mouse click event // - supplying a valid THREE.Vector3 will move the cursor// - supplying an invalid point will defocus the text raycastervar intersections = raycastertextField;
Planned Features:
API
- mobile typing support
- shift + arrows to make selection
- control + c / v / x - copy cut paste
- click and drag to make selection
- control + arrows to jump words
- control + delete / backspace to delete / backspace whole words
- better text alignment & spacing
- text outline
- thickness
- dotted
- empty interior
- extrusion & bevel
- add onFocus / onUnFocus events
- formatting
INTERNALS:
- generate letter by letter to not have to regenreate the whole string every time a letter is changed
- add & remove document listeners on focus & unfocus