Troubleshooting @font-face with SVG node.getBBox issue

When using Raphael.text() to render text with custom fonts loaded via @font-face, I encounter an issue with the getBBox function not returning accurate size values based on the font family. The rendered text appears correctly in the SVG with the desired font, but the width returned by getBBox is not reflective of the actual font being used.

To demonstrate the problem, consider the following pseudo code snippet:

// Loading font and injecting into stylesheets
opentype.load('someFontFamily', (err, font) => {
  const path = paper.text(x, y, 'Hello World')
    .attr({
      'font-family': 'someFontFamily'
      'font-size': 100
    })
  path.getBBox() // Inaccurate width returned
}) 

I have tried preloading the fonts using @font-face declarations before page loading, as well as using timeouts, but the issue persists. It seems like a timing problem where the font needs to be downloaded and triggered for rendering prior to SVG sizing correctly.

Is there a way to ensure that the SVG returns size values reflecting the font being displayed? Perhaps through a callback or event post-render?

Answer №1

Sharing my insights here in case it can help someone else facing a similar issue.

There are various implementations of the @font-face loading events available.

For more information, check out this informative article.

Although cross-browser support may be inconsistent, Font Face Observer appears to be a promising library:

You might also find this resource useful:

Some browsers have built-in support for these events. After rendering text, try running the following code snippet:

document.fonts.load('70px someFontFamily')
  .then(function() {
    console.log(path.node.getBBox())
  })

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

Is there a way to display my current connected clients to a new client using socket.io?

I am currently working on a project involving socket.io. Upon connecting to a new socket, my input username is displayed in the "usersDiv" where all clients are supposed to be listed. However, I've noticed that when I open another tab and input my nam ...

Unable to view indicators and slides are not transitioning in Bootstrap carousel

I am in the process of developing a website with bootstrap, but I am encountering issues with the carousel feature. Despite copying the code directly from their official page, the carousel remains non-functional. The indicators are missing, and the slides ...

Remove any overlapping datetime values from a JavaScript array of objects

Is there a way to efficiently remove any overlaps between the start and end times (moment.js) in a given array of objects? [{ start: moment("2019-03-23T15:55:00.000"), end: moment("2019-03-23T16:55:00.000")}, { start: moment("2019-03-23T14:40:00.000"), e ...

Modify the URL when using the f5 key or hitting the refresh button

Is there a way to update the URL when the user presses the F5 key or refreshes the page using the browser's refresh button in order to get an updated value for a key? For example, if my current URL is: www.examplexyz.com?key=abcd And if the user hit ...

How can we add hover effects to Ionic / Angular components when touching or clicking?

For my app, I need to replicate the hover effect from CSS on mobile devices. Users can click and drag elements, and when they drag over certain elements, I want those elements to change z-index and other styles. Is there a way in Angular / Ionic or even ju ...

How to trigger a click event on a div and effectively filter Vuex data in Vue.js?

Currently facing a challenge with exporting the filter function and utilizing it in a vuex store. Everything was smooth sailing until this point. Now, I'm attempting to add an @click event to divs. When I click on a particular brand, such as "Audi," I ...

Encountering Storage Access Problem on Android following upgrade to API 33

I am currently working on an Ionic, Angular, and Capacitor application where I am utilizing the Cordova-diagnostic-plugin to download images onto a device. However, since updating to Android API 33, I have encountered an issue. Issue Description: Once I ...

center a horizontal line using StyledSheets in your project

After drawing a horizontal line, I noticed that it is positioned towards the left side of the screen. I am hesitant to increase its width. Are there any other methods to move it to the center? I attempted wrapping it with another view and using alignConten ...

Leverage the power of JavaScript by combining it with the .on and .editableselect methods

I am facing an issue with a select input that I am trying to make both editable and trigger an ajax request using the on change JS function. However, only one of the functions is working as expected because I have used the same id for the select input twic ...

Javascript Error: Object Expected

Recently, I have encountered an error in my code that says "object expected" in JavaScript. Surprisingly, the code was working perfectly fine before this issue arose. Strangely, the code is still functioning properly in another solution. Even after making ...

What is the method for making text uppercase within the Heading feature of Grommet?

Currently implementing grommet in my React project and exploring how to capitalize text within a few <Heading /> components. ...

It appears that the Next.js environment variables are not defined

Upon setting up a fresh next.js project using npx create-next-app@latest and configuring some environment variables in the .env.local file, I encountered an error when attempting to run the server. "Failed to load env from .env.local TypeError: Cannot ...

Is there a way to trigger a function after Vue makes changes to the DOM?

There is a select box that displays either one of the two select boxes based on its value change: <form id="reportForm"> <select class="form-control chosen-enabled" v-model="selectedDataType"> <option value="First"&g ...

Functionality of JQuery hamburger menus

I am in the process of developing a script that will control two key functions upon clicking the menu button on the website. The menu button is designed as a hamburger menu and will toggle the display of the menu links. The first function focuses on showin ...

Using Angular to Apply a Custom Validation Condition on a FormGroup Nested Within Another FormGroup

I am facing an issue with my form validation logic. I have a set of checkboxes that need to be validated only when a specific value is selected from a dropdown. The current validator checks the checkboxes regardless of the dropdown value. Here's the c ...

Generating Bootstrap Vue Dropdown components in real time

I am currently utilizing Bootstrap Vue to construct a dynamic Dropdown component that can render different elements such as item, title, and divider. Is there a method to accomplish this task effectively? The desired outcome for the Dropdown component wou ...

Struggling to retrieve the proper return value from my API request

Currently, I am utilizing the API in an attempt to retrieve an object containing both a person's city and country information. Here is my existing code: $(document).ready(function() { var locationAPI = "http://ip-api.com/json"; var K, C, F; var P ...

The problem with the CSS Grid effect

Looking for assistance in creating a grid layout similar to the one on this website: Upon inspecting the page source, I found the following code snippet: http://jsfiddle.net/o45LLsxd/ <div ng-view="" class="ng-scope"><div class="biogrid ng-scope ...

GraphQL is not capable of fetching data directly from a mysql database

Trying to incorporate GraphQL with MySQL in a Node.js Express server has been my recent challenge. Unfortunately, every time I execute my query, an error surfaces. Here is the specific error message: { "errors": [ { "message&quo ...

Angular failing to append hash to ng-href in browsers that do not support it

When I attach an ng-href to a link like this: ng-href="{{post.btn.url}}" The resulting value is: ng-href="/news/some-post" In browsers that do not support html5 mode, these links do not work properly because they require a #. How can I handle this in I ...