cropper.js 104 KB

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