最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Fabricjs. image with rounded corner and stroke - Stack Overflow

programmeradmin5浏览0评论

Is there a simple way of clipping an image with rounded corner and inner stroke?

/

$('#clip').click(function() {
    canvas._objects[1].set({
        'clipTo': function(ctx) {
            var rect = new fabric.Rect({
                left: 0,
                top: 0,
                rx: 20 / this.scaleX,
                ry: 20 / this.scaleY,
                width: this.width,
                height: this.height,
                fill: '#000000'
            });
            rect._render(ctx, false);
        }
    });
    canvas.renderAll();
});

Is there a simple way of clipping an image with rounded corner and inner stroke?

https://jsfiddle/4tursk3y/

$('#clip').click(function() {
    canvas._objects[1].set({
        'clipTo': function(ctx) {
            var rect = new fabric.Rect({
                left: 0,
                top: 0,
                rx: 20 / this.scaleX,
                ry: 20 / this.scaleY,
                width: this.width,
                height: this.height,
                fill: '#000000'
            });
            rect._render(ctx, false);
        }
    });
    canvas.renderAll();
});
Share Improve this question asked Apr 29, 2019 at 21:45 John MagnoliaJohn Magnolia 16.8k39 gold badges169 silver badges274 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You can use a rectangle with a pattern fill to get rounded image corners without clipping the stroke.

var rect = new fabric.Rect({
  left: 10,
  top: 10,
  width: 140,
  height: 215,
  stroke: 'red',
  strokeWidth: 3,
  rx:10,
  ry:10
});
canvas.add(rect);

fabric.util.loadImage('http://fabricjs./assets/pug.jpg', function (img) {
  rect.setPatternFill({
    source: img,
    repeat: 'no-repeat',
    patternTransform: [0.2, 0, 0, 0.2, 0, 0]
  });
  canvas.renderAll();
});

https://jsfiddle/melchiar/78nt10ua/

for newer fabric versions use this

const roundedCorners = (fabricObject, cornerRadius) => new fabric.Rect({
 width: fabricObject.width,
 height: fabricObject.height,
 rx: cornerRadius / fabricObject.scaleX,
 ry: cornerRadius / fabricObject.scaleY,
 left: -fabricObject.width / 2,
 top: -fabricObject.height / 2
})

image.set("clipPath", roundedCorners(image, 20))
发布评论

评论列表(0)

  1. 暂无评论