简体   繁体   中英

Fabric JS - draw a percent of a line between two points

So I can draw a line in Fabric JS;

var line = new fabric.Line([x1, y1, x2, y2], {
    stroke: 'red',
    strokeWidth: 0.9
});
canvas.add(line);

However I only want to draw perhaps half of the line or 20% of the line, so it needs to go from x1,y1 but only be drawn 20% of the way towards x2,y2

The docs don't seem to offer this functionality, is there a way to do this in Fabric already? Or any anyone help with another solution?

Ye Olde Maths approach:

var line = new fabric.Line([x1, y1, x1 + ((x2 - x1) / 5), y1 + ((y2 - y1) / 5)]

Should get you about 20% there on a straight line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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