YUI recommends YUI 3.

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

YUI Library Home

YUI Library Examples: ImageCropper Control: Connection Manager assisted image crop

ImageCropper Control: Connection Manager assisted image crop

This example shows how to use Connection Manager to issue an image crop request.

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<img src="assets/yui.jpg" id="yui_img" height="333" width="500"
2<div id="button1"></div> 
3<div id="results"></div> 
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        var crop = new YAHOO.widget.ImageCropper('yui_img', { 
6            initialXY: [20, 20], 
7            keyTick: 5, 
8            shiftKeyTick: 50 
9        }); 
10})(); 
view plain | print | ?

Create the Button

Now we need to create the button that we will use to fire the Connection Manager request.

1var _button = new YAHOO.widget.Button({ 
2    id: 'cropIt'
3    container: 'button1'
4    label: 'Crop Image'
5    value: 'crop' 
6}); 
view plain | print | ?

Setup Connection Manager

Now that we have the button created, we need to setup the Connection Manager request.

We will listen to the Button's click event, then fire the request.

1var callback = { 
2    success: function(o) { 
3        var json = o.responseText.substring(o.responseText.indexOf('{'), o.responseText.lastIndexOf('}') + 1); 
4        var data = eval('(' + json + ')');                 
5        results.innerHTML = '<p><strong>' + data.data + '</strong></p>'
6    }, 
7    failure: function() { 
8        results.innerHTML = '<p><strong>An error occurred, please try again later.</strong></p>'
9    } 
10}; 
11 
12_button.on('click'function() { 
13    var coords = crop.getCropCoords(); 
14    var url = 'assets/crop.php?top=' + coords.top + '&left=' + coords.left + '&height=' + coords.height + '&width=' + coords.width; 
15    conn = YAHOO.util.Connect.asyncRequest('GET', url, callback); 
16}); 
view plain | print | ?

Full Example Source

1(function() { 
2    var Dom = YAHOO.util.Dom, 
3        Event = YAHOO.util.Event, 
4        conn = null
5        results = null
6 
7    Event.onDOMReady(function() { 
8        results = Dom.get('results'); 
9        var callback = { 
10            success: function(o) { 
11                var json = o.responseText.substring(o.responseText.indexOf('{'), o.responseText.lastIndexOf('}') + 1); 
12                var data = eval('(' + json + ')');                 
13                results.innerHTML = '<p><strong>' + data.data + '</strong></p>'
14            }, 
15            failure: function() { 
16                results.innerHTML = '<p><strong>An error occurred, please try again later.</strong></p>'
17            } 
18        }; 
19        var crop = new YAHOO.widget.ImageCropper('yui_img', { 
20            initialXY: [20, 20], 
21            keyTick: 5, 
22            shiftKeyTick: 50 
23        }); 
24 
25        var _button = new YAHOO.widget.Button({ 
26            id: 'cropIt'
27            container: 'button1'
28            label: 'Crop Image'
29            value: 'crop' 
30        }); 
31 
32        _button.on('click'function() { 
33            var coords = crop.getCropCoords(); 
34            var url = 'assets/crop.php?top=' + coords.top + '&left=' + coords.left + '&height=' + coords.height + '&width=' + coords.width; 
35            conn = YAHOO.util.Connect.asyncRequest('GET', url, callback); 
36        }); 
37    }); 
38})(); 
view plain | print | ?

Configuration for This Example

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.

YUI Logger Output:

Logger Console

INFO 80ms (+21) 11:21:56 PM:

LogReader instance0

LogReader initialized

INFO 59ms (+1) 11:21:56 PM:

Get

Appending node: ../../../2.x/build/event-mouseenter/event-mouseenter-min.js

INFO 58ms (+0) 11:21:56 PM:

Get

attempting to load ../../../2.x/build/event-mouseenter/event-mouseenter-min.js

INFO 58ms (+58) 11:21:56 PM:

Get

_next: q0, loaded: undefined

INFO 0ms (+0) 11:21:56 PM:

global

Logger initialized

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 © 2011 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings