be_comp_image_cropper.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Document : be_comp_image_cropper.js
  3. * Author : pixelcave
  4. * Description: Custom JS code used in Image Cropper Page
  5. */
  6. var BeCompImageCropper = function() {
  7. // Image Cropper, for more examples you can check out https://fengyuanchen.github.io/cropperjs/
  8. var initImageCropper = function(){
  9. // Get Image Container
  10. var image = document.getElementById('js-img-cropper');
  11. // Set Options
  12. Cropper.setDefaults({
  13. aspectRatio: 4 / 3,
  14. preview: '.js-img-cropper-preview'
  15. });
  16. // Init Image Cropper
  17. var cropper = new Cropper(image, {
  18. crop: function (e) {
  19. // e.detail contains all data required to crop the image server side
  20. // You will have to send it to your custom server side script and crop the image there
  21. // Since this event is fired each time you set the crop section, you could also use getData()
  22. // method on demand. Please check out https://fengyuanchen.github.io/cropperjs/ for more info
  23. // console.log(e.detail);
  24. }
  25. });
  26. // Mini Cropper API
  27. jQuery('[data-toggle="cropper"]').on('click', function(){
  28. var btn = jQuery(this);
  29. var method = btn.data('method') || false;
  30. var option = btn.data('option') || false;
  31. // Method selection
  32. switch(method) {
  33. case 'zoom':
  34. cropper.zoom(option);
  35. break;
  36. case 'setDragMode':
  37. cropper.setDragMode(option);
  38. break;
  39. case 'rotate':
  40. cropper.rotate(option);
  41. break;
  42. case 'scaleX':
  43. cropper.scaleX(option);
  44. btn.data('option', -(option));
  45. break;
  46. case 'scaleY':
  47. cropper.scaleY(option);
  48. btn.data('option', -(option));
  49. break;
  50. case 'setAspectRatio':
  51. cropper.setAspectRatio(option);
  52. break;
  53. case 'crop':
  54. cropper.crop();
  55. break;
  56. case 'clear':
  57. cropper.clear();
  58. break;
  59. }
  60. });
  61. };
  62. return {
  63. init: function () {
  64. // Init Image Cropper
  65. initImageCropper();
  66. }
  67. };
  68. }();
  69. // Initialize when page loads
  70. jQuery(function(){ BeCompImageCropper.init(); });