【问题标题】:How to add outline to the active text in Fabric.js如何将大纲添加到Fabric.js中的活动文本
【发布时间】:2013-08-18 12:09:56
【问题描述】:

我在 html5 中使用了使用 fabric js 的画布。 我想将轮廓应用于画布上的活动文本。我编写的以下代码工作正常,但问题是当我增加轮廓的厚度时,它会与文本重叠,这意味着文本颜色消失。

activeObject1.stroke = color;
activeObject1.strokeWidth = 5;

另外一件事我无法应用第二个大纲。 我有一个例子,但它不适用于fabricjs。

http://jsfiddle.net/vNWn6/

【问题讨论】:

  • Fabric 仅支持 1 次冲程。 “当我增加轮廓的厚度时,它会覆盖在文本上” 是的,它向内增长。您可以增加文本大小来应对它。
  • 但看起来不会增加轮廓
  • 为什么linewidth属性不支持fabric js中的activeobjects?
  • 任何解决方案了吗?我也在挣扎。

标签: javascript html html5-canvas fabricjs


【解决方案1】:

Fabric.js 首先应用填充,然后是描边。您需要颠倒顺序才能获得结果。

 original
  _renderText: function(ctx) {
  this._renderTextFill(ctx);
  this._renderTextStroke(ctx);
  }

Before

  modified
   _renderText: function(ctx) {
  this._renderTextStroke(ctx);
  this._renderTextFill(ctx);
  }

After

版本:fabric-1-7-1.js

【讨论】:

    【解决方案2】:
    fabric.Text.prototype.set({
        fill: '#000',
        stroke: '#fff',
        strokeWidth: 4,
        paintFirst: 'stroke', // stroke behind fill
    })
    

    自从2017-09-17

    【讨论】:

      【解决方案3】:
      var active_obj = canvas.getActiveObject();
      active_obj.stroke = '#000';
      active_obj.strokeWidth = 5;
      canvas.renderAll();
      

      【讨论】:

      • 好的答案解释并提供代码。考虑更新您的答案,以包含有关此代码如何工作以及为什么它是最佳选择的说明。
      猜你喜欢
      • 2012-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-07
      • 1970-01-01
      • 2019-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多