Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. # --- IMPORTANT ---
  2. #
  3. # You must assume that the page has inputs to receive the dimensions and the ratio!
  4. #
  5. # --- IMPORTANT ---
  6.  
  7. class App.Cropper
  8. # Variables
  9. @croppieInstance = undefined
  10. @originalHeight = undefined
  11. @originalWidth = undefined
  12.  
  13. constructor: (@img, @cropButton, @cancelButton, @initCropButton, @targetDiv, @uploadInput) ->
  14.  
  15. # Initialize croppie
  16. initCroppie: ->
  17. @croppieInstance?.croppie('destroy')
  18. @croppieInstance = $(@img).croppie
  19. enableExif: true,
  20. viewport:
  21. width: 542,
  22. height: 285
  23. ,
  24. boundary:
  25. width: 582,
  26. height: 510
  27. cropMode()
  28. return
  29.  
  30. # Show the crop button
  31. normalMode: ->
  32. $(@initCropButton).show()
  33. $(@cropButton).hide()
  34. $(@cancelButton).hide()
  35.  
  36. # Show the confirm and cancel crop buttons
  37. cropMode: ->
  38. $(@initCropButton).hide()
  39. $(@cropButton).show()
  40. $(@cancelButton).show()
  41.  
  42. uploadAndEdit: (input) ->
  43. _URL = window.URL || window.webkitURL
  44.  
  45. files = input.target.files
  46.  
  47. if files[0].type.match('image.*')
  48. reader = new FileReader
  49. img = new Image
  50.  
  51. reader.onload = (e) ->
  52. $(@img).attr('src', e.target.result)
  53. @croppieInstance.croppie('bind', url: e.target.result ).then ->
  54. console.log 'Bind feito!'
  55.  
  56. reader.readAsDataURL(files[0])
  57.  
  58. img.onload = ->
  59. height = this.naturalHeight || this.height
  60. width = this.naturalWidth || this.width
  61.  
  62. console.log "Altura original: #{height}, comprimento original: #{width}"
  63.  
  64. @originalHeight = height
  65. @originalWidth = width
  66.  
  67. img.src = _URL.createObjectURL(files[0])
  68.  
  69. cropMode()
  70. else
  71. swal("Eita!", "Desculpe, mas seu browser não suporta nosso upload de imagens :(", "error")
  72.  
  73. cropImage: ->
  74. cropMode()
  75.  
  76. @croppieInstance.croppie 'result',
  77. type: 'canvas'
  78. size: 'viewport'
  79. quality: 1
  80. .then (resp) ->
  81. $(@img).attr('src', resp)
  82.  
  83. cropResults = @croppieInstance.croppie('get')
  84. console.log cropResults.points
  85.  
  86. cropW = parseInt(cropResults.points[2]) - parseInt(cropResults.points[0])
  87. cropH = parseInt(cropResults.points[3]) - parseInt(cropResults.points[1])
  88.  
  89. console.log "Altura do crop: #{cropH} comprimento do crop: #{cropW}"
  90.  
  91. ratio = [@originalWidth / cropW, @originalHeight / cropH]
  92. console.log "Ratio array: #{ratio}"
  93. ratio = Math.floor(Math.min(ratio[0], ratio[1]))
  94. $('#ratio').val(ratio)
  95.  
  96. console.log "Ratio: #{ratio}"
  97. $('#crop_x').val(parseInt(cropResults.points[0]) * ratio)
  98. $('#crop_y').val(parseInt(cropResults.points[1]) * ratio)
  99. $('#crop_h').val(cropH * ratio)
  100. $('#crop_w').val(cropW * ratio)
  101.  
  102. normalMode()
  103.  
  104. destroyInstance()
  105.  
  106. destroyInstance: ->
  107. @croppieInstance.croppie('destroy')
  108. $(@img).detach().prependTo(@targetDiv)
  109.  
  110. $(document).on 'turbolinks:load', ->
  111. return unless $('.party-templates-edit').length > 0
  112. cropper = new App.Cropper(
  113. '#file-1imgid',
  114. '#crop-ok',
  115. '#crop-cancel',
  116. '#crop-init',
  117. '#cover-img-preview',
  118. '#party_cover'
  119. )
  120.  
  121. # Initialize croppie
  122. $(cropper.initCropButton).click (evt) ->
  123. evt.preventDefault()
  124.  
  125. cropper.initCroppie()
  126.  
  127.  
  128. $(cropper.cancelButton).click (evt) ->
  129. evt.preventDefault()
  130.  
  131. cropper.normalMode()
  132. cropper.destroyInstance()
  133.  
  134. # Init croppie on new image
  135. $(cropper.uploadInput).change (evt) ->
  136. cropper.initCroppie()
  137. cropper.uploadAndEdit(evt)
  138.  
  139. # Get the result of the crop
  140. $(cropper.cropButton).click (evt) ->
  141. evt.preventDefault()
  142.  
  143. cropper.cropImage()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement