Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
174
rated 0 times [  179] [ 5]  / answers: 1 / hits: 34344  / 11 Years ago, wed, september 11, 2013, 12:00:00

How to implement crop tool on the image that is loaded on the canvas using fabric.js ?
I have a image loaded on the canvas .Now i want to implement crop tool where the user is allowed to crop the image and reload it on to the canvas when he is done.


More From » canvas

 Answers
19

In Summary




  1. set el.selectable = false

  2. draw a rectangle on it with mouse event

  3. get rectangle left/top and width/height

  4. then use the following function



Sorry, let me explain. The ctx.rect will crop the image from the center point of the object. Also the scaleX factor should be taken into account.



x = select_el.left - object.left;
y = select_el.top - object.top;

x *= 1 / scale;
y *= 1 / scale;

width = select_el.width * 1 / scale;
heigh = select_el.height * 1 / scale;

object.clipTo = function (ctx) {
ctx.rect (x, y, width, height);
}


Complete example: http://jsfiddle.net/hellomaya/kNEaX/1/



And check out this http://jsfiddle.net/hellomaya/hzqrM/ for generating the select box.
And a reference for the Fabric events: https://github.com/kangax/fabric.js/wiki/Working-with-events


[#75774] Tuesday, September 10, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pranav

Total Points: 693
Total Questions: 119
Total Answers: 119

Location: Greenland
Member since Fri, Jul 31, 2020
4 Years ago
;