Skip to content

Instantly share code, notes, and snippets.

@linchpinstudios
Last active December 25, 2023 18:27
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save linchpinstudios/61ed36f5ff42088753ba1bf0ea42fffa to your computer and use it in GitHub Desktop.
Save linchpinstudios/61ed36f5ff42088753ba1bf0ea42fffa to your computer and use it in GitHub Desktop.
Fabric JS Arrow
fabric.Arrow = fabric.util.createClass(fabric.Line, {
type: 'Arrow',
initialize: function(element, options) {
options || (options = {});
this.callSuper('initialize', element, options);
},
toObject: function() {
return fabric.util.object.extend(this.callSuper('toObject'));
},
_render: function(ctx){
this.callSuper('_render', ctx);
// do not render if width/height are zeros or object is not visible
if (this.width === 0 && this.height === 0 || !this.visible) return;
ctx.save();
var xDiff = this.x2 - this.x1;
var yDiff = this.y2 - this.y1;
var angle = Math.atan2(yDiff, xDiff);
ctx.translate((this.x2 - this.x1) / 2, (this.y2 - this.y1) / 2);
ctx.rotate(angle);
ctx.beginPath();
//move 10px in front of line to start the arrow so it does not have the square line end showing in front (0,0)
ctx.moveTo(10,0);
ctx.lineTo(-10, 10);
ctx.lineTo(-10, -10);
ctx.closePath();
ctx.fillStyle = this.stroke;
ctx.fill();
ctx.restore();
},
clipTo: function(ctx) {
this._render(ctx);
}
});
fabric.Arrow.fromObject = function (object, callback) {
callback && callback(new fabric.Arrow([object.x1, object.y1, object.x2, object.y2],object));
};
fabric.Arrow.async = true;
@RamakrishnaChilaka
Copy link

clipTo fails when you minify the code, instead use,

clipTo: function() {
var ctx = arguments[0];
this._render(ctx);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment