【问题标题】:Fabric js Text characters garbled on iPhone and iPadFabric js文本字符在iPhone和iPad上出现乱码
【发布时间】:2017-12-14 02:53:48
【问题描述】:

我们正在使用 Fabric js 在画布中插入文本,我们正在插入 2 个文本,一个是正确加载且具有 Open Sans 字体的较大文本。另一个是较小的文本,这是我们遇到问题的地方,它具有从谷歌字体库动态加载的动态字体。它不显示字符,而是在框中显示问号。我们尝试使用 Open Sans 字体,但没有任何变化。

如需进一步说明,您可以查看以下链接 我使用了以下代码,这会产生问题

_preloadText: function(index,value)
        {
            var fontSize = value.default;
            var coords = this._getXYCoordinates(value.coordinates);
            var fontFamily = this.options.fontFamily;
            var options = $.extend({
                            fontFamily: fontFamily,
                            fontSize: fontSize,
                            useNative:true
                        },this._getOptions(coords.x,coords.y));
            if('text' == value.type)
            {
                options.fontStyle = 'bold';
                options.fontFamily = 'Open Sans';
                var title = new fabric.Text(value.text,options);
                this.canvasLayer2.add(title);
                this.canvasLayer2.centerObjectH(title);
                this.canvasLayer2.setActiveObject(title);
                this.canvasLayer2.renderAll();
            }else{
                /*The below code is where the problem of fallback font is.*/
                var slogan = new fabric.Text(value.text,options);
                this.canvasLayer3.add(slogan);
                this.canvasLayer3.centerObjectH(slogan);
                this.canvasLayer3.setActiveObject(slogan);
                this.canvasLayer3.renderAll();
            }           
        }

还有其他人也遇到过这个问题。您可以通过在 iPhone 或 iPad 中打开以下 codepen 来检查 https://codepen.io/kelvin-im/pen/xrRNEx/

我也在fabric js github账号中发现了这个问题 https://github.com/kangax/fabric.js/issues/4009

但开发者在没有向 cmets 支付任何隐藏信息的情况下突然关闭了问题。

编辑

这是初始化这个 jquery 小部件时首先调用的字体加载代码。

_prepareFont: function(){
            var that = this;
            if(this.options.fontFamily !== null){
                require(['https://ajax.googleapis.com/ajax/libs/webfont/1.6.26/webfont.js'],function(WebFont){
                    WebFont.load({
                        google: {
                            families: [that.options.fontFamily]
                        },
                        active: function() {
                            that._prepareCanvas();
                            that._prepareControls();                            
                        },
                        fontactive: function(familyName, fvd){
                            that.options.fontFamily = familyName;
                        }
                    });
                });
            }
        }

在 WebFont 库中,我们正在加载字体,在活动事件中,我们正在调用 _prepareControls 方法,它将文本预加载到画布上。如果我们没有从 google WebFont Library 中获得所需的字体,我们使用的是 Open Sans 字体,该字体已添加到页面中。

我们使用了 fabric.Text 类的 2 个实例,一个正在加载,另一个没有。

【问题讨论】:

    标签: ios css iphone ipad fabricjs


    【解决方案1】:

    突然的开发者我问了两次字体加载代码。 在尝试在画布上使用字体之前,必须由浏览器加载字体。

    否则您会在屏幕上看到后备字体或垃圾。

    现在我把 codepen 移到了这里,我在 2 秒的超时时间内包裹了标题。 您会注意到,在 iphone/ipad 或其他速度较慢的设备上,这 2 秒应该足以让 google 服务器首先为您提供 css 字符串,然后是浏览器需要获取字体文件和加载画布的第二次请求它,而其他文本可能作为标题不应该出现的垃圾。

    $(document).ready(function() {
        $('.browser-type').html(bowser.name + ' ' + bowser.version);    
      
        var canvas = new fabric.Canvas('c');
      
        $('.fabric-type').html(fabric.version);
      
        var headline = new fabric.IText('Headline', {
          "originX": "center",
          "originY": "center",
          "left": 512,
          "top": 117,
          "width": 83.6,
          "height": 56.5,
          "fill": "#ffffff",
          "stroke": null,
          "strokeWidth": 0,
          "strokeDashArray": null,
          "strokeLineCap": "butt",
          "strokeLineJoin": "miter",
          "strokeMiterLimit": 10,
          "scaleX": 0.8,
          "scaleY": 1,
          "angle": 0,
          "flipX": false,
          "flipY": false,
          "opacity": 1,
          "shadow": "",      
          "visible": true,
          "clipTo": null,
          "backgroundColor": "",
          "fillRule": "nonzero",
          "globalCompositeOperation": "source-over",
          "transformMatrix": null,
          "skewX": 0,
          "skewY": 0,
          "name": "Headline txt",
          "locked": ["stroke"],
          "perPixelTargetFind": false,
          "fontSize": 165,
          "fontWeight": "800",
          "fontFamily": "Open Sans",
          "fontStyle": "italic",
          "lineHeight": 1.16,
          "textDecoration": "",
          "textAlign": "center",
          "textBackgroundColor": "",
          "styles": {
            "0": {
              "1": {},
              "2": {},
              "3": {},
              "4": {},
              "5": {},
              "6": {},
              "7": {}
            }
          }
        });
      
        var dateTime = new fabric.IText('Date | Time', {
          "originX": "center",
          "originY": "center",
          "left": 512,
          "top": 206,
          "width": 170.31,
          "height": 31.64,
          "fill": "#ffffff",
          "stroke": null,
          "strokeWidth": 0,
          "strokeDashArray": null,
          "strokeLineCap": "butt",
          "strokeLineJoin": "miter",
          "strokeMiterLimit": 10,
          "scaleX": 0.9,
          "scaleY": 1,
          "angle": 0,
          "flipX": false,
          "flipY": false,
          "opacity": 1,
          "shadow": "",
          "visible": true,
          "clipTo": null,
          "backgroundColor": "",
          "fillRule": "nonzero",
          "globalCompositeOperation": "source-over",
          "transformMatrix": null,
          "skewX": 0,
          "skewY": 0,
          "name": "date/time",
          "locked": ["stroke"],
          "perPixelTargetFind": false,
          "fontSize": 28,
          "fontWeight": "400",
          "fontFamily": "Open Sans",
          "fontStyle": "italic",
          "lineHeight": 1.16,
          "textDecoration": "",
          "textAlign": "center",
          "textBackgroundColor": "",
          "styles": {
            "0": {
              "1": {},
              "2": {},
              "3": {},
              "4": {}
            }
          }
        });
      
        var vsText = new fabric.IText('vs.', {
          "originX": "center",
          "originY": "center",
          "left": 512,
          "top": 255,
          "width": 83.6,
          "height": 56.5,
          "fill": "#ffffff",
          "stroke": null,
          "strokeWidth": 0,
          "strokeDashArray": null,
          "strokeLineCap": "butt",
          "strokeLineJoin": "miter",
          "strokeMiterLimit": 10,
          "scaleX": 0.8,
          "scaleY": 1,
          "angle": 0,
          "flipX": false,
          "flipY": false,
          "opacity": 1,
          "shadow": "",
          "visible": true,
          "clipTo": null,
          "backgroundColor": "",
          "fillRule": "nonzero",
          "globalCompositeOperation": "source-over",
          "transformMatrix": null,
          "skewX": 0,
          "skewY": 0,
          "name": "versus txt",
          "locked": ["stroke"],
          "perPixelTargetFind": false,
          "fontSize": 50,
          "fontWeight": "400",
          "fontFamily": "RIVERHACK",
          "fontStyle": "",
          "lineHeight": 1.16,
          "textDecoration": "",
          "textAlign": "left",
          "textBackgroundColor": "",
          "styles": {
            "0": {
              "1": {},
              "2": {},
              "3": {}
            }
          }
        });
      
        var sideText = new fabric.IText('SIDEBAR', {
          "originX": "center",
          "originY": "center",
          "left": 50,
          "top": 30,
          "width": 90.72,
          "height": 36.21,
          "fill": "#0c0c0c",
          "stroke": null,
          "strokeWidth": 0,
          "strokeDashArray": null,
          "strokeLineCap": "butt",
          "strokeLineJoin": "miter",
          "strokeMiterLimit": 10,
          "scaleX": 1,
          "scaleY": 1,
          "angle": 0,
          "flipX": false,
          "flipY": false,
          "opacity": 1,
          "shadow": "",
          "visible": true,
          "clipTo": null,
          "backgroundColor": "",
          "fillRule": "nonzero",
          "globalCompositeOperation": "source-over",
          "transformMatrix": null,
          "skewX": 0,
          "skewY": 0,
          "name": "sidebar txt",
          "locked": ["stroke"],
          "perPixelTargetFind": false,
          "fontSize": 18,
          "fontWeight": "400",
          "fontFamily": "Open Sans",
          "fontStyle": "",
          "lineHeight": 0.78,
          "textDecoration": "",
          "textAlign": "center",
          "textBackgroundColor": "",
          "styles": {
            "0": {
              "1": {},
              "2": {},
              "3": {},
              "4": {},
              "5": {},
              "6": {},
              "7": {},
              "8": {}
            },
            "1": {
              "0": {},
              "1": {},
              "2": {},
              "3": {},
              "4": {}
            }
          }
        });
        
        canvas.add(sideText);
        canvas.add(dateTime);
        canvas.add(vsText);
        setTimeout(function() {
            canvas.add(headline);
        }, 2000);
    });
    @import 
    url(https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i,800,800i);
    
    #c {
      border: 1px solid #ccc;
      background-color: #aaa;
      box-shadow: 3px 3px 3px #999;
      position: relative;
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/bowser/1.0.0/bowser.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/fabric.js/1.7.13/fabric.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <div>
      <b>Browser:</b> <span class="browser-type"></span><br>
      <b>Fabric JS:</b> <span class="fabric-type"></span>
      <br><br>
      
      <div>
        <canvas id='c' width="1024" height="1024"></canvas>
      </div>  
    </div>

    在野生动物园中:

    【讨论】:

    • 让我在问题中编辑和更新字体加载代码,我正在从 google webfont js 加载字体后初始化画布。
    • 你能在 iPhone 上看到这个 sn-p 的喜爱吗?
    • 很遗憾,我们无法在 SO 中的 iPhone 上运行 sn-p。
    • 我已经使用您的代码创建了一支笔,这里的网址是相同的:codepen.io/taralpatoliya/pen/PjxMZW,它在较小的文本中显示乱码。
    • 这样可以确认您的应用需要更多时间来加载字体。因为额外的时间只应用于大标题文本。使用更好的字体加载器。试试 webFontloader 体积小,跨浏览器,易于集成
    猜你喜欢
    • 2017-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 2011-05-01
    • 1970-01-01
    相关资源
    最近更新 更多