1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
\r
2 <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
\r
4 <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
\r
5 <meta http-equiv="Content-Language" content="en-us" />
\r
6 <title>Dynamic image test</title>
\r
7 <script src="../lib/prototype.js" type="text/javascript"></script>
\r
8 <script src="../lib/scriptaculous.js?load=builder,dragdrop" type="text/javascript"></script>
\r
9 <script src="../cropper.js" type="text/javascript"></script>
\r
12 <script type="text/javascript" charset="utf-8">
\r
15 * A little manager that allows us to swap the image dynamically
\r
18 var CropImageManager = {
\r
20 * Holds the current Cropper.Img object
\r
26 * Initialises the cropImageManager
\r
32 this.attachCropper();
\r
36 * Handles the changing of the select to change the image, the option value
\r
37 * is a pipe seperated list of imgSrc|width|height
\r
43 onChange: function( e ) {
\r
44 var vals = $F( Event.element( e ) ).split('|');
\r
45 this.setImage( vals[0], vals[1], vals[2] );
\r
49 * Sets the image within the element & attaches/resets the image cropper
\r
52 * @param string Source path of new image
\r
53 * @param int Width of new image in pixels
\r
54 * @param int Height of new image in pixels
\r
57 setImage: function( imgSrc, w, h ) {
\r
58 $PR( 'testImage' ).src = imgSrc;
\r
59 $PR( 'testImage' ).width = w;
\r
60 $PR( 'testImage' ).height = h;
\r
61 this.attachCropper();
\r
65 * Attaches/resets the image cropper
\r
70 attachCropper: function() {
\r
71 if( this.curCrop == null ) this.curCrop = new Cropper.Img( 'testImage', { onEndCrop: onEndCrop } );
\r
72 else this.curCrop.reset();
\r
76 * Removes the cropper
\r
81 removeCropper: function() {
\r
82 if( this.curCrop != null ) {
\r
83 this.curCrop.remove();
\r
88 * Resets the cropper, either re-setting or re-applying
\r
93 resetCropper: function() {
\r
94 this.attachCropper();
\r
99 // setup the callback function
\r
100 function onEndCrop( coords, dimensions ) {
\r
101 $PR( 'x1' ).value = coords.x1;
\r
102 $PR( 'y1' ).value = coords.y1;
\r
103 $PR( 'x2' ).value = coords.x2;
\r
104 $PR( 'y2' ).value = coords.y2;
\r
105 $PR( 'width' ).value = dimensions.width;
\r
106 $PR( 'height' ).value = dimensions.height;
\r
114 CropImageManager.init();
\r
115 Event.observe( $PR('removeCropper'), 'click', CropImageManager.removeCropper.bindAsEventListener( CropImageManager ), false );
\r
116 Event.observe( $PR('resetCropper'), 'click', CropImageManager.resetCropper.bindAsEventListener( CropImageManager ), false );
\r
117 Event.observe( $PR('imageChoice'), 'change', CropImageManager.onChange.bindAsEventListener( CropImageManager ), false );
\r
123 if( typeof(dump) != 'function' ) {
\r
124 Debug.init(true, '/');
\r
126 function dump( msg ) {
\r
127 Debug.raise( msg );
\r
129 } else dump( '---------------------------------------\n' );
\r
133 <link rel="stylesheet" type="text/css" href="debug.css" media="all" />
\r
134 <style type="text/css">
\r
147 margin: 20px 0 0 50px; /* Just while testing, to make sure we return the correct positions for the image & not the window */
\r
152 <h2>Dynamic image test</h2>
\r
154 Test of dynamically changing images or removing & re-applying the cropper
\r
157 <div id="testWrap">
\r
158 <img src="castle.jpg" alt="test image" id="testImage" width="500" height="333" />
\r
162 <label for="imageChoice">image:</label>
\r
163 <select name="imageChoice" id="imageChoice">
\r
164 <option value="castle.jpg|500|333">Castle</option>
\r
165 <option value="poppy.jpg|311|466">Flower</option>
\r
170 <input type="button" id="removeCropper" value="Remove Cropper" />
\r
171 <input type="button" id="resetCropper" value="Reset Cropper" />
\r
176 <label for="x1">x1:</label>
\r
177 <input type="text" name="x1" id="x1" />
\r
180 <label for="y1">y1:</label>
\r
181 <input type="text" name="y1" id="y1" />
\r
184 <label for="x2">x2:</label>
\r
185 <input type="text" name="x2" id="x2" />
\r
188 <label for="y2">y2:</label>
\r
189 <input type="text" name="y2" id="y2" />
\r
192 <label for="width">width:</label>
\r
193 <input type="text" name="width" id="width" />
\r
196 <label for="height">height</label>
\r
197 <input type="text" name="height" id="height" />
\r