简体   繁体   English

在 Fabric.js 中调整基于文本的对象的大小时,如何防止包含文本的垂直和水平拉伸?

[英]How can I prevent both vertical and horizontal stretching of contained text while resizing text-based objects in Fabric.js?

I want to be able to scale text-based objects (for example with classes and subclasses of IText, Textbox) without stretching the text inside of the object.我希望能够在不拉伸 object 内部的文本的情况下缩放基于文本的对象(例如,使用 IText、文本框的类和子类)。 The interaction of scaling and moving is on user-side (UI), not programmatically (I am working on a drawing editor).缩放和移动的交互是在用户端 (UI),而不是以编程方式(我正在使用绘图编辑器)。

在此处输入图像描述

The behaviour I am looking for is similar to the one in the sticky notes apps: You can scale the paper but this action does not scale your text too.我正在寻找的行为类似于便笺应用程序中的行为:您可以缩放纸张,但此操作也不会缩放您的文本。

I have already checked this, but this is only for horizontal prevention: Fabric.js How to resize IText horizontally without stretching the text我已经检查过了,但这仅用于横向预防: Fabric.js 如何在不拉伸文本的情况下水平调整 IText 的大小

This is neither what I want/mean: Fabric.js: How to set a custom size to Text or IText?这既不是我想要/的意思: Fabric.js:如何将自定义大小设置为 Text 或 IText?

I know that a scaling factor different than 1 implies the stretching of the inner text, and that by changing the width and height I can resize the object while keeping the text unscaled, so I tried updating these during scaling using events listeners.我知道缩放因子不同于 1 意味着内部文本的拉伸,并且通过更改widthheight ,我可以调整 object 的大小,同时保持文本未缩放,因此我尝试在缩放期间使用事件侦听器更新这些。

I tried many combinations of the IText,Textbox with some scaling events, like this one:我尝试了 IText,Textbox 和一些缩放事件的许多组合,比如这个:

fbtext.on('scaling', function() {
                      this.set({
                        width : this.width  * this.scaleX,
                        height: this.height * this.scaleY,
                        scaleX: 1,
                        scaleY: 1,
                      });
                    });

I also tried this with Textbox:我也用文本框试过这个:

fbtext.on('scaling', function() {
                      this.set({
                        height: this.height * this.scaleY,
                      });
                    });

But nothing worked so far.但到目前为止没有任何效果。 I am out of ideas.我没主意了。

 var canvas = new fabric.Canvas('mycanvas'); let textbox = new fabric.Textbox("Sample text", { left: 100, top: 100 }); let lastHeight; const updateTextSize = () => { const controlPoint = textbox.__corner; //mr and ml are the only controlPoints that dont modify textbox height if( controlPoint && controlPoint != "mr" && controlPoint != "ml"){ lastHeight = textbox.height * textbox.scaleY; } textbox.set({ height: lastHeight || textbox.height, scaleY:1 }); canvas.renderAll(); }; textbox.on('scaling', updateTextSize ); textbox.on('editing:entered', updateTextSize); canvas.on('text:changed', updateTextSize); canvas.add(textbox);
 canvas { border: 1px solid black; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/3.3.2/fabric.min.js"></script> <canvas id="mycanvas" width="400" height="400"></canvas>

Here is a code sample that will let you modify the "sticky note size" without streching out the text这是一个代码示例,可让您在不拉伸文本的情况下修改“便笺大小”

Thanks @Ivan, your solution is working for me, with a small glitch.谢谢@Ivan,您的解决方案对我有用,但有一个小故障。 When increasing the size of the text box using corners the text is getting stretched and compressed.当使用角增加文本框的大小时,文本会被拉伸和压缩。 I haven't found the solution of this yet and have currently implemented a workaround to hide all the corner scaling points.我还没有找到解决方案,目前已经实施了一种解决方法来隐藏所有角落缩放点。

box.setControlsVisibility({
bl: false,
br: false,
tl: false,
tr: false
});

http://jsfiddle.net/bkgvewh6/ http://jsfiddle.net/bkgvewh6/

Just to add I am posting this as an answer and not as a comment to your answer as I don't have stack overflow reputation to do so.只是补充一下,我将其发布为答案,而不是作为对您答案的评论,因为我没有堆栈溢出声誉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM