简体   繁体   English

如何在修改对象后获取Text对象的font-size

[英]How to get Text object font-size after modifying object

How can I get Text object font size after modifying object in fabric.js? 在fabric.js中修改对象后,如何获取Text对象的字体大小?

Below is my code. 以下是我的代码。

var text = new fabric.Text(imgText, {
                left: 10,
                top: 5,
                fontSize: 15,
                fontFamily: 'Verdana',
                fill: 'white'
            });
            text.scaleToWidth(canvas.width * 0.5);
            text.setControlsVisibility(canvasConfig);
            canvas.add(text);
            canvas.renderAll();
var objects = canvas.getActiveObject();

            var obj = objects;
            if (!obj) {
                return;
            }
            //console.log(obj.get('fontSize') *= obj.scaleX); 
            var angle = obj.get('angle');

            var objWidth = obj.get('width') * obj.scaleX;
            var objWidthPercent = objWidth / canvas.width * 100;

            var objHeight = obj.get('height') * obj.scaleY;
            var objHeightPercent = objHeight / canvas.height * 100;

            var bound = obj.getBoundingRect();
            var objLeft = obj.get('left') / canvas.width * 100;
            var objTop = obj.get('top') / canvas.height * 100;

            var newfontsize = obj.fontSize * obj.scaleX;

Above I set default FontSize to 15. then I modify object I can get proper Height, Width, Left, Top, but I am not able to get FontSize. 上面我将默认FontSize设置为15.然后我修改对象我可以获得正确的Height, Width, Left, Top,但我无法获得FontSize。

In backend i set image and text like below screenshot. 在后端我设置图像和文本,如下面的截图。

在此输入图像描述

In frontend what i get like below screenshot. 在前端我得到如下截图。

在此输入图像描述

Below style for image & text on frontend. 下面是前端图像和文字的样式。

element.style {
    left: 64.37%;
    top: 14.54%;
    width: 28.25%;
    height: 14.37%;
    font-size: 63.58px;
    color: #E346FF;
    font-family: Times New Roman;
    position: absolute;
    max-width: 100%;
    z-index: 996;
    max-height: 100%;
}

element.style {
    left: 56.5%;
    top: 0.81%;
    width: 42.86%;
    height: 42.86%;
    max-width: 100%;
    max-height: 100%;
    position: absolute;
    z-index: 995;
    display: block;
    background-image: url(http://10.16.16.101/LabelExtension/pub/media/labelimageuploader/images/image/o/r/orange_38.png);
    background-position: center;
    background-repeat: no-repeat;
    background-size: contain;
}

When i add below code it working perfect but in backend object is getting blur. 当我添加下面的代码时它工作完美但在后端对象变得模糊。

this.canvas.setHeight(300);
this.canvas.setWidth(240);
this.canvas.backgroundColor = '#E8EEF1';
this.canvas.setDimensions({width: '480px', height: '600px'}, {cssOnly: true});

Here is the way to match Fabric.js font transforms to CSS transforms: https://jsfiddle.net/mmalex/evpky3tn/ 以下是将Fabric.js字体转换与CSS转换相匹配的方法: https ://jsfiddle.net/mmalex/evpky3tn/

转换Fabric.js转换为CSS转换

The solution is to match transformations of texts, not trying to adjust font size . 解决方案是匹配文本的转换, 而不是尝试调整字体大小


Step 1 - Prepare scene, compose canvas, group text with rectangle, let user manipulate this group, rotate and scale it. 步骤1 - 准备场景,撰写画布,用矩形分组文本,让用户操作该组,旋转并缩放它。

var canvas = new fabric.Canvas(document.getElementById('c'));

var rect = new fabric.Rect({
    width: 50,
    height: 50,
    left: 90,
    top: 120,
    fill: 'rgba(255,0,0,0.25)'
});

var text = new fabric.Text("123", {
    left: rect.left,
    top: rect.top,
    fontSize: 15,
    fontFamily: 'Verdana',
    fill: 'black'
});

text.scaleToWidth(rect.width);
canvas.add(text);

var group = new fabric.Group([rect, text], {
    originX: 'center',
    originY: 'center',
    angle: 25,
    scaleY: 1.7
});

canvas.add(group);
canvas.renderAll();

Step 2 - prepare DIV style for scaling 第2步 - 准备DIV样式进行缩放

.scaled {  // this style is applied to DIV element with text
    ...
    font-size: 15px; // Fabric.js text is also 15px size
    transform: scale(1, 1); // define initial transform
    transition: all 0.25s linear; // let it animate
    ...
}

Step 3 - Evaluate Fabric.js transforms and apply CSS on DIV element, for example: 第3步 - 评估Fabric.js转换并在DIV元素上应用CSS,例如:

element.style {
    transform: rotate(25deg) scale(1.74774, 2.97116); 
}

The solution (aka button handler) converts Fabric.js transform to CSS transform: 解决方案(也就是按钮处理程序)将Fabric.js转换为CSS转换:

function matchCssTransform() {
    let textScale = text.getTotalObjectScaling();
    let pixelRatio = window.devicePixelRatio;
    let styleStr = `rotate(${group.angle}deg) scale(${textScale.scaleX / pixelRatio}, ${textScale.scaleY / pixelRatio}) `;
    document.getElementById("scaled").style.transform = styleStr;
}

getHeightOfLine(lineIndex) → {Number} : Computes height of character at given position and return fontSize of the character. getHeightOfLine(lineIndex)→{Number}:计算给定位置的字符高度并返回字符的fontSize。 Is it? 是吗? tell me. 告诉我。 It's a method of text class. 这是一种文本类的方法。 http://fabricjs.com/docs/fabric.Text.html#getHeightOfLine http://fabricjs.com/docs/fabric.Text.html#getHeightOfLine

This is the vanilla js solution. 这是vanilla js解决方案。

I am not able to get FontSize. 我无法获得FontSize。

You can achieve fontSize using the computedStyle like 你可以使用computedStyle来实现fontSize

  let style = window.getComputedStyle(text, null).getPropertyValue('font-size');
  let fontSize = parseFloat(style); 

https://developer.mozilla.org/de/docs/Web/API/Window/getComputedStyle https://developer.mozilla.org/de/docs/Web/API/Window/getComputedStyle

In addition to this I recommend you to work with vw and vh 除此之外,我建议你使用vwvh

.text {
  font-size: 10vw;
}

https://web-design-weekly.com/2014/11/18/viewport-units-vw-vh-vmin-vmax/ https://web-design-weekly.com/2014/11/18/viewport-units-vw-vh-vmin-vmax/

All together gives us https://jsfiddle.net/a8woL1eh/ 一起给我们https://jsfiddle.net/a8woL1eh/

The problem is Text object does not change its fontSize property when it is modified or transformed unless set fontSize property manually. 问题是Text对象在修改或转换时不会更改其fontSize属性,除非手动设置fontSize属性。 It's the reason why you get blurred text because despite changed the transform size the main fontSize remaining same. 这就是为什么你得到模糊文本的原因,因为尽管改变了变换大小,但主要的fontSize保持不变。 So possible solution will be somehow changing fontSize property dynamically. 因此,可能的解决方案将以某种方式动态更改fontSize属性。 http://jsfiddle.net/therowf/xkhwagd8/41/ Here I have attached a jsfiddle example form your code. http://jsfiddle.net/therowf/xkhwagd8/41/这里我附上了一个代码的jsfiddle示例。 This code is very similar to your one. 此代码与您的代码非常相似。

Thanks and let me know if this is worked. 谢谢,让我知道这是否有效。 (: (:

 var canvas = new fabric.Canvas(document.getElementById('c')); var imgText ="This is text"; var canvasConfig = true; var text = new fabric.Text(imgText, { left: 10, top: 5, fontSize: 50, fontFamily: 'Verdana', fill: 'black' }); text.scaleToWidth(canvas.width * 0.5); text.setControlsVisibility(canvasConfig); canvas.add(text); canvas.renderAll(); var objects = canvas.getActiveObject(); function moveHandler(evt){ //evt.target.fontSize = 10; var fontSizeX = evt.target.scaleX; var fontSizeY = evt.target.scaleY; if(fontSizeX === fontSizeY){ evt.target.fontSize = fontSizeX*100; console.log(evt.target.fontSize); } } canvas.on('object:scaling', moveHandler); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/2.6.0/fabric.min.js"></script> <canvas id="c" width="400" height="400"></canvas> 

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

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