cropper.esm.js 79 KB

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