简体   繁体   English

我可以在Fabric.js中创建更复杂的形状吗?

[英]Can I create more complex shapes like this one in Fabric.js?

I'm creating a simple graphic editor with JavaScript/Fabric.js and I need to be able to create somewhat more complex shape than the basic ones (line, triangle, circle, ellipse, square, rectangle ) like this one: 我正在使用JavaScript / Fabric.js创建一个简单的图形编辑器,我需要能够创建比基本形状(线,三角形,圆形,椭圆形,正方形,矩形)更复杂的形状,如下所示:

fabricjs椭圆组描边填充描边宽度

The green lines are the border of the shape and the black is the fill. 绿线是形状的边界,黑线是填充。 I need to be able to manipulate the border size/color and fill color like I can for the basic shapes. 我需要能够像基本形状一样操纵边框的大小/颜色和填充颜色。 Is this possible in fabric.js? 在fabric.js中可以吗? If yes, could you point me in the direction I should look at because currently I have no clue how it could be done. 如果是的话,您能指出我应该看的方向,因为目前我不知道如何完成。

This should get you started. 这应该使您入门。

fabricjs椭圆组描边填充描边宽度

JavaScript: JavaScript的:

var canvas = new fabric.Canvas('c');
var defaultProperties = {
  fill: 'black',
  stroke: 'lime',
  strokeWidth: 12
};
var ellipse = new fabric.Ellipse({
  left: 30,
  top: 30,
  rx: 170,
  ry: 110,
  strokeWidth: 12
});
var horizontalLine = new fabric.Line([30, 140, 370, 140]);
var verticalLine = new fabric.Line([200, 30, 200, 140]);
var group = new fabric.Group([ellipse, horizontalLine, verticalLine]);
group.set(defaultProperties);
canvas.add(group);
var toggle = true;
document.getElementsByTagName('button')[0].addEventListener('click', function() {
  if (toggle) {
    group.set({
      fill: 'red',
      stroke: 'pink',
      strokeWidth: 5
    });
  } else {
    group.set(defaultProperties);
  }
  toggle = !toggle;
  canvas.renderAll();
});

And the all important JSFiddle: https://jsfiddle.net/rekrah/mj5c2bx0/ . 以及所有重要的JSFiddle: https ://jsfiddle.net/rekrah/mj5c2bx0/。

Let me know if you have any further questions. 如果您还有其他问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM