SimonW Posted March 1, 2016 Share Posted March 1, 2016 I'm trying to add labels to polygons, and I was hoping I would be able to limit the label to the size of the polygon by setting label.width to polygon.getBounds().width. Unfortunately that doesn't seem to work. Setting the text width is certainly doing something, but it isn't setting it to the width of the polygon. Example here:https://jsfiddle.net/yvk256L1/5/ As you can see, making the string longer doesn't change anything. Different fonts give different widths. The scale does seem to be proportional to the width of the rectangle at least. Quote Link to comment Share on other sites More sharing options...
samlancashire Posted March 1, 2016 Share Posted March 1, 2016 You need to set the width of the string to the rectangles width after you scale it. Quote Link to comment Share on other sites More sharing options...
SimonW Posted March 1, 2016 Author Share Posted March 1, 2016 All the scale is doing is adjusting the height to keep the aspect ratio constant. It shouldn't affect the width of the text (note that it's not changing scale.x). Quote Link to comment Share on other sites More sharing options...
fire7side Posted March 1, 2016 Share Posted March 1, 2016 You are adding it as a child which might be throwing it off. I'm just starting to use Pixi, though. The bounds are probably global on the rectangle. Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted March 2, 2016 Share Posted March 2, 2016 I dont see why is that, yet. But I'll crack it soon Quote Link to comment Share on other sites More sharing options...
fire7side Posted March 2, 2016 Share Posted March 2, 2016 You can get the width of the rectangle bounds and divide it by the width of the text, and use that to scale it. Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted March 2, 2016 Share Posted March 2, 2016 9 hours ago, fire7side said: You can get the width of the rectangle bounds and divide it by the width of the text, and use that to scale it. He's doing the same. When assigning width, scale.x is changing. Quote Link to comment Share on other sites More sharing options...
fire7side Posted March 2, 2016 Share Posted March 2, 2016 I tried it and it worked, but I only tried it on the first rectangle. They were identical in length. Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted March 2, 2016 Share Posted March 2, 2016 Yeah, right. Its a pixi bug. Need to update width first, then to assign it ) SOLUTION: rect1.addChild(centerLabel1); centerLabel1.anchor = new PIXI.Point(0.5, 0.5); console.log(centerLabel1.width); // WAY 1 - THIS IS UPDATING centerLabel1.width centerLabel1.updateText(); // WAY 2 - THIS IS UPDATING IT TOO centerLabel1.width = bounds1.width; centerLabel1.scale.y = centerLabel1.scale.x; SimonW 1 Quote Link to comment Share on other sites More sharing options...
SimonW Posted March 2, 2016 Author Share Posted March 2, 2016 Excellent, thanks. I tried an update function they mentioned in response to another bug report, but I guess it was the wrong one. I should probably give up on the documentation and just read the source code. Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted March 2, 2016 Share Posted March 2, 2016 its a real bug - updateText should be called inside setter of width Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.