YUI recommends YUI3.

YUI 2 has been deprecated since 2011. This site acts as an archive for files and documentation.

This documentation is no longer maintained.

YUI Library Examples: ImageCropper Control: Real Time Crop Feedback

ImageCropper Control: Real Time Crop Feedback

This example shows how to use a few of the built in events to real time crop feedback.

  • Height: (91)
  • Width: (150)
  • Top: (20)
  • Left: (20)

Setting up the Image Cropper

The ImageCropper Control will only work when applied to an image. So we place an image on the page and give it an id.

1#results { 
2    border1px solid black
3    height83px
4    width125px
5    positionrelative
6    overflowhidden
7} 
8#results img { 
9    positionabsolute
10    top-20px
11    left-20px
12} 
view plain | print | ?
1<p><img src="assets/yui.jpg" id="yui_img" height="333" width="500"></p> 
2 
3<div id="results"><img src="assets/yui.jpg"></div> 
4<ul> 
5    <li>Height: (<span id="h">91</span>)</li> 
6    <li>Width: (<span id="w">150</span>)</li> 
7    <li>Top: (<span id="t">20</span>)</li> 
8    <li>Left: (<span id="l">20</span>)</li> 
9</ul> 
view plain | print | ?

Creating the ImageCropper instance

Next we call the ImageCropper constructor on the image.

1(function() { 
2    var Dom = YAHOO.util.Dom, 
3        Event = YAHOO.util.Event; 
4 
5    Event.onDOMReady(function() { 
6        var crop = new YAHOO.widget.ImageCropper('yui_img', { 
7            initialXY: [20, 20], 
8            keyTick: 5, 
9            shiftKeyTick: 50 
10        }); 
11    }); 
12})(); 
view plain | print | ?

Hooking into the ImageCroppers Events

Next we listen for the moveEvent and react to the changes. By calling the getCropCoords method, we will be returned an object like this:

1
2    top: 10, 
3    left: 10, 
4    height: 100. 
5    width: 300 
6
view plain | print | ?

Using this information, we can use DOM to update the results with the proper information.

1(function() { 
2    var Dom = YAHOO.util.Dom, 
3        Event = YAHOO.util.Event, 
4        results = null
5     
6    Event.onDOMReady(function() { 
7            results = Dom.get('results');     
8            var crop = new YAHOO.widget.ImageCropper('yui_img', { 
9                initialXY: [20, 20], 
10                keyTick: 5, 
11                shiftKeyTick: 50 
12            }); 
13            crop.on('moveEvent'function() { 
14                var region = crop.getCropCoords(); 
15                results.firstChild.style.top = '-' + region.top + 'px'
16                results.firstChild.style.left = '-' + region.left + 'px'
17                results.style.height = region.height + 'px'
18                results.style.width = region.width + 'px'
19                Dom.get('t').innerHTML = region.top; 
20                Dom.get('l').innerHTML = region.left; 
21                Dom.get('h').innerHTML = region.height; 
22                Dom.get('w').innerHTML = region.width; 
23            }); 
24    }); 
25})(); 
view plain | print | ?

YUI Logger Output:

Note: You are viewing this example in debug mode with logging enabled. This can significantly slow performance.

Reload with logging
and debugging disabled.

More ImageCropper Control Resources:

Copyright © 2013 Yahoo! Inc. All rights reserved.

Privacy Policy - Copyright Policy - Job Openings