We are using fabricjs and creating a simple cubic bezier curve example. We are unable to make the curve update when controlobjectpoint1 is dragged or moved.
Any help will be much appreicated.
// Set up Fabric.js canvas
var canvas = new fabric.Canvas('canvas');
// Control points for the Bezier curve
var startPoint = new fabric.Point(100, 300);
var controlPoint1 = new fabric.Point(200, 100);
var controlPoint2 = new fabric.Point(400, 500);
var endPoint = new fabric.Point(500, 300);
var bezierPath = new fabric.Path('M ' + startPoint.x + ' ' + startPoint.y + ' C ' + controlPoint1.x + ' ' + controlPoint1.y + ', ' + controlPoint2.x + ' ' + controlPoint2.y + ', ' + endPoint.x + ' ' + endPoint.y, {
fill: '',
stroke: 'black',
strokeWidth: 2,
selectable: false
});
console.log(bezierPath.path);
// Display control points
var controlPointRadius = 5;
var controlPointOptions = {
fill: 'red',
selectable: true,
hasControls: false,
hasBorders: false
};
var startControlPoint = new fabric.Circle({
left: startPoint.x - controlPointRadius / 2,
top: startPoint.y - controlPointRadius / 2,
radius: controlPointRadius,
...controlPointOptions
});
var controlPoint1Object = new fabric.Circle({
left: controlPoint1.x - controlPointRadius / 2,
top: controlPoint1.y - controlPointRadius / 2,
radius: controlPointRadius,
...controlPointOptions
});
var controlPoint2Object = new fabric.Circle({
left: controlPoint2.x - controlPointRadius / 2,
top: controlPoint2.y - controlPointRadius / 2,
radius: controlPointRadius,
...controlPointOptions
});
var endControlPoint = new fabric.Circle({
left: endPoint.x - controlPointRadius / 2,
top: endPoint.y - controlPointRadius / 2,
radius: controlPointRadius,
...controlPointOptions
});
document.getElementById("updatepath")
.addEventListener("click", function(e) {
updateBezierCurve();
canvas.renderAll();
});
// Update Bezier curve when control points are dragged
function updateBezierCurve() {
** bezierPath.path[1] = ['C',50,50,400,500,500,300 ];
canvas.renderAll();
console.log(bezierPath.path);**
}
// Event listeners for control points
controlPoint1Object.on('moving', updateBezierCurve);
controlPoint2Object.on('moving', updateBezierCurve);
console.log(bezierPath.path);
// Add objects to canvas
canvas.add(bezierPath, startControlPoint, controlPoint1Object, controlPoint2Object, endControlPoint);
option1 updateBezierCurve() is function which is mapped to controlPoint1/2 'moving' event on standard Fabricjs.
beizerPath is path array and inside we have two entries. We changed the values in updateBezierCurve. But it never updates the curve.
We tried string and array option both, but it is not working. We already used set and setcorrds functions also.
Option2: thinking that moving is not working, so we created a button in html and click event should trigger updateVeizerCurve function.
In both options, we can see the function updateBezierCurve is called , but the renderAll is not updating the curve path. It is updating the path inside the array.
How about we remove and add a new bezierPath ?
Here is the code
This is quick and dirty sample code just to show you the curve moves...
There are offsets to account for when we recreate the new path, that I'm just not considering, but once you can do this right on one event you can add code for the others.
performance
If you are concerned with performance then consider not using FabricJS and writing the code to just use what you need, like
bezierCurveTosimple enough