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

Updating the file path for JS and CSS files in the finalized build

Looking for some guidance here. I'm trying to deploy a vuejs app using Vue CLI 3 and when I run the build command, the files are built into the dist folder as expected. However, there are js and css folders inside dist that contain the respective file ...

Showcasing pictures on ReactJS

I am currently working on developing an application to showcase images on my React webpage. These images have already been uploaded to a local folder. In order to integrate my React application with a NodeJS server and MongoDB, I connected the two. My goa ...

Having trouble with importing a TypeScript class: encountering a "cannot resolve" error message

Could you lend me your expertise? I'm puzzled by this issue that seems to be quite simple and straightforward: export class Rectangle { height: number = 0 width: number = 0 constructor(height: number, width: number) { this. ...

Is it possible to use JavaScript to retrieve a client's bookmarks with their consent?

Is there a way to streamline the process of importing bookmarks to my server for users? Can I use JavaScript to automatically retrieve a user's bookmarks, or is that not possible due to security concerns in browsers? ...

Installing Vue CLI on Mac OS may not function correctly

I recently installed vue-cli by executing the following command: npm install -g @vue/cli /Users/me/npm/bin/vue -> /Users/me/npm/lib/node_modules/@vue/cli/bin/vue.js /Users/me/npm/lib └── @vue/<a href="/cdn-cgi/l/email-protection" class="__cf_ ...

Difficulty encountered in storing information in the database

I have been developing a survey generator but I am facing an issue where everything is empty. The client enters one or many questions, along with one or many answers for each question. These inputs are sent to my database. I have successfully coded the d ...

Filtering options in a dropdown box with jQuery

I have implemented a feature where the content of one select box is filtered based on the option selected in another select box. Below is the code snippet that achieves this functionality: // Filter content based on the selected option in a region select ...

Having difficulty managing asynchronous functions with the useState hook in React

import React from "react"; import { UserContext } from "./../contexts"; import { removeStoredAuthData, storedAuthIsValid, storeNewAuthData, } from "./../utils/auth"; import { getUserInfos } from "./../api/userAuthen ...

React component fails to re-render after state change

For the past two days, I've been struggling with this error and can't seem to fix it! I'm currently working on creating a weather app in React which utilizes the API. The app features a Bootstrap Navbar with a search option that allows user ...

When utilizing a script to deliver a true or false response for jQuery's .load() function

Currently, I have found myself delving deep into jquery to manage the ajax requests in my web applications. Specifically, I am working on a bidding system in PHP that heavily relies on mod_rewrite. Using jQuery with a confirm dialog, I send an Ajax request ...

error: encountering issue with Vue TypeScript Jest tests - '$store' property is undefined

I've been facing issues with my tests failing after a recent npm install. I've tried adjusting versions up and down, but haven't had any success. Interestingly, the $store isn't directly used in any of the components or tests. Despit ...

Add the search outcome to the input field in vue

Within this section, I have implemented an @click event <h2 @click="action(customer.id)">{{ customer.name }}</h2> My goal is to append the clicked result to the input when any result is clicked. Do you have any ideas on how to achieve this? ...

Display only the spans that contain text or are not empty

I need a solution to hide only those spans that are empty. <div class="img-wrapper"> <div class="showcase-caption" style="display: block; "> <span id="MainContent_ImageCaption_0">This one has caption</span> </div> ...

Troubles with the compatibility of javascript and jquery's multiselect plugin

I've been utilizing the multiselect API to create a dropdown with multiple select options. This is my HTML code: <select id="options" multiple="multiple"></select> And this is my JS code: render:function(){ // $('#viewTemp& ...

Angular sending information from one page and retrieving it on another

The reportForm page allows users to input information and submit it to create a report. reportData = { headline: $scope.headline, fromDate: $scope.fldFromDate, toDate: $scope.fldToDate, whatever: $scope.whatever } $http.post(reportUrl + $scope.repor ...

Toggle the play/pause function by clicking - Trigger Ajax

I have a campaign that can be either played or paused. Currently, I have implemented the pause feature using AJAX and now I need to add the play feature as well. When I click on the pause button, I want it to be replaced with the play button. This is the ...

What is the best way to calculate the product of these two fields?

What is the best way to calculate the total amount by multiplying the unit price with the quantity and adding the VAT? For example: Quantity: 2 VAT: 15% Unit price: 2000 =2000x1.15 (Unit price x VAT) =2300x2 Can someone guide me on how to achieve this? Y ...

Ways to verify if the flash plugin has been disabled in the Chrome browser

Is there a way to use JavaScript or jQuery to detect if the flash plugin is blocked in Google Chrome? You can check for a disabled flash plugin using the following code: ((typeof navigator.plugins != "undefined" && typeof navigator.plugins["Shock ...

Identifying duplicate values in an array of objects using JavaScript

I am facing an issue with my array that collects data from a spreadsheet into studentCCAList. Sometimes, a student may have multiple subjects. For example, Name: Joseph Subject: English Name: Peter Math Name: Joseph Subject: Science My concern i ...

Transforming the primary image to the thumbnail image when hovering over it with JSON objects

Currently, I am facing an issue with my single-page web application project that involves using jQuery, JSON Bootstrap. So far, I have successfully created a category page, product selection page, and item page. The problem arises on the item page where ...