25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

375 satır
8.5 KiB

  1. import { h } from 'vue'
  2. import Cropper from 'cropperjs'
  3. const previewPropType = typeof window === 'undefined'
  4. ? [String, Array]
  5. : [String, Array, Element, NodeList]
  6. export default {
  7. render() {
  8. const crossorigin = this.crossorigin || undefined;
  9. return h('div', { style: this.containerStyle }, [
  10. h('img', {
  11. ref: 'img',
  12. src: this.src,
  13. alt: this.alt || 'image',
  14. style: [
  15. { 'max-width': '100%' },
  16. this.imgStyle
  17. ],
  18. crossorigin,
  19. })
  20. ])
  21. },
  22. props: {
  23. // Library props
  24. containerStyle: Object,
  25. src: {
  26. type: String,
  27. default: ''
  28. },
  29. alt: String,
  30. imgStyle: Object,
  31. // CropperJS props
  32. viewMode: Number,
  33. dragMode: String,
  34. initialAspectRatio: Number,
  35. aspectRatio: Number,
  36. data: Object,
  37. preview: previewPropType,
  38. responsive: {
  39. type: Boolean,
  40. default: true
  41. },
  42. restore: {
  43. type: Boolean,
  44. default: true
  45. },
  46. checkCrossOrigin: {
  47. type: Boolean,
  48. default: true
  49. },
  50. checkOrientation: {
  51. type: Boolean,
  52. default: true
  53. },
  54. crossorigin: {
  55. type: String,
  56. },
  57. modal: {
  58. type: Boolean,
  59. default: true
  60. },
  61. guides: {
  62. type: Boolean,
  63. default: true
  64. },
  65. center: {
  66. type: Boolean,
  67. default: true
  68. },
  69. highlight: {
  70. type: Boolean,
  71. default: true
  72. },
  73. background: {
  74. type: Boolean,
  75. default: true
  76. },
  77. autoCrop: {
  78. type: Boolean,
  79. default: true
  80. },
  81. autoCropArea: Number,
  82. movable: {
  83. type: Boolean,
  84. default: true
  85. },
  86. rotatable: {
  87. type: Boolean,
  88. default: true
  89. },
  90. scalable: {
  91. type: Boolean,
  92. default: true
  93. },
  94. zoomable: {
  95. type: Boolean,
  96. default: true
  97. },
  98. zoomOnTouch: {
  99. type: Boolean,
  100. default: true
  101. },
  102. zoomOnWheel: {
  103. type: Boolean,
  104. default: true
  105. },
  106. wheelZoomRatio: Number,
  107. cropBoxMovable: {
  108. type: Boolean,
  109. default: true
  110. },
  111. cropBoxResizable: {
  112. type: Boolean,
  113. default: true
  114. },
  115. toggleDragModeOnDblclick: {
  116. type: Boolean,
  117. default: true
  118. },
  119. // Size limitation
  120. minCanvasWidth: Number,
  121. minCanvasHeight: Number,
  122. minCropBoxWidth: Number,
  123. minCropBoxHeight: Number,
  124. minContainerWidth: Number,
  125. minContainerHeight: Number,
  126. // callbacks
  127. ready: Function,
  128. cropstart: Function,
  129. cropmove: Function,
  130. cropend: Function,
  131. crop: Function,
  132. zoom: Function
  133. },
  134. mounted() {
  135. const { containerStyle, src, alt, imgStyle, ...data } = this.$options.props
  136. const props = {}
  137. for (const key in data) {
  138. if (this[key] !== undefined) {
  139. props[key] = this[key]
  140. }
  141. }
  142. this.cropper = new Cropper(this.$refs.img, props)
  143. },
  144. methods: {
  145. // Reset the image and crop box to their initial states
  146. reset() {
  147. return this.cropper.reset()
  148. },
  149. // Clear the crop box
  150. clear() {
  151. return this.cropper.clear()
  152. },
  153. // Init crop box manually
  154. initCrop() {
  155. return this.cropper.crop()
  156. },
  157. /**
  158. * Replace the image's src and rebuild the cropper
  159. * @param {string} url - The new URL.
  160. * @param {boolean} [onlyColorChanged] - Indicate if the new image only changed color.
  161. * @returns {Object} this
  162. */
  163. replace(url, onlyColorChanged = false) {
  164. return this.cropper.replace(url, onlyColorChanged)
  165. },
  166. // Enable (unfreeze) the cropper
  167. enable() {
  168. return this.cropper.enable()
  169. },
  170. // Disable (freeze) the cropper
  171. disable() {
  172. return this.cropper.disable()
  173. },
  174. // Destroy the cropper and remove the instance from the image
  175. destroy() {
  176. return this.cropper.destroy()
  177. },
  178. /**
  179. * Move the canvas with relative offsets
  180. * @param {number} offsetX - The relative offset distance on the x-axis.
  181. * @param {number} offsetY - The relative offset distance on the y-axis.
  182. * @returns {Object} this
  183. */
  184. move(offsetX, offsetY) {
  185. return this.cropper.move(offsetX, offsetY)
  186. },
  187. /**
  188. * Move the canvas to an absolute point
  189. * @param {number} x - The x-axis coordinate.
  190. * @param {number} [y=x] - The y-axis coordinate.
  191. * @returns {Object} this
  192. */
  193. moveTo(x, y = x) {
  194. return this.cropper.moveTo(x, y)
  195. },
  196. /**
  197. * Zoom the canvas with a relative ratio
  198. * @param {number} ratio - The target ratio.
  199. * @param {Event} _originalEvent - The original event if any.
  200. * @returns {Object} this
  201. */
  202. relativeZoom(ratio, _originalEvent) {
  203. return this.cropper.zoom(ratio, _originalEvent)
  204. },
  205. /**
  206. * Zoom the canvas to an absolute ratio
  207. * @param {number} ratio - The target ratio.
  208. * @param {Event} _originalEvent - The original event if any.
  209. * @returns {Object} this
  210. */
  211. zoomTo(ratio, _originalEvent) {
  212. return this.cropper.zoomTo(ratio, _originalEvent)
  213. },
  214. /**
  215. * Rotate the canvas with a relative degree
  216. * @param {number} degree - The rotate degree.
  217. * @returns {Object} this
  218. */
  219. rotate(degree) {
  220. return this.cropper.rotate(degree)
  221. },
  222. /**
  223. * Rotate the canvas to an absolute degree
  224. * @param {number} degree - The rotate degree.
  225. * @returns {Object} this
  226. */
  227. rotateTo(degree) {
  228. return this.cropper.rotateTo(degree)
  229. },
  230. /**
  231. * Scale the image on the x-axis.
  232. * @param {number} scaleX - The scale ratio on the x-axis.
  233. * @returns {Object} this
  234. */
  235. scaleX(scaleX) {
  236. return this.cropper.scaleX(scaleX)
  237. },
  238. /**
  239. * Scale the image on the y-axis.
  240. * @param {number} scaleY - The scale ratio on the y-axis.
  241. * @returns {Object} this
  242. */
  243. scaleY(scaleY) {
  244. return this.cropper.scaleY(scaleY)
  245. },
  246. /**
  247. * Scale the image
  248. * @param {number} scaleX - The scale ratio on the x-axis.
  249. * @param {number} [scaleY=scaleX] - The scale ratio on the y-axis.
  250. * @returns {Object} this
  251. */
  252. scale(scaleX, scaleY = scaleX) {
  253. return this.cropper.scale(scaleX, scaleY)
  254. },
  255. /**
  256. * Get the cropped area position and size data (base on the original image)
  257. * @param {boolean} [rounded=false] - Indicate if round the data values or not.
  258. * @returns {Object} The result cropped data.
  259. */
  260. getData(rounded = false) {
  261. return this.cropper.getData(rounded)
  262. },
  263. /**
  264. * Set the cropped area position and size with new data
  265. * @param {Object} data - The new data.
  266. * @returns {Object} this
  267. */
  268. setData(data) {
  269. return this.cropper.setData(data)
  270. },
  271. /**
  272. * Get the container size data.
  273. * @returns {Object} The result container data.
  274. */
  275. getContainerData() {
  276. return this.cropper.getContainerData()
  277. },
  278. /**
  279. * Get the image position and size data.
  280. * @returns {Object} The result image data.
  281. */
  282. getImageData() {
  283. return this.cropper.getImageData()
  284. },
  285. /**
  286. * Get the canvas position and size data.
  287. * @returns {Object} The result canvas data.
  288. */
  289. getCanvasData() {
  290. return this.cropper.getCanvasData()
  291. },
  292. /**
  293. * Set the canvas position and size with new data.
  294. * @param {Object} data - The new canvas data.
  295. * @returns {Object} this
  296. */
  297. setCanvasData(data) {
  298. return this.cropper.setCanvasData(data)
  299. },
  300. /**
  301. * Get the crop box position and size data.
  302. * @returns {Object} The result crop box data.
  303. */
  304. getCropBoxData() {
  305. return this.cropper.getCropBoxData()
  306. },
  307. /**
  308. * Set the crop box position and size with new data.
  309. * @param {Object} data - The new crop box data.
  310. * @returns {Object} this
  311. */
  312. setCropBoxData(data) {
  313. return this.cropper.setCropBoxData(data)
  314. },
  315. /**
  316. * Get a canvas drawn the cropped image.
  317. * @param {Object} [options={}] - The config options.
  318. * @returns {HTMLCanvasElement} - The result canvas.
  319. */
  320. getCroppedCanvas(options = {}) {
  321. return this.cropper.getCroppedCanvas(options)
  322. },
  323. /**
  324. * Change the aspect ratio of the crop box.
  325. * @param {number} aspectRatio - The new aspect ratio.
  326. * @returns {Object} this
  327. */
  328. setAspectRatio(aspectRatio) {
  329. return this.cropper.setAspectRatio(aspectRatio)
  330. },
  331. /**
  332. * Change the drag mode.
  333. * @param {string} mode - The new drag mode.
  334. * @returns {Object} this
  335. */
  336. setDragMode(mode) {
  337. return this.cropper.setDragMode(mode)
  338. }
  339. }
  340. }