cropper.js 79 KB

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