cropper.common.js 79 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154
  1. /*!
  2. * Cropper v3.0.0-rc.2
  3. * https://github.com/fengyuanchen/cropper
  4. *
  5. * Copyright (c) 2017 Fengyuan Chen
  6. * Released under the MIT license
  7. *
  8. * Date: 2017-05-30T05:04:38.958Z
  9. */
  10. 'use strict';
  11. function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
  12. var $ = _interopDefault(require('jquery'));
  13. var DEFAULTS = {
  14. // Define the view mode of the cropper
  15. viewMode: 0, // 0, 1, 2, 3
  16. // Define the dragging mode of the cropper
  17. dragMode: 'crop', // 'crop', 'move' or 'none'
  18. // Define the aspect ratio of the crop box
  19. aspectRatio: NaN,
  20. // An object with the previous cropping result data
  21. data: null,
  22. // A selector for adding extra containers to preview
  23. preview: '',
  24. // Re-render the cropper when resize the window
  25. responsive: true,
  26. // Restore the cropped area after resize the window
  27. restore: true,
  28. // Check if the current image is a cross-origin image
  29. checkCrossOrigin: true,
  30. // Check the current image's Exif Orientation information
  31. checkOrientation: true,
  32. // Show the black modal
  33. modal: true,
  34. // Show the dashed lines for guiding
  35. guides: true,
  36. // Show the center indicator for guiding
  37. center: true,
  38. // Show the white modal to highlight the crop box
  39. highlight: true,
  40. // Show the grid background
  41. background: true,
  42. // Enable to crop the image automatically when initialize
  43. autoCrop: true,
  44. // Define the percentage of automatic cropping area when initializes
  45. autoCropArea: 0.8,
  46. // Enable to move the image
  47. movable: true,
  48. // Enable to rotate the image
  49. rotatable: true,
  50. // Enable to scale the image
  51. scalable: true,
  52. // Enable to zoom the image
  53. zoomable: true,
  54. // Enable to zoom the image by dragging touch
  55. zoomOnTouch: true,
  56. // Enable to zoom the image by wheeling mouse
  57. zoomOnWheel: true,
  58. // Define zoom ratio when zoom the image by wheeling mouse
  59. wheelZoomRatio: 0.1,
  60. // Enable to move the crop box
  61. cropBoxMovable: true,
  62. // Enable to resize the crop box
  63. cropBoxResizable: true,
  64. // Toggle drag mode between "crop" and "move" when click twice on the cropper
  65. toggleDragModeOnDblclick: true,
  66. // Size limitation
  67. minCanvasWidth: 0,
  68. minCanvasHeight: 0,
  69. minCropBoxWidth: 0,
  70. minCropBoxHeight: 0,
  71. minContainerWidth: 200,
  72. minContainerHeight: 100,
  73. // Shortcuts of events
  74. ready: null,
  75. cropstart: null,
  76. cropmove: null,
  77. cropend: null,
  78. crop: null,
  79. zoom: null
  80. };
  81. var TEMPLATE = '<div class="cropper-container">' + '<div class="cropper-wrap-box">' + '<div class="cropper-canvas"></div>' + '</div>' + '<div class="cropper-drag-box"></div>' + '<div class="cropper-crop-box">' + '<span class="cropper-view-box"></span>' + '<span class="cropper-dashed dashed-h"></span>' + '<span class="cropper-dashed dashed-v"></span>' + '<span class="cropper-center"></span>' + '<span class="cropper-face"></span>' + '<span class="cropper-line line-e" data-action="e"></span>' + '<span class="cropper-line line-n" data-action="n"></span>' + '<span class="cropper-line line-w" data-action="w"></span>' + '<span class="cropper-line line-s" data-action="s"></span>' + '<span class="cropper-point point-e" data-action="e"></span>' + '<span class="cropper-point point-n" data-action="n"></span>' + '<span class="cropper-point point-w" data-action="w"></span>' + '<span class="cropper-point point-s" data-action="s"></span>' + '<span class="cropper-point point-ne" data-action="ne"></span>' + '<span class="cropper-point point-nw" data-action="nw"></span>' + '<span class="cropper-point point-sw" data-action="sw"></span>' + '<span class="cropper-point point-se" data-action="se"></span>' + '</div>' + '</div>';
  82. var REGEXP_DATA_URL_HEAD = /^data:.*,/;
  83. var REGEXP_USERAGENT = /(Macintosh|iPhone|iPod|iPad).*AppleWebKit/i;
  84. var navigator = typeof window !== 'undefined' ? window.navigator : null;
  85. var IS_SAFARI_OR_UIWEBVIEW = navigator && REGEXP_USERAGENT.test(navigator.userAgent);
  86. var fromCharCode = String.fromCharCode;
  87. function isNumber(n) {
  88. return typeof n === 'number' && !isNaN(n);
  89. }
  90. function isUndefined(n) {
  91. return typeof n === 'undefined';
  92. }
  93. function toArray(obj, offset) {
  94. var args = [];
  95. // This is necessary for IE8
  96. if (isNumber(offset)) {
  97. args.push(offset);
  98. }
  99. return args.slice.apply(obj, args);
  100. }
  101. // Custom proxy to avoid jQuery's guid
  102. function proxy(fn, context) {
  103. for (var _len = arguments.length, args = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
  104. args[_key - 2] = arguments[_key];
  105. }
  106. return function () {
  107. for (var _len2 = arguments.length, args2 = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  108. args2[_key2] = arguments[_key2];
  109. }
  110. return fn.apply(context, args.concat(toArray(args2)));
  111. };
  112. }
  113. function objectKeys(obj) {
  114. var keys = [];
  115. $.each(obj, function (key) {
  116. keys.push(key);
  117. });
  118. return keys;
  119. }
  120. function isCrossOriginURL(url) {
  121. var parts = url.match(/^(https?:)\/\/([^:/?#]+):?(\d*)/i);
  122. return parts && (parts[1] !== location.protocol || parts[2] !== location.hostname || parts[3] !== location.port);
  123. }
  124. function addTimestamp(url) {
  125. var timestamp = 'timestamp=' + new Date().getTime();
  126. return url + (url.indexOf('?') === -1 ? '?' : '&') + timestamp;
  127. }
  128. function getImageSize(image, callback) {
  129. // Modern browsers (ignore Safari, #120 & #509)
  130. if (image.naturalWidth && !IS_SAFARI_OR_UIWEBVIEW) {
  131. callback(image.naturalWidth, image.naturalHeight);
  132. return;
  133. }
  134. // IE8: Don't use `new Image()` here (#319)
  135. var newImage = document.createElement('img');
  136. newImage.onload = function load() {
  137. callback(this.width, this.height);
  138. };
  139. newImage.src = image.src;
  140. }
  141. function getTransform(options) {
  142. var transforms = [];
  143. var translateX = options.translateX;
  144. var translateY = options.translateY;
  145. var rotate = options.rotate;
  146. var scaleX = options.scaleX;
  147. var scaleY = options.scaleY;
  148. if (isNumber(translateX) && translateX !== 0) {
  149. transforms.push('translateX(' + translateX + 'px)');
  150. }
  151. if (isNumber(translateY) && translateY !== 0) {
  152. transforms.push('translateY(' + translateY + 'px)');
  153. }
  154. // Rotate should come first before scale to match orientation transform
  155. if (isNumber(rotate) && rotate !== 0) {
  156. transforms.push('rotate(' + rotate + 'deg)');
  157. }
  158. if (isNumber(scaleX) && scaleX !== 1) {
  159. transforms.push('scaleX(' + scaleX + ')');
  160. }
  161. if (isNumber(scaleY) && scaleY !== 1) {
  162. transforms.push('scaleY(' + scaleY + ')');
  163. }
  164. return transforms.length ? transforms.join(' ') : 'none';
  165. }
  166. function getRotatedSizes(data, isReversed) {
  167. var deg = Math.abs(data.degree) % 180;
  168. var arc = (deg > 90 ? 180 - deg : deg) * Math.PI / 180;
  169. var sinArc = Math.sin(arc);
  170. var cosArc = Math.cos(arc);
  171. var width = data.width;
  172. var height = data.height;
  173. var aspectRatio = data.aspectRatio;
  174. var newWidth = void 0;
  175. var newHeight = void 0;
  176. if (!isReversed) {
  177. newWidth = width * cosArc + height * sinArc;
  178. newHeight = width * sinArc + height * cosArc;
  179. } else {
  180. newWidth = width / (cosArc + sinArc / aspectRatio);
  181. newHeight = newWidth / aspectRatio;
  182. }
  183. return {
  184. width: newWidth,
  185. height: newHeight
  186. };
  187. }
  188. function getSourceCanvas(image, data) {
  189. var canvas = $('<canvas>')[0];
  190. var context = canvas.getContext('2d');
  191. var dstX = 0;
  192. var dstY = 0;
  193. var dstWidth = data.naturalWidth;
  194. var dstHeight = data.naturalHeight;
  195. var rotate = data.rotate;
  196. var scaleX = data.scaleX;
  197. var scaleY = data.scaleY;
  198. var scalable = isNumber(scaleX) && isNumber(scaleY) && (scaleX !== 1 || scaleY !== 1);
  199. var rotatable = isNumber(rotate) && rotate !== 0;
  200. var advanced = rotatable || scalable;
  201. var canvasWidth = dstWidth * Math.abs(scaleX || 1);
  202. var canvasHeight = dstHeight * Math.abs(scaleY || 1);
  203. var translateX = void 0;
  204. var translateY = void 0;
  205. var rotated = void 0;
  206. if (scalable) {
  207. translateX = canvasWidth / 2;
  208. translateY = canvasHeight / 2;
  209. }
  210. if (rotatable) {
  211. rotated = getRotatedSizes({
  212. width: canvasWidth,
  213. height: canvasHeight,
  214. degree: rotate
  215. });
  216. canvasWidth = rotated.width;
  217. canvasHeight = rotated.height;
  218. translateX = canvasWidth / 2;
  219. translateY = canvasHeight / 2;
  220. }
  221. canvas.width = canvasWidth;
  222. canvas.height = canvasHeight;
  223. if (advanced) {
  224. dstX = -dstWidth / 2;
  225. dstY = -dstHeight / 2;
  226. context.save();
  227. context.translate(translateX, translateY);
  228. }
  229. // Rotate should come first before scale as in the "getTransform" function
  230. if (rotatable) {
  231. context.rotate(rotate * Math.PI / 180);
  232. }
  233. if (scalable) {
  234. context.scale(scaleX, scaleY);
  235. }
  236. context.drawImage(image, Math.floor(dstX), Math.floor(dstY), Math.floor(dstWidth), Math.floor(dstHeight));
  237. if (advanced) {
  238. context.restore();
  239. }
  240. return canvas;
  241. }
  242. function getStringFromCharCode(dataView, start, length) {
  243. var str = '';
  244. var i = void 0;
  245. for (i = start, length += start; i < length; i++) {
  246. str += fromCharCode(dataView.getUint8(i));
  247. }
  248. return str;
  249. }
  250. function getOrientation(arrayBuffer) {
  251. var dataView = new DataView(arrayBuffer);
  252. var length = dataView.byteLength;
  253. var orientation = void 0;
  254. var exifIDCode = void 0;
  255. var tiffOffset = void 0;
  256. var firstIFDOffset = void 0;
  257. var littleEndian = void 0;
  258. var endianness = void 0;
  259. var app1Start = void 0;
  260. var ifdStart = void 0;
  261. var offset = void 0;
  262. var i = void 0;
  263. // Only handle JPEG image (start by 0xFFD8)
  264. if (dataView.getUint8(0) === 0xFF && dataView.getUint8(1) === 0xD8) {
  265. offset = 2;
  266. while (offset < length) {
  267. if (dataView.getUint8(offset) === 0xFF && dataView.getUint8(offset + 1) === 0xE1) {
  268. app1Start = offset;
  269. break;
  270. }
  271. offset++;
  272. }
  273. }
  274. if (app1Start) {
  275. exifIDCode = app1Start + 4;
  276. tiffOffset = app1Start + 10;
  277. if (getStringFromCharCode(dataView, exifIDCode, 4) === 'Exif') {
  278. endianness = dataView.getUint16(tiffOffset);
  279. littleEndian = endianness === 0x4949;
  280. if (littleEndian || endianness === 0x4D4D /* bigEndian */) {
  281. if (dataView.getUint16(tiffOffset + 2, littleEndian) === 0x002A) {
  282. firstIFDOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
  283. if (firstIFDOffset >= 0x00000008) {
  284. ifdStart = tiffOffset + firstIFDOffset;
  285. }
  286. }
  287. }
  288. }
  289. }
  290. if (ifdStart) {
  291. length = dataView.getUint16(ifdStart, littleEndian);
  292. for (i = 0; i < length; i++) {
  293. offset = ifdStart + i * 12 + 2;
  294. if (dataView.getUint16(offset, littleEndian) === 0x0112 /* Orientation */) {
  295. // 8 is the offset of the current tag's value
  296. offset += 8;
  297. // Get the original orientation value
  298. orientation = dataView.getUint16(offset, littleEndian);
  299. // Override the orientation with its default value for Safari (#120)
  300. if (IS_SAFARI_OR_UIWEBVIEW) {
  301. dataView.setUint16(offset, 1, littleEndian);
  302. }
  303. break;
  304. }
  305. }
  306. }
  307. return orientation;
  308. }
  309. function dataURLToArrayBuffer(dataURL) {
  310. var base64 = dataURL.replace(REGEXP_DATA_URL_HEAD, '');
  311. var binary = atob(base64);
  312. var length = binary.length;
  313. var arrayBuffer = new ArrayBuffer(length);
  314. var dataView = new Uint8Array(arrayBuffer);
  315. var i = void 0;
  316. for (i = 0; i < length; i++) {
  317. dataView[i] = binary.charCodeAt(i);
  318. }
  319. return arrayBuffer;
  320. }
  321. // Only available for JPEG image
  322. function arrayBufferToDataURL(arrayBuffer) {
  323. var dataView = new Uint8Array(arrayBuffer);
  324. var length = dataView.length;
  325. var base64 = '';
  326. var i = void 0;
  327. for (i = 0; i < length; i++) {
  328. base64 += fromCharCode(dataView[i]);
  329. }
  330. return 'data:image/jpeg;base64,' + btoa(base64);
  331. }
  332. var render$1 = {
  333. render: function render() {
  334. var self = this;
  335. self.initContainer();
  336. self.initCanvas();
  337. self.initCropBox();
  338. self.renderCanvas();
  339. if (self.cropped) {
  340. self.renderCropBox();
  341. }
  342. },
  343. initContainer: function initContainer() {
  344. var self = this;
  345. var options = self.options;
  346. var $this = self.$element;
  347. var $container = self.$container;
  348. var $cropper = self.$cropper;
  349. var hidden = 'cropper-hidden';
  350. $cropper.addClass(hidden);
  351. $this.removeClass(hidden);
  352. $cropper.css(self.container = {
  353. width: Math.max($container.width(), Number(options.minContainerWidth) || 200),
  354. height: Math.max($container.height(), Number(options.minContainerHeight) || 100)
  355. });
  356. $this.addClass(hidden);
  357. $cropper.removeClass(hidden);
  358. },
  359. // Canvas (image wrapper)
  360. initCanvas: function initCanvas() {
  361. var self = this;
  362. var viewMode = self.options.viewMode;
  363. var container = self.container;
  364. var containerWidth = container.width;
  365. var containerHeight = container.height;
  366. var image = self.image;
  367. var imageNaturalWidth = image.naturalWidth;
  368. var imageNaturalHeight = image.naturalHeight;
  369. var is90Degree = Math.abs(image.rotate) % 180 === 90;
  370. var naturalWidth = is90Degree ? imageNaturalHeight : imageNaturalWidth;
  371. var naturalHeight = is90Degree ? imageNaturalWidth : imageNaturalHeight;
  372. var aspectRatio = naturalWidth / naturalHeight;
  373. var canvasWidth = containerWidth;
  374. var canvasHeight = containerHeight;
  375. if (containerHeight * aspectRatio > containerWidth) {
  376. if (viewMode === 3) {
  377. canvasWidth = containerHeight * aspectRatio;
  378. } else {
  379. canvasHeight = containerWidth / aspectRatio;
  380. }
  381. } else if (viewMode === 3) {
  382. canvasHeight = containerWidth / aspectRatio;
  383. } else {
  384. canvasWidth = containerHeight * aspectRatio;
  385. }
  386. var canvas = {
  387. naturalWidth: naturalWidth,
  388. naturalHeight: naturalHeight,
  389. aspectRatio: aspectRatio,
  390. width: canvasWidth,
  391. height: canvasHeight
  392. };
  393. canvas.oldLeft = canvas.left = (containerWidth - canvasWidth) / 2;
  394. canvas.oldTop = canvas.top = (containerHeight - canvasHeight) / 2;
  395. self.canvas = canvas;
  396. self.limited = viewMode === 1 || viewMode === 2;
  397. self.limitCanvas(true, true);
  398. self.initialImage = $.extend({}, image);
  399. self.initialCanvas = $.extend({}, canvas);
  400. },
  401. limitCanvas: function limitCanvas(isSizeLimited, isPositionLimited) {
  402. var self = this;
  403. var options = self.options;
  404. var viewMode = options.viewMode;
  405. var container = self.container;
  406. var containerWidth = container.width;
  407. var containerHeight = container.height;
  408. var canvas = self.canvas;
  409. var aspectRatio = canvas.aspectRatio;
  410. var cropBox = self.cropBox;
  411. var cropped = self.cropped && cropBox;
  412. if (isSizeLimited) {
  413. var minCanvasWidth = Number(options.minCanvasWidth) || 0;
  414. var minCanvasHeight = Number(options.minCanvasHeight) || 0;
  415. if (viewMode) {
  416. if (viewMode > 1) {
  417. minCanvasWidth = Math.max(minCanvasWidth, containerWidth);
  418. minCanvasHeight = Math.max(minCanvasHeight, containerHeight);
  419. if (viewMode === 3) {
  420. if (minCanvasHeight * aspectRatio > minCanvasWidth) {
  421. minCanvasWidth = minCanvasHeight * aspectRatio;
  422. } else {
  423. minCanvasHeight = minCanvasWidth / aspectRatio;
  424. }
  425. }
  426. } else if (minCanvasWidth) {
  427. minCanvasWidth = Math.max(minCanvasWidth, cropped ? cropBox.width : 0);
  428. } else if (minCanvasHeight) {
  429. minCanvasHeight = Math.max(minCanvasHeight, cropped ? cropBox.height : 0);
  430. } else if (cropped) {
  431. minCanvasWidth = cropBox.width;
  432. minCanvasHeight = cropBox.height;
  433. if (minCanvasHeight * aspectRatio > minCanvasWidth) {
  434. minCanvasWidth = minCanvasHeight * aspectRatio;
  435. } else {
  436. minCanvasHeight = minCanvasWidth / aspectRatio;
  437. }
  438. }
  439. }
  440. if (minCanvasWidth && minCanvasHeight) {
  441. if (minCanvasHeight * aspectRatio > minCanvasWidth) {
  442. minCanvasHeight = minCanvasWidth / aspectRatio;
  443. } else {
  444. minCanvasWidth = minCanvasHeight * aspectRatio;
  445. }
  446. } else if (minCanvasWidth) {
  447. minCanvasHeight = minCanvasWidth / aspectRatio;
  448. } else if (minCanvasHeight) {
  449. minCanvasWidth = minCanvasHeight * aspectRatio;
  450. }
  451. canvas.minWidth = minCanvasWidth;
  452. canvas.minHeight = minCanvasHeight;
  453. canvas.maxWidth = Infinity;
  454. canvas.maxHeight = Infinity;
  455. }
  456. if (isPositionLimited) {
  457. if (viewMode) {
  458. var newCanvasLeft = containerWidth - canvas.width;
  459. var newCanvasTop = containerHeight - canvas.height;
  460. canvas.minLeft = Math.min(0, newCanvasLeft);
  461. canvas.minTop = Math.min(0, newCanvasTop);
  462. canvas.maxLeft = Math.max(0, newCanvasLeft);
  463. canvas.maxTop = Math.max(0, newCanvasTop);
  464. if (cropped && self.limited) {
  465. canvas.minLeft = Math.min(cropBox.left, cropBox.left + cropBox.width - canvas.width);
  466. canvas.minTop = Math.min(cropBox.top, cropBox.top + cropBox.height - canvas.height);
  467. canvas.maxLeft = cropBox.left;
  468. canvas.maxTop = cropBox.top;
  469. if (viewMode === 2) {
  470. if (canvas.width >= containerWidth) {
  471. canvas.minLeft = Math.min(0, newCanvasLeft);
  472. canvas.maxLeft = Math.max(0, newCanvasLeft);
  473. }
  474. if (canvas.height >= containerHeight) {
  475. canvas.minTop = Math.min(0, newCanvasTop);
  476. canvas.maxTop = Math.max(0, newCanvasTop);
  477. }
  478. }
  479. }
  480. } else {
  481. canvas.minLeft = -canvas.width;
  482. canvas.minTop = -canvas.height;
  483. canvas.maxLeft = containerWidth;
  484. canvas.maxTop = containerHeight;
  485. }
  486. }
  487. },
  488. renderCanvas: function renderCanvas(isChanged) {
  489. var self = this;
  490. var canvas = self.canvas;
  491. var image = self.image;
  492. var rotate = image.rotate;
  493. var naturalWidth = image.naturalWidth;
  494. var naturalHeight = image.naturalHeight;
  495. if (self.rotated) {
  496. self.rotated = false;
  497. // Computes rotated sizes with image sizes
  498. var rotated = getRotatedSizes({
  499. width: image.width,
  500. height: image.height,
  501. degree: rotate
  502. });
  503. var aspectRatio = rotated.width / rotated.height;
  504. var isSquareImage = image.aspectRatio === 1;
  505. if (isSquareImage || aspectRatio !== canvas.aspectRatio) {
  506. canvas.left -= (rotated.width - canvas.width) / 2;
  507. canvas.top -= (rotated.height - canvas.height) / 2;
  508. canvas.width = rotated.width;
  509. canvas.height = rotated.height;
  510. canvas.aspectRatio = aspectRatio;
  511. canvas.naturalWidth = naturalWidth;
  512. canvas.naturalHeight = naturalHeight;
  513. // Computes rotated sizes with natural image sizes
  514. if (isSquareImage && rotate % 90 || rotate % 180) {
  515. var rotated2 = getRotatedSizes({
  516. width: naturalWidth,
  517. height: naturalHeight,
  518. degree: rotate
  519. });
  520. canvas.naturalWidth = rotated2.width;
  521. canvas.naturalHeight = rotated2.height;
  522. }
  523. self.limitCanvas(true, false);
  524. }
  525. }
  526. if (canvas.width > canvas.maxWidth || canvas.width < canvas.minWidth) {
  527. canvas.left = canvas.oldLeft;
  528. }
  529. if (canvas.height > canvas.maxHeight || canvas.height < canvas.minHeight) {
  530. canvas.top = canvas.oldTop;
  531. }
  532. canvas.width = Math.min(Math.max(canvas.width, canvas.minWidth), canvas.maxWidth);
  533. canvas.height = Math.min(Math.max(canvas.height, canvas.minHeight), canvas.maxHeight);
  534. self.limitCanvas(false, true);
  535. canvas.oldLeft = canvas.left = Math.min(Math.max(canvas.left, canvas.minLeft), canvas.maxLeft);
  536. canvas.oldTop = canvas.top = Math.min(Math.max(canvas.top, canvas.minTop), canvas.maxTop);
  537. self.$canvas.css({
  538. width: canvas.width,
  539. height: canvas.height,
  540. transform: getTransform({
  541. translateX: canvas.left,
  542. translateY: canvas.top
  543. })
  544. });
  545. self.renderImage();
  546. if (self.cropped && self.limited) {
  547. self.limitCropBox(true, true);
  548. }
  549. if (isChanged) {
  550. self.output();
  551. }
  552. },
  553. renderImage: function renderImage(isChanged) {
  554. var self = this;
  555. var canvas = self.canvas;
  556. var image = self.image;
  557. var reversed = void 0;
  558. if (image.rotate) {
  559. reversed = getRotatedSizes({
  560. width: canvas.width,
  561. height: canvas.height,
  562. degree: image.rotate,
  563. aspectRatio: image.aspectRatio
  564. }, true);
  565. }
  566. $.extend(image, reversed ? {
  567. width: reversed.width,
  568. height: reversed.height,
  569. left: (canvas.width - reversed.width) / 2,
  570. top: (canvas.height - reversed.height) / 2
  571. } : {
  572. width: canvas.width,
  573. height: canvas.height,
  574. left: 0,
  575. top: 0
  576. });
  577. self.$clone.css({
  578. width: image.width,
  579. height: image.height,
  580. transform: getTransform($.extend({
  581. translateX: image.left,
  582. translateY: image.top
  583. }, image))
  584. });
  585. if (isChanged) {
  586. self.output();
  587. }
  588. },
  589. initCropBox: function initCropBox() {
  590. var self = this;
  591. var options = self.options;
  592. var canvas = self.canvas;
  593. var aspectRatio = options.aspectRatio;
  594. var autoCropArea = Number(options.autoCropArea) || 0.8;
  595. var cropBox = {
  596. width: canvas.width,
  597. height: canvas.height
  598. };
  599. if (aspectRatio) {
  600. if (canvas.height * aspectRatio > canvas.width) {
  601. cropBox.height = cropBox.width / aspectRatio;
  602. } else {
  603. cropBox.width = cropBox.height * aspectRatio;
  604. }
  605. }
  606. self.cropBox = cropBox;
  607. self.limitCropBox(true, true);
  608. // Initialize auto crop area
  609. cropBox.width = Math.min(Math.max(cropBox.width, cropBox.minWidth), cropBox.maxWidth);
  610. cropBox.height = Math.min(Math.max(cropBox.height, cropBox.minHeight), cropBox.maxHeight);
  611. // The width of auto crop area must large than "minWidth", and the height too. (#164)
  612. cropBox.width = Math.max(cropBox.minWidth, cropBox.width * autoCropArea);
  613. cropBox.height = Math.max(cropBox.minHeight, cropBox.height * autoCropArea);
  614. cropBox.oldLeft = cropBox.left = canvas.left + (canvas.width - cropBox.width) / 2;
  615. cropBox.oldTop = cropBox.top = canvas.top + (canvas.height - cropBox.height) / 2;
  616. self.initialCropBox = $.extend({}, cropBox);
  617. },
  618. limitCropBox: function limitCropBox(isSizeLimited, isPositionLimited) {
  619. var self = this;
  620. var options = self.options;
  621. var aspectRatio = options.aspectRatio;
  622. var container = self.container;
  623. var containerWidth = container.width;
  624. var containerHeight = container.height;
  625. var canvas = self.canvas;
  626. var cropBox = self.cropBox;
  627. var limited = self.limited;
  628. if (isSizeLimited) {
  629. var minCropBoxWidth = Number(options.minCropBoxWidth) || 0;
  630. var minCropBoxHeight = Number(options.minCropBoxHeight) || 0;
  631. var maxCropBoxWidth = Math.min(containerWidth, limited ? canvas.width : containerWidth);
  632. var maxCropBoxHeight = Math.min(containerHeight, limited ? canvas.height : containerHeight);
  633. // The min/maxCropBoxWidth/Height must be less than containerWidth/Height
  634. minCropBoxWidth = Math.min(minCropBoxWidth, containerWidth);
  635. minCropBoxHeight = Math.min(minCropBoxHeight, containerHeight);
  636. if (aspectRatio) {
  637. if (minCropBoxWidth && minCropBoxHeight) {
  638. if (minCropBoxHeight * aspectRatio > minCropBoxWidth) {
  639. minCropBoxHeight = minCropBoxWidth / aspectRatio;
  640. } else {
  641. minCropBoxWidth = minCropBoxHeight * aspectRatio;
  642. }
  643. } else if (minCropBoxWidth) {
  644. minCropBoxHeight = minCropBoxWidth / aspectRatio;
  645. } else if (minCropBoxHeight) {
  646. minCropBoxWidth = minCropBoxHeight * aspectRatio;
  647. }
  648. if (maxCropBoxHeight * aspectRatio > maxCropBoxWidth) {
  649. maxCropBoxHeight = maxCropBoxWidth / aspectRatio;
  650. } else {
  651. maxCropBoxWidth = maxCropBoxHeight * aspectRatio;
  652. }
  653. }
  654. // The minWidth/Height must be less than maxWidth/Height
  655. cropBox.minWidth = Math.min(minCropBoxWidth, maxCropBoxWidth);
  656. cropBox.minHeight = Math.min(minCropBoxHeight, maxCropBoxHeight);
  657. cropBox.maxWidth = maxCropBoxWidth;
  658. cropBox.maxHeight = maxCropBoxHeight;
  659. }
  660. if (isPositionLimited) {
  661. if (limited) {
  662. cropBox.minLeft = Math.max(0, canvas.left);
  663. cropBox.minTop = Math.max(0, canvas.top);
  664. cropBox.maxLeft = Math.min(containerWidth, canvas.left + canvas.width) - cropBox.width;
  665. cropBox.maxTop = Math.min(containerHeight, canvas.top + canvas.height) - cropBox.height;
  666. } else {
  667. cropBox.minLeft = 0;
  668. cropBox.minTop = 0;
  669. cropBox.maxLeft = containerWidth - cropBox.width;
  670. cropBox.maxTop = containerHeight - cropBox.height;
  671. }
  672. }
  673. },
  674. renderCropBox: function renderCropBox() {
  675. var self = this;
  676. var options = self.options;
  677. var container = self.container;
  678. var containerWidth = container.width;
  679. var containerHeight = container.height;
  680. var cropBox = self.cropBox;
  681. if (cropBox.width > cropBox.maxWidth || cropBox.width < cropBox.minWidth) {
  682. cropBox.left = cropBox.oldLeft;
  683. }
  684. if (cropBox.height > cropBox.maxHeight || cropBox.height < cropBox.minHeight) {
  685. cropBox.top = cropBox.oldTop;
  686. }
  687. cropBox.width = Math.min(Math.max(cropBox.width, cropBox.minWidth), cropBox.maxWidth);
  688. cropBox.height = Math.min(Math.max(cropBox.height, cropBox.minHeight), cropBox.maxHeight);
  689. self.limitCropBox(false, true);
  690. cropBox.oldLeft = cropBox.left = Math.min(Math.max(cropBox.left, cropBox.minLeft), cropBox.maxLeft);
  691. cropBox.oldTop = cropBox.top = Math.min(Math.max(cropBox.top, cropBox.minTop), cropBox.maxTop);
  692. if (options.movable && options.cropBoxMovable) {
  693. // Turn to move the canvas when the crop box is equal to the container
  694. self.$face.data('action', cropBox.width === containerWidth && cropBox.height === containerHeight ? 'move' : 'all');
  695. }
  696. self.$cropBox.css({
  697. width: cropBox.width,
  698. height: cropBox.height,
  699. transform: getTransform({
  700. translateX: cropBox.left,
  701. translateY: cropBox.top
  702. })
  703. });
  704. if (self.cropped && self.limited) {
  705. self.limitCanvas(true, true);
  706. }
  707. if (!self.disabled) {
  708. self.output();
  709. }
  710. },
  711. output: function output() {
  712. var self = this;
  713. self.preview();
  714. if (self.completed) {
  715. self.trigger('crop', self.getData());
  716. }
  717. }
  718. };
  719. var DATA_PREVIEW = 'preview';
  720. var preview$1 = {
  721. initPreview: function initPreview() {
  722. var self = this;
  723. var crossOrigin = self.crossOrigin;
  724. var url = crossOrigin ? self.crossOriginUrl : self.url;
  725. var image = document.createElement('img');
  726. if (crossOrigin) {
  727. image.crossOrigin = crossOrigin;
  728. }
  729. image.src = url;
  730. var $clone2 = $(image);
  731. self.$preview = $(self.options.preview);
  732. self.$clone2 = $clone2;
  733. self.$viewBox.html($clone2);
  734. self.$preview.each(function (i, element) {
  735. var $this = $(element);
  736. var img = document.createElement('img');
  737. // Save the original size for recover
  738. $this.data(DATA_PREVIEW, {
  739. width: $this.width(),
  740. height: $this.height(),
  741. html: $this.html()
  742. });
  743. if (crossOrigin) {
  744. img.crossOrigin = crossOrigin;
  745. }
  746. img.src = url;
  747. /**
  748. * Override img element styles
  749. * Add `display:block` to avoid margin top issue
  750. * Add `height:auto` to override `height` attribute on IE8
  751. * (Occur only when margin-top <= -height)
  752. */
  753. img.style.cssText = 'display:block;' + 'width:100%;' + 'height:auto;' + 'min-width:0!important;' + 'min-height:0!important;' + 'max-width:none!important;' + 'max-height:none!important;' + 'image-orientation:0deg!important;"';
  754. $this.html(img);
  755. });
  756. },
  757. resetPreview: function resetPreview() {
  758. this.$preview.each(function (i, element) {
  759. var $this = $(element);
  760. var data = $this.data(DATA_PREVIEW);
  761. $this.css({
  762. width: data.width,
  763. height: data.height
  764. }).html(data.html).removeData(DATA_PREVIEW);
  765. });
  766. },
  767. preview: function preview() {
  768. var self = this;
  769. var image = self.image;
  770. var canvas = self.canvas;
  771. var cropBox = self.cropBox;
  772. var cropBoxWidth = cropBox.width;
  773. var cropBoxHeight = cropBox.height;
  774. var width = image.width;
  775. var height = image.height;
  776. var left = cropBox.left - canvas.left - image.left;
  777. var top = cropBox.top - canvas.top - image.top;
  778. if (!self.cropped || self.disabled) {
  779. return;
  780. }
  781. self.$clone2.css({
  782. width: width,
  783. height: height,
  784. transform: getTransform($.extend({
  785. translateX: -left,
  786. translateY: -top
  787. }, image))
  788. });
  789. self.$preview.each(function (i, element) {
  790. var $this = $(element);
  791. var data = $this.data(DATA_PREVIEW);
  792. var originalWidth = data.width;
  793. var originalHeight = data.height;
  794. var newWidth = originalWidth;
  795. var newHeight = originalHeight;
  796. var ratio = 1;
  797. if (cropBoxWidth) {
  798. ratio = originalWidth / cropBoxWidth;
  799. newHeight = cropBoxHeight * ratio;
  800. }
  801. if (cropBoxHeight && newHeight > originalHeight) {
  802. ratio = originalHeight / cropBoxHeight;
  803. newWidth = cropBoxWidth * ratio;
  804. newHeight = originalHeight;
  805. }
  806. $this.css({
  807. width: newWidth,
  808. height: newHeight
  809. }).find('img').css({
  810. width: width * ratio,
  811. height: height * ratio,
  812. transform: getTransform($.extend({
  813. translateX: -left * ratio,
  814. translateY: -top * ratio
  815. }, image))
  816. });
  817. });
  818. }
  819. };
  820. // Globals
  821. var PointerEvent = typeof window !== 'undefined' ? window.PointerEvent : null;
  822. // Events
  823. var EVENT_POINTER_DOWN = PointerEvent ? 'pointerdown' : 'touchstart mousedown';
  824. var EVENT_POINTER_MOVE = PointerEvent ? 'pointermove' : 'touchmove mousemove';
  825. var EVENT_POINTER_UP = PointerEvent ? ' pointerup pointercancel' : 'touchend touchcancel mouseup';
  826. var EVENT_WHEEL = 'wheel mousewheel DOMMouseScroll';
  827. var EVENT_DBLCLICK = 'dblclick';
  828. var EVENT_RESIZE = 'resize';
  829. var EVENT_CROP_START = 'cropstart';
  830. var EVENT_CROP_MOVE = 'cropmove';
  831. var EVENT_CROP_END = 'cropend';
  832. var EVENT_CROP = 'crop';
  833. var EVENT_ZOOM = 'zoom';
  834. var events = {
  835. bind: function bind() {
  836. var self = this;
  837. var options = self.options;
  838. var $this = self.$element;
  839. var $cropper = self.$cropper;
  840. if ($.isFunction(options.cropstart)) {
  841. $this.on(EVENT_CROP_START, options.cropstart);
  842. }
  843. if ($.isFunction(options.cropmove)) {
  844. $this.on(EVENT_CROP_MOVE, options.cropmove);
  845. }
  846. if ($.isFunction(options.cropend)) {
  847. $this.on(EVENT_CROP_END, options.cropend);
  848. }
  849. if ($.isFunction(options.crop)) {
  850. $this.on(EVENT_CROP, options.crop);
  851. }
  852. if ($.isFunction(options.zoom)) {
  853. $this.on(EVENT_ZOOM, options.zoom);
  854. }
  855. $cropper.on(EVENT_POINTER_DOWN, proxy(self.cropStart, this));
  856. if (options.zoomable && options.zoomOnWheel) {
  857. $cropper.on(EVENT_WHEEL, proxy(self.wheel, this));
  858. }
  859. if (options.toggleDragModeOnDblclick) {
  860. $cropper.on(EVENT_DBLCLICK, proxy(self.dblclick, this));
  861. }
  862. $(document).on(EVENT_POINTER_MOVE, self.onCropMove = proxy(self.cropMove, this)).on(EVENT_POINTER_UP, self.onCropEnd = proxy(self.cropEnd, this));
  863. if (options.responsive) {
  864. $(window).on(EVENT_RESIZE, self.onResize = proxy(self.resize, this));
  865. }
  866. },
  867. unbind: function unbind() {
  868. var self = this;
  869. var options = self.options;
  870. var $this = self.$element;
  871. var $cropper = self.$cropper;
  872. if ($.isFunction(options.cropstart)) {
  873. $this.off(EVENT_CROP_START, options.cropstart);
  874. }
  875. if ($.isFunction(options.cropmove)) {
  876. $this.off(EVENT_CROP_MOVE, options.cropmove);
  877. }
  878. if ($.isFunction(options.cropend)) {
  879. $this.off(EVENT_CROP_END, options.cropend);
  880. }
  881. if ($.isFunction(options.crop)) {
  882. $this.off(EVENT_CROP, options.crop);
  883. }
  884. if ($.isFunction(options.zoom)) {
  885. $this.off(EVENT_ZOOM, options.zoom);
  886. }
  887. $cropper.off(EVENT_POINTER_DOWN, self.cropStart);
  888. if (options.zoomable && options.zoomOnWheel) {
  889. $cropper.off(EVENT_WHEEL, self.wheel);
  890. }
  891. if (options.toggleDragModeOnDblclick) {
  892. $cropper.off(EVENT_DBLCLICK, self.dblclick);
  893. }
  894. $(document).off(EVENT_POINTER_MOVE, self.onCropMove).off(EVENT_POINTER_UP, self.onCropEnd);
  895. if (options.responsive) {
  896. $(window).off(EVENT_RESIZE, self.onResize);
  897. }
  898. }
  899. };
  900. var REGEXP_ACTIONS = /^(e|w|s|n|se|sw|ne|nw|all|crop|move|zoom)$/;
  901. function getPointer(_ref, endOnly) {
  902. var pageX = _ref.pageX,
  903. pageY = _ref.pageY;
  904. var end = {
  905. endX: pageX,
  906. endY: pageY
  907. };
  908. if (endOnly) {
  909. return end;
  910. }
  911. return $.extend({
  912. startX: pageX,
  913. startY: pageY
  914. }, end);
  915. }
  916. var handlers = {
  917. resize: function resize() {
  918. var self = this;
  919. var options = self.options;
  920. var $container = self.$container;
  921. var container = self.container;
  922. var minContainerWidth = Number(options.minContainerWidth) || 200;
  923. var minContainerHeight = Number(options.minContainerHeight) || 100;
  924. if (self.disabled || container.width === minContainerWidth || container.height === minContainerHeight) {
  925. return;
  926. }
  927. var ratio = $container.width() / container.width;
  928. // Resize when width changed or height changed
  929. if (ratio !== 1 || $container.height() !== container.height) {
  930. (function () {
  931. var canvasData = void 0;
  932. var cropBoxData = void 0;
  933. if (options.restore) {
  934. canvasData = self.getCanvasData();
  935. cropBoxData = self.getCropBoxData();
  936. }
  937. self.render();
  938. if (options.restore) {
  939. self.setCanvasData($.each(canvasData, function (i, n) {
  940. canvasData[i] = n * ratio;
  941. }));
  942. self.setCropBoxData($.each(cropBoxData, function (i, n) {
  943. cropBoxData[i] = n * ratio;
  944. }));
  945. }
  946. })();
  947. }
  948. },
  949. dblclick: function dblclick() {
  950. var self = this;
  951. if (self.disabled || self.options.dragMode === 'none') {
  952. return;
  953. }
  954. self.setDragMode(self.$dragBox.hasClass('cropper-crop') ? 'move' : 'crop');
  955. },
  956. wheel: function wheel(event) {
  957. var self = this;
  958. var e = event.originalEvent || event;
  959. var ratio = Number(self.options.wheelZoomRatio) || 0.1;
  960. if (self.disabled) {
  961. return;
  962. }
  963. event.preventDefault();
  964. // Limit wheel speed to prevent zoom too fast
  965. if (self.wheeling) {
  966. return;
  967. }
  968. self.wheeling = true;
  969. setTimeout(function () {
  970. self.wheeling = false;
  971. }, 50);
  972. var delta = 1;
  973. if (e.deltaY) {
  974. delta = e.deltaY > 0 ? 1 : -1;
  975. } else if (e.wheelDelta) {
  976. delta = -e.wheelDelta / 120;
  977. } else if (e.detail) {
  978. delta = e.detail > 0 ? 1 : -1;
  979. }
  980. self.zoom(-delta * ratio, event);
  981. },
  982. cropStart: function cropStart(e) {
  983. var self = this;
  984. if (self.disabled) {
  985. return;
  986. }
  987. var options = self.options;
  988. var pointers = self.pointers;
  989. var originalEvent = e.originalEvent;
  990. var action = void 0;
  991. if (originalEvent && originalEvent.changedTouches) {
  992. // Handle touch event
  993. $.each(originalEvent.changedTouches, function (i, touch) {
  994. pointers[touch.identifier] = getPointer(touch);
  995. });
  996. } else {
  997. // Handle mouse event and pointer event
  998. pointers[originalEvent && originalEvent.pointerId || 0] = getPointer(originalEvent || e);
  999. }
  1000. if (objectKeys(pointers).length > 1 && options.zoomable && options.zoomOnTouch) {
  1001. action = 'zoom';
  1002. } else {
  1003. action = $(e.target).data('action');
  1004. }
  1005. if (!REGEXP_ACTIONS.test(action)) {
  1006. return;
  1007. }
  1008. if (self.trigger('cropstart', {
  1009. originalEvent: originalEvent,
  1010. action: action
  1011. }).isDefaultPrevented()) {
  1012. return;
  1013. }
  1014. e.preventDefault();
  1015. self.action = action;
  1016. self.cropping = false;
  1017. if (action === 'crop') {
  1018. self.cropping = true;
  1019. self.$dragBox.addClass('cropper-modal');
  1020. }
  1021. },
  1022. cropMove: function cropMove(e) {
  1023. var self = this;
  1024. var action = self.action;
  1025. if (self.disabled || !action) {
  1026. return;
  1027. }
  1028. var pointers = self.pointers;
  1029. var originalEvent = e.originalEvent;
  1030. e.preventDefault();
  1031. if (self.trigger('cropmove', {
  1032. originalEvent: originalEvent,
  1033. action: action
  1034. }).isDefaultPrevented()) {
  1035. return;
  1036. }
  1037. if (originalEvent && originalEvent.changedTouches) {
  1038. $.each(originalEvent.changedTouches, function (i, touch) {
  1039. $.extend(pointers[touch.identifier], getPointer(touch, true));
  1040. });
  1041. } else {
  1042. $.extend(pointers[originalEvent && originalEvent.pointerId || 0], getPointer(originalEvent || e, true));
  1043. }
  1044. self.change(e);
  1045. },
  1046. cropEnd: function cropEnd(e) {
  1047. var self = this;
  1048. if (self.disabled) {
  1049. return;
  1050. }
  1051. var action = self.action;
  1052. var pointers = self.pointers;
  1053. var originalEvent = e.originalEvent;
  1054. if (originalEvent && originalEvent.changedTouches) {
  1055. $.each(originalEvent.changedTouches, function (i, touch) {
  1056. delete pointers[touch.identifier];
  1057. });
  1058. } else {
  1059. delete pointers[originalEvent && originalEvent.pointerId || 0];
  1060. }
  1061. if (!action) {
  1062. return;
  1063. }
  1064. e.preventDefault();
  1065. if (!objectKeys(pointers).length) {
  1066. self.action = '';
  1067. }
  1068. if (self.cropping) {
  1069. self.cropping = false;
  1070. self.$dragBox.toggleClass('cropper-modal', self.cropped && self.options.modal);
  1071. }
  1072. self.trigger('cropend', {
  1073. originalEvent: originalEvent,
  1074. action: action
  1075. });
  1076. }
  1077. };
  1078. // Actions
  1079. var ACTION_EAST = 'e';
  1080. var ACTION_WEST = 'w';
  1081. var ACTION_SOUTH = 's';
  1082. var ACTION_NORTH = 'n';
  1083. var ACTION_SOUTH_EAST = 'se';
  1084. var ACTION_SOUTH_WEST = 'sw';
  1085. var ACTION_NORTH_EAST = 'ne';
  1086. var ACTION_NORTH_WEST = 'nw';
  1087. function getMaxZoomRatio(pointers) {
  1088. var pointers2 = $.extend({}, pointers);
  1089. var ratios = [];
  1090. $.each(pointers, function (pointerId, pointer) {
  1091. delete pointers2[pointerId];
  1092. $.each(pointers2, function (pointerId2, pointer2) {
  1093. var x1 = Math.abs(pointer.startX - pointer2.startX);
  1094. var y1 = Math.abs(pointer.startY - pointer2.startY);
  1095. var x2 = Math.abs(pointer.endX - pointer2.endX);
  1096. var y2 = Math.abs(pointer.endY - pointer2.endY);
  1097. var z1 = Math.sqrt(x1 * x1 + y1 * y1);
  1098. var z2 = Math.sqrt(x2 * x2 + y2 * y2);
  1099. var ratio = (z2 - z1) / z1;
  1100. ratios.push(ratio);
  1101. });
  1102. });
  1103. ratios.sort(function (a, b) {
  1104. return Math.abs(a) < Math.abs(b);
  1105. });
  1106. return ratios[0];
  1107. }
  1108. var change$1 = {
  1109. change: function change(e) {
  1110. var self = this;
  1111. var options = self.options;
  1112. var pointers = self.pointers;
  1113. var pointer = pointers[objectKeys(pointers)[0]];
  1114. var container = self.container;
  1115. var canvas = self.canvas;
  1116. var cropBox = self.cropBox;
  1117. var action = self.action;
  1118. var aspectRatio = options.aspectRatio;
  1119. var width = cropBox.width;
  1120. var height = cropBox.height;
  1121. var left = cropBox.left;
  1122. var top = cropBox.top;
  1123. var right = left + width;
  1124. var bottom = top + height;
  1125. var minLeft = 0;
  1126. var minTop = 0;
  1127. var maxWidth = container.width;
  1128. var maxHeight = container.height;
  1129. var renderable = true;
  1130. var offset = void 0;
  1131. // Locking aspect ratio in "free mode" by holding shift key (#259)
  1132. if (!aspectRatio && e.shiftKey) {
  1133. aspectRatio = width && height ? width / height : 1;
  1134. }
  1135. if (self.limited) {
  1136. minLeft = cropBox.minLeft;
  1137. minTop = cropBox.minTop;
  1138. maxWidth = minLeft + Math.min(container.width, canvas.width, canvas.left + canvas.width);
  1139. maxHeight = minTop + Math.min(container.height, canvas.height, canvas.top + canvas.height);
  1140. }
  1141. var range = {
  1142. x: pointer.endX - pointer.startX,
  1143. y: pointer.endY - pointer.startY
  1144. };
  1145. if (aspectRatio) {
  1146. range.X = range.y * aspectRatio;
  1147. range.Y = range.x / aspectRatio;
  1148. }
  1149. switch (action) {
  1150. // Move crop box
  1151. case 'all':
  1152. left += range.x;
  1153. top += range.y;
  1154. break;
  1155. // Resize crop box
  1156. case ACTION_EAST:
  1157. if (range.x >= 0 && (right >= maxWidth || aspectRatio && (top <= minTop || bottom >= maxHeight))) {
  1158. renderable = false;
  1159. break;
  1160. }
  1161. width += range.x;
  1162. if (aspectRatio) {
  1163. height = width / aspectRatio;
  1164. top -= range.Y / 2;
  1165. }
  1166. if (width < 0) {
  1167. action = ACTION_WEST;
  1168. width = 0;
  1169. }
  1170. break;
  1171. case ACTION_NORTH:
  1172. if (range.y <= 0 && (top <= minTop || aspectRatio && (left <= minLeft || right >= maxWidth))) {
  1173. renderable = false;
  1174. break;
  1175. }
  1176. height -= range.y;
  1177. top += range.y;
  1178. if (aspectRatio) {
  1179. width = height * aspectRatio;
  1180. left += range.X / 2;
  1181. }
  1182. if (height < 0) {
  1183. action = ACTION_SOUTH;
  1184. height = 0;
  1185. }
  1186. break;
  1187. case ACTION_WEST:
  1188. if (range.x <= 0 && (left <= minLeft || aspectRatio && (top <= minTop || bottom >= maxHeight))) {
  1189. renderable = false;
  1190. break;
  1191. }
  1192. width -= range.x;
  1193. left += range.x;
  1194. if (aspectRatio) {
  1195. height = width / aspectRatio;
  1196. top += range.Y / 2;
  1197. }
  1198. if (width < 0) {
  1199. action = ACTION_EAST;
  1200. width = 0;
  1201. }
  1202. break;
  1203. case ACTION_SOUTH:
  1204. if (range.y >= 0 && (bottom >= maxHeight || aspectRatio && (left <= minLeft || right >= maxWidth))) {
  1205. renderable = false;
  1206. break;
  1207. }
  1208. height += range.y;
  1209. if (aspectRatio) {
  1210. width = height * aspectRatio;
  1211. left -= range.X / 2;
  1212. }
  1213. if (height < 0) {
  1214. action = ACTION_NORTH;
  1215. height = 0;
  1216. }
  1217. break;
  1218. case ACTION_NORTH_EAST:
  1219. if (aspectRatio) {
  1220. if (range.y <= 0 && (top <= minTop || right >= maxWidth)) {
  1221. renderable = false;
  1222. break;
  1223. }
  1224. height -= range.y;
  1225. top += range.y;
  1226. width = height * aspectRatio;
  1227. } else {
  1228. if (range.x >= 0) {
  1229. if (right < maxWidth) {
  1230. width += range.x;
  1231. } else if (range.y <= 0 && top <= minTop) {
  1232. renderable = false;
  1233. }
  1234. } else {
  1235. width += range.x;
  1236. }
  1237. if (range.y <= 0) {
  1238. if (top > minTop) {
  1239. height -= range.y;
  1240. top += range.y;
  1241. }
  1242. } else {
  1243. height -= range.y;
  1244. top += range.y;
  1245. }
  1246. }
  1247. if (width < 0 && height < 0) {
  1248. action = ACTION_SOUTH_WEST;
  1249. height = 0;
  1250. width = 0;
  1251. } else if (width < 0) {
  1252. action = ACTION_NORTH_WEST;
  1253. width = 0;
  1254. } else if (height < 0) {
  1255. action = ACTION_SOUTH_EAST;
  1256. height = 0;
  1257. }
  1258. break;
  1259. case ACTION_NORTH_WEST:
  1260. if (aspectRatio) {
  1261. if (range.y <= 0 && (top <= minTop || left <= minLeft)) {
  1262. renderable = false;
  1263. break;
  1264. }
  1265. height -= range.y;
  1266. top += range.y;
  1267. width = height * aspectRatio;
  1268. left += range.X;
  1269. } else {
  1270. if (range.x <= 0) {
  1271. if (left > minLeft) {
  1272. width -= range.x;
  1273. left += range.x;
  1274. } else if (range.y <= 0 && top <= minTop) {
  1275. renderable = false;
  1276. }
  1277. } else {
  1278. width -= range.x;
  1279. left += range.x;
  1280. }
  1281. if (range.y <= 0) {
  1282. if (top > minTop) {
  1283. height -= range.y;
  1284. top += range.y;
  1285. }
  1286. } else {
  1287. height -= range.y;
  1288. top += range.y;
  1289. }
  1290. }
  1291. if (width < 0 && height < 0) {
  1292. action = ACTION_SOUTH_EAST;
  1293. height = 0;
  1294. width = 0;
  1295. } else if (width < 0) {
  1296. action = ACTION_NORTH_EAST;
  1297. width = 0;
  1298. } else if (height < 0) {
  1299. action = ACTION_SOUTH_WEST;
  1300. height = 0;
  1301. }
  1302. break;
  1303. case ACTION_SOUTH_WEST:
  1304. if (aspectRatio) {
  1305. if (range.x <= 0 && (left <= minLeft || bottom >= maxHeight)) {
  1306. renderable = false;
  1307. break;
  1308. }
  1309. width -= range.x;
  1310. left += range.x;
  1311. height = width / aspectRatio;
  1312. } else {
  1313. if (range.x <= 0) {
  1314. if (left > minLeft) {
  1315. width -= range.x;
  1316. left += range.x;
  1317. } else if (range.y >= 0 && bottom >= maxHeight) {
  1318. renderable = false;
  1319. }
  1320. } else {
  1321. width -= range.x;
  1322. left += range.x;
  1323. }
  1324. if (range.y >= 0) {
  1325. if (bottom < maxHeight) {
  1326. height += range.y;
  1327. }
  1328. } else {
  1329. height += range.y;
  1330. }
  1331. }
  1332. if (width < 0 && height < 0) {
  1333. action = ACTION_NORTH_EAST;
  1334. height = 0;
  1335. width = 0;
  1336. } else if (width < 0) {
  1337. action = ACTION_SOUTH_EAST;
  1338. width = 0;
  1339. } else if (height < 0) {
  1340. action = ACTION_NORTH_WEST;
  1341. height = 0;
  1342. }
  1343. break;
  1344. case ACTION_SOUTH_EAST:
  1345. if (aspectRatio) {
  1346. if (range.x >= 0 && (right >= maxWidth || bottom >= maxHeight)) {
  1347. renderable = false;
  1348. break;
  1349. }
  1350. width += range.x;
  1351. height = width / aspectRatio;
  1352. } else {
  1353. if (range.x >= 0) {
  1354. if (right < maxWidth) {
  1355. width += range.x;
  1356. } else if (range.y >= 0 && bottom >= maxHeight) {
  1357. renderable = false;
  1358. }
  1359. } else {
  1360. width += range.x;
  1361. }
  1362. if (range.y >= 0) {
  1363. if (bottom < maxHeight) {
  1364. height += range.y;
  1365. }
  1366. } else {
  1367. height += range.y;
  1368. }
  1369. }
  1370. if (width < 0 && height < 0) {
  1371. action = ACTION_NORTH_WEST;
  1372. height = 0;
  1373. width = 0;
  1374. } else if (width < 0) {
  1375. action = ACTION_SOUTH_WEST;
  1376. width = 0;
  1377. } else if (height < 0) {
  1378. action = ACTION_NORTH_EAST;
  1379. height = 0;
  1380. }
  1381. break;
  1382. // Move canvas
  1383. case 'move':
  1384. self.move(range.x, range.y);
  1385. renderable = false;
  1386. break;
  1387. // Zoom canvas
  1388. case 'zoom':
  1389. self.zoom(getMaxZoomRatio(pointers), e.originalEvent);
  1390. renderable = false;
  1391. break;
  1392. // Create crop box
  1393. case 'crop':
  1394. if (!range.x || !range.y) {
  1395. renderable = false;
  1396. break;
  1397. }
  1398. offset = self.$cropper.offset();
  1399. left = pointer.startX - offset.left;
  1400. top = pointer.startY - offset.top;
  1401. width = cropBox.minWidth;
  1402. height = cropBox.minHeight;
  1403. if (range.x > 0) {
  1404. action = range.y > 0 ? ACTION_SOUTH_EAST : ACTION_NORTH_EAST;
  1405. } else if (range.x < 0) {
  1406. left -= width;
  1407. action = range.y > 0 ? ACTION_SOUTH_WEST : ACTION_NORTH_WEST;
  1408. }
  1409. if (range.y < 0) {
  1410. top -= height;
  1411. }
  1412. // Show the crop box if is hidden
  1413. if (!self.cropped) {
  1414. self.$cropBox.removeClass('cropper-hidden');
  1415. self.cropped = true;
  1416. if (self.limited) {
  1417. self.limitCropBox(true, true);
  1418. }
  1419. }
  1420. break;
  1421. // No default
  1422. }
  1423. if (renderable) {
  1424. cropBox.width = width;
  1425. cropBox.height = height;
  1426. cropBox.left = left;
  1427. cropBox.top = top;
  1428. self.action = action;
  1429. self.renderCropBox();
  1430. }
  1431. // Override
  1432. $.each(pointers, function (i, p) {
  1433. p.startX = p.endX;
  1434. p.startY = p.endY;
  1435. });
  1436. }
  1437. };
  1438. var classCallCheck = function (instance, Constructor) {
  1439. if (!(instance instanceof Constructor)) {
  1440. throw new TypeError("Cannot call a class as a function");
  1441. }
  1442. };
  1443. var createClass = function () {
  1444. function defineProperties(target, props) {
  1445. for (var i = 0; i < props.length; i++) {
  1446. var descriptor = props[i];
  1447. descriptor.enumerable = descriptor.enumerable || false;
  1448. descriptor.configurable = true;
  1449. if ("value" in descriptor) descriptor.writable = true;
  1450. Object.defineProperty(target, descriptor.key, descriptor);
  1451. }
  1452. }
  1453. return function (Constructor, protoProps, staticProps) {
  1454. if (protoProps) defineProperties(Constructor.prototype, protoProps);
  1455. if (staticProps) defineProperties(Constructor, staticProps);
  1456. return Constructor;
  1457. };
  1458. }();
  1459. var get = function get(object, property, receiver) {
  1460. if (object === null) object = Function.prototype;
  1461. var desc = Object.getOwnPropertyDescriptor(object, property);
  1462. if (desc === undefined) {
  1463. var parent = Object.getPrototypeOf(object);
  1464. if (parent === null) {
  1465. return undefined;
  1466. } else {
  1467. return get(parent, property, receiver);
  1468. }
  1469. } else if ("value" in desc) {
  1470. return desc.value;
  1471. } else {
  1472. var getter = desc.get;
  1473. if (getter === undefined) {
  1474. return undefined;
  1475. }
  1476. return getter.call(receiver);
  1477. }
  1478. };
  1479. var set = function set(object, property, value, receiver) {
  1480. var desc = Object.getOwnPropertyDescriptor(object, property);
  1481. if (desc === undefined) {
  1482. var parent = Object.getPrototypeOf(object);
  1483. if (parent !== null) {
  1484. set(parent, property, value, receiver);
  1485. }
  1486. } else if ("value" in desc && desc.writable) {
  1487. desc.value = value;
  1488. } else {
  1489. var setter = desc.set;
  1490. if (setter !== undefined) {
  1491. setter.call(receiver, value);
  1492. }
  1493. }
  1494. return value;
  1495. };
  1496. var toConsumableArray = function (arr) {
  1497. if (Array.isArray(arr)) {
  1498. for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
  1499. return arr2;
  1500. } else {
  1501. return Array.from(arr);
  1502. }
  1503. };
  1504. function getPointersCenter(pointers) {
  1505. var pageX = 0;
  1506. var pageY = 0;
  1507. var count = 0;
  1508. $.each(pointers, function (i, _ref) {
  1509. var startX = _ref.startX,
  1510. startY = _ref.startY;
  1511. pageX += startX;
  1512. pageY += startY;
  1513. count += 1;
  1514. });
  1515. pageX /= count;
  1516. pageY /= count;
  1517. return {
  1518. pageX: pageX,
  1519. pageY: pageY
  1520. };
  1521. }
  1522. var methods = {
  1523. // Show the crop box manually
  1524. crop: function crop() {
  1525. var self = this;
  1526. if (!self.ready || self.disabled) {
  1527. return;
  1528. }
  1529. if (!self.cropped) {
  1530. self.cropped = true;
  1531. self.limitCropBox(true, true);
  1532. if (self.options.modal) {
  1533. self.$dragBox.addClass('cropper-modal');
  1534. }
  1535. self.$cropBox.removeClass('cropper-hidden');
  1536. }
  1537. self.setCropBoxData(self.initialCropBox);
  1538. },
  1539. // Reset the image and crop box to their initial states
  1540. reset: function reset() {
  1541. var self = this;
  1542. if (!self.ready || self.disabled) {
  1543. return;
  1544. }
  1545. self.image = $.extend({}, self.initialImage);
  1546. self.canvas = $.extend({}, self.initialCanvas);
  1547. self.cropBox = $.extend({}, self.initialCropBox);
  1548. self.renderCanvas();
  1549. if (self.cropped) {
  1550. self.renderCropBox();
  1551. }
  1552. },
  1553. // Clear the crop box
  1554. clear: function clear() {
  1555. var self = this;
  1556. if (!self.cropped || self.disabled) {
  1557. return;
  1558. }
  1559. $.extend(self.cropBox, {
  1560. left: 0,
  1561. top: 0,
  1562. width: 0,
  1563. height: 0
  1564. });
  1565. self.cropped = false;
  1566. self.renderCropBox();
  1567. self.limitCanvas(true, true);
  1568. // Render canvas after crop box rendered
  1569. self.renderCanvas();
  1570. self.$dragBox.removeClass('cropper-modal');
  1571. self.$cropBox.addClass('cropper-hidden');
  1572. },
  1573. /**
  1574. * Replace the image's src and rebuild the cropper
  1575. *
  1576. * @param {String} url
  1577. * @param {Boolean} onlyColorChanged (optional)
  1578. */
  1579. replace: function replace(url, onlyColorChanged) {
  1580. var self = this;
  1581. if (!self.disabled && url) {
  1582. if (self.isImg) {
  1583. self.$element.attr('src', url);
  1584. }
  1585. if (onlyColorChanged) {
  1586. self.url = url;
  1587. self.$clone.attr('src', url);
  1588. if (self.ready) {
  1589. self.$preview.find('img').add(self.$clone2).attr('src', url);
  1590. }
  1591. } else {
  1592. if (self.isImg) {
  1593. self.replaced = true;
  1594. }
  1595. // Clear previous data
  1596. self.options.data = null;
  1597. self.load(url);
  1598. }
  1599. }
  1600. },
  1601. // Enable (unfreeze) the cropper
  1602. enable: function enable() {
  1603. var self = this;
  1604. if (self.ready) {
  1605. self.disabled = false;
  1606. self.$cropper.removeClass('cropper-disabled');
  1607. }
  1608. },
  1609. // Disable (freeze) the cropper
  1610. disable: function disable() {
  1611. var self = this;
  1612. if (self.ready) {
  1613. self.disabled = true;
  1614. self.$cropper.addClass('cropper-disabled');
  1615. }
  1616. },
  1617. // Destroy the cropper and remove the instance from the image
  1618. destroy: function destroy() {
  1619. var self = this;
  1620. var $this = self.$element;
  1621. if (self.loaded) {
  1622. if (self.isImg && self.replaced) {
  1623. $this.attr('src', self.originalUrl);
  1624. }
  1625. self.unbuild();
  1626. $this.removeClass('cropper-hidden');
  1627. } else if (self.isImg) {
  1628. $this.off('load', self.start);
  1629. } else if (self.$clone) {
  1630. self.$clone.remove();
  1631. }
  1632. $this.removeData('cropper');
  1633. },
  1634. /**
  1635. * Move the canvas with relative offsets
  1636. *
  1637. * @param {Number} offsetX
  1638. * @param {Number} offsetY (optional)
  1639. */
  1640. move: function move(offsetX, offsetY) {
  1641. var self = this;
  1642. var canvas = self.canvas;
  1643. self.moveTo(isUndefined(offsetX) ? offsetX : canvas.left + Number(offsetX), isUndefined(offsetY) ? offsetY : canvas.top + Number(offsetY));
  1644. },
  1645. /**
  1646. * Move the canvas to an absolute point
  1647. *
  1648. * @param {Number} x
  1649. * @param {Number} y (optional)
  1650. */
  1651. moveTo: function moveTo(x, y) {
  1652. var self = this;
  1653. var canvas = self.canvas;
  1654. var changed = false;
  1655. // If "y" is not present, its default value is "x"
  1656. if (isUndefined(y)) {
  1657. y = x;
  1658. }
  1659. x = Number(x);
  1660. y = Number(y);
  1661. if (self.ready && !self.disabled && self.options.movable) {
  1662. if (isNumber(x)) {
  1663. canvas.left = x;
  1664. changed = true;
  1665. }
  1666. if (isNumber(y)) {
  1667. canvas.top = y;
  1668. changed = true;
  1669. }
  1670. if (changed) {
  1671. self.renderCanvas(true);
  1672. }
  1673. }
  1674. },
  1675. /**
  1676. * Zoom the canvas with a relative ratio
  1677. *
  1678. * @param {Number} ratio
  1679. * @param {jQuery Event} _event (private)
  1680. */
  1681. zoom: function zoom(ratio, _event) {
  1682. var self = this;
  1683. var canvas = self.canvas;
  1684. ratio = Number(ratio);
  1685. if (ratio < 0) {
  1686. ratio = 1 / (1 - ratio);
  1687. } else {
  1688. ratio = 1 + ratio;
  1689. }
  1690. self.zoomTo(canvas.width * ratio / canvas.naturalWidth, _event);
  1691. },
  1692. /**
  1693. * Zoom the canvas to an absolute ratio
  1694. *
  1695. * @param {Number} ratio
  1696. * @param {jQuery Event} _event (private)
  1697. */
  1698. zoomTo: function zoomTo(ratio, _event) {
  1699. var self = this;
  1700. var options = self.options;
  1701. var pointers = self.pointers;
  1702. var canvas = self.canvas;
  1703. var width = canvas.width;
  1704. var height = canvas.height;
  1705. var naturalWidth = canvas.naturalWidth;
  1706. var naturalHeight = canvas.naturalHeight;
  1707. ratio = Number(ratio);
  1708. if (ratio >= 0 && self.ready && !self.disabled && options.zoomable) {
  1709. var newWidth = naturalWidth * ratio;
  1710. var newHeight = naturalHeight * ratio;
  1711. var originalEvent = void 0;
  1712. if (_event) {
  1713. originalEvent = _event.originalEvent;
  1714. }
  1715. if (self.trigger('zoom', {
  1716. originalEvent: originalEvent,
  1717. oldRatio: width / naturalWidth,
  1718. ratio: newWidth / naturalWidth
  1719. }).isDefaultPrevented()) {
  1720. return;
  1721. }
  1722. if (originalEvent) {
  1723. var offset = self.$cropper.offset();
  1724. var center = pointers && objectKeys(pointers).length ? getPointersCenter(pointers) : {
  1725. pageX: _event.pageX || originalEvent.pageX || 0,
  1726. pageY: _event.pageY || originalEvent.pageY || 0
  1727. };
  1728. // Zoom from the triggering point of the event
  1729. canvas.left -= (newWidth - width) * ((center.pageX - offset.left - canvas.left) / width);
  1730. canvas.top -= (newHeight - height) * ((center.pageY - offset.top - canvas.top) / height);
  1731. } else {
  1732. // Zoom from the center of the canvas
  1733. canvas.left -= (newWidth - width) / 2;
  1734. canvas.top -= (newHeight - height) / 2;
  1735. }
  1736. canvas.width = newWidth;
  1737. canvas.height = newHeight;
  1738. self.renderCanvas(true);
  1739. }
  1740. },
  1741. /**
  1742. * Rotate the canvas with a relative degree
  1743. *
  1744. * @param {Number} degree
  1745. */
  1746. rotate: function rotate(degree) {
  1747. var self = this;
  1748. self.rotateTo((self.image.rotate || 0) + Number(degree));
  1749. },
  1750. /**
  1751. * Rotate the canvas to an absolute degree
  1752. * https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function#rotate()
  1753. *
  1754. * @param {Number} degree
  1755. */
  1756. rotateTo: function rotateTo(degree) {
  1757. var self = this;
  1758. degree = Number(degree);
  1759. if (isNumber(degree) && self.ready && !self.disabled && self.options.rotatable) {
  1760. self.image.rotate = degree % 360;
  1761. self.rotated = true;
  1762. self.renderCanvas(true);
  1763. }
  1764. },
  1765. /**
  1766. * Scale the image
  1767. * https://developer.mozilla.org/en-US/docs/Web/CSS/transform-function#scale()
  1768. *
  1769. * @param {Number} scaleX
  1770. * @param {Number} scaleY (optional)
  1771. */
  1772. scale: function scale(scaleX, scaleY) {
  1773. var self = this;
  1774. var image = self.image;
  1775. var changed = false;
  1776. // If "scaleY" is not present, its default value is "scaleX"
  1777. if (isUndefined(scaleY)) {
  1778. scaleY = scaleX;
  1779. }
  1780. scaleX = Number(scaleX);
  1781. scaleY = Number(scaleY);
  1782. if (self.ready && !self.disabled && self.options.scalable) {
  1783. if (isNumber(scaleX)) {
  1784. image.scaleX = scaleX;
  1785. changed = true;
  1786. }
  1787. if (isNumber(scaleY)) {
  1788. image.scaleY = scaleY;
  1789. changed = true;
  1790. }
  1791. if (changed) {
  1792. self.renderImage(true);
  1793. }
  1794. }
  1795. },
  1796. /**
  1797. * Scale the abscissa of the image
  1798. *
  1799. * @param {Number} scaleX
  1800. */
  1801. scaleX: function scaleX(_scaleX) {
  1802. var self = this;
  1803. var scaleY = self.image.scaleY;
  1804. self.scale(_scaleX, isNumber(scaleY) ? scaleY : 1);
  1805. },
  1806. /**
  1807. * Scale the ordinate of the image
  1808. *
  1809. * @param {Number} scaleY
  1810. */
  1811. scaleY: function scaleY(_scaleY) {
  1812. var self = this;
  1813. var scaleX = self.image.scaleX;
  1814. self.scale(isNumber(scaleX) ? scaleX : 1, _scaleY);
  1815. },
  1816. /**
  1817. * Get the cropped area position and size data (base on the original image)
  1818. *
  1819. * @param {Boolean} isRounded (optional)
  1820. * @return {Object} data
  1821. */
  1822. getData: function getData(isRounded) {
  1823. var self = this;
  1824. var options = self.options;
  1825. var image = self.image;
  1826. var canvas = self.canvas;
  1827. var cropBox = self.cropBox;
  1828. var ratio = void 0;
  1829. var data = void 0;
  1830. if (self.ready && self.cropped) {
  1831. data = {
  1832. x: cropBox.left - canvas.left,
  1833. y: cropBox.top - canvas.top,
  1834. width: cropBox.width,
  1835. height: cropBox.height
  1836. };
  1837. ratio = image.width / image.naturalWidth;
  1838. $.each(data, function (i, n) {
  1839. n /= ratio;
  1840. data[i] = isRounded ? Math.round(n) : n;
  1841. });
  1842. } else {
  1843. data = {
  1844. x: 0,
  1845. y: 0,
  1846. width: 0,
  1847. height: 0
  1848. };
  1849. }
  1850. if (options.rotatable) {
  1851. data.rotate = image.rotate || 0;
  1852. }
  1853. if (options.scalable) {
  1854. data.scaleX = image.scaleX || 1;
  1855. data.scaleY = image.scaleY || 1;
  1856. }
  1857. return data;
  1858. },
  1859. /**
  1860. * Set the cropped area position and size with new data
  1861. *
  1862. * @param {Object} data
  1863. */
  1864. setData: function setData(data) {
  1865. var self = this;
  1866. var options = self.options;
  1867. var image = self.image;
  1868. var canvas = self.canvas;
  1869. var cropBoxData = {};
  1870. var rotated = void 0;
  1871. var isScaled = void 0;
  1872. var ratio = void 0;
  1873. if ($.isFunction(data)) {
  1874. data = data.call(self.element);
  1875. }
  1876. if (self.ready && !self.disabled && $.isPlainObject(data)) {
  1877. if (options.rotatable) {
  1878. if (isNumber(data.rotate) && data.rotate !== image.rotate) {
  1879. image.rotate = data.rotate;
  1880. self.rotated = rotated = true;
  1881. }
  1882. }
  1883. if (options.scalable) {
  1884. if (isNumber(data.scaleX) && data.scaleX !== image.scaleX) {
  1885. image.scaleX = data.scaleX;
  1886. isScaled = true;
  1887. }
  1888. if (isNumber(data.scaleY) && data.scaleY !== image.scaleY) {
  1889. image.scaleY = data.scaleY;
  1890. isScaled = true;
  1891. }
  1892. }
  1893. if (rotated) {
  1894. self.renderCanvas();
  1895. } else if (isScaled) {
  1896. self.renderImage();
  1897. }
  1898. ratio = image.width / image.naturalWidth;
  1899. if (isNumber(data.x)) {
  1900. cropBoxData.left = data.x * ratio + canvas.left;
  1901. }
  1902. if (isNumber(data.y)) {
  1903. cropBoxData.top = data.y * ratio + canvas.top;
  1904. }
  1905. if (isNumber(data.width)) {
  1906. cropBoxData.width = data.width * ratio;
  1907. }
  1908. if (isNumber(data.height)) {
  1909. cropBoxData.height = data.height * ratio;
  1910. }
  1911. self.setCropBoxData(cropBoxData);
  1912. }
  1913. },
  1914. /**
  1915. * Get the container size data
  1916. *
  1917. * @return {Object} data
  1918. */
  1919. getContainerData: function getContainerData() {
  1920. return this.ready ? this.container : {};
  1921. },
  1922. /**
  1923. * Get the image position and size data
  1924. *
  1925. * @return {Object} data
  1926. */
  1927. getImageData: function getImageData() {
  1928. return this.loaded ? this.image : {};
  1929. },
  1930. /**
  1931. * Get the canvas position and size data
  1932. *
  1933. * @return {Object} data
  1934. */
  1935. getCanvasData: function getCanvasData() {
  1936. var self = this;
  1937. var canvas = self.canvas;
  1938. var data = {};
  1939. if (self.ready) {
  1940. $.each(['left', 'top', 'width', 'height', 'naturalWidth', 'naturalHeight'], function (i, n) {
  1941. data[n] = canvas[n];
  1942. });
  1943. }
  1944. return data;
  1945. },
  1946. /**
  1947. * Set the canvas position and size with new data
  1948. *
  1949. * @param {Object} data
  1950. */
  1951. setCanvasData: function setCanvasData(data) {
  1952. var self = this;
  1953. var canvas = self.canvas;
  1954. var aspectRatio = canvas.aspectRatio;
  1955. if ($.isFunction(data)) {
  1956. data = data.call(self.$element);
  1957. }
  1958. if (self.ready && !self.disabled && $.isPlainObject(data)) {
  1959. if (isNumber(data.left)) {
  1960. canvas.left = data.left;
  1961. }
  1962. if (isNumber(data.top)) {
  1963. canvas.top = data.top;
  1964. }
  1965. if (isNumber(data.width)) {
  1966. canvas.width = data.width;
  1967. canvas.height = data.width / aspectRatio;
  1968. } else if (isNumber(data.height)) {
  1969. canvas.height = data.height;
  1970. canvas.width = data.height * aspectRatio;
  1971. }
  1972. self.renderCanvas(true);
  1973. }
  1974. },
  1975. /**
  1976. * Get the crop box position and size data
  1977. *
  1978. * @return {Object} data
  1979. */
  1980. getCropBoxData: function getCropBoxData() {
  1981. var self = this;
  1982. var cropBox = self.cropBox;
  1983. return self.ready && self.cropped ? {
  1984. left: cropBox.left,
  1985. top: cropBox.top,
  1986. width: cropBox.width,
  1987. height: cropBox.height
  1988. } : {};
  1989. },
  1990. /**
  1991. * Set the crop box position and size with new data
  1992. *
  1993. * @param {Object} data
  1994. */
  1995. setCropBoxData: function setCropBoxData(data) {
  1996. var self = this;
  1997. var cropBox = self.cropBox;
  1998. var aspectRatio = self.options.aspectRatio;
  1999. var widthChanged = void 0;
  2000. var heightChanged = void 0;
  2001. if ($.isFunction(data)) {
  2002. data = data.call(self.$element);
  2003. }
  2004. if (self.ready && self.cropped && !self.disabled && $.isPlainObject(data)) {
  2005. if (isNumber(data.left)) {
  2006. cropBox.left = data.left;
  2007. }
  2008. if (isNumber(data.top)) {
  2009. cropBox.top = data.top;
  2010. }
  2011. if (isNumber(data.width) && data.width !== cropBox.width) {
  2012. widthChanged = true;
  2013. cropBox.width = data.width;
  2014. }
  2015. if (isNumber(data.height) && data.height !== cropBox.height) {
  2016. heightChanged = true;
  2017. cropBox.height = data.height;
  2018. }
  2019. if (aspectRatio) {
  2020. if (widthChanged) {
  2021. cropBox.height = cropBox.width / aspectRatio;
  2022. } else if (heightChanged) {
  2023. cropBox.width = cropBox.height * aspectRatio;
  2024. }
  2025. }
  2026. self.renderCropBox();
  2027. }
  2028. },
  2029. /**
  2030. * Get a canvas drawn the cropped image
  2031. *
  2032. * @param {Object} options (optional)
  2033. * @return {HTMLCanvasElement} canvas
  2034. */
  2035. getCroppedCanvas: function getCroppedCanvas(options) {
  2036. var self = this;
  2037. if (!self.ready || !window.HTMLCanvasElement) {
  2038. return null;
  2039. }
  2040. if (!self.cropped) {
  2041. return getSourceCanvas(self.$clone[0], self.image);
  2042. }
  2043. if (!$.isPlainObject(options)) {
  2044. options = {};
  2045. }
  2046. var data = self.getData();
  2047. var originalWidth = data.width;
  2048. var originalHeight = data.height;
  2049. var aspectRatio = originalWidth / originalHeight;
  2050. var scaledWidth = void 0;
  2051. var scaledHeight = void 0;
  2052. var scaledRatio = void 0;
  2053. if ($.isPlainObject(options)) {
  2054. scaledWidth = options.width;
  2055. scaledHeight = options.height;
  2056. if (scaledWidth) {
  2057. scaledHeight = scaledWidth / aspectRatio;
  2058. scaledRatio = scaledWidth / originalWidth;
  2059. } else if (scaledHeight) {
  2060. scaledWidth = scaledHeight * aspectRatio;
  2061. scaledRatio = scaledHeight / originalHeight;
  2062. }
  2063. }
  2064. // The canvas element will use `Math.Math.floor` on a float number, so Math.floor first
  2065. var canvasWidth = Math.floor(scaledWidth || originalWidth);
  2066. var canvasHeight = Math.floor(scaledHeight || originalHeight);
  2067. var canvas = $('<canvas>')[0];
  2068. var context = canvas.getContext('2d');
  2069. canvas.width = canvasWidth;
  2070. canvas.height = canvasHeight;
  2071. if (options.fillColor) {
  2072. context.fillStyle = options.fillColor;
  2073. context.fillRect(0, 0, canvasWidth, canvasHeight);
  2074. }
  2075. // https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D.drawImage
  2076. var parameters = function () {
  2077. var source = getSourceCanvas(self.$clone[0], self.image);
  2078. var sourceWidth = source.width;
  2079. var sourceHeight = source.height;
  2080. var canvasData = self.canvas;
  2081. var params = [source];
  2082. // Source canvas
  2083. var srcX = data.x + canvasData.naturalWidth * (Math.abs(data.scaleX || 1) - 1) / 2;
  2084. var srcY = data.y + canvasData.naturalHeight * (Math.abs(data.scaleY || 1) - 1) / 2;
  2085. var srcWidth = void 0;
  2086. var srcHeight = void 0;
  2087. // Destination canvas
  2088. var dstX = void 0;
  2089. var dstY = void 0;
  2090. var dstWidth = void 0;
  2091. var dstHeight = void 0;
  2092. if (srcX <= -originalWidth || srcX > sourceWidth) {
  2093. srcX = srcWidth = dstX = dstWidth = 0;
  2094. } else if (srcX <= 0) {
  2095. dstX = -srcX;
  2096. srcX = 0;
  2097. srcWidth = dstWidth = Math.min(sourceWidth, originalWidth + srcX);
  2098. } else if (srcX <= sourceWidth) {
  2099. dstX = 0;
  2100. srcWidth = dstWidth = Math.min(originalWidth, sourceWidth - srcX);
  2101. }
  2102. if (srcWidth <= 0 || srcY <= -originalHeight || srcY > sourceHeight) {
  2103. srcY = srcHeight = dstY = dstHeight = 0;
  2104. } else if (srcY <= 0) {
  2105. dstY = -srcY;
  2106. srcY = 0;
  2107. srcHeight = dstHeight = Math.min(sourceHeight, originalHeight + srcY);
  2108. } else if (srcY <= sourceHeight) {
  2109. dstY = 0;
  2110. srcHeight = dstHeight = Math.min(originalHeight, sourceHeight - srcY);
  2111. }
  2112. // All the numerical parameters should be integer for `drawImage` (#476)
  2113. params.push(Math.floor(srcX), Math.floor(srcY), Math.floor(srcWidth), Math.floor(srcHeight));
  2114. // Scale destination sizes
  2115. if (scaledRatio) {
  2116. dstX *= scaledRatio;
  2117. dstY *= scaledRatio;
  2118. dstWidth *= scaledRatio;
  2119. dstHeight *= scaledRatio;
  2120. }
  2121. // Avoid "IndexSizeError" in IE and Firefox
  2122. if (dstWidth > 0 && dstHeight > 0) {
  2123. params.push(Math.floor(dstX), Math.floor(dstY), Math.floor(dstWidth), Math.floor(dstHeight));
  2124. }
  2125. return params;
  2126. }();
  2127. context.drawImage.apply(context, toConsumableArray(parameters));
  2128. return canvas;
  2129. },
  2130. /**
  2131. * Change the aspect ratio of the crop box
  2132. *
  2133. * @param {Number} aspectRatio
  2134. */
  2135. setAspectRatio: function setAspectRatio(aspectRatio) {
  2136. var self = this;
  2137. var options = self.options;
  2138. if (!self.disabled && !isUndefined(aspectRatio)) {
  2139. // 0 -> NaN
  2140. options.aspectRatio = Math.max(0, aspectRatio) || NaN;
  2141. if (self.ready) {
  2142. self.initCropBox();
  2143. if (self.cropped) {
  2144. self.renderCropBox();
  2145. }
  2146. }
  2147. }
  2148. },
  2149. /**
  2150. * Change the drag mode
  2151. *
  2152. * @param {String} mode (optional)
  2153. */
  2154. setDragMode: function setDragMode(mode) {
  2155. var self = this;
  2156. var options = self.options;
  2157. var croppable = void 0;
  2158. var movable = void 0;
  2159. if (self.loaded && !self.disabled) {
  2160. croppable = mode === 'crop';
  2161. movable = options.movable && mode === 'move';
  2162. mode = croppable || movable ? mode : 'none';
  2163. self.$dragBox.data('action', mode).toggleClass('cropper-crop', croppable).toggleClass('cropper-move', movable);
  2164. if (!options.cropBoxMovable) {
  2165. // Sync drag mode to crop box when it is not movable(#300)
  2166. self.$face.data('action', mode).toggleClass('cropper-crop', croppable).toggleClass('cropper-move', movable);
  2167. }
  2168. }
  2169. }
  2170. };
  2171. var CLASS_HIDDEN = 'cropper-hidden';
  2172. var REGEXP_DATA_URL = /^data:/;
  2173. var REGEXP_DATA_URL_JPEG = /^data:image\/jpeg;base64,/;
  2174. var Cropper = function () {
  2175. function Cropper(element, options) {
  2176. classCallCheck(this, Cropper);
  2177. var self = this;
  2178. self.$element = $(element);
  2179. self.options = $.extend({}, DEFAULTS, $.isPlainObject(options) && options);
  2180. self.loaded = false;
  2181. self.ready = false;
  2182. self.completed = false;
  2183. self.rotated = false;
  2184. self.cropped = false;
  2185. self.disabled = false;
  2186. self.replaced = false;
  2187. self.limited = false;
  2188. self.wheeling = false;
  2189. self.isImg = false;
  2190. self.originalUrl = '';
  2191. self.canvas = null;
  2192. self.cropBox = null;
  2193. self.pointers = {};
  2194. self.init();
  2195. }
  2196. createClass(Cropper, [{
  2197. key: 'init',
  2198. value: function init() {
  2199. var self = this;
  2200. var $this = self.$element;
  2201. var url = void 0;
  2202. if ($this.is('img')) {
  2203. self.isImg = true;
  2204. // Should use `$.fn.attr` here. e.g.: "img/picture.jpg"
  2205. self.originalUrl = url = $this.attr('src');
  2206. // Stop when it's a blank image
  2207. if (!url) {
  2208. return;
  2209. }
  2210. // Should use `$.fn.prop` here. e.g.: "http://example.com/img/picture.jpg"
  2211. url = $this.prop('src');
  2212. } else if ($this.is('canvas') && window.HTMLCanvasElement) {
  2213. url = $this[0].toDataURL();
  2214. }
  2215. self.load(url);
  2216. }
  2217. // A shortcut for triggering custom events
  2218. }, {
  2219. key: 'trigger',
  2220. value: function trigger(type, data) {
  2221. var e = $.Event(type, data);
  2222. this.$element.trigger(e);
  2223. return e;
  2224. }
  2225. }, {
  2226. key: 'load',
  2227. value: function load(url) {
  2228. var self = this;
  2229. var options = self.options;
  2230. var $this = self.$element;
  2231. if (!url) {
  2232. return;
  2233. }
  2234. self.url = url;
  2235. self.image = {};
  2236. if (!options.checkOrientation || !window.ArrayBuffer) {
  2237. self.clone();
  2238. return;
  2239. }
  2240. // XMLHttpRequest disallows to open a Data URL in some browsers like IE11 and Safari
  2241. if (REGEXP_DATA_URL.test(url)) {
  2242. if (REGEXP_DATA_URL_JPEG.test(url)) {
  2243. self.read(dataURLToArrayBuffer(url));
  2244. } else {
  2245. self.clone();
  2246. }
  2247. return;
  2248. }
  2249. var xhr = new XMLHttpRequest();
  2250. xhr.onerror = xhr.onabort = $.proxy(function () {
  2251. self.clone();
  2252. }, this);
  2253. xhr.onload = function load() {
  2254. self.read(this.response);
  2255. };
  2256. if (options.checkCrossOrigin && isCrossOriginURL(url) && $this.prop('crossOrigin')) {
  2257. url = addTimestamp(url);
  2258. }
  2259. xhr.open('get', url);
  2260. xhr.responseType = 'arraybuffer';
  2261. xhr.withCredentials = $this.prop('crossOrigin') === 'use-credentials';
  2262. xhr.send();
  2263. }
  2264. }, {
  2265. key: 'read',
  2266. value: function read(arrayBuffer) {
  2267. var self = this;
  2268. var options = self.options;
  2269. var orientation = getOrientation(arrayBuffer);
  2270. var image = self.image;
  2271. var rotate = 0;
  2272. var scaleX = 1;
  2273. var scaleY = 1;
  2274. if (orientation > 1) {
  2275. self.url = arrayBufferToDataURL(arrayBuffer);
  2276. switch (orientation) {
  2277. // flip horizontal
  2278. case 2:
  2279. scaleX = -1;
  2280. break;
  2281. // rotate left 180°
  2282. case 3:
  2283. rotate = -180;
  2284. break;
  2285. // flip vertical
  2286. case 4:
  2287. scaleY = -1;
  2288. break;
  2289. // flip vertical + rotate right 90°
  2290. case 5:
  2291. rotate = 90;
  2292. scaleY = -1;
  2293. break;
  2294. // rotate right 90°
  2295. case 6:
  2296. rotate = 90;
  2297. break;
  2298. // flip horizontal + rotate right 90°
  2299. case 7:
  2300. rotate = 90;
  2301. scaleX = -1;
  2302. break;
  2303. // rotate left 90°
  2304. case 8:
  2305. rotate = -90;
  2306. break;
  2307. }
  2308. }
  2309. if (options.rotatable) {
  2310. image.rotate = rotate;
  2311. }
  2312. if (options.scalable) {
  2313. image.scaleX = scaleX;
  2314. image.scaleY = scaleY;
  2315. }
  2316. self.clone();
  2317. }
  2318. }, {
  2319. key: 'clone',
  2320. value: function clone() {
  2321. var self = this;
  2322. var options = self.options;
  2323. var $this = self.$element;
  2324. var url = self.url;
  2325. var crossOrigin = '';
  2326. var crossOriginUrl = void 0;
  2327. if (options.checkCrossOrigin && isCrossOriginURL(url)) {
  2328. crossOrigin = $this.prop('crossOrigin');
  2329. if (crossOrigin) {
  2330. crossOriginUrl = url;
  2331. } else {
  2332. crossOrigin = 'anonymous';
  2333. // Bust cache (#148) when there is not a "crossOrigin" property
  2334. crossOriginUrl = addTimestamp(url);
  2335. }
  2336. }
  2337. self.crossOrigin = crossOrigin;
  2338. self.crossOriginUrl = crossOriginUrl;
  2339. var image = document.createElement('img');
  2340. if (crossOrigin) {
  2341. image.crossOrigin = crossOrigin;
  2342. }
  2343. image.src = crossOriginUrl || url;
  2344. var $clone = $(image);
  2345. self.$clone = $clone;
  2346. if (self.isImg) {
  2347. if ($this[0].complete) {
  2348. self.start();
  2349. } else {
  2350. $this.one('load', $.proxy(self.start, this));
  2351. }
  2352. } else {
  2353. $clone.one('load', $.proxy(self.start, this)).one('error', $.proxy(self.stop, this)).addClass('cropper-hide').insertAfter($this);
  2354. }
  2355. }
  2356. }, {
  2357. key: 'start',
  2358. value: function start() {
  2359. var self = this;
  2360. var $clone = self.$clone;
  2361. var $image = self.$element;
  2362. if (!self.isImg) {
  2363. $clone.off('error', self.stop);
  2364. $image = $clone;
  2365. }
  2366. getImageSize($image[0], function (naturalWidth, naturalHeight) {
  2367. $.extend(self.image, {
  2368. naturalWidth: naturalWidth,
  2369. naturalHeight: naturalHeight,
  2370. aspectRatio: naturalWidth / naturalHeight
  2371. });
  2372. self.loaded = true;
  2373. self.build();
  2374. });
  2375. }
  2376. }, {
  2377. key: 'stop',
  2378. value: function stop() {
  2379. var self = this;
  2380. self.$clone.remove();
  2381. self.$clone = null;
  2382. }
  2383. }, {
  2384. key: 'build',
  2385. value: function build() {
  2386. var self = this;
  2387. var options = self.options;
  2388. var $this = self.$element;
  2389. var $clone = self.$clone;
  2390. var $cropper = void 0;
  2391. var $cropBox = void 0;
  2392. var $face = void 0;
  2393. if (!self.loaded) {
  2394. return;
  2395. }
  2396. // Unbuild first when replace
  2397. if (self.ready) {
  2398. self.unbuild();
  2399. }
  2400. // Create cropper elements
  2401. self.$container = $this.parent();
  2402. self.$cropper = $cropper = $(TEMPLATE);
  2403. self.$canvas = $cropper.find('.cropper-canvas').append($clone);
  2404. self.$dragBox = $cropper.find('.cropper-drag-box');
  2405. self.$cropBox = $cropBox = $cropper.find('.cropper-crop-box');
  2406. self.$viewBox = $cropper.find('.cropper-view-box');
  2407. self.$face = $face = $cropBox.find('.cropper-face');
  2408. // Hide the original image
  2409. $this.addClass(CLASS_HIDDEN).after($cropper);
  2410. // Show the clone image if is hidden
  2411. if (!self.isImg) {
  2412. $clone.removeClass('cropper-hide');
  2413. }
  2414. self.initPreview();
  2415. self.bind();
  2416. options.aspectRatio = Math.max(0, options.aspectRatio) || NaN;
  2417. options.viewMode = Math.max(0, Math.min(3, Math.round(options.viewMode))) || 0;
  2418. self.cropped = options.autoCrop;
  2419. if (options.autoCrop) {
  2420. if (options.modal) {
  2421. self.$dragBox.addClass('cropper-modal');
  2422. }
  2423. } else {
  2424. $cropBox.addClass(CLASS_HIDDEN);
  2425. }
  2426. if (!options.guides) {
  2427. $cropBox.find('.cropper-dashed').addClass(CLASS_HIDDEN);
  2428. }
  2429. if (!options.center) {
  2430. $cropBox.find('.cropper-center').addClass(CLASS_HIDDEN);
  2431. }
  2432. if (options.cropBoxMovable) {
  2433. $face.addClass('cropper-move').data('action', 'all');
  2434. }
  2435. if (!options.highlight) {
  2436. $face.addClass('cropper-invisible');
  2437. }
  2438. if (options.background) {
  2439. $cropper.addClass('cropper-bg');
  2440. }
  2441. if (!options.cropBoxResizable) {
  2442. $cropBox.find('.cropper-line, .cropper-point').addClass(CLASS_HIDDEN);
  2443. }
  2444. self.setDragMode(options.dragMode);
  2445. self.render();
  2446. self.ready = true;
  2447. self.setData(options.data);
  2448. // Trigger the ready event asynchronously to keep `data('cropper')` is defined
  2449. self.completing = setTimeout(function () {
  2450. if ($.isFunction(options.ready)) {
  2451. $this.one('ready', options.ready);
  2452. }
  2453. self.trigger('ready');
  2454. self.trigger('crop', self.getData());
  2455. self.completed = true;
  2456. }, 0);
  2457. }
  2458. }, {
  2459. key: 'unbuild',
  2460. value: function unbuild() {
  2461. var self = this;
  2462. if (!self.ready) {
  2463. return;
  2464. }
  2465. if (!self.completed) {
  2466. clearTimeout(self.completing);
  2467. }
  2468. self.ready = false;
  2469. self.completed = false;
  2470. self.initialImage = null;
  2471. // Clear `initialCanvas` is necessary when replace
  2472. self.initialCanvas = null;
  2473. self.initialCropBox = null;
  2474. self.container = null;
  2475. self.canvas = null;
  2476. // Clear `cropBox` is necessary when replace
  2477. self.cropBox = null;
  2478. self.unbind();
  2479. self.resetPreview();
  2480. self.$preview = null;
  2481. self.$viewBox = null;
  2482. self.$cropBox = null;
  2483. self.$dragBox = null;
  2484. self.$canvas = null;
  2485. self.$container = null;
  2486. self.$cropper.remove();
  2487. self.$cropper = null;
  2488. }
  2489. }], [{
  2490. key: 'setDefaults',
  2491. value: function setDefaults(options) {
  2492. $.extend(DEFAULTS, $.isPlainObject(options) && options);
  2493. }
  2494. }]);
  2495. return Cropper;
  2496. }();
  2497. $.extend(Cropper.prototype, render$1);
  2498. $.extend(Cropper.prototype, preview$1);
  2499. $.extend(Cropper.prototype, events);
  2500. $.extend(Cropper.prototype, handlers);
  2501. $.extend(Cropper.prototype, change$1);
  2502. $.extend(Cropper.prototype, methods);
  2503. var NAMESPACE = 'cropper';
  2504. var OtherCropper = $.fn.cropper;
  2505. $.fn.cropper = function jQueryCropper(option) {
  2506. for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  2507. args[_key - 1] = arguments[_key];
  2508. }
  2509. var result = void 0;
  2510. this.each(function (i, element) {
  2511. var $this = $(element);
  2512. var data = $this.data(NAMESPACE);
  2513. if (!data) {
  2514. if (/destroy/.test(option)) {
  2515. return;
  2516. }
  2517. var options = $.extend({}, $this.data(), $.isPlainObject(option) && option);
  2518. $this.data(NAMESPACE, data = new Cropper(element, options));
  2519. }
  2520. if (typeof option === 'string') {
  2521. var fn = data[option];
  2522. if ($.isFunction(fn)) {
  2523. result = fn.apply(data, args);
  2524. }
  2525. }
  2526. });
  2527. return typeof result !== 'undefined' ? result : this;
  2528. };
  2529. $.fn.cropper.Constructor = Cropper;
  2530. $.fn.cropper.setDefaults = Cropper.setDefaults;
  2531. // No conflict
  2532. $.fn.cropper.noConflict = function noConflict() {
  2533. $.fn.cropper = OtherCropper;
  2534. return this;
  2535. };