Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fabric.Text.scaleToWidth is not working properly as expected in v4.6.0+ #7950

Closed
dennisideaonce opened this issue May 18, 2022 · 1 comment
Closed

Comments

@dennisideaonce
Copy link

dennisideaonce commented May 18, 2022

Version

4.6.0 and above [Tried till the latest version of fabric js]

Problem

I want to set the scaleToWidth of a textbox to some value say n. But when it seems not to be correct, its behavior is random sometimes n+1, n+2 or n+3.

What did I try?

http://jsfiddle.net/7zjueqgm/12/

Some code:

var canvas = this.__canvas = new fabric.Canvas('c');
  
var size = 200;
var color = "#ff0000";
var fontScale = 1;

console.log("size to be "+size);
    
    var opt = {
             fill: color,
             stroke: color,
             scaleX: fontScale,
             scaleY: fontScale,
             fontFamily: "Times New Roman",
             fontSize: 40,
             fontWeight: "",
             fontStyle: "",
             textAlign: "center"
          }
    var textObject = new fabric.Text("Hello", opt);
    canvas.add(textObject);
    textObject.setCoords(); // @asturur suggested this in the below mention github issue
    textObject.scaleToWidth(size);
    canvas.renderAll();    
    console.log("new size after render "+textObject.getScaledWidth());

The above code results in:

"size to be 200"
"new size after render 202.25054945054944" // I am expecting this value to be 200 not 202.25054945054944.

Issue related to?

#3719
https://stackoverflow.com/questions/69900924/fabric-js-scaletowidth-not-working-for-text-items-and-resulting-in-different-wid

Any help is highly appreciated.

@ShaMan123
Copy link
Contributor

ShaMan123 commented May 18, 2022

add strokeWidth: 0 default is 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants