Skip to content

Font.textBounds() throws "J is not defined" #465

@marcabisamra

Description

@marcabisamra

Expected Behavior

The Font.textBounds() method should return the bounding box of the specified text when called, allowing for accurate text positioning and layout calculations

Actual Behavior

Instead of returning the bounding box, calling Font.textBounds() throws a React.js error:

react.js:15120 Uncaught ReferenceError: J is not defined at S.value (react.js:15120:11)

This error prevents the proper calculation of text bounds and disrupts the functionality of sketches that rely on this method when using the @p5-wrapper/react package.

Steps to Reproduce the Problem

  1. Create a p5.js sketch using the @p5-wrapper/react components.
  2. Load a font using p5.loadFont() in the sketch.
  3. Attempt to use the Font.textBounds() method on the loaded font.
  4. Observe the console for the error message.

Example code snippet:

import React from 'react';
import { ReactP5Wrapper } from "@p5-wrapper/react";

function sketch(p5) {
  let myFont;

  p5.preload = () => {
    myFont = p5.loadFont('path/to/font.ttf');
  }

  p5.setup = () => {
    p5.createCanvas(400, 400);
    p5.textFont(myFont);
    let bounds = myFont.textBounds('Hello', 0, 0, 32);
    // This line throws the error
  }

}

function App() {
  return <ReactP5Wrapper sketch={sketch} />;
}

export default App;

Specifications

  • Package Version: 4.4.2 (tried most published @p5-wrapper/react versions)
  • React version: 18.3.1
  • p5.js version: 1.4.1
  • Browser: Chrome 127.0.6533.120
  • OS: MacOS 14.5

Additional Notes

  • Using this with @p5-wrapper/next
  • The error seems to be related to the interaction between React and p5.js when using the wrapper.
  • It's unclear if this issue is specific to the wrapper or if it's a more general problem with using p5.js in React applications.

Possible Workarounds

While investigating this issue, using p5.textWidth() in combination with the font size might serve as a temporary workaround for some use cases, although it doesn't provide the full functionality of textBounds().

Attempts to Resolve

  • I have tried this with multiple versions of the @p5-wrapper/react package, but the issue persists.
  • I have verified that the font is loading correctly and that other font-related functions work as expected.

Impact

This issue significantly impacts projects that rely on accurate text bounding boxes for layout and positioning, especially when working with custom fonts or complex text arrangements in p5.js sketches within React applications.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions