• <bdo id='rC5ec'></bdo><ul id='rC5ec'></ul>
  1. <i id='rC5ec'><tr id='rC5ec'><dt id='rC5ec'><q id='rC5ec'><span id='rC5ec'><b id='rC5ec'><form id='rC5ec'><ins id='rC5ec'></ins><ul id='rC5ec'></ul><sub id='rC5ec'></sub></form><legend id='rC5ec'></legend><bdo id='rC5ec'><pre id='rC5ec'><center id='rC5ec'></center></pre></bdo></b><th id='rC5ec'></th></span></q></dt></tr></i><div id='rC5ec'><tfoot id='rC5ec'></tfoot><dl id='rC5ec'><fieldset id='rC5ec'></fieldset></dl></div>
      <legend id='rC5ec'><style id='rC5ec'><dir id='rC5ec'><q id='rC5ec'></q></dir></style></legend>
      <tfoot id='rC5ec'></tfoot>

      <small id='rC5ec'></small><noframes id='rC5ec'>

    1. Fabric.js 的文本框不换行

      Textbox of Fabric.js does not wrap a long word(Fabric.js 的文本框不换行)
        <tbody id='oJk4i'></tbody>

      <legend id='oJk4i'><style id='oJk4i'><dir id='oJk4i'><q id='oJk4i'></q></dir></style></legend>
      <i id='oJk4i'><tr id='oJk4i'><dt id='oJk4i'><q id='oJk4i'><span id='oJk4i'><b id='oJk4i'><form id='oJk4i'><ins id='oJk4i'></ins><ul id='oJk4i'></ul><sub id='oJk4i'></sub></form><legend id='oJk4i'></legend><bdo id='oJk4i'><pre id='oJk4i'><center id='oJk4i'></center></pre></bdo></b><th id='oJk4i'></th></span></q></dt></tr></i><div id='oJk4i'><tfoot id='oJk4i'></tfoot><dl id='oJk4i'><fieldset id='oJk4i'></fieldset></dl></div>

      1. <small id='oJk4i'></small><noframes id='oJk4i'>

          <bdo id='oJk4i'></bdo><ul id='oJk4i'></ul>
          • <tfoot id='oJk4i'></tfoot>

                本文介绍了Fabric.js 的文本框不换行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用 Fabric.jsTextbox.我给了一个固定的宽度.但是,如果用户键入一个没有任何空格的长单词,超过了给定的文本框宽度,则它不会换行.

                I am using the Textbox of Fabric.js. I have given a fixed width. But if a user types a long word without any space that exceeds the given width of textbox, it does not wrap.

                有什么办法吗?

                推荐答案

                是的,有一个你可能喜欢或不喜欢的解决方案来实现分词:

                Yes there is a solution that you may like or not to implement word breaking:

                覆盖fabric默认的换行函数:

                override the fabric default function for line breaking:

                fabric.Textbox.prototype._wrapLine = function(ctx, text, lineIndex) {
                var lineWidth        = 0,
                    lines            = [],
                    line             = '',
                    words            = text.split(' '),
                    word             = '',
                    letter           = '',
                    offset           = 0,
                    infix            = ' ',
                    wordWidth        = 0,
                    infixWidth       = 0,
                    letterWidth      = 0,
                    largestWordWidth = 0;
                
                for (var i = 0; i < words.length; i++) {
                    word = words[i];
                    wordWidth = this._measureText(ctx, word, lineIndex, offset);
                    lineWidth += infixWidth;
                
                    // Break Words if wordWidth is greater than textbox width
                    if (this.breakWords && wordWidth > this.width) {
                        line += infix;
                        var wordLetters = word.split('');
                        while (wordLetters.length) {
                            letterWidth = this._getWidthOfChar(ctx, wordLetters[0], lineIndex, offset);
                            if (lineWidth + letterWidth > this.width) {
                                lines.push(line);
                                line = '';
                                lineWidth = 0;
                            }
                            line += wordLetters.shift();
                            offset++;
                            lineWidth += letterWidth;
                        }
                        word = '';
                    } else {
                        lineWidth += wordWidth;
                    }
                
                    if (lineWidth >= this.width && line !== '') {
                        lines.push(line);
                        line = '';
                        lineWidth = wordWidth;
                    }
                
                    if (line !== '' || i === 1) {
                        line += infix;
                    }
                    line += word;
                    offset += word.length;
                    infixWidth = this._measureText(ctx, infix, lineIndex, offset);
                    offset++;
                
                    // keep track of largest word
                    if (wordWidth > largestWordWidth && !this.breakWords) {
                        largestWordWidth = wordWidth;
                    }
                }
                
                i && lines.push(line);
                
                if (largestWordWidth > this.dynamicMinWidth) {
                        this.dynamicMinWidth = largestWordWidth;
                    }
                
                    return lines;
                };
                

                用法:

                var breakingTextbox = new fabric.Textbox(longText, {
                        width: 200,
                        breakWords: true
                });
                

                这篇关于Fabric.js 的文本框不换行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                How to make rooftext effect and valley text effect in HTML5 (or Fabric.js)(如何在 HTML5(或 Fabric.js)中制作屋顶文字效果和山谷文字效果)
                Draw border around nontransparent part of image on canvas(在画布上的图像不透明部分周围绘制边框)
                dragging and resizing an image on html5 canvas(在 html5 画布上拖动图像并调整其大小)
                What#39;s the difference between a boolean as primitive and a boolean as property of an object?(作为原始对象的布尔值和作为对象属性的布尔值有什么区别?)
                I want to do animation of an object along a particular path(我想沿特定路径对对象进行动画处理)
                How to upload image into HTML5 canvas(如何将图像上传到 HTML5 画布中)

                1. <legend id='I1wiI'><style id='I1wiI'><dir id='I1wiI'><q id='I1wiI'></q></dir></style></legend>
                2. <tfoot id='I1wiI'></tfoot>

                  <small id='I1wiI'></small><noframes id='I1wiI'>

                      <bdo id='I1wiI'></bdo><ul id='I1wiI'></ul>

                      1. <i id='I1wiI'><tr id='I1wiI'><dt id='I1wiI'><q id='I1wiI'><span id='I1wiI'><b id='I1wiI'><form id='I1wiI'><ins id='I1wiI'></ins><ul id='I1wiI'></ul><sub id='I1wiI'></sub></form><legend id='I1wiI'></legend><bdo id='I1wiI'><pre id='I1wiI'><center id='I1wiI'></center></pre></bdo></b><th id='I1wiI'></th></span></q></dt></tr></i><div id='I1wiI'><tfoot id='I1wiI'></tfoot><dl id='I1wiI'><fieldset id='I1wiI'></fieldset></dl></div>
                          <tbody id='I1wiI'></tbody>