Skip to content

[p5.js 2.0+ Bug Report]: fontWidth() doesn't work with push/pop in WebGL #8746

@davepagurek

Description

@davepagurek

Most appropriate sub-area of p5.js?

  • Accessibility
  • Color
  • Core/Environment/Rendering
  • Data
  • DOM
  • Events
  • Image
  • IO
  • Math
  • Typography
  • Utilities
  • WebGL
  • WebGPU
  • p5.strands
  • Build process
  • Unit testing
  • Internationalization
  • Friendly errors
  • Other (specify if possible)

p5.js version

2.2.3

Web browser and version

All

Operating system

All

Steps to reproduce this

Steps:

  1. Set a font and text size
  2. push()
  3. Change the text size
  4. Measure something with fontWidth(...)
  5. pop()
  6. Measure something again

The measurements should be different after the pop since the text size should return to normal. This works when in 2D mode, but doesn't in WebGL mode. I think that's because in 2D mode, the text measurement canvas IS the main canvas, so pushes and pops apply to it. In WebGL mode, it is a separate canvas, so there are no pushes/pops.

Snippet:

let font
async function setup() {
  createCanvas(400, 400, WEBGL); // Try without WEBGL too
  font = await loadFont('https://fonts.gstatic.com/s/inter/v20/UcCO3FwrK3iLTeHuS_nVMrMxCp50SjIw2boKoduKmMEVuLyfMZhrib2Bg-4.ttf')
  
  push()
  textFont(font)
  textSize(12)
  push()
  textSize(20)
  console.log(fontWidth('X'))
  pop()
  console.log(fontWidth('X'))
  pop()
}

Live: https://editor.p5js.org/davepagurek/sketches/Am5XG1E1O

In WebGL, I get the same value logged twice. In 2D mode, I get different values.

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions