How to get Text object font-size after modifying object

后端 未结 4 2009
旧时难觅i
旧时难觅i 2021-01-18 06:10

How can I get Text object font size after modifying object in fabric.js?

Below is my code.

var text = new fabric.Text(imgText, {
            


        
相关标签:
4条回答
  • 2021-01-18 06:37

    getHeightOfLine(lineIndex) → {Number} : Computes height of character at given position and return fontSize of the character. Is it? tell me. It's a method of text class. http://fabricjs.com/docs/fabric.Text.html#getHeightOfLine

    0 讨论(0)
  • 2021-01-18 06:38

    The problem is Text object does not change its fontSize property when it is modified or transformed unless set fontSize property manually. It's the reason why you get blurred text because despite changed the transform size the main fontSize remaining same. So possible solution will be somehow changing fontSize property dynamically. http://jsfiddle.net/therowf/xkhwagd8/41/ Here I have attached a jsfiddle example form your code. 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>

    0 讨论(0)
  • 2021-01-18 06:51

    Here is the way to match Fabric.js font transforms to CSS transforms: https://jsfiddle.net/mmalex/evpky3tn/

    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.

    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

    .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:

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

    The solution (aka button handler) converts Fabric.js transform to CSS transform:

    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;
    }
    
    0 讨论(0)
  • 2021-01-18 06:55

    This is the vanilla js solution.

    I am not able to get FontSize.

    You can achieve fontSize using the computedStyle like

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

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

    In addition to this I recommend you to work with vw and vh

    .text {
      font-size: 10vw;
    }
    

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

    All together gives us https://jsfiddle.net/a8woL1eh/

    0 讨论(0)
提交回复
热议问题