Skip to content

Text metrics #356

Closed
Closed
@scottgarner

Description

@scottgarner

textWidth() seems to only return the correct value after calling text().

function setup() {

  background(0);
  fill(255);
  noStroke();

  textSize(20);
  s = "String.";

  // Returns 28.9013671875
  console.log(textWidth(s));

  text(s, 0, 50);
  // Returns 57.802734375

  console.log(textWidth(s)); 

}

I think it's just a matter of setting this.drawingContext.font before calling this.drawingContext.measureText(), but I'm not sure what would be the preferred approach.

I'm thinking the best plan is to take this out of text(), which is inefficient since there's no reason to set the styling for every call if it hasn't changed, and instead set this.drawingContext.font anytime a relavant property (size, style, font) is updated. This way everything is always up to date for text(), textWidth() and textHeight();

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions