0

I want to draw Bezier, Used Fabric.js Path method, The question is Path will connect the beginning to the end, But I don't want to do that.

enter image description here

Fabian Schultz
  • 18,138
  • 5
  • 49
  • 56
Charles
  • 3
  • 1
  • 7

1 Answers1

1

For your question that Path will connect the beginning to the end.

Yes it will connect.

But I don't want to do that.

Though there is nothing as of now using which you can avoid this behavior. But yes there is a workaround using which your path wont look as if the beginning and end are joined. See the demo below. There are two paths one which shows the beginning and end joined(the triangle). Second where the end and beginning do not appear to be joined(the arrow). The difference between these two is just the fill property set to false in the second path i.e The arrow. But if you omit the fill property or set it to some color it will look as similar to the first path(triangle) with joint end and beginning. Hence you can create a bezier curve as required by you.

// initialize fabric canvas and assign to global windows object for debug
var canvas = window._canvas = new fabric.Canvas('c');
//var canvas = new fabric.StaticCanvas('c');
var path = new fabric.Path('C 50 50 100 100 150 50 C 200 100 250 50 300 100', {
left: 100,
top: 50,
stroke: 'red',
strokeWidth: 1,
fill: false,
scaleY:3
});
canvas.add(path);


var p = new fabric.Path('C 50 50 100 100 150 50 C 200 100 250 50 300 100',{
  left: 200,
  top: 60,
  strokeWidth:2,
  scaleY:3
});

canvas.add(p);
<script src="https://rawgithub.com/kangax/fabric.js/master/dist/fabric.js"></script>
<canvas id="c" width="600" height="600"></canvas>

Hope it helps :)

UPDATE

Fiddle

Above is the fiddle link in this you will see two paths being created and the third one is not getting created because the path string passed is not valid.

Also can you please create a demo in fiddle with the actual problem you are facing. Would be easy for me to help and understand the issue.

Manish
  • 4,692
  • 3
  • 29
  • 41
  • Sorry,Because some things,So long to reply you,I used your method,But still failed to achieve the desired results,That's how I draw the Bessel curve,For example: var path = new fabric.Path('C 50 50 100 100 150 50C200 100 250 50 300 100') – Charles Jan 07 '17 at 11:58
  • @Charles have updated the answer according to your path. Let me know if you still face issues. – Manish Jan 07 '17 at 14:12
  • Points is the data requested by the interface,But I don't know why it's not showing up on Fiddle,Can you help me see it, thank you! [link](http://jsfiddle.net/Charles_/3fk61ueb/3/) – Charles Jan 09 '17 at 12:20
  • i think the issue is with your data only. new fabric.Path('C 50 50 100 100 150 50C200 100 250 50 300 100') if you see here 50C200 is together but there shoud be a space between 50 and C and C AND 200. Will create a fiddle. – Manish Jan 09 '17 at 12:26
  • Thank you for your patience, the problem is solved, The reason is that more than a 'z'. – Charles Jan 10 '17 at 03:08
  • I want to adjust the `zIndex` of the element, With the `moveto()`,But it seems like you need to judge,Is not required to use`getObjects()`or`size()`,I don't know how to use these two methods,Can you tell me that it's better to be a code case.thanks – Charles Jan 10 '17 at 14:24
  • can you please elaborate a bit i didn't get what you are trying to ask? – Manish Jan 10 '17 at 18:37
  • I want to add the elements to the drawing, I use `moveto()`,Bt it needs `if`judge, look like ` getObjects()` or `size()`, I don't know how to use it. Why can't you notice a new topic? I use **@** . what chat software do you use, can you tell me? – Charles Jan 11 '17 at 02:18
  • seem to **_[link](http://stackoverflow.com/questions/6939782/fabric-js-problem-with-drawing-multiple-images-zindex)_** – Charles Jan 11 '17 at 11:24
  • sorry for the late response. It seems like you are trying to bring some objects on top of others using z-index. as `moveto()` will set the index of the object to whatever value you pass as argument. I would suggest to use `bringToFront()` instead of moveto(). Give that a try it will bring the object on to top of all other objects. – Manish Jan 11 '17 at 12:07
  • New question is : Will draw a lot of pictures in canvas, What will be the asynchronous problems,When draw(`canvas.add(oImg)`) a element I will `sum++`, I use `fromUrl` load picture,`callback` implement **[link](https://jsfiddle.net/Charles_/y6b2yrng/3/)** – Charles Jan 11 '17 at 16:00
  • i dont think there will be any problems. You only need to take care of callbacks properly that is it. no need to worry about anything. You can add as many pictures you want. – Manish Jan 12 '17 at 06:39