Friday, September 19, 2014

GWT Image Cropper

In a previous project where the client was GWT based (in combination with GWTP and Errai) we needed an image cropping component. One of the standard widgets you need in nearly every second project and which is not provided by GWT as a pre-baked UI component. The ones I found on open source repositories I didn't like too much for the lack of documentation, huge parts of spaghetti code, lots of functionalities we didn't need or the combination of all those reasons. Wrapping an existing JS component was not an option, we didn't want to depend on another library. So I decided to implement a GWT only based cropper which might come handy for someone else too.

The inspiration on how it should behave was coming from Jira, the issue tracker we just migrated to back then. I had a look at the responsible DOM elements and how the adapt upon user interaction and tried to implement the cropping behaviour the same way. The idea is to create an outer div showing the image as background and three child divs: one for the glass (area to crop), one for the selection (area to clip) and one for adding the four resize handles to the corner of the selection.

The ImageCropper class takes care of registering the needed handlers, loading the source image and adding the selection component. A LoadHandler is used to size the div showing the background image correctly. To make this work on IE the image needs to be visible otherwise we get 0 for the height and width.


The abstract ResizeHandle contains the base logic for styling the handles and capturing mouse events. The calculation of how the selection should be adapted (getScaledYDelta() and getScaledXDelta()) is delegated to the concrete implementations of the handlers which are instantiated in the SelectionChooser class.


Besides creating the handlers the SelectionChooser class is also responsible of creating the glass and the clipping area as well as adapting the sizes and positions of them on user input.


I didn't test the implementation on all different browsers and versions since the application was used only on Chrome and Firefox (lucky us...). It should though work on IE10 as well when I remember correctly.

On the back end side of things we used Thumbnailator to do the actual cropping. With its fluent API the process of manipulating images becomes straightforward compared to using Java's 2D API along with other techniques.



3 comments:

  1. The article presents a practical approach to building an image cropper entirely with GWT rather than depending on an external JavaScript library. The explanation of using nested div elements for the image, crop area, selection, and resize handles provides a clear look at how interactive image manipulation can be implemented from the ground up.

    The decision to create a lightweight component specifically for the project's requirements is particularly interesting, especially because the author considers documentation, unnecessary functionality, and external dependencies. The image selection and resizing behavior makes the implementation relevant to Image Processing Projects For Final Year, where similar image manipulation concepts can be explored.

    ReplyDelete
  2. The discussion of DOM elements, user interaction, event handlers, and dynamically sizing the background image also shows how frontend components can combine visual behavior with application logic. Developers interested in building modern web interfaces can connect these concepts with AI Web Development Course to explore broader web development techniques and practical implementation patterns.

    ReplyDelete
  3. Another useful detail is the attention given to browser compatibility, particularly the issue of obtaining correct image dimensions in Internet Explorer. The article demonstrates how careful handling of loading events and UI elements can make a custom component more reliable, while AI Web Development Course can offer additional perspective on developing interactive web applications.

    ReplyDelete