What is the best way to create a TextGeometry object with multiple lines? Is there a way to contain it within a square shape, similar to how text wraps within a div

I've been experimenting with creating 3D text using WebGL, three.js, and THREE.TextGeometry. So far, everything is working smoothly as I'm able to generate a single line of 3D text.

Now, my goal is to produce multiline text resembling a short paragraph. Ideally, I want the text to naturally wrap when it reaches the boundaries of a box or rectangle that contains it. The behavior I'm aiming for is similar to how standard HTML text wraps within a div, flowing into multiple lines once it hits the edge of its parent container.

Below is an example of how I currently create a single line:

 textGeo = new THREE.TextGeometry(
    'Hello there. Am I a paragraph? I hope so.',
    'size': 30
    'height': 2
    'font': 'helvetiker'
    'weight': 'normal'
    'style': 'normal'
    bevelThickness: 0.1
    bevelSize: 0
    bevelEnabled: true
    material: 0
    extrudeMaterial: 1
  )

  materialArray = [
    new THREE.MeshBasicMaterial( { color: 0xFFFFFF  } )
    new THREE.MeshBasicMaterial( { color: 0x666666, shading: THREE.SmoothShading } )
  ]

  textMaterial = new THREE.MeshFaceMaterial(materialArray)
  textGeo = new THREE.Mesh(textGeo, textMaterial)

  textGeo.position.x = -150
  textGeo.rotation.y = de2ra(15)

  scene.add textGeo

How can I transform this into multiline text? Additionally, how do I enclose this in a square shape for wrapping purposes? What steps are involved in creating the square?

Answer №1

To add your text in a 3D scene, you can either create it in HTML and then render it within the scene, or you can programmatically adjust its size by checking its dimensions and splitting it into smaller parts if needed. To determine the size of a geometry, use the geometry.boundingBox property after calculating it with geometry.computeBoundingBox(). The bounding box contains min and max vectors representing opposite corners, allowing you to easily measure the geometry along a specific axis like so:

geometry.boundingBox.max.x - geometry.boundingBox.min.x

Answer №2

The functionality provided by the three.js API is quite basic. In my opinion, achieving this in a straightforward manner may not be feasible. However, a workaround could involve generating several TextGeometry instances, each representing a single line of text, and then adjusting their positions manually to place them at varying y coordinates.

Answer №3

Continuing from the earlier message. You have the option to separate lines using \n and the text formatting will acknowledge it. Nevertheless, this does not address the issue of "html-like wrapping"

For example

let content = `initial line. \n second line. \n third line.`;

Answer №4

Here is a new trick to render multi-line text using TextGeometry.

If you employ a template literal syntax and break down the text into separate lines within the string used for TextGeometry, the spaces will be preserved by the TextGeometry. While this method allows for manually creating a multi-line effect, it does not automatically wrap the text within a square.

For instance:

let message = `
    This is 
    a sample
    multiline 
    text.
`;

textShape = new THREE.TextGeometry(message, {
    font: myFont,
    size: 10,
    height: 0,
    curveSegments: 10
}).center();

Answer №5

To create a line break

In your script, instead of

'Hey, this is a sentence with multiple lines.',

Update it to

'Hey, this is a sentence with \nlines separated by a line break.',

Include the '\n' character whenever you want to start a new line.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Enhance the form values using HTML and Javascript before submitting them via POST

Greetings to all! Apologies if my question seems elementary, as I am relatively new to using Javascript/HTML... I am encountering an issue with PayPal integration. In the past, I have successfully implemented it with a single fixed price, but now I have ...

Tips on implementing a jQuery .load() function using an anchor tag within dynamic content

I am working on a search page where user input is taken from a form and then sent to a PHP file that makes a cURL call to an external server. The PHP file receives an array from the server, which it uses to display HTML in a "results" div on the original s ...

Retrieve the input data of a similar structure when a user triggers a button click using jQuery

I have several forms on the same page, and currently I am using jQuery ajax to send their content to another server. I have been using unique selectors for each form to retrieve their values before sending them. I am looking to streamline this process and ...

It seems like there may be an issue with the React Table Pagination in reactjs

As a newcomer to React JS, I have been utilizing react-table to create a component that can filter, sort, and paginate sample data from a JSON file. If you are interested in the tutorial I am following, you can find it here: Currently, I am encountering ...

There is no overload that matches this call. The previous overload resulted in an error stating that type 'text' cannot be assigned to type 'json'

I am attempting to retrieve an HTML page from my API using Angular and including JWT tokens in the header. However, when I do not specify the response type as text, the compiler returns a parsing error (as it initially tries to parse the HTML as JSON), so ...

Why does app.post function while router.post does not seem to work?

I have encountered an issue while logging in with React on the front end. The process goes to my node/Express/Passport backend and I am able to successfully log in when it reaches the backend. However, I am facing difficulties in communicating that informa ...

Using Jquery to create a fading effect when changing the color by checking a checkbox

I'm struggling with adding a fading effect to change the background color of the body element when a checkbox is checked. The color changes successfully, but I can't figure out how to make it fade smoothly. Here is the HTML code: <div class=& ...

Exploring the eccentricities of mirror reflections in ThreeJS

(Just a heads up: this is my first post on SO. I've done some searching on SO and other platforms for a solution to my issue, but haven't found anything that directly addresses it.) Problematic Mirror This is my initial attempt at creating a sc ...

AngularJS: ng-model not reflecting changes in entire object, only specific field within the object is being updated

I came across an issue while working on my AngularJS project. Here is the scenario: form.html: <form> <input ng-model="someVariable" type="text"/> </form> details.html: <div ng-include="'form.html'"></div> {{ s ...

Is it possible to check the terms and conditions checkbox using Javascript and Protractor?

Looking for guidance on writing Protractor Angular E2E testing code for the terms and conditions checkbox in Angular testing. The create profile button should only be enabled when the checkbox is clicked. // Example of checkbox behavior <md-checkbo ...

Issues with creating modal images using only JavaScript

I am facing an issue with modal popup images. I have a collection of images and attempted to implement this code (with some modifications): https://www.w3schools.com/howto/tryit.asp?filename=tryhow_css_modal_img However, it seems to only work for the initi ...

Three.js Decal fails to display

Having some trouble creating a decal on my mesh using three.js and react-three-fiber. Even though I set up the mesh and material correctly, the decal isn't showing up on the canvas for some reason. https://i.sstatic.net/IgXSw.png This parent compone ...

The error message "Uncaught (in promise) ReferenceError: dispatch is not defined" indicates that

Currently, I am utilizing vuex with index.js and auth.js stored in the store folder. My goal is to perform a basic sign-in operation within my signin.vue by calling an action from the store. However, I encountered the error 'Uncaught (in promise) Refe ...

Encountering an unanticipated DOMException after transitioning to Angular 13

My Angular project is utilizing Bootstrap 4.6.2. One of the components features a table with ngb-accordion, which was functioning properly until I upgraded the project to Angular 13. Upon accessing the page containing the accordion in Angular 13, I encount ...

Using OpenCV.js to detect QR codes

Currently, I am in the process of creating a web application for detecting QrCodes using Opencv.Js. Everything is working smoothly with functions like findcountour and GaussianBlur, but it appears that cv.QRCodeDetector() is not available. let detector = ...

Using HTML: The trick to obtaining selection offsets in relation to HTML tags

Let's say I have HTML code like this: <div> <p> start<span>span</span>end </p> </div> I am looking for a way to retrieve the offsets when text is selected, while still taking into account the presence of span ...

How can I remove the popover parents when clicking the confirm button using jQuery?

I am having trouble sending an AJAX request and removing the parent of a popover after the request is successful. I can't seem to access the parent of the popover in order to remove it, which is causing me some frustration. // Code for deleting w ...

Injecting a variable into a string before adding it to a pre-existing array

Trying to insert a variable into a string while adding the string itself to an existing array like this: var myVar = 'slug'; myArray.push(['item one', '/path/to/' + myVar + '/']); This approach is not yielding the ...

Would it be a security risk to share the Stripe charge ID and connected account ID with clients on the frontend?

I am currently making an ajax call to my backend and I am unsure whether it is secure to reveal the charge id and the connected account id on the client side. Just to clarify, there are no secret keys or sensitive information stored in the browser. Your ...

Sending documents via ExpressJS

I'm currently developing a small application using the latest NodeJS and ExpressJS, but I've encountered an issue with uploading files. My routes are set up like this: app.get('/Share', share.index); app.post('/Share/Process&apos ...