Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

form-image-crop.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'use strict';
  2. $(document).ready(function() {
  3. var FormImageCrop = function() {
  4. var demo1 = function() {
  5. $('#demo1').Jcrop();
  6. }
  7. var demo2 = function() {
  8. var jcrop_api;
  9. $('#demo2').Jcrop({
  10. onChange: showCoords,
  11. onSelect: showCoords,
  12. onRelease: clearCoords
  13. }, function() {
  14. jcrop_api = this;
  15. });
  16. $('#coords').on('change', 'input', function(e) {
  17. var x1 = $('#x1').val(),
  18. x2 = $('#x2').val(),
  19. y1 = $('#y1').val(),
  20. y2 = $('#y2').val();
  21. jcrop_api.setSelect([x1, y1, x2, y2]);
  22. });
  23. // Simple event handler, called from onChange and onSelect
  24. // event handlers, as per the Jcrop invocation above
  25. function showCoords(c) {
  26. $('#x1').val(c.x);
  27. $('#y1').val(c.y);
  28. $('#x2').val(c.x2);
  29. $('#y2').val(c.y2);
  30. $('#w').val(c.w);
  31. $('#h').val(c.h);
  32. };
  33. function clearCoords() {
  34. $('#coords input').val('');
  35. };
  36. }
  37. var demo3 = function() {
  38. // Create variables (in this scope) to hold the API and image size
  39. var jcrop_api,
  40. boundx,
  41. boundy,
  42. // Grab some information about the preview pane
  43. $preview = $('#preview-pane'),
  44. $pcnt = $('#preview-pane .preview-container'),
  45. $pimg = $('#preview-pane .preview-container img'),
  46. xsize = $pcnt.width(),
  47. ysize = $pcnt.height();
  48. console.log('init', [xsize, ysize]);
  49. $('#demo3').Jcrop({
  50. onChange: updatePreview,
  51. onSelect: updatePreview,
  52. aspectRatio: xsize / ysize
  53. }, function() {
  54. // Use the API to get the real image size
  55. var bounds = this.getBounds();
  56. boundx = bounds[0];
  57. boundy = bounds[1];
  58. // Store the API in the jcrop_api variable
  59. jcrop_api = this;
  60. // Move the preview into the jcrop container for css positioning
  61. $preview.appendTo(jcrop_api.ui.holder);
  62. });
  63. function updatePreview(c) {
  64. if (parseInt(c.w) > 0) {
  65. var rx = xsize / c.w;
  66. var ry = ysize / c.h;
  67. $pimg.css({
  68. width: Math.round(rx * boundx) + 'px',
  69. height: Math.round(ry * boundy) + 'px',
  70. marginLeft: '-' + Math.round(rx * c.x) + 'px',
  71. marginTop: '-' + Math.round(ry * c.y) + 'px'
  72. });
  73. }
  74. };
  75. }
  76. return {
  77. //main function to initiate the module
  78. init: function() {
  79. if (!jQuery().Jcrop) {;
  80. return;
  81. }
  82. demo1();
  83. demo2();
  84. demo3();
  85. }
  86. };
  87. }();
  88. });