javascript - Image clipTo Path with FabricJS -
i'm trying clip image path can't seem find with.
i've found examples clip image rectangle or circle, path example seems missing. i've managed clip entire canvas path, want clipping 1 image. appreciated!
the following example clipping canvas path.
var canvas = new fabric.canvas('c'); canvas.setdimensions({width:_winwidth,height:_winheight}); var path = new fabric.path('m2.9,245h-5.8c-35.3,0-69.3-14.4-93.3-39.4c-23.3-24.3-35.2-56.4-33.7-90.4c1.8-40.1,21-68.9,39.6-91.8l2.2-2.8\ c10.6-13,22.6-27.8,23.6-43.4c0.9-14.6-7.1-27.8-15-38.9c-14.5-20.3-31-43.3-31-74.5c-0.1-28,11.5-55.1,32.4-76.1\ c-57.2-233.1-29.8-245-2.9-245h5.8c27,0,54.3,11.9,75.1,32.7c21,21,32.5,48,32.4,76c110.4-105,93.9-82,79.4-61.7\ c-7.9,11.1-15.9,24.4-15,38.9c1,15.6,13,30.4,23.6,43.4l2.2,2.8c18.6,22.9,37.8,51.7,39.6,91.8c1.5,34-10.4,66.2-33.7,90.4\ c72.2,230.6,38.2,245,2.9,245z'); path.centeredscaling = true; path.scale(1); //path.selectable = true; path.originx = 'center'; path.originy = 'center'; path.set({ left: (_winwidth/2), top: (_winheight/2)}); fabric.image.fromurl('../images/home/2.jpg', function(img) { img.selectable = false; img.scale(1.2); canvas.centerobject(img); canvas.add(img); canvas.clipto = function (ctx) { path.render(ctx); } canvas.renderall(); });
to clip single image, try this:-
fabric.image.fromurl('http://fabricjs.com/assets/pug_small.jpg', function(img) { img.scale(0.5).set({ left: 100, top: 100, angle: -15, clipto: function (ctx) { path.render(ctx); } }); canvas.add(img).setactiveobject(img); });
note: i'd use var mypath = xxxx
instead since 'path' close svg's path
Comments
Post a Comment