首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将笔划设置在特定的一侧?

如何将笔划设置在特定的一侧?
EN

Stack Overflow用户
提问于 2015-05-14 14:38:38
回答 3查看 1.9K关注 0票数 4

有没有办法只在画布对象的一侧进行描边?例如,我只想对顶部进行描边。

当我应用"strokeWidth:1"时,它适用于所有方面。我没有找到任何属性或方法来解决我的问题。

EN

回答 3

Stack Overflow用户

发布于 2015-05-14 19:40:16

对于已定义的边,它没有任何方法。您可以在图像后面添加一个矩形,使其看起来像一个边框。例如“上边框”宽度10px

代码语言:javascript
复制
var canvas = new fabric.Canvas('c');
var radius = 150;

fabric.Image.fromURL('http://cdn.younghollywood.com/images/stories/newsIMG/wenn/20140901/wenn21338105_46_4145_8.jpg', function(oImg) {
    oImg.scale(1.0).set({
        left: 50,
        top: 50,
        width: 200,
        height: 200
    });

    canvas.add(oImg);
    canvas.renderAll();
});

var border = new fabric.Rect({ 
    width: 200, 
    height: 200+10, 
    left: 50, 
    top: 40,
    fill: "#FFFFFF"
});

canvas.add(border);
canvas.renderAll();

小提琴:http://jsfiddle.net/q6Y6k/11/

否则,请检查以下内容:How to set a stroke-width:1 on only certain sides of SVG shapes?

票数 2
EN

Stack Overflow用户

发布于 2018-03-26 08:59:30

我找到了一个使用strokeDashArray的解决方案。

代码语言:javascript
复制
var canvas = new fabric.Canvas('c');

var border = new fabric.Rect({ 
    width: 200, 
    height: 100, 
    left: 50, 
    top: 40,
    fill: "#FFFFFF",
    strokeWidth: 1,
    stroke: '#333',
    strokeDashArray: [200, 100] // same as [width, height]

});

canvas.add(border);
canvas.renderAll();

jsFiddle的答案是here

基于这个答案,你可以找到任何类型的自定义边框的解决方案。

票数 0
EN

Stack Overflow用户

发布于 2021-10-20 20:54:37

遇到相同的问题。我必须覆盖_render()方法并单独绘制线条,而不是一次性绘制所有线条。

代码语言:javascript
复制
let canvas = new fabric.Canvas("my-canvas");
canvas.setBackgroundColor("#ccc");

fabric.RectWithBorders = fabric.util.createClass(fabric.Rect, {
  type: "rectWithBorders",
  borderTop: true,
  borderBottom: true,
  borderLeft: true,
  borderRight: false,

  initialize: function (options, borderOptions) {
    this.callSuper("initialize", options);
    this.borderTop = borderOptions.borderTop;
    this.borderBottom = borderOptions.borderBottom;
    this.borderLeft = borderOptions.borderLeft;
    this.borderRight = borderOptions.borderRight;
 },
 _render: function (ctx) {
    const originalStroke = this.stroke;
    var rx = this.rx ? Math.min(this.rx, this.width / 2) : 0,
    ry = this.ry ? Math.min(this.ry, this.height / 2) : 0,
    w = this.width,
    h = this.height,
    x = -this.width / 2,
    y = -this.height / 2,
    isRounded = rx !== 0 || ry !== 0,
    k = 1 - 0.5522847498;

// Border Top
ctx.beginPath();
this.stroke = this.borderTop ? originalStroke : null;
ctx.moveTo(x + rx, y);
ctx.lineTo(x + w - rx, y);
isRounded &&
  this.borderRight &&
  ctx.bezierCurveTo(x + w - k * rx, y, x + w, y + k * ry, x + w, y + ry);
ctx.moveTo(x + w, y + h - ry);
ctx.closePath();
this._renderPaintInOrder(ctx);

// Border Right
ctx.beginPath();
this.stroke = this.borderRight ? originalStroke : null;
ctx.moveTo(x + w, y + ry);
ctx.lineTo(x + w, y + h - ry);
isRounded &&
  this.borderBottom &&
  ctx.bezierCurveTo(
    x + w,
    y + h - k * ry,
    x + w - k * rx,
    y + h,
    x + w - rx,
    y + h
  );
ctx.moveTo(x + rx, y + h);
ctx.closePath();
this._renderPaintInOrder(ctx);

// Border Bottom
ctx.beginPath();
this.stroke = this.borderBottom ? originalStroke : null;
ctx.moveTo(x + w - rx, y + h);
ctx.lineTo(x + rx, y + h);
isRounded &&
  this.borderLeft &&
  ctx.bezierCurveTo(x + k * rx, y + h, x, y + h - k * ry, x, y + h - ry);
ctx.moveTo(x, y + ry);
ctx.closePath();
this._renderPaintInOrder(ctx);

// Border Left
ctx.beginPath();
this.stroke = this.borderLeft ? originalStroke : null;
ctx.moveTo(x, y + h - ry);
ctx.lineTo(x, y + ry);
isRounded &&
  this.borderTop &&
  ctx.bezierCurveTo(x, y + k * ry, x + k * rx, y, x + rx, y);
ctx.moveTo(x + w - rx, y);
ctx.closePath();

this._renderPaintInOrder(ctx);
}
});

canvas.add(
  new fabric.RectWithBorders({
   width: 150,
   height: 150,
   left: 25,
   top: 25,
   fill: "transparent",
   strokeWidth: 1,
   stroke: "#333"
   }, 
   {borderTop: false, borderBottom: true,
    borderLeft: true, borderRight: true})
 );

不确定这会对性能造成多大影响。还没有完全测试,因为以后不需要这样做。

对于演示,fabricjs border sides可以检查此代码

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30230903

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档