How to give Text a bounding box and scale the text to fit within that bounding box?

1 Answer 1182 Views
Drawing
Ryan
Top achievements
Rank 1
Ryan asked on 18 Apr 2022, 05:13 PM

Hi folks,

I've got a question about the Drawing utilities in Kendo React. Specifically, I'm drawing some Text on the Surface and would like that Text to scale to fit within a particular "bounding box".

When creating the Text object, I do not see any option or concept of a bounding box. I only see the ability to set the starting point to draw the Text at and then a way to set the Text's CSS Font properties.

While I could try to come up with some code to calculate a font size that would scale that Text up given a bounding box dimension, I was curious if Kendo React had any support or concept of this already built in? Or perhaps a suggestion on how to do this?

Attached is a screen shot of the Text that I am drawing and then an additionally-drawn bounding box. The box and Text have no relation - they're just two things I'm drawing right now. This shows the box in which I'd like the Text to scale up to fit into, though.

Any ideas? Thanks.

1 Answer, 1 is accepted

Sort by
1
Accepted
Filip
Telerik team
answered on 20 Apr 2022, 08:26 AM

Hi, Ryan,

Thank you for reaching out to us and for the provided image.

Based on the picture, a possible solution would be to use the fit property which scales uniformly an element so that it fits in a given rectangle.

I hope this helps.

Regards, FilipProgress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Ryan
Top achievements
Rank 1
commented on 20 Apr 2022, 09:06 PM

Hello,

I'm unsure how to use the `fit` method correctly. Your post refers to `fit` as a property, but the only `fit` I found is a method. I'm trying to call the `fit` method - passing it in my `Text` instance and giving it the bounding box `Rect`. Nothing seems to change, though. The `Text` size does not change - it looks like it did before and the same as if I were to comment out the call to `fit` altogether.

Does the below code look like a correct way to use `fit`?


      const vertices = m.BoundingBox.Vertices.$values;
      const position = new geometry.Point(vertices[0].X, vertices[0].Y);

      const text = new Text(m.Text, position, {
        stroke: {
          color: fontColor
        }
      });

      // const bboxWidth = vertices[1].
      const bboxDimensions = getBoundingBoxDimensions(vertices);
      const bbox = new geometry.Rect([0, 0], bboxDimensions);
      fit(text, bbox);

Filip
Telerik team
commented on 21 Apr 2022, 11:13 AM

Hello, Ryan,

My apologies it is indeed a method. That should be the way to use it indeed. Can you provide a working example that reproduces your issue so that I can inspect it further and see why it is not working?

Regards,Filip

Ryan
Top achievements
Rank 1
commented on 21 Apr 2022, 07:35 PM | edited

Filip,

I put together a working example that reproduces the way that I'm using the fit method. You can see that it doesn't seem to do anything. If you change the dimensions of the bounding box the text does not scale to fill it up.

Any ideas?

https://stackblitz.com/edit/react-ts-rob7wj?file=index.tsx

Konstantin Dikov
Telerik team
commented on 22 Apr 2022, 08:34 AM

Hi Ryan,

Thank you for sharing the example.

As stated in the documentation, if the element passed to the fit function is already small enough to fit in the rectangle, there will be no changes applied.

In order to scale up the Text element you can use the transform.scale instead:

Based on the exact requirement, you can change the logic and keep the ratio of the original Text the same by unifying the scale on x and y (using the smaller of the two values: rect.x/text.x and rect.y/text.y. Using the smaller value will ensure that the Text will fit in the rectangle after the scaling.

Hope this helps.

 

Ryan
Top achievements
Rank 1
commented on 22 Apr 2022, 03:53 PM

That works! Thank you so much. I didn't think to scale it using a transform on the Text object.
Tags
Drawing
Asked by
Ryan
Top achievements
Rank 1
Answers by
Filip
Telerik team
Share this question
or