What are the steps to incorporate ThreeJS' FontLoader in a Vue project?

I am encountering an issue while attempting to generate text in three.js using a font loader. Despite my efforts, I am facing difficulties in loading the font file. What could be causing this problem?

const loader = new FontLoader();

loader.load(
  'http://localhost:8080/src/store/fonts/noto_sans_kr_regular.json',
  font => {
    const color = 0x006699;

    const matLite = new Three.MeshBasicMaterial({
      color: color,
      transparent: true,
      opacity: 0.4,
      side: Three.DoubleSide,
    });

    const message = '   Three.js\nSimple text.';

    const shapes = font.generateShapes(message, 100);

    const geometry = new Three.MeshBasicMaterial(shapes);

    geometry.computeBoundingBox();

    const xMid =
      -0.5 *
      (geometry.boundingBox.max.x - geometry.boundingBox.min.x);

    geometry.translate(xMid, 0, 0);

    // creating shape (N.B. edge view not visible)

    const text = new Three.Mesh(geometry, matLite);
    text.position.z = -150;
    state.scene.add(text);
  },
);

The above snippet showcases my code, and below are various methods that I have attempted to resolve the issue.

loader.load('http://localhost:8080/src/store/fonts/noto_sans_kr_regular.json');
  loader.load('http://localhost:8080/@/store/fonts/noto_sans_kr_regular.json');
  loader.load('/noto_sans_kr_regular.json');
  loader.load('@/store/fonts/noto_sans_kr_regular.json');
  loader.load('/src/store/fonts/noto_sans_kr_regular.json');
  loader.load('./fonts/noto_sans_kr_regular.json');

This is the error message that is being displayed:

https://i.stack.imgur.com/fuhDu.png

This link leads to the location of the font file:

https://i.stack.imgur.com/xisVN.png

I am unsure if any additional parts of the code are required for troubleshooting purposes. Apologies for any inconvenience caused.

Answer №1

I'm a bit uncertain about how to load files from a specific directory, but the code below shows that using 'public' could work:

<template>
  <div id="app">
    <canvas ref="canvas"></canvas>
  </div>
</template>

<script>
import { FontLoader } from 'three/examples/jsm/loaders/FontLoader.js';
import { TextGeometry } from 'three/examples/jsm/geometries/TextGeometry.js';
import { Mesh, MeshNormalMaterial, WebGLRenderer, PerspectiveCamera, Scene } from 'three'

export default {
  mounted() {
    const loader = new FontLoader()
    const scene = new Scene();
    const camera = new PerspectiveCamera(75, 800 / 600)
    camera.position.set(70, 0, 100)

    const renderer = new WebGLRenderer({
      canvas: this.$refs.canvas,
    })
    renderer.setSize(800, 600)
    loader.load("/macondo_font.json", function (font) {
      const textGeometry = new TextGeometry("Hello world!", {
        font,
        size: 20,
        height: 5,
        curveSegments: 12
      });
      const material = new MeshNormalMaterial();
      const textMesh = new Mesh(textGeometry, material);

      scene.add(textMesh);
      renderer.render(scene, camera)
    });
  },
}
</script>

Furthermore, the font must be uploaded to facetype.js in order to generate a JSON file (since a .ttf or similar format is not common in the 3D realm).

Below you'll find the project structure along with the Macondo font utilized in the example.

https://i.stack.imgur.com/Dw3NS.png

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

Unable to make a post using vue.js

I am facing an issue while trying to submit form data using "vue-resource" in my code. The error message I receive mentions a problem with the use of this method alongside vue-cli and vuetify. Error [Vue warn]: Error in v-on handler: "TypeError: this.$h ...

Looking to cycle through arrays containing objects using Vue

Within my array list, each group contains different objects. My goal is to iterate through each group and verify if any object in a list meets a specific condition. Although I attempted to achieve this, my current code falls short of iterating through eve ...

Encountering a Django Channels issue while loading the JavaScript wrapper

After successfully running the server, the following appeared in my cmd prompt: System check identified no issues (0 silenced). January 21, 2020 - 17:43:42 Django version 3.0.2, using settings 'crm.settings' Starting ASGI/Channels version 2.4.0 ...

Export was not discovered, yet the names are still effective

There seems to be a slight issue that I can't quite figure out at the moment... In my Vue project, I have a file that exports keycodes in two different formats: one for constants (allCodes) and another for Vue (keyCodes): export default { allCodes ...

Removing a row from an HTML table using JavaScript

I have a piece of JavaScript code that is responsible for managing an HTML table. One of the functionalities it needs to support is deleting a row from the table. Currently, I am using the following snippet of code to achieve row deletion: var rowToDele ...

An issue arose when attempting to load the page using jQuery

Currently, I am implementing the slidedeck jquery plugin on my webpage to display slides. While everything is functioning properly, I am facing an issue with the CSS loading process. After these slides, I have an import statement for another page that retr ...

What is the most effective method for ensuring a Vue application can accurately interpret the environmental variables configured on Heroku?

I implemented a Vue app that is currently being hosted on Heroku. Since Heroku doesn't support static apps, I decided to create a basic express server to serve my app. //server.js const express = require('express') const serveStatic = requi ...

Why isn't my List<string> being retrieved from the MVC Controller in an $ajax request?

I am attempting to generate customized lists in my cshtml file through an ajax request. .ajax({ type: "GET", cache: false, url: "@Url.Action("getValidationLists")", contentType: "application/json", dataType: "json", ...

A guide on iterating through an array in vue.js and appending a new attribute to each object

To incorporate a new property or array item into an existing virtual DOM element in Vue.js, the $set function must be utilized. Attempting to do so directly can cause issues: For objects: this.myObject.newProperty = "value"; For arrays: ...

Why are all my requests being denied for the chat-app with nested collections in Firestore Rules?

I am encountering a challenge with configuring Firebase Rules for my chat app. I want to restrict users from accessing conversations they are not part of, but when I try to implement the rules, access gets denied even when checking for conversation existen ...

Unable to successfully retrieve the output from a function within an AJAX request

Hey, I'm having trouble getting the value from this function. It keeps returning undefined and I can't figure out why. Here's the code snippet: function getData() { axios.get('/task') .then(response => { ...

Adjusting the URL variable based on the selected checkbox option

My mobile website offers users the option of a bright or dark interface using a checkbox styled like an iPhone IOS7 switch. The functionality is working as expected, but I'm now facing an issue with passing the status of the checkbox between pages. I ...

Encountering an issue with WebDriver in the realm of JavaScript

I am struggling to use JavaScript to locate specific controls and send values to them. One example is changing the text in a textbox with the ID "ID" to "123456". Below is the code I tried: ((IJavaScriptExecutor)driver).ExecuteScript("document.getElement ...

Is there a way to check for invalid string literals within a string?

Looking for a way to test for invalid (unclosed) strings within a given string? This regex might help: /"[^"\\]*(?:\\[\S\s][^"\\]*)*"|'[^'\\]*(?:\\[\S\s][^'\\]* ...

Can someone provide a list of events for the .on function in Vanilla NodeJS?

Currently experimenting with NodeJS by testing basic backend functionalities like sending various HTTP requests from my index.html file to the server.js file. I plan to delve into Express soon. I've noticed a lack of documentation on NodeJS 'eve ...

Trouble arises when trying to use Kendo-UI's Edit template alongside Vue's Single File Component

Utilizing the Kendoui grid Vue wrapper, I discovered in the Kendoui Vue documentation that a single file Component can be used for Kendo templates. Currently, I am working on an editable grid with a popup editor. In my attempt to set the "editable" propert ...

Navigating pages with Jqueryor Using J

Currently, I am utilizing jQuery and AJAX to retrieve data from a remotely stored PHP file on a server. After successfully fetching the initial page and displaying the outcomes in an HTML table, I've encountered a new challenge. My goal now is to fetc ...

Utilizing a Clear Button to Reset Specific Fields in a Form Without Clearing the Entire Form

I am currently working on a multipart form that includes 'Name', 'Email', and 'Phone Number' fields. It's important to note that the phone number field is actually composed of 10 individual single-digit fields. To enhan ...

Extracting specific key-value pairs from JSON data

Working with JSON data, I encountered a need to pass only specific key-value pairs as data to a component. Initially, I resorted to using the delete method to remove unwanted key-value pairs, which did the job but left me feeling unsatisfied. What I truly ...

JavaScript - continuously update the image marked as "active"

I have a collection of 4 pictures that I want to rotate through in my web application. Each picture should have the "active" class applied to it, similar to when you hover over an image. .active, .pic:hover{ position: absolute; border: 1px solid ...