jcrop-demo.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. $(function() {
  2. "use strict";
  3. $('.jcrop-basic').Jcrop();
  4. });
  5. $(function($) {
  6. var jcrop_api;
  7. $('.jcrop-handler').Jcrop({
  8. onChange: showCoords,
  9. onSelect: showCoords,
  10. onRelease: clearCoords
  11. }, function() {
  12. jcrop_api = this;
  13. });
  14. $('#coords').on('change', 'input', function(e) {
  15. var x1 = $('#x1').val(),
  16. x2 = $('#x2').val(),
  17. y1 = $('#y1').val(),
  18. y2 = $('#y2').val();
  19. jcrop_api.setSelect([x1, y1, x2, y2]);
  20. });
  21. });
  22. function showCoords(c) {
  23. $('#x1').val(c.x);
  24. $('#y1').val(c.y);
  25. $('#x2').val(c.x2);
  26. $('#y2').val(c.y2);
  27. $('#w').val(c.w);
  28. $('#h').val(c.h);
  29. };
  30. function clearCoords() {
  31. $('#coords input').val('');
  32. };
  33. jQuery(function($) {
  34. // Create variables (in this scope) to hold the API and image size
  35. var jcrop_api,
  36. boundx,
  37. boundy,
  38. // Grab some information about the preview pane
  39. $preview = $('#preview-pane'),
  40. $pcnt = $('#preview-pane .preview-container'),
  41. $pimg = $('#preview-pane .preview-container img'),
  42. xsize = $pcnt.width(),
  43. ysize = $pcnt.height();
  44. $('.jcrop-preview-big').Jcrop({
  45. onChange: updatePreview,
  46. onSelect: updatePreview,
  47. aspectRatio: xsize / ysize
  48. }, function() {
  49. // Use the API to get the real image size
  50. var bounds = this.getBounds();
  51. boundx = bounds[0];
  52. boundy = bounds[1];
  53. // Store the API in the jcrop_api variable
  54. jcrop_api = this;
  55. // Move the preview into the jcrop container for css positioning
  56. $preview.appendTo(jcrop_api.ui.holder);
  57. });
  58. function updatePreview(c) {
  59. if (parseInt(c.w) > 0) {
  60. var rx = xsize / c.w;
  61. var ry = ysize / c.h;
  62. $pimg.css({
  63. width: Math.round(rx * boundx) + 'px',
  64. height: Math.round(ry * boundy) + 'px',
  65. marginLeft: '-' + Math.round(rx * c.x) + 'px',
  66. marginTop: '-' + Math.round(ry * c.y) + 'px'
  67. });
  68. }
  69. };
  70. });
  71. jQuery(function($) {
  72. var jcrop_api;
  73. $('#target').Jcrop({
  74. bgFade: true,
  75. bgOpacity: .2,
  76. setSelect: [60, 70, 540, 330]
  77. }, function() {
  78. jcrop_api = this;
  79. });
  80. $('#fadetog').change(function() {
  81. jcrop_api.setOptions({
  82. bgFade: this.checked
  83. });
  84. }).attr('checked', 'checked');
  85. $('#shadetog').change(function() {
  86. if (this.checked) $('#shadetxt').slideDown();
  87. else $('#shadetxt').slideUp();
  88. jcrop_api.setOptions({
  89. shade: this.checked
  90. });
  91. }).attr('checked', false);
  92. // Define page sections
  93. var sections = {
  94. bgc_buttons: 'Change bgColor',
  95. bgo_buttons: 'Change bgOpacity',
  96. anim_buttons: 'Animate Selection'
  97. };
  98. // Define animation buttons
  99. var ac = {
  100. anim1: [217, 122, 382, 284],
  101. anim2: [20, 20, 580, 380],
  102. anim3: [24, 24, 176, 376],
  103. anim4: [347, 165, 550, 355],
  104. anim5: [136, 55, 472, 183]
  105. };
  106. // Define bgOpacity buttons
  107. var bgo = {
  108. Low: .2,
  109. Mid: .5,
  110. High: .8,
  111. Full: 1
  112. };
  113. // Define bgColor buttons
  114. var bgc = {
  115. R: '#900',
  116. B: '#4BB6F0',
  117. Y: '#F0B207',
  118. G: '#46B81C',
  119. W: 'white',
  120. K: 'black'
  121. };
  122. // Create fieldset targets for buttons
  123. for (i in sections)
  124. insertSection(i, sections[i]);
  125. function create_btn(c) {
  126. var $o = $('<button />').addClass('btn btn-blue-alt');
  127. if (c) $o.append(c);
  128. return $o;
  129. }
  130. var a_count = 1;
  131. // Create animation buttons
  132. for (i in ac) {
  133. $('#anim_buttons .btn-group')
  134. .append(
  135. create_btn(a_count++).click(animHandler(ac[i])),
  136. ' '
  137. );
  138. }
  139. $('#anim_buttons .btn-group').append(
  140. create_btn('Bye!').click(function(e) {
  141. $(e.target).addClass('active');
  142. jcrop_api.animateTo(
  143. [300, 200, 300, 200],
  144. function() {
  145. this.release();
  146. $(e.target).closest('.btn-group').find('.active').removeClass('active');
  147. }
  148. );
  149. return false;
  150. })
  151. );
  152. // Create bgOpacity buttons
  153. for (i in bgo) {
  154. $('#bgo_buttons .btn-group').append(
  155. create_btn(i).click(setoptHandler('bgOpacity', bgo[i])),
  156. ' '
  157. );
  158. }
  159. // Create bgColor buttons
  160. for (i in bgc) {
  161. $('#bgc_buttons .btn-group').append(
  162. create_btn(i).css({
  163. background: bgc[i],
  164. color: ((i == 'K') || (i == 'R')) ? 'white' : 'black'
  165. }).click(setoptHandler('bgColor', bgc[i])), ' '
  166. );
  167. }
  168. // Function to insert named sections into interface
  169. function insertSection(k, v) {
  170. $('#interface').prepend(
  171. $('<fieldset></fieldset>').attr('id', k).append(
  172. $('<legend></legend>').append(v),
  173. '<div class="btn-toolbar"><div class="btn-group"></div></div>'
  174. )
  175. );
  176. };
  177. // Handler for option-setting buttons
  178. function setoptHandler(k, v) {
  179. return function(e) {
  180. $(e.target).closest('.btn-group').find('.active').removeClass('active');
  181. $(e.target).addClass('active');
  182. var opt = {};
  183. opt[k] = v;
  184. jcrop_api.setOptions(opt);
  185. return false;
  186. };
  187. };
  188. // Handler for animation buttons
  189. function animHandler(v) {
  190. return function(e) {
  191. $(e.target).addClass('active');
  192. jcrop_api.animateTo(v, function() {
  193. $(e.target).closest('.btn-group').find('.active').removeClass('active');
  194. });
  195. return false;
  196. };
  197. };
  198. $('#bgo_buttons .btn:first,#bgc_buttons .btn:last').addClass('active');
  199. $('#interface').show();
  200. });