tutorial5.html 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>API Demo | Jcrop Demo</title>
  5. <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
  6. <script src="../js/jquery.min.js"></script>
  7. <script src="../js/jquery.Jcrop.js"></script>
  8. <script type="text/javascript">
  9. jQuery(function($){
  10. // The variable jcrop_api will hold a reference to the
  11. // Jcrop API once Jcrop is instantiated.
  12. var jcrop_api;
  13. // In this example, since Jcrop may be attached or detached
  14. // at the whim of the user, I've wrapped the call into a function
  15. initJcrop();
  16. // The function is pretty simple
  17. function initJcrop()//{{{
  18. {
  19. // Hide any interface elements that require Jcrop
  20. // (This is for the local user interface portion.)
  21. $('.requiresjcrop').hide();
  22. // Invoke Jcrop in typical fashion
  23. $('#target').Jcrop({
  24. onRelease: releaseCheck
  25. },function(){
  26. jcrop_api = this;
  27. jcrop_api.animateTo([100,100,400,300]);
  28. // Setup and dipslay the interface for "enabled"
  29. $('#can_click,#can_move,#can_size').attr('checked','checked');
  30. $('#ar_lock,#size_lock,#bg_swap').attr('checked',false);
  31. $('.requiresjcrop').show();
  32. });
  33. };
  34. //}}}
  35. // Use the API to find cropping dimensions
  36. // Then generate a random selection
  37. // This function is used by setSelect and animateTo buttons
  38. // Mainly for demonstration purposes
  39. function getRandom() {
  40. var dim = jcrop_api.getBounds();
  41. return [
  42. Math.round(Math.random() * dim[0]),
  43. Math.round(Math.random() * dim[1]),
  44. Math.round(Math.random() * dim[0]),
  45. Math.round(Math.random() * dim[1])
  46. ];
  47. };
  48. // This function is bound to the onRelease handler...
  49. // In certain circumstances (such as if you set minSize
  50. // and aspectRatio together), you can inadvertently lose
  51. // the selection. This callback re-enables creating selections
  52. // in such a case. Although the need to do this is based on a
  53. // buggy behavior, it's recommended that you in some way trap
  54. // the onRelease callback if you use allowSelect: false
  55. function releaseCheck()
  56. {
  57. jcrop_api.setOptions({ allowSelect: true });
  58. $('#can_click').attr('checked',false);
  59. };
  60. // Attach interface buttons
  61. // This may appear to be a lot of code but it's simple stuff
  62. $('#setSelect').click(function(e) {
  63. // Sets a random selection
  64. jcrop_api.setSelect(getRandom());
  65. });
  66. $('#animateTo').click(function(e) {
  67. // Animates to a random selection
  68. jcrop_api.animateTo(getRandom());
  69. });
  70. $('#release').click(function(e) {
  71. // Release method clears the selection
  72. jcrop_api.release();
  73. });
  74. $('#disable').click(function(e) {
  75. // Disable Jcrop instance
  76. jcrop_api.disable();
  77. // Update the interface to reflect disabled state
  78. $('#enable').show();
  79. $('.requiresjcrop').hide();
  80. });
  81. $('#enable').click(function(e) {
  82. // Re-enable Jcrop instance
  83. jcrop_api.enable();
  84. // Update the interface to reflect enabled state
  85. $('#enable').hide();
  86. $('.requiresjcrop').show();
  87. });
  88. $('#rehook').click(function(e) {
  89. // This button is visible when Jcrop has been destroyed
  90. // It performs the re-attachment and updates the UI
  91. $('#rehook,#enable').hide();
  92. initJcrop();
  93. $('#unhook,.requiresjcrop').show();
  94. return false;
  95. });
  96. $('#unhook').click(function(e) {
  97. // Destroy Jcrop widget, restore original state
  98. jcrop_api.destroy();
  99. // Update the interface to reflect un-attached state
  100. $('#unhook,#enable,.requiresjcrop').hide();
  101. $('#rehook').show();
  102. return false;
  103. });
  104. // Hook up the three image-swapping buttons
  105. $('#img1').click(function(e) {
  106. $(this).addClass('active').closest('.btn-group')
  107. .find('button.active').not(this).removeClass('active');
  108. jcrop_api.setImage('demo_files/sago.jpg');
  109. jcrop_api.setOptions({ bgOpacity: .6 });
  110. return false;
  111. });
  112. $('#img2').click(function(e) {
  113. $(this).addClass('active').closest('.btn-group')
  114. .find('button.active').not(this).removeClass('active');
  115. jcrop_api.setImage('demo_files/pool.jpg');
  116. jcrop_api.setOptions({ bgOpacity: .6 });
  117. return false;
  118. });
  119. $('#img3').click(function(e) {
  120. $(this).addClass('active').closest('.btn-group')
  121. .find('button.active').not(this).removeClass('active');
  122. jcrop_api.setImage('demo_files/sago.jpg',function(){
  123. this.setOptions({
  124. bgOpacity: 1,
  125. outerImage: 'demo_files/sagomod.jpg'
  126. });
  127. this.animateTo(getRandom());
  128. });
  129. return false;
  130. });
  131. // The checkboxes simply set options based on it's checked value
  132. // Options are changed by passing a new options object
  133. // Also, to prevent strange behavior, they are initially checked
  134. // This matches the default initial state of Jcrop
  135. $('#can_click').change(function(e) {
  136. jcrop_api.setOptions({ allowSelect: !!this.checked });
  137. jcrop_api.focus();
  138. });
  139. $('#can_move').change(function(e) {
  140. jcrop_api.setOptions({ allowMove: !!this.checked });
  141. jcrop_api.focus();
  142. });
  143. $('#can_size').change(function(e) {
  144. jcrop_api.setOptions({ allowResize: !!this.checked });
  145. jcrop_api.focus();
  146. });
  147. $('#ar_lock').change(function(e) {
  148. jcrop_api.setOptions(this.checked?
  149. { aspectRatio: 4/3 }: { aspectRatio: 0 });
  150. jcrop_api.focus();
  151. });
  152. $('#size_lock').change(function(e) {
  153. jcrop_api.setOptions(this.checked? {
  154. minSize: [ 80, 80 ],
  155. maxSize: [ 350, 350 ]
  156. }: {
  157. minSize: [ 0, 0 ],
  158. maxSize: [ 0, 0 ]
  159. });
  160. jcrop_api.focus();
  161. });
  162. });
  163. </script>
  164. <link rel="stylesheet" href="demo_files/main.css" type="text/css" />
  165. <link rel="stylesheet" href="demo_files/demos.css" type="text/css" />
  166. <link rel="stylesheet" href="../css/jquery.Jcrop.css" type="text/css" />
  167. <style type="text/css">
  168. .optdual { position: relative; }
  169. .optdual .offset { position: absolute; left: 18em; }
  170. .optlist label { width: 16em; display: block; }
  171. #dl_links { margin-top: .5em; }
  172. </style>
  173. </head>
  174. <body>
  175. <div class="container">
  176. <div class="row">
  177. <div class="span12">
  178. <div class="jc-demo-box">
  179. <div class="page-header">
  180. <ul class="breadcrumb first">
  181. <li><a href="../index.html">Jcrop</a> <span class="divider">/</span></li>
  182. <li><a href="../index.html">Demos</a> <span class="divider">/</span></li>
  183. <li class="active">API Demo</li>
  184. </ul>
  185. <h1>API Demo</h1>
  186. </div>
  187. <img src="demo_files/sago.jpg" id="target" alt="[Jcrop Example]" />
  188. <div style="margin: .8em 0 .5em;">
  189. <span class="requiresjcrop">
  190. <button id="setSelect" class="btn btn-mini">setSelect</button>
  191. <button id="animateTo" class="btn btn-mini">animateTo</button>
  192. <button id="release" class="btn btn-mini">Release</button>
  193. <button id="disable" class="btn btn-mini">Disable</button>
  194. </span>
  195. <button id="enable" class="btn btn-mini" style="display:none;">Re-Enable</button>
  196. <button id="unhook" class="btn btn-mini">Destroy!</button>
  197. <button id="rehook" class="btn btn-mini" style="display:none;">Attach Jcrop</button>
  198. </div>
  199. <fieldset class="optdual requiresjcrop">
  200. <legend>Option Toggles</legend>
  201. <div class="optlist offset">
  202. <label><input type="checkbox" id="ar_lock" />Aspect ratio</label>
  203. <label><input type="checkbox" id="size_lock" />minSize/maxSize setting</label>
  204. </div>
  205. <div class="optlist">
  206. <label><input type="checkbox" id="can_click" />Allow new selections</label>
  207. <label><input type="checkbox" id="can_move" />Selection can be moved</label>
  208. <label><input type="checkbox" id="can_size" />Resizable selection</label>
  209. </div>
  210. </fieldset>
  211. <fieldset class="requiresjcrop" style="margin: .5em 0;">
  212. <legend>Change Image</legend>
  213. <div class="btn-group">
  214. <button class="btn" id="img2">Pool</button>
  215. <button class="btn active" id="img1">Sago</button>
  216. <button class="btn" id="img3">Sago w/ outerImage</button>
  217. </div>
  218. </fieldset>
  219. <div class="tapmodo-footer">
  220. <a href="http://tapmodo.com" class="tapmodo-logo segment">tapmodo.com</a>
  221. <div class="segment"><b>&copy; 2008-2013 Tapmodo Interactive LLC</b><br />
  222. Jcrop is free software released under <a href="../MIT-LICENSE.txt">MIT License</a>
  223. </div>
  224. </div>
  225. <div class="clearfix"></div>
  226. </div>
  227. </div>
  228. </div>
  229. </div>
  230. </body>
  231. </html>