This example shows how to use a few of the built in events to real time crop feedback.
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 | border: 1px solid black; |
3 | height: 83px; |
4 | width: 125px; |
5 | position: relative; |
6 | overflow: hidden; |
7 | } |
8 | #results img { |
9 | position: absolute; |
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 | ? |
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 | ? |
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 | ? |
You can load the necessary JavaScript and CSS for this example from Yahoo's servers. Click here to load the YUI Dependency Configurator with all of this example's dependencies preconfigured.
Note: Logging and debugging is currently turned off for this example.
Copyright © 2009 Yahoo! Inc. All rights reserved.
Privacy Policy - Terms of Service - Copyright Policy - Job Openings