cropper.esm.js 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542
  1. /*!
  2. * Cropper.js v1.5.1
  3. * https://fengyuanchen.github.io/cropperjs
  4. *
  5. * Copyright 2015-present Chen Fengyuan
  6. * Released under the MIT license
  7. *
  8. * Date: 2019-03-10T09:55:53.729Z
  9. */
  10. function _typeof(obj) {
  11. if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
  12. _typeof = function (obj) {
  13. return typeof obj;
  14. };
  15. } else {
  16. _typeof = function (obj) {
  17. return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
  18. };
  19. }
  20. return _typeof(obj);
  21. }
  22. function _classCallCheck(instance, Constructor) {
  23. if (!(instance instanceof Constructor)) {
  24. throw new TypeError("Cannot call a class as a function");
  25. }
  26. }
  27. function _defineProperties(target, props) {
  28. for (var i = 0; i < props.length; i++) {
  29. var descriptor = props[i];
  30. descriptor.enumerable = descriptor.enumerable || false;
  31. descriptor.configurable = true;
  32. if ("value" in descriptor) descriptor.writable = true;
  33. Object.defineProperty(target, descriptor.key, descriptor);
  34. }
  35. }
  36. function _createClass(Constructor, protoProps, staticProps) {
  37. if (protoProps) _defineProperties(Constructor.prototype, protoProps);
  38. if (staticProps) _defineProperties(Constructor, staticProps);
  39. return Constructor;
  40. }
  41. function _toConsumableArray(arr) {
  42. return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread();
  43. }
  44. function _arrayWithoutHoles(arr) {
  45. if (Array.isArray(arr)) {
  46. for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) arr2[i] = arr[i];
  47. return arr2;
  48. }
  49. }
  50. function _iterableToArray(iter) {
  51. if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter);
  52. }
  53. function _nonIterableSpread() {
  54. throw new TypeError("Invalid attempt to spread non-iterable instance");
  55. }
  56. var IS_BROWSER = typeof window !== 'undefined';
  57. var WINDOW = IS_BROWSER ? window : {};
  58. var IS_TOUCH_DEVICE = IS_BROWSER ? 'ontouchstart' in WINDOW.document.documentElement : false;
  59. var HAS_POINTER_EVENT = IS_BROWSER ? 'PointerEvent' in WINDOW : false;
  60. var NAMESPACE = 'cropper'; // Actions
  61. var ACTION_ALL = 'all';
  62. var ACTION_CROP = 'crop';
  63. var ACTION_MOVE = 'move';
  64. var ACTION_ZOOM = 'zoom';
  65. var ACTION_EAST = 'e';
  66. var ACTION_WEST = 'w';
  67. var ACTION_SOUTH = 's';
  68. var ACTION_NORTH = 'n';
  69. var ACTION_NORTH_EAST = 'ne';
  70. var ACTION_NORTH_WEST = 'nw';
  71. var ACTION_SOUTH_EAST = 'se';
  72. var ACTION_SOUTH_WEST = 'sw'; // Classes
  73. var CLASS_CROP = "".concat(NAMESPACE, "-crop");
  74. var CLASS_DISABLED = "".concat(NAMESPACE, "-disabled");
  75. var CLASS_HIDDEN = "".concat(NAMESPACE, "-hidden");
  76. var CLASS_HIDE = "".concat(NAMESPACE, "-hide");
  77. var CLASS_INVISIBLE = "".concat(NAMESPACE, "-invisible");
  78. var CLASS_MODAL = "".concat(NAMESPACE, "-modal");
  79. var CLASS_MOVE = "".concat(NAMESPACE, "-move"); // Data keys
  80. var DATA_ACTION = "".concat(NAMESPACE, "Action");
  81. var DATA_PREVIEW = "".concat(NAMESPACE, "Preview"); // Drag modes
  82. var DRAG_MODE_CROP = 'crop';
  83. var DRAG_MODE_MOVE = 'move';
  84. var DRAG_MODE_NONE = 'none'; // Events
  85. var EVENT_CROP = 'crop';
  86. var EVENT_CROP_END = 'cropend';
  87. var EVENT_CROP_MOVE = 'cropmove';
  88. var EVENT_CROP_START = 'cropstart';
  89. var EVENT_DBLCLICK = 'dblclick';
  90. var EVENT_TOUCH_START = IS_TOUCH_DEVICE ? 'touchstart' : 'mousedown';
  91. var EVENT_TOUCH_MOVE = IS_TOUCH_DEVICE ? 'touchmove' : 'mousemove';
  92. var EVENT_TOUCH_END = IS_TOUCH_DEVICE ? 'touchend touchcancel' : 'mouseup';
  93. var EVENT_POINTER_DOWN = HAS_POINTER_EVENT ? 'pointerdown' : EVENT_TOUCH_START;
  94. var EVENT_POINTER_MOVE = HAS_POINTER_EVENT ? 'pointermove' : EVENT_TOUCH_MOVE;
  95. var EVENT_POINTER_UP = HAS_POINTER_EVENT ? 'pointerup pointercancel' : EVENT_TOUCH_END;
  96. var EVENT_READY = 'ready';
  97. var EVENT_RESIZE = 'resize';
  98. var EVENT_WHEEL = 'wheel';
  99. var EVENT_ZOOM = 'zoom'; // Mime types
  100. var MIME_TYPE_JPEG = 'image/jpeg'; // RegExps
  101. var REGEXP_ACTIONS = /^e|w|s|n|se|sw|ne|nw|all|crop|move|zoom$/;
  102. var REGEXP_DATA_URL_JPEG = /^data:image\/jpeg;base64,/;
  103. var REGEXP_TAG_NAME = /^img|canvas$/i; // Misc
  104. // Inspired by the default width and height of a canvas element.
  105. var MIN_CONTAINER_WIDTH = 200;
  106. var MIN_CONTAINER_HEIGHT = 100;
  107. var DEFAULTS = {
  108. // Define the view mode of the cropper
  109. viewMode: 0,
  110. // 0, 1, 2, 3
  111. // Define the dragging mode of the cropper
  112. dragMode: DRAG_MODE_CROP,
  113. // 'crop', 'move' or 'none'
  114. // Define the initial aspect ratio of the crop box
  115. initialAspectRatio: NaN,
  116. // Define the aspect ratio of the crop box
  117. aspectRatio: NaN,
  118. // An object with the previous cropping result data
  119. data: null,
  120. // A selector for adding extra containers to preview
  121. preview: '',
  122. // Re-render the cropper when resize the window
  123. responsive: true,
  124. // Restore the cropped area after resize the window
  125. restore: true,
  126. // Check if the current image is a cross-origin image
  127. checkCrossOrigin: true,
  128. // Check the current image's Exif Orientation information
  129. checkOrientation: true,
  130. // Show the black modal
  131. modal: true,
  132. // Show the dashed lines for guiding
  133. guides: true,
  134. // Show the center indicator for guiding
  135. center: true,
  136. // Show the white modal to highlight the crop box
  137. highlight: true,
  138. // Show the grid background
  139. background: true,
  140. // Enable to crop the image automatically when initialize
  141. autoCrop: true,
  142. // Define the percentage of automatic cropping area when initializes
  143. autoCropArea: 0.8,
  144. // Enable to move the image
  145. movable: true,
  146. // Enable to rotate the image
  147. rotatable: true,
  148. // Enable to scale the image
  149. scalable: true,
  150. // Enable to zoom the image
  151. zoomable: true,
  152. // Enable to zoom the image by dragging touch
  153. zoomOnTouch: true,
  154. // Enable to zoom the image by wheeling mouse
  155. zoomOnWheel: true,
  156. // Define zoom ratio when zoom the image by wheeling mouse
  157. wheelZoomRatio: 0.1,
  158. // Enable to move the crop box
  159. cropBoxMovable: true,
  160. // Enable to resize the crop box
  161. cropBoxResizable: true,
  162. // Toggle drag mode between "crop" and "move" when click twice on the cropper
  163. toggleDragModeOnDblclick: true,
  164. // Size limitation
  165. minCanvasWidth: 0,
  166. minCanvasHeight: 0,
  167. minCropBoxWidth: 0,
  168. minCropBoxHeight: 0,
  169. minContainerWidth: 200,
  170. minContainerHeight: 100,
  171. // Shortcuts of events
  172. ready: null,
  173. cropstart: null,
  174. cropmove: null,
  175. cropend: null,
  176. crop: null,
  177. zoom: null
  178. };
  179. var TEMPLATE = '<div class="cropper-container" touch-action="none">' + '<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-cropper-action="e"></span>' + '<span class="cropper-line line-n" data-cropper-action="n"></span>' + '<span class="cropper-line line-w" data-cropper-action="w"></span>' + '<span class="cropper-line line-s" data-cropper-action="s"></span>' + '<span class="cropper-point point-e" data-cropper-action="e"></span>' + '<span class="cropper-point point-n" data-cropper-action="n"></span>' + '<span class="cropper-point point-w" data-cropper-action="w"></span>' + '<span class="cropper-point point-s" data-cropper-action="s"></span>' + '<span class="cropper-point point-ne" data-cropper-action="ne"></span>' + '<span class="cropper-point point-nw" data-cropper-action="nw"></span>' + '<span class="cropper-point point-sw" data-cropper-action="sw"></span>' + '<span class="cropper-point point-se" data-cropper-action="se"></span>' + '</div>' + '</div>';
  180. /**
  181. * Check if the given value is not a number.
  182. */
  183. var isNaN = Number.isNaN || WINDOW.isNaN;
  184. /**
  185. * Check if the given value is a number.
  186. * @param {*} value - The value to check.
  187. * @returns {boolean} Returns `true` if the given value is a number, else `false`.
  188. */
  189. function isNumber(value) {
  190. return typeof value === 'number' && !isNaN(value);
  191. }
  192. /**
  193. * Check if the given value is a positive number.
  194. * @param {*} value - The value to check.
  195. * @returns {boolean} Returns `true` if the given value is a positive number, else `false`.
  196. */
  197. var isPositiveNumber = function isPositiveNumber(value) {
  198. return value > 0 && value < Infinity;
  199. };
  200. /**
  201. * Check if the given value is undefined.
  202. * @param {*} value - The value to check.
  203. * @returns {boolean} Returns `true` if the given value is undefined, else `false`.
  204. */
  205. function isUndefined(value) {
  206. return typeof value === 'undefined';
  207. }
  208. /**
  209. * Check if the given value is an object.
  210. * @param {*} value - The value to check.
  211. * @returns {boolean} Returns `true` if the given value is an object, else `false`.
  212. */
  213. function isObject(value) {
  214. return _typeof(value) === 'object' && value !== null;
  215. }
  216. var hasOwnProperty = Object.prototype.hasOwnProperty;
  217. /**
  218. * Check if the given value is a plain object.
  219. * @param {*} value - The value to check.
  220. * @returns {boolean} Returns `true` if the given value is a plain object, else `false`.
  221. */
  222. function isPlainObject(value) {
  223. if (!isObject(value)) {
  224. return false;
  225. }
  226. try {
  227. var _constructor = value.constructor;
  228. var prototype = _constructor.prototype;
  229. return _constructor && prototype && hasOwnProperty.call(prototype, 'isPrototypeOf');
  230. } catch (error) {
  231. return false;
  232. }
  233. }
  234. /**
  235. * Check if the given value is a function.
  236. * @param {*} value - The value to check.
  237. * @returns {boolean} Returns `true` if the given value is a function, else `false`.
  238. */
  239. function isFunction(value) {
  240. return typeof value === 'function';
  241. }
  242. var slice = Array.prototype.slice;
  243. /**
  244. * Convert array-like or iterable object to an array.
  245. * @param {*} value - The value to convert.
  246. * @returns {Array} Returns a new array.
  247. */
  248. function toArray(value) {
  249. return Array.from ? Array.from(value) : slice.call(value);
  250. }
  251. /**
  252. * Iterate the given data.
  253. * @param {*} data - The data to iterate.
  254. * @param {Function} callback - The process function for each element.
  255. * @returns {*} The original data.
  256. */
  257. function forEach(data, callback) {
  258. if (data && isFunction(callback)) {
  259. if (Array.isArray(data) || isNumber(data.length)
  260. /* array-like */
  261. ) {
  262. toArray(data).forEach(function (value, key) {
  263. callback.call(data, value, key, data);
  264. });
  265. } else if (isObject(data)) {
  266. Object.keys(data).forEach(function (key) {
  267. callback.call(data, data[key], key, data);
  268. });
  269. }
  270. }
  271. return data;
  272. }
  273. /**
  274. * Extend the given object.
  275. * @param {*} target - The target object to extend.
  276. * @param {*} args - The rest objects for merging to the target object.
  277. * @returns {Object} The extended object.
  278. */
  279. var assign = Object.assign || function assign(target) {
  280. for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  281. args[_key - 1] = arguments[_key];
  282. }
  283. if (isObject(target) && args.length > 0) {
  284. args.forEach(function (arg) {
  285. if (isObject(arg)) {
  286. Object.keys(arg).forEach(function (key) {
  287. target[key] = arg[key];
  288. });
  289. }
  290. });
  291. }
  292. return target;
  293. };
  294. var REGEXP_DECIMALS = /\.\d*(?:0|9){12}\d*$/;
  295. /**
  296. * Normalize decimal number.
  297. * Check out {@link http://0.30000000000000004.com/}
  298. * @param {number} value - The value to normalize.
  299. * @param {number} [times=100000000000] - The times for normalizing.
  300. * @returns {number} Returns the normalized number.
  301. */
  302. function normalizeDecimalNumber(value) {
  303. var times = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 100000000000;
  304. return REGEXP_DECIMALS.test(value) ? Math.round(value * times) / times : value;
  305. }
  306. var REGEXP_SUFFIX = /^width|height|left|top|marginLeft|marginTop$/;
  307. /**
  308. * Apply styles to the given element.
  309. * @param {Element} element - The target element.
  310. * @param {Object} styles - The styles for applying.
  311. */
  312. function setStyle(element, styles) {
  313. var style = element.style;
  314. forEach(styles, function (value, property) {
  315. if (REGEXP_SUFFIX.test(property) && isNumber(value)) {
  316. value = "".concat(value, "px");
  317. }
  318. style[property] = value;
  319. });
  320. }
  321. /**
  322. * Check if the given element has a special class.
  323. * @param {Element} element - The element to check.
  324. * @param {string} value - The class to search.
  325. * @returns {boolean} Returns `true` if the special class was found.
  326. */
  327. function hasClass(element, value) {
  328. return element.classList ? element.classList.contains(value) : element.className.indexOf(value) > -1;
  329. }
  330. /**
  331. * Add classes to the given element.
  332. * @param {Element} element - The target element.
  333. * @param {string} value - The classes to be added.
  334. */
  335. function addClass(element, value) {
  336. if (!value) {
  337. return;
  338. }
  339. if (isNumber(element.length)) {
  340. forEach(element, function (elem) {
  341. addClass(elem, value);
  342. });
  343. return;
  344. }
  345. if (element.classList) {
  346. element.classList.add(value);
  347. return;
  348. }
  349. var className = element.className.trim();
  350. if (!className) {
  351. element.className = value;
  352. } else if (className.indexOf(value) < 0) {
  353. element.className = "".concat(className, " ").concat(value);
  354. }
  355. }
  356. /**
  357. * Remove classes from the given element.
  358. * @param {Element} element - The target element.
  359. * @param {string} value - The classes to be removed.
  360. */
  361. function removeClass(element, value) {
  362. if (!value) {
  363. return;
  364. }
  365. if (isNumber(element.length)) {
  366. forEach(element, function (elem) {
  367. removeClass(elem, value);
  368. });
  369. return;
  370. }
  371. if (element.classList) {
  372. element.classList.remove(value);
  373. return;
  374. }
  375. if (element.className.indexOf(value) >= 0) {
  376. element.className = element.className.replace(value, '');
  377. }
  378. }
  379. /**
  380. * Add or remove classes from the given element.
  381. * @param {Element} element - The target element.
  382. * @param {string} value - The classes to be toggled.
  383. * @param {boolean} added - Add only.
  384. */
  385. function toggleClass(element, value, added) {
  386. if (!value) {
  387. return;
  388. }
  389. if (isNumber(element.length)) {
  390. forEach(element, function (elem) {
  391. toggleClass(elem, value, added);
  392. });
  393. return;
  394. } // IE10-11 doesn't support the second parameter of `classList.toggle`
  395. if (added) {
  396. addClass(element, value);
  397. } else {
  398. removeClass(element, value);
  399. }
  400. }
  401. var REGEXP_CAMEL_CASE = /([a-z\d])([A-Z])/g;
  402. /**
  403. * Transform the given string from camelCase to kebab-case
  404. * @param {string} value - The value to transform.
  405. * @returns {string} The transformed value.
  406. */
  407. function toParamCase(value) {
  408. return value.replace(REGEXP_CAMEL_CASE, '$1-$2').toLowerCase();
  409. }
  410. /**
  411. * Get data from the given element.
  412. * @param {Element} element - The target element.
  413. * @param {string} name - The data key to get.
  414. * @returns {string} The data value.
  415. */
  416. function getData(element, name) {
  417. if (isObject(element[name])) {
  418. return element[name];
  419. }
  420. if (element.dataset) {
  421. return element.dataset[name];
  422. }
  423. return element.getAttribute("data-".concat(toParamCase(name)));
  424. }
  425. /**
  426. * Set data to the given element.
  427. * @param {Element} element - The target element.
  428. * @param {string} name - The data key to set.
  429. * @param {string} data - The data value.
  430. */
  431. function setData(element, name, data) {
  432. if (isObject(data)) {
  433. element[name] = data;
  434. } else if (element.dataset) {
  435. element.dataset[name] = data;
  436. } else {
  437. element.setAttribute("data-".concat(toParamCase(name)), data);
  438. }
  439. }
  440. /**
  441. * Remove data from the given element.
  442. * @param {Element} element - The target element.
  443. * @param {string} name - The data key to remove.
  444. */
  445. function removeData(element, name) {
  446. if (isObject(element[name])) {
  447. try {
  448. delete element[name];
  449. } catch (error) {
  450. element[name] = undefined;
  451. }
  452. } else if (element.dataset) {
  453. // #128 Safari not allows to delete dataset property
  454. try {
  455. delete element.dataset[name];
  456. } catch (error) {
  457. element.dataset[name] = undefined;
  458. }
  459. } else {
  460. element.removeAttribute("data-".concat(toParamCase(name)));
  461. }
  462. }
  463. var REGEXP_SPACES = /\s\s*/;
  464. var onceSupported = function () {
  465. var supported = false;
  466. if (IS_BROWSER) {
  467. var once = false;
  468. var listener = function listener() {};
  469. var options = Object.defineProperty({}, 'once', {
  470. get: function get() {
  471. supported = true;
  472. return once;
  473. },
  474. /**
  475. * This setter can fix a `TypeError` in strict mode
  476. * {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Getter_only}
  477. * @param {boolean} value - The value to set
  478. */
  479. set: function set(value) {
  480. once = value;
  481. }
  482. });
  483. WINDOW.addEventListener('test', listener, options);
  484. WINDOW.removeEventListener('test', listener, options);
  485. }
  486. return supported;
  487. }();
  488. /**
  489. * Remove event listener from the target element.
  490. * @param {Element} element - The event target.
  491. * @param {string} type - The event type(s).
  492. * @param {Function} listener - The event listener.
  493. * @param {Object} options - The event options.
  494. */
  495. function removeListener(element, type, listener) {
  496. var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  497. var handler = listener;
  498. type.trim().split(REGEXP_SPACES).forEach(function (event) {
  499. if (!onceSupported) {
  500. var listeners = element.listeners;
  501. if (listeners && listeners[event] && listeners[event][listener]) {
  502. handler = listeners[event][listener];
  503. delete listeners[event][listener];
  504. if (Object.keys(listeners[event]).length === 0) {
  505. delete listeners[event];
  506. }
  507. if (Object.keys(listeners).length === 0) {
  508. delete element.listeners;
  509. }
  510. }
  511. }
  512. element.removeEventListener(event, handler, options);
  513. });
  514. }
  515. /**
  516. * Add event listener to the target element.
  517. * @param {Element} element - The event target.
  518. * @param {string} type - The event type(s).
  519. * @param {Function} listener - The event listener.
  520. * @param {Object} options - The event options.
  521. */
  522. function addListener(element, type, listener) {
  523. var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
  524. var _handler = listener;
  525. type.trim().split(REGEXP_SPACES).forEach(function (event) {
  526. if (options.once && !onceSupported) {
  527. var _element$listeners = element.listeners,
  528. listeners = _element$listeners === void 0 ? {} : _element$listeners;
  529. _handler = function handler() {
  530. delete listeners[event][listener];
  531. element.removeEventListener(event, _handler, options);
  532. for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  533. args[_key2] = arguments[_key2];
  534. }
  535. listener.apply(element, args);
  536. };
  537. if (!listeners[event]) {
  538. listeners[event] = {};
  539. }
  540. if (listeners[event][listener]) {
  541. element.removeEventListener(event, listeners[event][listener], options);
  542. }
  543. listeners[event][listener] = _handler;
  544. element.listeners = listeners;
  545. }
  546. element.addEventListener(event, _handler, options);
  547. });
  548. }
  549. /**
  550. * Dispatch event on the target element.
  551. * @param {Element} element - The event target.
  552. * @param {string} type - The event type(s).
  553. * @param {Object} data - The additional event data.
  554. * @returns {boolean} Indicate if the event is default prevented or not.
  555. */
  556. function dispatchEvent(element, type, data) {
  557. var event; // Event and CustomEvent on IE9-11 are global objects, not constructors
  558. if (isFunction(Event) && isFunction(CustomEvent)) {
  559. event = new CustomEvent(type, {
  560. detail: data,
  561. bubbles: true,
  562. cancelable: true
  563. });
  564. } else {
  565. event = document.createEvent('CustomEvent');
  566. event.initCustomEvent(type, true, true, data);
  567. }
  568. return element.dispatchEvent(event);
  569. }
  570. /**
  571. * Get the offset base on the document.
  572. * @param {Element} element - The target element.
  573. * @returns {Object} The offset data.
  574. */
  575. function getOffset(element) {
  576. var box = element.getBoundingClientRect();
  577. return {
  578. left: box.left + (window.pageXOffset - document.documentElement.clientLeft),
  579. top: box.top + (window.pageYOffset - document.documentElement.clientTop)
  580. };
  581. }
  582. var location = WINDOW.location;
  583. var REGEXP_ORIGINS = /^(\w+:)\/\/([^:/?#]*):?(\d*)/i;
  584. /**
  585. * Check if the given URL is a cross origin URL.
  586. * @param {string} url - The target URL.
  587. * @returns {boolean} Returns `true` if the given URL is a cross origin URL, else `false`.
  588. */
  589. function isCrossOriginURL(url) {
  590. var parts = url.match(REGEXP_ORIGINS);
  591. return parts !== null && (parts[1] !== location.protocol || parts[2] !== location.hostname || parts[3] !== location.port);
  592. }
  593. /**
  594. * Add timestamp to the given URL.
  595. * @param {string} url - The target URL.
  596. * @returns {string} The result URL.
  597. */
  598. function addTimestamp(url) {
  599. var timestamp = "timestamp=".concat(new Date().getTime());
  600. return url + (url.indexOf('?') === -1 ? '?' : '&') + timestamp;
  601. }
  602. /**
  603. * Get transforms base on the given object.
  604. * @param {Object} obj - The target object.
  605. * @returns {string} A string contains transform values.
  606. */
  607. function getTransforms(_ref) {
  608. var rotate = _ref.rotate,
  609. scaleX = _ref.scaleX,
  610. scaleY = _ref.scaleY,
  611. translateX = _ref.translateX,
  612. translateY = _ref.translateY;
  613. var values = [];
  614. if (isNumber(translateX) && translateX !== 0) {
  615. values.push("translateX(".concat(translateX, "px)"));
  616. }
  617. if (isNumber(translateY) && translateY !== 0) {
  618. values.push("translateY(".concat(translateY, "px)"));
  619. } // Rotate should come first before scale to match orientation transform
  620. if (isNumber(rotate) && rotate !== 0) {
  621. values.push("rotate(".concat(rotate, "deg)"));
  622. }
  623. if (isNumber(scaleX) && scaleX !== 1) {
  624. values.push("scaleX(".concat(scaleX, ")"));
  625. }
  626. if (isNumber(scaleY) && scaleY !== 1) {
  627. values.push("scaleY(".concat(scaleY, ")"));
  628. }
  629. var transform = values.length ? values.join(' ') : 'none';
  630. return {
  631. WebkitTransform: transform,
  632. msTransform: transform,
  633. transform: transform
  634. };
  635. }
  636. /**
  637. * Get the max ratio of a group of pointers.
  638. * @param {string} pointers - The target pointers.
  639. * @returns {number} The result ratio.
  640. */
  641. function getMaxZoomRatio(pointers) {
  642. var pointers2 = assign({}, pointers);
  643. var ratios = [];
  644. forEach(pointers, function (pointer, pointerId) {
  645. delete pointers2[pointerId];
  646. forEach(pointers2, function (pointer2) {
  647. var x1 = Math.abs(pointer.startX - pointer2.startX);
  648. var y1 = Math.abs(pointer.startY - pointer2.startY);
  649. var x2 = Math.abs(pointer.endX - pointer2.endX);
  650. var y2 = Math.abs(pointer.endY - pointer2.endY);
  651. var z1 = Math.sqrt(x1 * x1 + y1 * y1);
  652. var z2 = Math.sqrt(x2 * x2 + y2 * y2);
  653. var ratio = (z2 - z1) / z1;
  654. ratios.push(ratio);
  655. });
  656. });
  657. ratios.sort(function (a, b) {
  658. return Math.abs(a) < Math.abs(b);
  659. });
  660. return ratios[0];
  661. }
  662. /**
  663. * Get a pointer from an event object.
  664. * @param {Object} event - The target event object.
  665. * @param {boolean} endOnly - Indicates if only returns the end point coordinate or not.
  666. * @returns {Object} The result pointer contains start and/or end point coordinates.
  667. */
  668. function getPointer(_ref2, endOnly) {
  669. var pageX = _ref2.pageX,
  670. pageY = _ref2.pageY;
  671. var end = {
  672. endX: pageX,
  673. endY: pageY
  674. };
  675. return endOnly ? end : assign({
  676. startX: pageX,
  677. startY: pageY
  678. }, end);
  679. }
  680. /**
  681. * Get the center point coordinate of a group of pointers.
  682. * @param {Object} pointers - The target pointers.
  683. * @returns {Object} The center point coordinate.
  684. */
  685. function getPointersCenter(pointers) {
  686. var pageX = 0;
  687. var pageY = 0;
  688. var count = 0;
  689. forEach(pointers, function (_ref3) {
  690. var startX = _ref3.startX,
  691. startY = _ref3.startY;
  692. pageX += startX;
  693. pageY += startY;
  694. count += 1;
  695. });
  696. pageX /= count;
  697. pageY /= count;
  698. return {
  699. pageX: pageX,
  700. pageY: pageY
  701. };
  702. }
  703. /**
  704. * Get the max sizes in a rectangle under the given aspect ratio.
  705. * @param {Object} data - The original sizes.
  706. * @param {string} [type='contain'] - The adjust type.
  707. * @returns {Object} The result sizes.
  708. */
  709. function getAdjustedSizes(_ref4) // or 'cover'
  710. {
  711. var aspectRatio = _ref4.aspectRatio,
  712. height = _ref4.height,
  713. width = _ref4.width;
  714. var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'contain';
  715. var isValidWidth = isPositiveNumber(width);
  716. var isValidHeight = isPositiveNumber(height);
  717. if (isValidWidth && isValidHeight) {
  718. var adjustedWidth = height * aspectRatio;
  719. if (type === 'contain' && adjustedWidth > width || type === 'cover' && adjustedWidth < width) {
  720. height = width / aspectRatio;
  721. } else {
  722. width = height * aspectRatio;
  723. }
  724. } else if (isValidWidth) {
  725. height = width / aspectRatio;
  726. } else if (isValidHeight) {
  727. width = height * aspectRatio;
  728. }
  729. return {
  730. width: width,
  731. height: height
  732. };
  733. }
  734. /**
  735. * Get the new sizes of a rectangle after rotated.
  736. * @param {Object} data - The original sizes.
  737. * @returns {Object} The result sizes.
  738. */
  739. function getRotatedSizes(_ref5) {
  740. var width = _ref5.width,
  741. height = _ref5.height,
  742. degree = _ref5.degree;
  743. degree = Math.abs(degree) % 180;
  744. if (degree === 90) {
  745. return {
  746. width: height,
  747. height: width
  748. };
  749. }
  750. var arc = degree % 90 * Math.PI / 180;
  751. var sinArc = Math.sin(arc);
  752. var cosArc = Math.cos(arc);
  753. var newWidth = width * cosArc + height * sinArc;
  754. var newHeight = width * sinArc + height * cosArc;
  755. return degree > 90 ? {
  756. width: newHeight,
  757. height: newWidth
  758. } : {
  759. width: newWidth,
  760. height: newHeight
  761. };
  762. }
  763. /**
  764. * Get a canvas which drew the given image.
  765. * @param {HTMLImageElement} image - The image for drawing.
  766. * @param {Object} imageData - The image data.
  767. * @param {Object} canvasData - The canvas data.
  768. * @param {Object} options - The options.
  769. * @returns {HTMLCanvasElement} The result canvas.
  770. */
  771. function getSourceCanvas(image, _ref6, _ref7, _ref8) {
  772. var imageAspectRatio = _ref6.aspectRatio,
  773. imageNaturalWidth = _ref6.naturalWidth,
  774. imageNaturalHeight = _ref6.naturalHeight,
  775. _ref6$rotate = _ref6.rotate,
  776. rotate = _ref6$rotate === void 0 ? 0 : _ref6$rotate,
  777. _ref6$scaleX = _ref6.scaleX,
  778. scaleX = _ref6$scaleX === void 0 ? 1 : _ref6$scaleX,
  779. _ref6$scaleY = _ref6.scaleY,
  780. scaleY = _ref6$scaleY === void 0 ? 1 : _ref6$scaleY;
  781. var aspectRatio = _ref7.aspectRatio,
  782. naturalWidth = _ref7.naturalWidth,
  783. naturalHeight = _ref7.naturalHeight;
  784. var _ref8$fillColor = _ref8.fillColor,
  785. fillColor = _ref8$fillColor === void 0 ? 'transparent' : _ref8$fillColor,
  786. _ref8$imageSmoothingE = _ref8.imageSmoothingEnabled,
  787. imageSmoothingEnabled = _ref8$imageSmoothingE === void 0 ? true : _ref8$imageSmoothingE,
  788. _ref8$imageSmoothingQ = _ref8.imageSmoothingQuality,
  789. imageSmoothingQuality = _ref8$imageSmoothingQ === void 0 ? 'low' : _ref8$imageSmoothingQ,
  790. _ref8$maxWidth = _ref8.maxWidth,
  791. maxWidth = _ref8$maxWidth === void 0 ? Infinity : _ref8$maxWidth,
  792. _ref8$maxHeight = _ref8.maxHeight,
  793. maxHeight = _ref8$maxHeight === void 0 ? Infinity : _ref8$maxHeight,
  794. _ref8$minWidth = _ref8.minWidth,
  795. minWidth = _ref8$minWidth === void 0 ? 0 : _ref8$minWidth,
  796. _ref8$minHeight = _ref8.minHeight,
  797. minHeight = _ref8$minHeight === void 0 ? 0 : _ref8$minHeight;
  798. var canvas = document.createElement('canvas');
  799. var context = canvas.getContext('2d');
  800. var maxSizes = getAdjustedSizes({
  801. aspectRatio: aspectRatio,
  802. width: maxWidth,
  803. height: maxHeight
  804. });
  805. var minSizes = getAdjustedSizes({
  806. aspectRatio: aspectRatio,
  807. width: minWidth,
  808. height: minHeight
  809. }, 'cover');
  810. var width = Math.min(maxSizes.width, Math.max(minSizes.width, naturalWidth));
  811. var height = Math.min(maxSizes.height, Math.max(minSizes.height, naturalHeight)); // Note: should always use image's natural sizes for drawing as
  812. // imageData.naturalWidth === canvasData.naturalHeight when rotate % 180 === 90
  813. var destMaxSizes = getAdjustedSizes({
  814. aspectRatio: imageAspectRatio,
  815. width: maxWidth,
  816. height: maxHeight
  817. });
  818. var destMinSizes = getAdjustedSizes({
  819. aspectRatio: imageAspectRatio,
  820. width: minWidth,
  821. height: minHeight
  822. }, 'cover');
  823. var destWidth = Math.min(destMaxSizes.width, Math.max(destMinSizes.width, imageNaturalWidth));
  824. var destHeight = Math.min(destMaxSizes.height, Math.max(destMinSizes.height, imageNaturalHeight));
  825. var params = [-destWidth / 2, -destHeight / 2, destWidth, destHeight];
  826. canvas.width = normalizeDecimalNumber(width);
  827. canvas.height = normalizeDecimalNumber(height);
  828. context.fillStyle = fillColor;
  829. context.fillRect(0, 0, width, height);
  830. context.save();
  831. context.translate(width / 2, height / 2);
  832. context.rotate(rotate * Math.PI / 180);
  833. context.scale(scaleX, scaleY);
  834. context.imageSmoothingEnabled = imageSmoothingEnabled;
  835. context.imageSmoothingQuality = imageSmoothingQuality;
  836. context.drawImage.apply(context, [image].concat(_toConsumableArray(params.map(function (param) {
  837. return Math.floor(normalizeDecimalNumber(param));
  838. }))));
  839. context.restore();
  840. return canvas;
  841. }
  842. var fromCharCode = String.fromCharCode;
  843. /**
  844. * Get string from char code in data view.
  845. * @param {DataView} dataView - The data view for read.
  846. * @param {number} start - The start index.
  847. * @param {number} length - The read length.
  848. * @returns {string} The read result.
  849. */
  850. function getStringFromCharCode(dataView, start, length) {
  851. var str = '';
  852. length += start;
  853. for (var i = start; i < length; i += 1) {
  854. str += fromCharCode(dataView.getUint8(i));
  855. }
  856. return str;
  857. }
  858. var REGEXP_DATA_URL_HEAD = /^data:.*,/;
  859. /**
  860. * Transform Data URL to array buffer.
  861. * @param {string} dataURL - The Data URL to transform.
  862. * @returns {ArrayBuffer} The result array buffer.
  863. */
  864. function dataURLToArrayBuffer(dataURL) {
  865. var base64 = dataURL.replace(REGEXP_DATA_URL_HEAD, '');
  866. var binary = atob(base64);
  867. var arrayBuffer = new ArrayBuffer(binary.length);
  868. var uint8 = new Uint8Array(arrayBuffer);
  869. forEach(uint8, function (value, i) {
  870. uint8[i] = binary.charCodeAt(i);
  871. });
  872. return arrayBuffer;
  873. }
  874. /**
  875. * Transform array buffer to Data URL.
  876. * @param {ArrayBuffer} arrayBuffer - The array buffer to transform.
  877. * @param {string} mimeType - The mime type of the Data URL.
  878. * @returns {string} The result Data URL.
  879. */
  880. function arrayBufferToDataURL(arrayBuffer, mimeType) {
  881. var chunks = []; // Chunk Typed Array for better performance (#435)
  882. var chunkSize = 8192;
  883. var uint8 = new Uint8Array(arrayBuffer);
  884. while (uint8.length > 0) {
  885. // XXX: Babel's `toConsumableArray` helper will throw error in IE or Safari 9
  886. // eslint-disable-next-line prefer-spread
  887. chunks.push(fromCharCode.apply(null, toArray(uint8.subarray(0, chunkSize))));
  888. uint8 = uint8.subarray(chunkSize);
  889. }
  890. return "data:".concat(mimeType, ";base64,").concat(btoa(chunks.join('')));
  891. }
  892. /**
  893. * Get orientation value from given array buffer.
  894. * @param {ArrayBuffer} arrayBuffer - The array buffer to read.
  895. * @returns {number} The read orientation value.
  896. */
  897. function resetAndGetOrientation(arrayBuffer) {
  898. var dataView = new DataView(arrayBuffer);
  899. var orientation; // Ignores range error when the image does not have correct Exif information
  900. try {
  901. var littleEndian;
  902. var app1Start;
  903. var ifdStart; // Only handle JPEG image (start by 0xFFD8)
  904. if (dataView.getUint8(0) === 0xFF && dataView.getUint8(1) === 0xD8) {
  905. var length = dataView.byteLength;
  906. var offset = 2;
  907. while (offset + 1 < length) {
  908. if (dataView.getUint8(offset) === 0xFF && dataView.getUint8(offset + 1) === 0xE1) {
  909. app1Start = offset;
  910. break;
  911. }
  912. offset += 1;
  913. }
  914. }
  915. if (app1Start) {
  916. var exifIDCode = app1Start + 4;
  917. var tiffOffset = app1Start + 10;
  918. if (getStringFromCharCode(dataView, exifIDCode, 4) === 'Exif') {
  919. var endianness = dataView.getUint16(tiffOffset);
  920. littleEndian = endianness === 0x4949;
  921. if (littleEndian || endianness === 0x4D4D
  922. /* bigEndian */
  923. ) {
  924. if (dataView.getUint16(tiffOffset + 2, littleEndian) === 0x002A) {
  925. var firstIFDOffset = dataView.getUint32(tiffOffset + 4, littleEndian);
  926. if (firstIFDOffset >= 0x00000008) {
  927. ifdStart = tiffOffset + firstIFDOffset;
  928. }
  929. }
  930. }
  931. }
  932. }
  933. if (ifdStart) {
  934. var _length = dataView.getUint16(ifdStart, littleEndian);
  935. var _offset;
  936. var i;
  937. for (i = 0; i < _length; i += 1) {
  938. _offset = ifdStart + i * 12 + 2;
  939. if (dataView.getUint16(_offset, littleEndian) === 0x0112
  940. /* Orientation */
  941. ) {
  942. // 8 is the offset of the current tag's value
  943. _offset += 8; // Get the original orientation value
  944. orientation = dataView.getUint16(_offset, littleEndian); // Override the orientation with its default value
  945. dataView.setUint16(_offset, 1, littleEndian);
  946. break;
  947. }
  948. }
  949. }
  950. } catch (error) {
  951. orientation = 1;
  952. }
  953. return orientation;
  954. }
  955. /**
  956. * Parse Exif Orientation value.
  957. * @param {number} orientation - The orientation to parse.
  958. * @returns {Object} The parsed result.
  959. */
  960. function parseOrientation(orientation) {
  961. var rotate = 0;
  962. var scaleX = 1;
  963. var scaleY = 1;
  964. switch (orientation) {
  965. // Flip horizontal
  966. case 2:
  967. scaleX = -1;
  968. break;
  969. // Rotate left 180°
  970. case 3:
  971. rotate = -180;
  972. break;
  973. // Flip vertical
  974. case 4:
  975. scaleY = -1;
  976. break;
  977. // Flip vertical and rotate right 90°
  978. case 5:
  979. rotate = 90;
  980. scaleY = -1;
  981. break;
  982. // Rotate right 90°
  983. case 6:
  984. rotate = 90;
  985. break;
  986. // Flip horizontal and rotate right 90°
  987. case 7:
  988. rotate = 90;
  989. scaleX = -1;
  990. break;
  991. // Rotate left 90°
  992. case 8:
  993. rotate = -90;
  994. break;
  995. default:
  996. }
  997. return {
  998. rotate: rotate,
  999. scaleX: scaleX,
  1000. scaleY: scaleY
  1001. };
  1002. }
  1003. var render = {
  1004. render: function render() {
  1005. this.initContainer();
  1006. this.initCanvas();
  1007. this.initCropBox();
  1008. this.renderCanvas();
  1009. if (this.cropped) {
  1010. this.renderCropBox();
  1011. }
  1012. },
  1013. initContainer: function initContainer() {
  1014. var element = this.element,
  1015. options = this.options,
  1016. container = this.container,
  1017. cropper = this.cropper;
  1018. addClass(cropper, CLASS_HIDDEN);
  1019. removeClass(element, CLASS_HIDDEN);
  1020. var containerData = {
  1021. width: Math.max(container.offsetWidth, Number(options.minContainerWidth) || 200),
  1022. height: Math.max(container.offsetHeight, Number(options.minContainerHeight) || 100)
  1023. };
  1024. this.containerData = containerData;
  1025. setStyle(cropper, {
  1026. width: containerData.width,
  1027. height: containerData.height
  1028. });
  1029. addClass(element, CLASS_HIDDEN);
  1030. removeClass(cropper, CLASS_HIDDEN);
  1031. },
  1032. // Canvas (image wrapper)
  1033. initCanvas: function initCanvas() {
  1034. var containerData = this.containerData,
  1035. imageData = this.imageData;
  1036. var viewMode = this.options.viewMode;
  1037. var rotated = Math.abs(imageData.rotate) % 180 === 90;
  1038. var naturalWidth = rotated ? imageData.naturalHeight : imageData.naturalWidth;
  1039. var naturalHeight = rotated ? imageData.naturalWidth : imageData.naturalHeight;
  1040. var aspectRatio = naturalWidth / naturalHeight;
  1041. var canvasWidth = containerData.width;
  1042. var canvasHeight = containerData.height;
  1043. if (containerData.height * aspectRatio > containerData.width) {
  1044. if (viewMode === 3) {
  1045. canvasWidth = containerData.height * aspectRatio;
  1046. } else {
  1047. canvasHeight = containerData.width / aspectRatio;
  1048. }
  1049. } else if (viewMode === 3) {
  1050. canvasHeight = containerData.width / aspectRatio;
  1051. } else {
  1052. canvasWidth = containerData.height * aspectRatio;
  1053. }
  1054. var canvasData = {
  1055. aspectRatio: aspectRatio,
  1056. naturalWidth: naturalWidth,
  1057. naturalHeight: naturalHeight,
  1058. width: canvasWidth,
  1059. height: canvasHeight
  1060. };
  1061. canvasData.left = (containerData.width - canvasWidth) / 2;
  1062. canvasData.top = (containerData.height - canvasHeight) / 2;
  1063. canvasData.oldLeft = canvasData.left;
  1064. canvasData.oldTop = canvasData.top;
  1065. this.canvasData = canvasData;
  1066. this.limited = viewMode === 1 || viewMode === 2;
  1067. this.limitCanvas(true, true);
  1068. this.initialImageData = assign({}, imageData);
  1069. this.initialCanvasData = assign({}, canvasData);
  1070. },
  1071. limitCanvas: function limitCanvas(sizeLimited, positionLimited) {
  1072. var options = this.options,
  1073. containerData = this.containerData,
  1074. canvasData = this.canvasData,
  1075. cropBoxData = this.cropBoxData;
  1076. var viewMode = options.viewMode;
  1077. var aspectRatio = canvasData.aspectRatio;
  1078. var cropped = this.cropped && cropBoxData;
  1079. if (sizeLimited) {
  1080. var minCanvasWidth = Number(options.minCanvasWidth) || 0;
  1081. var minCanvasHeight = Number(options.minCanvasHeight) || 0;
  1082. if (viewMode > 1) {
  1083. minCanvasWidth = Math.max(minCanvasWidth, containerData.width);
  1084. minCanvasHeight = Math.max(minCanvasHeight, containerData.height);
  1085. if (viewMode === 3) {
  1086. if (minCanvasHeight * aspectRatio > minCanvasWidth) {
  1087. minCanvasWidth = minCanvasHeight * aspectRatio;
  1088. } else {
  1089. minCanvasHeight = minCanvasWidth / aspectRatio;
  1090. }
  1091. }
  1092. } else if (viewMode > 0) {
  1093. if (minCanvasWidth) {
  1094. minCanvasWidth = Math.max(minCanvasWidth, cropped ? cropBoxData.width : 0);
  1095. } else if (minCanvasHeight) {
  1096. minCanvasHeight = Math.max(minCanvasHeight, cropped ? cropBoxData.height : 0);
  1097. } else if (cropped) {
  1098. minCanvasWidth = cropBoxData.width;
  1099. minCanvasHeight = cropBoxData.height;
  1100. if (minCanvasHeight * aspectRatio > minCanvasWidth) {
  1101. minCanvasWidth = minCanvasHeight * aspectRatio;
  1102. } else {
  1103. minCanvasHeight = minCanvasWidth / aspectRatio;
  1104. }
  1105. }
  1106. }
  1107. var _getAdjustedSizes = getAdjustedSizes({
  1108. aspectRatio: aspectRatio,
  1109. width: minCanvasWidth,
  1110. height: minCanvasHeight
  1111. });
  1112. minCanvasWidth = _getAdjustedSizes.width;
  1113. minCanvasHeight = _getAdjustedSizes.height;
  1114. canvasData.minWidth = minCanvasWidth;
  1115. canvasData.minHeight = minCanvasHeight;
  1116. canvasData.maxWidth = Infinity;
  1117. canvasData.maxHeight = Infinity;
  1118. }
  1119. if (positionLimited) {
  1120. if (viewMode > (cropped ? 0 : 1)) {
  1121. var newCanvasLeft = containerData.width - canvasData.width;
  1122. var newCanvasTop = containerData.height - canvasData.height;
  1123. canvasData.minLeft = Math.min(0, newCanvasLeft);
  1124. canvasData.minTop = Math.min(0, newCanvasTop);
  1125. canvasData.maxLeft = Math.max(0, newCanvasLeft);
  1126. canvasData.maxTop = Math.max(0, newCanvasTop);
  1127. if (cropped && this.limited) {
  1128. canvasData.minLeft = Math.min(cropBoxData.left, cropBoxData.left + (cropBoxData.width - canvasData.width));
  1129. canvasData.minTop = Math.min(cropBoxData.top, cropBoxData.top + (cropBoxData.height - canvasData.height));
  1130. canvasData.maxLeft = cropBoxData.left;
  1131. canvasData.maxTop = cropBoxData.top;
  1132. if (viewMode === 2) {
  1133. if (canvasData.width >= containerData.width) {
  1134. canvasData.minLeft = Math.min(0, newCanvasLeft);
  1135. canvasData.maxLeft = Math.max(0, newCanvasLeft);
  1136. }
  1137. if (canvasData.height >= containerData.height) {
  1138. canvasData.minTop = Math.min(0, newCanvasTop);
  1139. canvasData.maxTop = Math.max(0, newCanvasTop);
  1140. }
  1141. }
  1142. }
  1143. } else {
  1144. canvasData.minLeft = -canvasData.width;
  1145. canvasData.minTop = -canvasData.height;
  1146. canvasData.maxLeft = containerData.width;
  1147. canvasData.maxTop = containerData.height;
  1148. }
  1149. }
  1150. },
  1151. renderCanvas: function renderCanvas(changed, transformed) {
  1152. var canvasData = this.canvasData,
  1153. imageData = this.imageData;
  1154. if (transformed) {
  1155. var _getRotatedSizes = getRotatedSizes({
  1156. width: imageData.naturalWidth * Math.abs(imageData.scaleX || 1),
  1157. height: imageData.naturalHeight * Math.abs(imageData.scaleY || 1),
  1158. degree: imageData.rotate || 0
  1159. }),
  1160. naturalWidth = _getRotatedSizes.width,
  1161. naturalHeight = _getRotatedSizes.height;
  1162. var width = canvasData.width * (naturalWidth / canvasData.naturalWidth);
  1163. var height = canvasData.height * (naturalHeight / canvasData.naturalHeight);
  1164. canvasData.left -= (width - canvasData.width) / 2;
  1165. canvasData.top -= (height - canvasData.height) / 2;
  1166. canvasData.width = width;
  1167. canvasData.height = height;
  1168. canvasData.aspectRatio = naturalWidth / naturalHeight;
  1169. canvasData.naturalWidth = naturalWidth;
  1170. canvasData.naturalHeight = naturalHeight;
  1171. this.limitCanvas(true, false);
  1172. }
  1173. if (canvasData.width > canvasData.maxWidth || canvasData.width < canvasData.minWidth) {
  1174. canvasData.left = canvasData.oldLeft;
  1175. }
  1176. if (canvasData.height > canvasData.maxHeight || canvasData.height < canvasData.minHeight) {
  1177. canvasData.top = canvasData.oldTop;
  1178. }
  1179. canvasData.width = Math.min(Math.max(canvasData.width, canvasData.minWidth), canvasData.maxWidth);
  1180. canvasData.height = Math.min(Math.max(canvasData.height, canvasData.minHeight), canvasData.maxHeight);
  1181. this.limitCanvas(false, true);
  1182. canvasData.left = Math.min(Math.max(canvasData.left, canvasData.minLeft), canvasData.maxLeft);
  1183. canvasData.top = Math.min(Math.max(canvasData.top, canvasData.minTop), canvasData.maxTop);
  1184. canvasData.oldLeft = canvasData.left;
  1185. canvasData.oldTop = canvasData.top;
  1186. setStyle(this.canvas, assign({
  1187. width: canvasData.width,
  1188. height: canvasData.height
  1189. }, getTransforms({
  1190. translateX: canvasData.left,
  1191. translateY: canvasData.top
  1192. })));
  1193. this.renderImage(changed);
  1194. if (this.cropped && this.limited) {
  1195. this.limitCropBox(true, true);
  1196. }
  1197. },
  1198. renderImage: function renderImage(changed) {
  1199. var canvasData = this.canvasData,
  1200. imageData = this.imageData;
  1201. var width = imageData.naturalWidth * (canvasData.width / canvasData.naturalWidth);
  1202. var height = imageData.naturalHeight * (canvasData.height / canvasData.naturalHeight);
  1203. assign(imageData, {
  1204. width: width,
  1205. height: height,
  1206. left: (canvasData.width - width) / 2,
  1207. top: (canvasData.height - height) / 2
  1208. });
  1209. setStyle(this.image, assign({
  1210. width: imageData.width,
  1211. height: imageData.height
  1212. }, getTransforms(assign({
  1213. translateX: imageData.left,
  1214. translateY: imageData.top
  1215. }, imageData))));
  1216. if (changed) {
  1217. this.output();
  1218. }
  1219. },
  1220. initCropBox: function initCropBox() {
  1221. var options = this.options,
  1222. canvasData = this.canvasData;
  1223. var aspectRatio = options.aspectRatio || options.initialAspectRatio;
  1224. var autoCropArea = Number(options.autoCropArea) || 0.8;
  1225. var cropBoxData = {
  1226. width: canvasData.width,
  1227. height: canvasData.height
  1228. };
  1229. if (aspectRatio) {
  1230. if (canvasData.height * aspectRatio > canvasData.width) {
  1231. cropBoxData.height = cropBoxData.width / aspectRatio;
  1232. } else {
  1233. cropBoxData.width = cropBoxData.height * aspectRatio;
  1234. }
  1235. }
  1236. this.cropBoxData = cropBoxData;
  1237. this.limitCropBox(true, true); // Initialize auto crop area
  1238. cropBoxData.width = Math.min(Math.max(cropBoxData.width, cropBoxData.minWidth), cropBoxData.maxWidth);
  1239. cropBoxData.height = Math.min(Math.max(cropBoxData.height, cropBoxData.minHeight), cropBoxData.maxHeight); // The width/height of auto crop area must large than "minWidth/Height"
  1240. cropBoxData.width = Math.max(cropBoxData.minWidth, cropBoxData.width * autoCropArea);
  1241. cropBoxData.height = Math.max(cropBoxData.minHeight, cropBoxData.height * autoCropArea);
  1242. cropBoxData.left = canvasData.left + (canvasData.width - cropBoxData.width) / 2;
  1243. cropBoxData.top = canvasData.top + (canvasData.height - cropBoxData.height) / 2;
  1244. cropBoxData.oldLeft = cropBoxData.left;
  1245. cropBoxData.oldTop = cropBoxData.top;
  1246. this.initialCropBoxData = assign({}, cropBoxData);
  1247. },
  1248. limitCropBox: function limitCropBox(sizeLimited, positionLimited) {
  1249. var options = this.options,
  1250. containerData = this.containerData,
  1251. canvasData = this.canvasData,
  1252. cropBoxData = this.cropBoxData,
  1253. limited = this.limited;
  1254. var aspectRatio = options.aspectRatio;
  1255. if (sizeLimited) {
  1256. var minCropBoxWidth = Number(options.minCropBoxWidth) || 0;
  1257. var minCropBoxHeight = Number(options.minCropBoxHeight) || 0;
  1258. var maxCropBoxWidth = limited ? Math.min(containerData.width, canvasData.width, canvasData.width + canvasData.left, containerData.width - canvasData.left) : containerData.width;
  1259. var maxCropBoxHeight = limited ? Math.min(containerData.height, canvasData.height, canvasData.height + canvasData.top, containerData.height - canvasData.top) : containerData.height; // The min/maxCropBoxWidth/Height must be less than container's width/height
  1260. minCropBoxWidth = Math.min(minCropBoxWidth, containerData.width);
  1261. minCropBoxHeight = Math.min(minCropBoxHeight, containerData.height);
  1262. if (aspectRatio) {
  1263. if (minCropBoxWidth && minCropBoxHeight) {
  1264. if (minCropBoxHeight * aspectRatio > minCropBoxWidth) {
  1265. minCropBoxHeight = minCropBoxWidth / aspectRatio;
  1266. } else {
  1267. minCropBoxWidth = minCropBoxHeight * aspectRatio;
  1268. }
  1269. } else if (minCropBoxWidth) {
  1270. minCropBoxHeight = minCropBoxWidth / aspectRatio;
  1271. } else if (minCropBoxHeight) {
  1272. minCropBoxWidth = minCropBoxHeight * aspectRatio;
  1273. }
  1274. if (maxCropBoxHeight * aspectRatio > maxCropBoxWidth) {
  1275. maxCropBoxHeight = maxCropBoxWidth / aspectRatio;
  1276. } else {
  1277. maxCropBoxWidth = maxCropBoxHeight * aspectRatio;
  1278. }
  1279. } // The minWidth/Height must be less than maxWidth/Height
  1280. cropBoxData.minWidth = Math.min(minCropBoxWidth, maxCropBoxWidth);
  1281. cropBoxData.minHeight = Math.min(minCropBoxHeight, maxCropBoxHeight);
  1282. cropBoxData.maxWidth = maxCropBoxWidth;
  1283. cropBoxData.maxHeight = maxCropBoxHeight;
  1284. }
  1285. if (positionLimited) {
  1286. if (limited) {
  1287. cropBoxData.minLeft = Math.max(0, canvasData.left);
  1288. cropBoxData.minTop = Math.max(0, canvasData.top);
  1289. cropBoxData.maxLeft = Math.min(containerData.width, canvasData.left + canvasData.width) - cropBoxData.width;
  1290. cropBoxData.maxTop = Math.min(containerData.height, canvasData.top + canvasData.height) - cropBoxData.height;
  1291. } else {
  1292. cropBoxData.minLeft = 0;
  1293. cropBoxData.minTop = 0;
  1294. cropBoxData.maxLeft = containerData.width - cropBoxData.width;
  1295. cropBoxData.maxTop = containerData.height - cropBoxData.height;
  1296. }
  1297. }
  1298. },
  1299. renderCropBox: function renderCropBox() {
  1300. var options = this.options,
  1301. containerData = this.containerData,
  1302. cropBoxData = this.cropBoxData;
  1303. if (cropBoxData.width > cropBoxData.maxWidth || cropBoxData.width < cropBoxData.minWidth) {
  1304. cropBoxData.left = cropBoxData.oldLeft;
  1305. }
  1306. if (cropBoxData.height > cropBoxData.maxHeight || cropBoxData.height < cropBoxData.minHeight) {
  1307. cropBoxData.top = cropBoxData.oldTop;
  1308. }
  1309. cropBoxData.width = Math.min(Math.max(cropBoxData.width, cropBoxData.minWidth), cropBoxData.maxWidth);
  1310. cropBoxData.height = Math.min(Math.max(cropBoxData.height, cropBoxData.minHeight), cropBoxData.maxHeight);
  1311. this.limitCropBox(false, true);
  1312. cropBoxData.left = Math.min(Math.max(cropBoxData.left, cropBoxData.minLeft), cropBoxData.maxLeft);
  1313. cropBoxData.top = Math.min(Math.max(cropBoxData.top, cropBoxData.minTop), cropBoxData.maxTop);
  1314. cropBoxData.oldLeft = cropBoxData.left;
  1315. cropBoxData.oldTop = cropBoxData.top;
  1316. if (options.movable && options.cropBoxMovable) {
  1317. // Turn to move the canvas when the crop box is equal to the container
  1318. setData(this.face, DATA_ACTION, cropBoxData.width >= containerData.width && cropBoxData.height >= containerData.height ? ACTION_MOVE : ACTION_ALL);
  1319. }
  1320. setStyle(this.cropBox, assign({
  1321. width: cropBoxData.width,
  1322. height: cropBoxData.height
  1323. }, getTransforms({
  1324. translateX: cropBoxData.left,
  1325. translateY: cropBoxData.top
  1326. })));
  1327. if (this.cropped && this.limited) {
  1328. this.limitCanvas(true, true);
  1329. }
  1330. if (!this.disabled) {
  1331. this.output();
  1332. }
  1333. },
  1334. output: function output() {
  1335. this.preview();
  1336. dispatchEvent(this.element, EVENT_CROP, this.getData());
  1337. }
  1338. };
  1339. var preview = {
  1340. initPreview: function initPreview() {
  1341. var crossOrigin = this.crossOrigin;
  1342. var preview = this.options.preview;
  1343. var url = crossOrigin ? this.crossOriginUrl : this.url;
  1344. var image = document.createElement('img');
  1345. if (crossOrigin) {
  1346. image.crossOrigin = crossOrigin;
  1347. }
  1348. image.src = url;
  1349. this.viewBox.appendChild(image);
  1350. this.viewBoxImage = image;
  1351. if (!preview) {
  1352. return;
  1353. }
  1354. var previews = preview;
  1355. if (typeof preview === 'string') {
  1356. previews = this.element.ownerDocument.querySelectorAll(preview);
  1357. } else if (preview.querySelector) {
  1358. previews = [preview];
  1359. }
  1360. this.previews = previews;
  1361. forEach(previews, function (el) {
  1362. var img = document.createElement('img'); // Save the original size for recover
  1363. setData(el, DATA_PREVIEW, {
  1364. width: el.offsetWidth,
  1365. height: el.offsetHeight,
  1366. html: el.innerHTML
  1367. });
  1368. if (crossOrigin) {
  1369. img.crossOrigin = crossOrigin;
  1370. }
  1371. img.src = url;
  1372. /**
  1373. * Override img element styles
  1374. * Add `display:block` to avoid margin top issue
  1375. * Add `height:auto` to override `height` attribute on IE8
  1376. * (Occur only when margin-top <= -height)
  1377. */
  1378. 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;"';
  1379. el.innerHTML = '';
  1380. el.appendChild(img);
  1381. });
  1382. },
  1383. resetPreview: function resetPreview() {
  1384. forEach(this.previews, function (element) {
  1385. var data = getData(element, DATA_PREVIEW);
  1386. setStyle(element, {
  1387. width: data.width,
  1388. height: data.height
  1389. });
  1390. element.innerHTML = data.html;
  1391. removeData(element, DATA_PREVIEW);
  1392. });
  1393. },
  1394. preview: function preview() {
  1395. var imageData = this.imageData,
  1396. canvasData = this.canvasData,
  1397. cropBoxData = this.cropBoxData;
  1398. var cropBoxWidth = cropBoxData.width,
  1399. cropBoxHeight = cropBoxData.height;
  1400. var width = imageData.width,
  1401. height = imageData.height;
  1402. var left = cropBoxData.left - canvasData.left - imageData.left;
  1403. var top = cropBoxData.top - canvasData.top - imageData.top;
  1404. if (!this.cropped || this.disabled) {
  1405. return;
  1406. }
  1407. setStyle(this.viewBoxImage, assign({
  1408. width: width,
  1409. height: height
  1410. }, getTransforms(assign({
  1411. translateX: -left,
  1412. translateY: -top
  1413. }, imageData))));
  1414. forEach(this.previews, function (element) {
  1415. var data = getData(element, DATA_PREVIEW);
  1416. var originalWidth = data.width;
  1417. var originalHeight = data.height;
  1418. var newWidth = originalWidth;
  1419. var newHeight = originalHeight;
  1420. var ratio = 1;
  1421. if (cropBoxWidth) {
  1422. ratio = originalWidth / cropBoxWidth;
  1423. newHeight = cropBoxHeight * ratio;
  1424. }
  1425. if (cropBoxHeight && newHeight > originalHeight) {
  1426. ratio = originalHeight / cropBoxHeight;
  1427. newWidth = cropBoxWidth * ratio;
  1428. newHeight = originalHeight;
  1429. }
  1430. setStyle(element, {
  1431. width: newWidth,
  1432. height: newHeight
  1433. });
  1434. setStyle(element.getElementsByTagName('img')[0], assign({
  1435. width: width * ratio,
  1436. height: height * ratio
  1437. }, getTransforms(assign({
  1438. translateX: -left * ratio,
  1439. translateY: -top * ratio
  1440. }, imageData))));
  1441. });
  1442. }
  1443. };
  1444. var events = {
  1445. bind: function bind() {
  1446. var element = this.element,
  1447. options = this.options,
  1448. cropper = this.cropper;
  1449. if (isFunction(options.cropstart)) {
  1450. addListener(element, EVENT_CROP_START, options.cropstart);
  1451. }
  1452. if (isFunction(options.cropmove)) {
  1453. addListener(element, EVENT_CROP_MOVE, options.cropmove);
  1454. }
  1455. if (isFunction(options.cropend)) {
  1456. addListener(element, EVENT_CROP_END, options.cropend);
  1457. }
  1458. if (isFunction(options.crop)) {
  1459. addListener(element, EVENT_CROP, options.crop);
  1460. }
  1461. if (isFunction(options.zoom)) {
  1462. addListener(element, EVENT_ZOOM, options.zoom);
  1463. }
  1464. addListener(cropper, EVENT_POINTER_DOWN, this.onCropStart = this.cropStart.bind(this));
  1465. if (options.zoomable && options.zoomOnWheel) {
  1466. addListener(cropper, EVENT_WHEEL, this.onWheel = this.wheel.bind(this), {
  1467. passive: false,
  1468. capture: true
  1469. });
  1470. }
  1471. if (options.toggleDragModeOnDblclick) {
  1472. addListener(cropper, EVENT_DBLCLICK, this.onDblclick = this.dblclick.bind(this));
  1473. }
  1474. addListener(element.ownerDocument, EVENT_POINTER_MOVE, this.onCropMove = this.cropMove.bind(this));
  1475. addListener(element.ownerDocument, EVENT_POINTER_UP, this.onCropEnd = this.cropEnd.bind(this));
  1476. if (options.responsive) {
  1477. addListener(window, EVENT_RESIZE, this.onResize = this.resize.bind(this));
  1478. }
  1479. },
  1480. unbind: function unbind() {
  1481. var element = this.element,
  1482. options = this.options,
  1483. cropper = this.cropper;
  1484. if (isFunction(options.cropstart)) {
  1485. removeListener(element, EVENT_CROP_START, options.cropstart);
  1486. }
  1487. if (isFunction(options.cropmove)) {
  1488. removeListener(element, EVENT_CROP_MOVE, options.cropmove);
  1489. }
  1490. if (isFunction(options.cropend)) {
  1491. removeListener(element, EVENT_CROP_END, options.cropend);
  1492. }
  1493. if (isFunction(options.crop)) {
  1494. removeListener(element, EVENT_CROP, options.crop);
  1495. }
  1496. if (isFunction(options.zoom)) {
  1497. removeListener(element, EVENT_ZOOM, options.zoom);
  1498. }
  1499. removeListener(cropper, EVENT_POINTER_DOWN, this.onCropStart);
  1500. if (options.zoomable && options.zoomOnWheel) {
  1501. removeListener(cropper, EVENT_WHEEL, this.onWheel, {
  1502. passive: false,
  1503. capture: true
  1504. });
  1505. }
  1506. if (options.toggleDragModeOnDblclick) {
  1507. removeListener(cropper, EVENT_DBLCLICK, this.onDblclick);
  1508. }
  1509. removeListener(element.ownerDocument, EVENT_POINTER_MOVE, this.onCropMove);
  1510. removeListener(element.ownerDocument, EVENT_POINTER_UP, this.onCropEnd);
  1511. if (options.responsive) {
  1512. removeListener(window, EVENT_RESIZE, this.onResize);
  1513. }
  1514. }
  1515. };
  1516. var handlers = {
  1517. resize: function resize() {
  1518. var options = this.options,
  1519. container = this.container,
  1520. containerData = this.containerData;
  1521. var minContainerWidth = Number(options.minContainerWidth) || MIN_CONTAINER_WIDTH;
  1522. var minContainerHeight = Number(options.minContainerHeight) || MIN_CONTAINER_HEIGHT;
  1523. if (this.disabled || containerData.width <= minContainerWidth || containerData.height <= minContainerHeight) {
  1524. return;
  1525. }
  1526. var ratio = container.offsetWidth / containerData.width; // Resize when width changed or height changed
  1527. if (ratio !== 1 || container.offsetHeight !== containerData.height) {
  1528. var canvasData;
  1529. var cropBoxData;
  1530. if (options.restore) {
  1531. canvasData = this.getCanvasData();
  1532. cropBoxData = this.getCropBoxData();
  1533. }
  1534. this.render();
  1535. if (options.restore) {
  1536. this.setCanvasData(forEach(canvasData, function (n, i) {
  1537. canvasData[i] = n * ratio;
  1538. }));
  1539. this.setCropBoxData(forEach(cropBoxData, function (n, i) {
  1540. cropBoxData[i] = n * ratio;
  1541. }));
  1542. }
  1543. }
  1544. },
  1545. dblclick: function dblclick() {
  1546. if (this.disabled || this.options.dragMode === DRAG_MODE_NONE) {
  1547. return;
  1548. }
  1549. this.setDragMode(hasClass(this.dragBox, CLASS_CROP) ? DRAG_MODE_MOVE : DRAG_MODE_CROP);
  1550. },
  1551. wheel: function wheel(event) {
  1552. var _this = this;
  1553. var ratio = Number(this.options.wheelZoomRatio) || 0.1;
  1554. var delta = 1;
  1555. if (this.disabled) {
  1556. return;
  1557. }
  1558. event.preventDefault(); // Limit wheel speed to prevent zoom too fast (#21)
  1559. if (this.wheeling) {
  1560. return;
  1561. }
  1562. this.wheeling = true;
  1563. setTimeout(function () {
  1564. _this.wheeling = false;
  1565. }, 50);
  1566. if (event.deltaY) {
  1567. delta = event.deltaY > 0 ? 1 : -1;
  1568. } else if (event.wheelDelta) {
  1569. delta = -event.wheelDelta / 120;
  1570. } else if (event.detail) {
  1571. delta = event.detail > 0 ? 1 : -1;
  1572. }
  1573. this.zoom(-delta * ratio, event);
  1574. },
  1575. cropStart: function cropStart(event) {
  1576. var buttons = event.buttons,
  1577. button = event.button;
  1578. if (this.disabled // No primary button (Usually the left button)
  1579. // Note that touch events have no `buttons` or `button` property
  1580. || isNumber(buttons) && buttons !== 1 || isNumber(button) && button !== 0 // Open context menu
  1581. || event.ctrlKey) {
  1582. return;
  1583. }
  1584. var options = this.options,
  1585. pointers = this.pointers;
  1586. var action;
  1587. if (event.changedTouches) {
  1588. // Handle touch event
  1589. forEach(event.changedTouches, function (touch) {
  1590. pointers[touch.identifier] = getPointer(touch);
  1591. });
  1592. } else {
  1593. // Handle mouse event and pointer event
  1594. pointers[event.pointerId || 0] = getPointer(event);
  1595. }
  1596. if (Object.keys(pointers).length > 1 && options.zoomable && options.zoomOnTouch) {
  1597. action = ACTION_ZOOM;
  1598. } else {
  1599. action = getData(event.target, DATA_ACTION);
  1600. }
  1601. if (!REGEXP_ACTIONS.test(action)) {
  1602. return;
  1603. }
  1604. if (dispatchEvent(this.element, EVENT_CROP_START, {
  1605. originalEvent: event,
  1606. action: action
  1607. }) === false) {
  1608. return;
  1609. } // This line is required for preventing page zooming in iOS browsers
  1610. event.preventDefault();
  1611. this.action = action;
  1612. this.cropping = false;
  1613. if (action === ACTION_CROP) {
  1614. this.cropping = true;
  1615. addClass(this.dragBox, CLASS_MODAL);
  1616. }
  1617. },
  1618. cropMove: function cropMove(event) {
  1619. var action = this.action;
  1620. if (this.disabled || !action) {
  1621. return;
  1622. }
  1623. var pointers = this.pointers;
  1624. event.preventDefault();
  1625. if (dispatchEvent(this.element, EVENT_CROP_MOVE, {
  1626. originalEvent: event,
  1627. action: action
  1628. }) === false) {
  1629. return;
  1630. }
  1631. if (event.changedTouches) {
  1632. forEach(event.changedTouches, function (touch) {
  1633. // The first parameter should not be undefined (#432)
  1634. assign(pointers[touch.identifier] || {}, getPointer(touch, true));
  1635. });
  1636. } else {
  1637. assign(pointers[event.pointerId || 0] || {}, getPointer(event, true));
  1638. }
  1639. this.change(event);
  1640. },
  1641. cropEnd: function cropEnd(event) {
  1642. if (this.disabled) {
  1643. return;
  1644. }
  1645. var action = this.action,
  1646. pointers = this.pointers;
  1647. if (event.changedTouches) {
  1648. forEach(event.changedTouches, function (touch) {
  1649. delete pointers[touch.identifier];
  1650. });
  1651. } else {
  1652. delete pointers[event.pointerId || 0];
  1653. }
  1654. if (!action) {
  1655. return;
  1656. }
  1657. event.preventDefault();
  1658. if (!Object.keys(pointers).length) {
  1659. this.action = '';
  1660. }
  1661. if (this.cropping) {
  1662. this.cropping = false;
  1663. toggleClass(this.dragBox, CLASS_MODAL, this.cropped && this.options.modal);
  1664. }
  1665. dispatchEvent(this.element, EVENT_CROP_END, {
  1666. originalEvent: event,
  1667. action: action
  1668. });
  1669. }
  1670. };
  1671. var change = {
  1672. change: function change(event) {
  1673. var options = this.options,
  1674. canvasData = this.canvasData,
  1675. containerData = this.containerData,
  1676. cropBoxData = this.cropBoxData,
  1677. pointers = this.pointers;
  1678. var action = this.action;
  1679. var aspectRatio = options.aspectRatio;
  1680. var left = cropBoxData.left,
  1681. top = cropBoxData.top,
  1682. width = cropBoxData.width,
  1683. height = cropBoxData.height;
  1684. var right = left + width;
  1685. var bottom = top + height;
  1686. var minLeft = 0;
  1687. var minTop = 0;
  1688. var maxWidth = containerData.width;
  1689. var maxHeight = containerData.height;
  1690. var renderable = true;
  1691. var offset; // Locking aspect ratio in "free mode" by holding shift key
  1692. if (!aspectRatio && event.shiftKey) {
  1693. aspectRatio = width && height ? width / height : 1;
  1694. }
  1695. if (this.limited) {
  1696. minLeft = cropBoxData.minLeft;
  1697. minTop = cropBoxData.minTop;
  1698. maxWidth = minLeft + Math.min(containerData.width, canvasData.width, canvasData.left + canvasData.width);
  1699. maxHeight = minTop + Math.min(containerData.height, canvasData.height, canvasData.top + canvasData.height);
  1700. }
  1701. var pointer = pointers[Object.keys(pointers)[0]];
  1702. var range = {
  1703. x: pointer.endX - pointer.startX,
  1704. y: pointer.endY - pointer.startY
  1705. };
  1706. var check = function check(side) {
  1707. switch (side) {
  1708. case ACTION_EAST:
  1709. if (right + range.x > maxWidth) {
  1710. range.x = maxWidth - right;
  1711. }
  1712. break;
  1713. case ACTION_WEST:
  1714. if (left + range.x < minLeft) {
  1715. range.x = minLeft - left;
  1716. }
  1717. break;
  1718. case ACTION_NORTH:
  1719. if (top + range.y < minTop) {
  1720. range.y = minTop - top;
  1721. }
  1722. break;
  1723. case ACTION_SOUTH:
  1724. if (bottom + range.y > maxHeight) {
  1725. range.y = maxHeight - bottom;
  1726. }
  1727. break;
  1728. default:
  1729. }
  1730. };
  1731. switch (action) {
  1732. // Move crop box
  1733. case ACTION_ALL:
  1734. left += range.x;
  1735. top += range.y;
  1736. break;
  1737. // Resize crop box
  1738. case ACTION_EAST:
  1739. if (range.x >= 0 && (right >= maxWidth || aspectRatio && (top <= minTop || bottom >= maxHeight))) {
  1740. renderable = false;
  1741. break;
  1742. }
  1743. check(ACTION_EAST);
  1744. width += range.x;
  1745. if (width < 0) {
  1746. action = ACTION_WEST;
  1747. width = -width;
  1748. left -= width;
  1749. }
  1750. if (aspectRatio) {
  1751. height = width / aspectRatio;
  1752. top += (cropBoxData.height - height) / 2;
  1753. }
  1754. break;
  1755. case ACTION_NORTH:
  1756. if (range.y <= 0 && (top <= minTop || aspectRatio && (left <= minLeft || right >= maxWidth))) {
  1757. renderable = false;
  1758. break;
  1759. }
  1760. check(ACTION_NORTH);
  1761. height -= range.y;
  1762. top += range.y;
  1763. if (height < 0) {
  1764. action = ACTION_SOUTH;
  1765. height = -height;
  1766. top -= height;
  1767. }
  1768. if (aspectRatio) {
  1769. width = height * aspectRatio;
  1770. left += (cropBoxData.width - width) / 2;
  1771. }
  1772. break;
  1773. case ACTION_WEST:
  1774. if (range.x <= 0 && (left <= minLeft || aspectRatio && (top <= minTop || bottom >= maxHeight))) {
  1775. renderable = false;
  1776. break;
  1777. }
  1778. check(ACTION_WEST);
  1779. width -= range.x;
  1780. left += range.x;
  1781. if (width < 0) {
  1782. action = ACTION_EAST;
  1783. width = -width;
  1784. left -= width;
  1785. }
  1786. if (aspectRatio) {
  1787. height = width / aspectRatio;
  1788. top += (cropBoxData.height - height) / 2;
  1789. }
  1790. break;
  1791. case ACTION_SOUTH:
  1792. if (range.y >= 0 && (bottom >= maxHeight || aspectRatio && (left <= minLeft || right >= maxWidth))) {
  1793. renderable = false;
  1794. break;
  1795. }
  1796. check(ACTION_SOUTH);
  1797. height += range.y;
  1798. if (height < 0) {
  1799. action = ACTION_NORTH;
  1800. height = -height;
  1801. top -= height;
  1802. }
  1803. if (aspectRatio) {
  1804. width = height * aspectRatio;
  1805. left += (cropBoxData.width - width) / 2;
  1806. }
  1807. break;
  1808. case ACTION_NORTH_EAST:
  1809. if (aspectRatio) {
  1810. if (range.y <= 0 && (top <= minTop || right >= maxWidth)) {
  1811. renderable = false;
  1812. break;
  1813. }
  1814. check(ACTION_NORTH);
  1815. height -= range.y;
  1816. top += range.y;
  1817. width = height * aspectRatio;
  1818. } else {
  1819. check(ACTION_NORTH);
  1820. check(ACTION_EAST);
  1821. if (range.x >= 0) {
  1822. if (right < maxWidth) {
  1823. width += range.x;
  1824. } else if (range.y <= 0 && top <= minTop) {
  1825. renderable = false;
  1826. }
  1827. } else {
  1828. width += range.x;
  1829. }
  1830. if (range.y <= 0) {
  1831. if (top > minTop) {
  1832. height -= range.y;
  1833. top += range.y;
  1834. }
  1835. } else {
  1836. height -= range.y;
  1837. top += range.y;
  1838. }
  1839. }
  1840. if (width < 0 && height < 0) {
  1841. action = ACTION_SOUTH_WEST;
  1842. height = -height;
  1843. width = -width;
  1844. top -= height;
  1845. left -= width;
  1846. } else if (width < 0) {
  1847. action = ACTION_NORTH_WEST;
  1848. width = -width;
  1849. left -= width;
  1850. } else if (height < 0) {
  1851. action = ACTION_SOUTH_EAST;
  1852. height = -height;
  1853. top -= height;
  1854. }
  1855. break;
  1856. case ACTION_NORTH_WEST:
  1857. if (aspectRatio) {
  1858. if (range.y <= 0 && (top <= minTop || left <= minLeft)) {
  1859. renderable = false;
  1860. break;
  1861. }
  1862. check(ACTION_NORTH);
  1863. height -= range.y;
  1864. top += range.y;
  1865. width = height * aspectRatio;
  1866. left += cropBoxData.width - width;
  1867. } else {
  1868. check(ACTION_NORTH);
  1869. check(ACTION_WEST);
  1870. if (range.x <= 0) {
  1871. if (left > minLeft) {
  1872. width -= range.x;
  1873. left += range.x;
  1874. } else if (range.y <= 0 && top <= minTop) {
  1875. renderable = false;
  1876. }
  1877. } else {
  1878. width -= range.x;
  1879. left += range.x;
  1880. }
  1881. if (range.y <= 0) {
  1882. if (top > minTop) {
  1883. height -= range.y;
  1884. top += range.y;
  1885. }
  1886. } else {
  1887. height -= range.y;
  1888. top += range.y;
  1889. }
  1890. }
  1891. if (width < 0 && height < 0) {
  1892. action = ACTION_SOUTH_EAST;
  1893. height = -height;
  1894. width = -width;
  1895. top -= height;
  1896. left -= width;
  1897. } else if (width < 0) {
  1898. action = ACTION_NORTH_EAST;
  1899. width = -width;
  1900. left -= width;
  1901. } else if (height < 0) {
  1902. action = ACTION_SOUTH_WEST;
  1903. height = -height;
  1904. top -= height;
  1905. }
  1906. break;
  1907. case ACTION_SOUTH_WEST:
  1908. if (aspectRatio) {
  1909. if (range.x <= 0 && (left <= minLeft || bottom >= maxHeight)) {
  1910. renderable = false;
  1911. break;
  1912. }
  1913. check(ACTION_WEST);
  1914. width -= range.x;
  1915. left += range.x;
  1916. height = width / aspectRatio;
  1917. } else {
  1918. check(ACTION_SOUTH);
  1919. check(ACTION_WEST);
  1920. if (range.x <= 0) {
  1921. if (left > minLeft) {
  1922. width -= range.x;
  1923. left += range.x;
  1924. } else if (range.y >= 0 && bottom >= maxHeight) {
  1925. renderable = false;
  1926. }
  1927. } else {
  1928. width -= range.x;
  1929. left += range.x;
  1930. }
  1931. if (range.y >= 0) {
  1932. if (bottom < maxHeight) {
  1933. height += range.y;
  1934. }
  1935. } else {
  1936. height += range.y;
  1937. }
  1938. }
  1939. if (width < 0 && height < 0) {
  1940. action = ACTION_NORTH_EAST;
  1941. height = -height;
  1942. width = -width;
  1943. top -= height;
  1944. left -= width;
  1945. } else if (width < 0) {
  1946. action = ACTION_SOUTH_EAST;
  1947. width = -width;
  1948. left -= width;
  1949. } else if (height < 0) {
  1950. action = ACTION_NORTH_WEST;
  1951. height = -height;
  1952. top -= height;
  1953. }
  1954. break;
  1955. case ACTION_SOUTH_EAST:
  1956. if (aspectRatio) {
  1957. if (range.x >= 0 && (right >= maxWidth || bottom >= maxHeight)) {
  1958. renderable = false;
  1959. break;
  1960. }
  1961. check(ACTION_EAST);
  1962. width += range.x;
  1963. height = width / aspectRatio;
  1964. } else {
  1965. check(ACTION_SOUTH);
  1966. check(ACTION_EAST);
  1967. if (range.x >= 0) {
  1968. if (right < maxWidth) {
  1969. width += range.x;
  1970. } else if (range.y >= 0 && bottom >= maxHeight) {
  1971. renderable = false;
  1972. }
  1973. } else {
  1974. width += range.x;
  1975. }
  1976. if (range.y >= 0) {
  1977. if (bottom < maxHeight) {
  1978. height += range.y;
  1979. }
  1980. } else {
  1981. height += range.y;
  1982. }
  1983. }
  1984. if (width < 0 && height < 0) {
  1985. action = ACTION_NORTH_WEST;
  1986. height = -height;
  1987. width = -width;
  1988. top -= height;
  1989. left -= width;
  1990. } else if (width < 0) {
  1991. action = ACTION_SOUTH_WEST;
  1992. width = -width;
  1993. left -= width;
  1994. } else if (height < 0) {
  1995. action = ACTION_NORTH_EAST;
  1996. height = -height;
  1997. top -= height;
  1998. }
  1999. break;
  2000. // Move canvas
  2001. case ACTION_MOVE:
  2002. this.move(range.x, range.y);
  2003. renderable = false;
  2004. break;
  2005. // Zoom canvas
  2006. case ACTION_ZOOM:
  2007. this.zoom(getMaxZoomRatio(pointers), event);
  2008. renderable = false;
  2009. break;
  2010. // Create crop box
  2011. case ACTION_CROP:
  2012. if (!range.x || !range.y) {
  2013. renderable = false;
  2014. break;
  2015. }
  2016. offset = getOffset(this.cropper);
  2017. left = pointer.startX - offset.left;
  2018. top = pointer.startY - offset.top;
  2019. width = cropBoxData.minWidth;
  2020. height = cropBoxData.minHeight;
  2021. if (range.x > 0) {
  2022. action = range.y > 0 ? ACTION_SOUTH_EAST : ACTION_NORTH_EAST;
  2023. } else if (range.x < 0) {
  2024. left -= width;
  2025. action = range.y > 0 ? ACTION_SOUTH_WEST : ACTION_NORTH_WEST;
  2026. }
  2027. if (range.y < 0) {
  2028. top -= height;
  2029. } // Show the crop box if is hidden
  2030. if (!this.cropped) {
  2031. removeClass(this.cropBox, CLASS_HIDDEN);
  2032. this.cropped = true;
  2033. if (this.limited) {
  2034. this.limitCropBox(true, true);
  2035. }
  2036. }
  2037. break;
  2038. default:
  2039. }
  2040. if (renderable) {
  2041. cropBoxData.width = width;
  2042. cropBoxData.height = height;
  2043. cropBoxData.left = left;
  2044. cropBoxData.top = top;
  2045. this.action = action;
  2046. this.renderCropBox();
  2047. } // Override
  2048. forEach(pointers, function (p) {
  2049. p.startX = p.endX;
  2050. p.startY = p.endY;
  2051. });
  2052. }
  2053. };
  2054. var methods = {
  2055. // Show the crop box manually
  2056. crop: function crop() {
  2057. if (this.ready && !this.cropped && !this.disabled) {
  2058. this.cropped = true;
  2059. this.limitCropBox(true, true);
  2060. if (this.options.modal) {
  2061. addClass(this.dragBox, CLASS_MODAL);
  2062. }
  2063. removeClass(this.cropBox, CLASS_HIDDEN);
  2064. this.setCropBoxData(this.initialCropBoxData);
  2065. }
  2066. return this;
  2067. },
  2068. // Reset the image and crop box to their initial states
  2069. reset: function reset() {
  2070. if (this.ready && !this.disabled) {
  2071. this.imageData = assign({}, this.initialImageData);
  2072. this.canvasData = assign({}, this.initialCanvasData);
  2073. this.cropBoxData = assign({}, this.initialCropBoxData);
  2074. this.renderCanvas();
  2075. if (this.cropped) {
  2076. this.renderCropBox();
  2077. }
  2078. }
  2079. return this;
  2080. },
  2081. // Clear the crop box
  2082. clear: function clear() {
  2083. if (this.cropped && !this.disabled) {
  2084. assign(this.cropBoxData, {
  2085. left: 0,
  2086. top: 0,
  2087. width: 0,
  2088. height: 0
  2089. });
  2090. this.cropped = false;
  2091. this.renderCropBox();
  2092. this.limitCanvas(true, true); // Render canvas after crop box rendered
  2093. this.renderCanvas();
  2094. removeClass(this.dragBox, CLASS_MODAL);
  2095. addClass(this.cropBox, CLASS_HIDDEN);
  2096. }
  2097. return this;
  2098. },
  2099. /**
  2100. * Replace the image's src and rebuild the cropper
  2101. * @param {string} url - The new URL.
  2102. * @param {boolean} [hasSameSize] - Indicate if the new image has the same size as the old one.
  2103. * @returns {Cropper} this
  2104. */
  2105. replace: function replace(url) {
  2106. var hasSameSize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  2107. if (!this.disabled && url) {
  2108. if (this.isImg) {
  2109. this.element.src = url;
  2110. }
  2111. if (hasSameSize) {
  2112. this.url = url;
  2113. this.image.src = url;
  2114. if (this.ready) {
  2115. this.viewBoxImage.src = url;
  2116. forEach(this.previews, function (element) {
  2117. element.getElementsByTagName('img')[0].src = url;
  2118. });
  2119. }
  2120. } else {
  2121. if (this.isImg) {
  2122. this.replaced = true;
  2123. }
  2124. this.options.data = null;
  2125. this.uncreate();
  2126. this.load(url);
  2127. }
  2128. }
  2129. return this;
  2130. },
  2131. // Enable (unfreeze) the cropper
  2132. enable: function enable() {
  2133. if (this.ready && this.disabled) {
  2134. this.disabled = false;
  2135. removeClass(this.cropper, CLASS_DISABLED);
  2136. }
  2137. return this;
  2138. },
  2139. // Disable (freeze) the cropper
  2140. disable: function disable() {
  2141. if (this.ready && !this.disabled) {
  2142. this.disabled = true;
  2143. addClass(this.cropper, CLASS_DISABLED);
  2144. }
  2145. return this;
  2146. },
  2147. /**
  2148. * Destroy the cropper and remove the instance from the image
  2149. * @returns {Cropper} this
  2150. */
  2151. destroy: function destroy() {
  2152. var element = this.element;
  2153. if (!element[NAMESPACE]) {
  2154. return this;
  2155. }
  2156. element[NAMESPACE] = undefined;
  2157. if (this.isImg && this.replaced) {
  2158. element.src = this.originalUrl;
  2159. }
  2160. this.uncreate();
  2161. return this;
  2162. },
  2163. /**
  2164. * Move the canvas with relative offsets
  2165. * @param {number} offsetX - The relative offset distance on the x-axis.
  2166. * @param {number} [offsetY=offsetX] - The relative offset distance on the y-axis.
  2167. * @returns {Cropper} this
  2168. */
  2169. move: function move(offsetX) {
  2170. var offsetY = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : offsetX;
  2171. var _this$canvasData = this.canvasData,
  2172. left = _this$canvasData.left,
  2173. top = _this$canvasData.top;
  2174. return this.moveTo(isUndefined(offsetX) ? offsetX : left + Number(offsetX), isUndefined(offsetY) ? offsetY : top + Number(offsetY));
  2175. },
  2176. /**
  2177. * Move the canvas to an absolute point
  2178. * @param {number} x - The x-axis coordinate.
  2179. * @param {number} [y=x] - The y-axis coordinate.
  2180. * @returns {Cropper} this
  2181. */
  2182. moveTo: function moveTo(x) {
  2183. var y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : x;
  2184. var canvasData = this.canvasData;
  2185. var changed = false;
  2186. x = Number(x);
  2187. y = Number(y);
  2188. if (this.ready && !this.disabled && this.options.movable) {
  2189. if (isNumber(x)) {
  2190. canvasData.left = x;
  2191. changed = true;
  2192. }
  2193. if (isNumber(y)) {
  2194. canvasData.top = y;
  2195. changed = true;
  2196. }
  2197. if (changed) {
  2198. this.renderCanvas(true);
  2199. }
  2200. }
  2201. return this;
  2202. },
  2203. /**
  2204. * Zoom the canvas with a relative ratio
  2205. * @param {number} ratio - The target ratio.
  2206. * @param {Event} _originalEvent - The original event if any.
  2207. * @returns {Cropper} this
  2208. */
  2209. zoom: function zoom(ratio, _originalEvent) {
  2210. var canvasData = this.canvasData;
  2211. ratio = Number(ratio);
  2212. if (ratio < 0) {
  2213. ratio = 1 / (1 - ratio);
  2214. } else {
  2215. ratio = 1 + ratio;
  2216. }
  2217. return this.zoomTo(canvasData.width * ratio / canvasData.naturalWidth, null, _originalEvent);
  2218. },
  2219. /**
  2220. * Zoom the canvas to an absolute ratio
  2221. * @param {number} ratio - The target ratio.
  2222. * @param {Object} pivot - The zoom pivot point coordinate.
  2223. * @param {Event} _originalEvent - The original event if any.
  2224. * @returns {Cropper} this
  2225. */
  2226. zoomTo: function zoomTo(ratio, pivot, _originalEvent) {
  2227. var options = this.options,
  2228. canvasData = this.canvasData;
  2229. var width = canvasData.width,
  2230. height = canvasData.height,
  2231. naturalWidth = canvasData.naturalWidth,
  2232. naturalHeight = canvasData.naturalHeight;
  2233. ratio = Number(ratio);
  2234. if (ratio >= 0 && this.ready && !this.disabled && options.zoomable) {
  2235. var newWidth = naturalWidth * ratio;
  2236. var newHeight = naturalHeight * ratio;
  2237. if (dispatchEvent(this.element, EVENT_ZOOM, {
  2238. ratio: ratio,
  2239. oldRatio: width / naturalWidth,
  2240. originalEvent: _originalEvent
  2241. }) === false) {
  2242. return this;
  2243. }
  2244. if (_originalEvent) {
  2245. var pointers = this.pointers;
  2246. var offset = getOffset(this.cropper);
  2247. var center = pointers && Object.keys(pointers).length ? getPointersCenter(pointers) : {
  2248. pageX: _originalEvent.pageX,
  2249. pageY: _originalEvent.pageY
  2250. }; // Zoom from the triggering point of the event
  2251. canvasData.left -= (newWidth - width) * ((center.pageX - offset.left - canvasData.left) / width);
  2252. canvasData.top -= (newHeight - height) * ((center.pageY - offset.top - canvasData.top) / height);
  2253. } else if (isPlainObject(pivot) && isNumber(pivot.x) && isNumber(pivot.y)) {
  2254. canvasData.left -= (newWidth - width) * ((pivot.x - canvasData.left) / width);
  2255. canvasData.top -= (newHeight - height) * ((pivot.y - canvasData.top) / height);
  2256. } else {
  2257. // Zoom from the center of the canvas
  2258. canvasData.left -= (newWidth - width) / 2;
  2259. canvasData.top -= (newHeight - height) / 2;
  2260. }
  2261. canvasData.width = newWidth;
  2262. canvasData.height = newHeight;
  2263. this.renderCanvas(true);
  2264. }
  2265. return this;
  2266. },
  2267. /**
  2268. * Rotate the canvas with a relative degree
  2269. * @param {number} degree - The rotate degree.
  2270. * @returns {Cropper} this
  2271. */
  2272. rotate: function rotate(degree) {
  2273. return this.rotateTo((this.imageData.rotate || 0) + Number(degree));
  2274. },
  2275. /**
  2276. * Rotate the canvas to an absolute degree
  2277. * @param {number} degree - The rotate degree.
  2278. * @returns {Cropper} this
  2279. */
  2280. rotateTo: function rotateTo(degree) {
  2281. degree = Number(degree);
  2282. if (isNumber(degree) && this.ready && !this.disabled && this.options.rotatable) {
  2283. this.imageData.rotate = degree % 360;
  2284. this.renderCanvas(true, true);
  2285. }
  2286. return this;
  2287. },
  2288. /**
  2289. * Scale the image on the x-axis.
  2290. * @param {number} scaleX - The scale ratio on the x-axis.
  2291. * @returns {Cropper} this
  2292. */
  2293. scaleX: function scaleX(_scaleX) {
  2294. var scaleY = this.imageData.scaleY;
  2295. return this.scale(_scaleX, isNumber(scaleY) ? scaleY : 1);
  2296. },
  2297. /**
  2298. * Scale the image on the y-axis.
  2299. * @param {number} scaleY - The scale ratio on the y-axis.
  2300. * @returns {Cropper} this
  2301. */
  2302. scaleY: function scaleY(_scaleY) {
  2303. var scaleX = this.imageData.scaleX;
  2304. return this.scale(isNumber(scaleX) ? scaleX : 1, _scaleY);
  2305. },
  2306. /**
  2307. * Scale the image
  2308. * @param {number} scaleX - The scale ratio on the x-axis.
  2309. * @param {number} [scaleY=scaleX] - The scale ratio on the y-axis.
  2310. * @returns {Cropper} this
  2311. */
  2312. scale: function scale(scaleX) {
  2313. var scaleY = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : scaleX;
  2314. var imageData = this.imageData;
  2315. var transformed = false;
  2316. scaleX = Number(scaleX);
  2317. scaleY = Number(scaleY);
  2318. if (this.ready && !this.disabled && this.options.scalable) {
  2319. if (isNumber(scaleX)) {
  2320. imageData.scaleX = scaleX;
  2321. transformed = true;
  2322. }
  2323. if (isNumber(scaleY)) {
  2324. imageData.scaleY = scaleY;
  2325. transformed = true;
  2326. }
  2327. if (transformed) {
  2328. this.renderCanvas(true, true);
  2329. }
  2330. }
  2331. return this;
  2332. },
  2333. /**
  2334. * Get the cropped area position and size data (base on the original image)
  2335. * @param {boolean} [rounded=false] - Indicate if round the data values or not.
  2336. * @returns {Object} The result cropped data.
  2337. */
  2338. getData: function getData() {
  2339. var rounded = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  2340. var options = this.options,
  2341. imageData = this.imageData,
  2342. canvasData = this.canvasData,
  2343. cropBoxData = this.cropBoxData;
  2344. var data;
  2345. if (this.ready && this.cropped) {
  2346. data = {
  2347. x: cropBoxData.left - canvasData.left,
  2348. y: cropBoxData.top - canvasData.top,
  2349. width: cropBoxData.width,
  2350. height: cropBoxData.height
  2351. };
  2352. var ratio = imageData.width / imageData.naturalWidth;
  2353. forEach(data, function (n, i) {
  2354. data[i] = n / ratio;
  2355. });
  2356. if (rounded) {
  2357. // In case rounding off leads to extra 1px in right or bottom border
  2358. // we should round the top-left corner and the dimension (#343).
  2359. var bottom = Math.round(data.y + data.height);
  2360. var right = Math.round(data.x + data.width);
  2361. data.x = Math.round(data.x);
  2362. data.y = Math.round(data.y);
  2363. data.width = right - data.x;
  2364. data.height = bottom - data.y;
  2365. }
  2366. } else {
  2367. data = {
  2368. x: 0,
  2369. y: 0,
  2370. width: 0,
  2371. height: 0
  2372. };
  2373. }
  2374. if (options.rotatable) {
  2375. data.rotate = imageData.rotate || 0;
  2376. }
  2377. if (options.scalable) {
  2378. data.scaleX = imageData.scaleX || 1;
  2379. data.scaleY = imageData.scaleY || 1;
  2380. }
  2381. return data;
  2382. },
  2383. /**
  2384. * Set the cropped area position and size with new data
  2385. * @param {Object} data - The new data.
  2386. * @returns {Cropper} this
  2387. */
  2388. setData: function setData(data) {
  2389. var options = this.options,
  2390. imageData = this.imageData,
  2391. canvasData = this.canvasData;
  2392. var cropBoxData = {};
  2393. if (this.ready && !this.disabled && isPlainObject(data)) {
  2394. var transformed = false;
  2395. if (options.rotatable) {
  2396. if (isNumber(data.rotate) && data.rotate !== imageData.rotate) {
  2397. imageData.rotate = data.rotate;
  2398. transformed = true;
  2399. }
  2400. }
  2401. if (options.scalable) {
  2402. if (isNumber(data.scaleX) && data.scaleX !== imageData.scaleX) {
  2403. imageData.scaleX = data.scaleX;
  2404. transformed = true;
  2405. }
  2406. if (isNumber(data.scaleY) && data.scaleY !== imageData.scaleY) {
  2407. imageData.scaleY = data.scaleY;
  2408. transformed = true;
  2409. }
  2410. }
  2411. if (transformed) {
  2412. this.renderCanvas(true, true);
  2413. }
  2414. var ratio = imageData.width / imageData.naturalWidth;
  2415. if (isNumber(data.x)) {
  2416. cropBoxData.left = data.x * ratio + canvasData.left;
  2417. }
  2418. if (isNumber(data.y)) {
  2419. cropBoxData.top = data.y * ratio + canvasData.top;
  2420. }
  2421. if (isNumber(data.width)) {
  2422. cropBoxData.width = data.width * ratio;
  2423. }
  2424. if (isNumber(data.height)) {
  2425. cropBoxData.height = data.height * ratio;
  2426. }
  2427. this.setCropBoxData(cropBoxData);
  2428. }
  2429. return this;
  2430. },
  2431. /**
  2432. * Get the container size data.
  2433. * @returns {Object} The result container data.
  2434. */
  2435. getContainerData: function getContainerData() {
  2436. return this.ready ? assign({}, this.containerData) : {};
  2437. },
  2438. /**
  2439. * Get the image position and size data.
  2440. * @returns {Object} The result image data.
  2441. */
  2442. getImageData: function getImageData() {
  2443. return this.sized ? assign({}, this.imageData) : {};
  2444. },
  2445. /**
  2446. * Get the canvas position and size data.
  2447. * @returns {Object} The result canvas data.
  2448. */
  2449. getCanvasData: function getCanvasData() {
  2450. var canvasData = this.canvasData;
  2451. var data = {};
  2452. if (this.ready) {
  2453. forEach(['left', 'top', 'width', 'height', 'naturalWidth', 'naturalHeight'], function (n) {
  2454. data[n] = canvasData[n];
  2455. });
  2456. }
  2457. return data;
  2458. },
  2459. /**
  2460. * Set the canvas position and size with new data.
  2461. * @param {Object} data - The new canvas data.
  2462. * @returns {Cropper} this
  2463. */
  2464. setCanvasData: function setCanvasData(data) {
  2465. var canvasData = this.canvasData;
  2466. var aspectRatio = canvasData.aspectRatio;
  2467. if (this.ready && !this.disabled && isPlainObject(data)) {
  2468. if (isNumber(data.left)) {
  2469. canvasData.left = data.left;
  2470. }
  2471. if (isNumber(data.top)) {
  2472. canvasData.top = data.top;
  2473. }
  2474. if (isNumber(data.width)) {
  2475. canvasData.width = data.width;
  2476. canvasData.height = data.width / aspectRatio;
  2477. } else if (isNumber(data.height)) {
  2478. canvasData.height = data.height;
  2479. canvasData.width = data.height * aspectRatio;
  2480. }
  2481. this.renderCanvas(true);
  2482. }
  2483. return this;
  2484. },
  2485. /**
  2486. * Get the crop box position and size data.
  2487. * @returns {Object} The result crop box data.
  2488. */
  2489. getCropBoxData: function getCropBoxData() {
  2490. var cropBoxData = this.cropBoxData;
  2491. var data;
  2492. if (this.ready && this.cropped) {
  2493. data = {
  2494. left: cropBoxData.left,
  2495. top: cropBoxData.top,
  2496. width: cropBoxData.width,
  2497. height: cropBoxData.height
  2498. };
  2499. }
  2500. return data || {};
  2501. },
  2502. /**
  2503. * Set the crop box position and size with new data.
  2504. * @param {Object} data - The new crop box data.
  2505. * @returns {Cropper} this
  2506. */
  2507. setCropBoxData: function setCropBoxData(data) {
  2508. var cropBoxData = this.cropBoxData;
  2509. var aspectRatio = this.options.aspectRatio;
  2510. var widthChanged;
  2511. var heightChanged;
  2512. if (this.ready && this.cropped && !this.disabled && isPlainObject(data)) {
  2513. if (isNumber(data.left)) {
  2514. cropBoxData.left = data.left;
  2515. }
  2516. if (isNumber(data.top)) {
  2517. cropBoxData.top = data.top;
  2518. }
  2519. if (isNumber(data.width) && data.width !== cropBoxData.width) {
  2520. widthChanged = true;
  2521. cropBoxData.width = data.width;
  2522. }
  2523. if (isNumber(data.height) && data.height !== cropBoxData.height) {
  2524. heightChanged = true;
  2525. cropBoxData.height = data.height;
  2526. }
  2527. if (aspectRatio) {
  2528. if (widthChanged) {
  2529. cropBoxData.height = cropBoxData.width / aspectRatio;
  2530. } else if (heightChanged) {
  2531. cropBoxData.width = cropBoxData.height * aspectRatio;
  2532. }
  2533. }
  2534. this.renderCropBox();
  2535. }
  2536. return this;
  2537. },
  2538. /**
  2539. * Get a canvas drawn the cropped image.
  2540. * @param {Object} [options={}] - The config options.
  2541. * @returns {HTMLCanvasElement} - The result canvas.
  2542. */
  2543. getCroppedCanvas: function getCroppedCanvas() {
  2544. var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  2545. if (!this.ready || !window.HTMLCanvasElement) {
  2546. return null;
  2547. }
  2548. var canvasData = this.canvasData;
  2549. var source = getSourceCanvas(this.image, this.imageData, canvasData, options); // Returns the source canvas if it is not cropped.
  2550. if (!this.cropped) {
  2551. return source;
  2552. }
  2553. var _this$getData = this.getData(),
  2554. initialX = _this$getData.x,
  2555. initialY = _this$getData.y,
  2556. initialWidth = _this$getData.width,
  2557. initialHeight = _this$getData.height;
  2558. var ratio = source.width / Math.floor(canvasData.naturalWidth);
  2559. if (ratio !== 1) {
  2560. initialX *= ratio;
  2561. initialY *= ratio;
  2562. initialWidth *= ratio;
  2563. initialHeight *= ratio;
  2564. }
  2565. var aspectRatio = initialWidth / initialHeight;
  2566. var maxSizes = getAdjustedSizes({
  2567. aspectRatio: aspectRatio,
  2568. width: options.maxWidth || Infinity,
  2569. height: options.maxHeight || Infinity
  2570. });
  2571. var minSizes = getAdjustedSizes({
  2572. aspectRatio: aspectRatio,
  2573. width: options.minWidth || 0,
  2574. height: options.minHeight || 0
  2575. }, 'cover');
  2576. var _getAdjustedSizes = getAdjustedSizes({
  2577. aspectRatio: aspectRatio,
  2578. width: options.width || (ratio !== 1 ? source.width : initialWidth),
  2579. height: options.height || (ratio !== 1 ? source.height : initialHeight)
  2580. }),
  2581. width = _getAdjustedSizes.width,
  2582. height = _getAdjustedSizes.height;
  2583. width = Math.min(maxSizes.width, Math.max(minSizes.width, width));
  2584. height = Math.min(maxSizes.height, Math.max(minSizes.height, height));
  2585. var canvas = document.createElement('canvas');
  2586. var context = canvas.getContext('2d');
  2587. canvas.width = normalizeDecimalNumber(width);
  2588. canvas.height = normalizeDecimalNumber(height);
  2589. context.fillStyle = options.fillColor || 'transparent';
  2590. context.fillRect(0, 0, width, height);
  2591. var _options$imageSmoothi = options.imageSmoothingEnabled,
  2592. imageSmoothingEnabled = _options$imageSmoothi === void 0 ? true : _options$imageSmoothi,
  2593. imageSmoothingQuality = options.imageSmoothingQuality;
  2594. context.imageSmoothingEnabled = imageSmoothingEnabled;
  2595. if (imageSmoothingQuality) {
  2596. context.imageSmoothingQuality = imageSmoothingQuality;
  2597. } // https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D.drawImage
  2598. var sourceWidth = source.width;
  2599. var sourceHeight = source.height; // Source canvas parameters
  2600. var srcX = initialX;
  2601. var srcY = initialY;
  2602. var srcWidth;
  2603. var srcHeight; // Destination canvas parameters
  2604. var dstX;
  2605. var dstY;
  2606. var dstWidth;
  2607. var dstHeight;
  2608. if (srcX <= -initialWidth || srcX > sourceWidth) {
  2609. srcX = 0;
  2610. srcWidth = 0;
  2611. dstX = 0;
  2612. dstWidth = 0;
  2613. } else if (srcX <= 0) {
  2614. dstX = -srcX;
  2615. srcX = 0;
  2616. srcWidth = Math.min(sourceWidth, initialWidth + srcX);
  2617. dstWidth = srcWidth;
  2618. } else if (srcX <= sourceWidth) {
  2619. dstX = 0;
  2620. srcWidth = Math.min(initialWidth, sourceWidth - srcX);
  2621. dstWidth = srcWidth;
  2622. }
  2623. if (srcWidth <= 0 || srcY <= -initialHeight || srcY > sourceHeight) {
  2624. srcY = 0;
  2625. srcHeight = 0;
  2626. dstY = 0;
  2627. dstHeight = 0;
  2628. } else if (srcY <= 0) {
  2629. dstY = -srcY;
  2630. srcY = 0;
  2631. srcHeight = Math.min(sourceHeight, initialHeight + srcY);
  2632. dstHeight = srcHeight;
  2633. } else if (srcY <= sourceHeight) {
  2634. dstY = 0;
  2635. srcHeight = Math.min(initialHeight, sourceHeight - srcY);
  2636. dstHeight = srcHeight;
  2637. }
  2638. var params = [srcX, srcY, srcWidth, srcHeight]; // Avoid "IndexSizeError"
  2639. if (dstWidth > 0 && dstHeight > 0) {
  2640. var scale = width / initialWidth;
  2641. params.push(dstX * scale, dstY * scale, dstWidth * scale, dstHeight * scale);
  2642. } // All the numerical parameters should be integer for `drawImage`
  2643. // https://github.com/fengyuanchen/cropper/issues/476
  2644. context.drawImage.apply(context, [source].concat(_toConsumableArray(params.map(function (param) {
  2645. return Math.floor(normalizeDecimalNumber(param));
  2646. }))));
  2647. return canvas;
  2648. },
  2649. /**
  2650. * Change the aspect ratio of the crop box.
  2651. * @param {number} aspectRatio - The new aspect ratio.
  2652. * @returns {Cropper} this
  2653. */
  2654. setAspectRatio: function setAspectRatio(aspectRatio) {
  2655. var options = this.options;
  2656. if (!this.disabled && !isUndefined(aspectRatio)) {
  2657. // 0 -> NaN
  2658. options.aspectRatio = Math.max(0, aspectRatio) || NaN;
  2659. if (this.ready) {
  2660. this.initCropBox();
  2661. if (this.cropped) {
  2662. this.renderCropBox();
  2663. }
  2664. }
  2665. }
  2666. return this;
  2667. },
  2668. /**
  2669. * Change the drag mode.
  2670. * @param {string} mode - The new drag mode.
  2671. * @returns {Cropper} this
  2672. */
  2673. setDragMode: function setDragMode(mode) {
  2674. var options = this.options,
  2675. dragBox = this.dragBox,
  2676. face = this.face;
  2677. if (this.ready && !this.disabled) {
  2678. var croppable = mode === DRAG_MODE_CROP;
  2679. var movable = options.movable && mode === DRAG_MODE_MOVE;
  2680. mode = croppable || movable ? mode : DRAG_MODE_NONE;
  2681. options.dragMode = mode;
  2682. setData(dragBox, DATA_ACTION, mode);
  2683. toggleClass(dragBox, CLASS_CROP, croppable);
  2684. toggleClass(dragBox, CLASS_MOVE, movable);
  2685. if (!options.cropBoxMovable) {
  2686. // Sync drag mode to crop box when it is not movable
  2687. setData(face, DATA_ACTION, mode);
  2688. toggleClass(face, CLASS_CROP, croppable);
  2689. toggleClass(face, CLASS_MOVE, movable);
  2690. }
  2691. }
  2692. return this;
  2693. }
  2694. };
  2695. var AnotherCropper = WINDOW.Cropper;
  2696. var Cropper =
  2697. /*#__PURE__*/
  2698. function () {
  2699. /**
  2700. * Create a new Cropper.
  2701. * @param {Element} element - The target element for cropping.
  2702. * @param {Object} [options={}] - The configuration options.
  2703. */
  2704. function Cropper(element) {
  2705. var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  2706. _classCallCheck(this, Cropper);
  2707. if (!element || !REGEXP_TAG_NAME.test(element.tagName)) {
  2708. throw new Error('The first argument is required and must be an <img> or <canvas> element.');
  2709. }
  2710. this.element = element;
  2711. this.options = assign({}, DEFAULTS, isPlainObject(options) && options);
  2712. this.cropped = false;
  2713. this.disabled = false;
  2714. this.pointers = {};
  2715. this.ready = false;
  2716. this.reloading = false;
  2717. this.replaced = false;
  2718. this.sized = false;
  2719. this.sizing = false;
  2720. this.init();
  2721. }
  2722. _createClass(Cropper, [{
  2723. key: "init",
  2724. value: function init() {
  2725. var element = this.element;
  2726. var tagName = element.tagName.toLowerCase();
  2727. var url;
  2728. if (element[NAMESPACE]) {
  2729. return;
  2730. }
  2731. element[NAMESPACE] = this;
  2732. if (tagName === 'img') {
  2733. this.isImg = true; // e.g.: "img/picture.jpg"
  2734. url = element.getAttribute('src') || '';
  2735. this.originalUrl = url; // Stop when it's a blank image
  2736. if (!url) {
  2737. return;
  2738. } // e.g.: "http://example.com/img/picture.jpg"
  2739. url = element.src;
  2740. } else if (tagName === 'canvas' && window.HTMLCanvasElement) {
  2741. url = element.toDataURL();
  2742. }
  2743. this.load(url);
  2744. }
  2745. }, {
  2746. key: "load",
  2747. value: function load(url) {
  2748. var _this = this;
  2749. if (!url) {
  2750. return;
  2751. }
  2752. this.url = url;
  2753. this.imageData = {};
  2754. var element = this.element,
  2755. options = this.options;
  2756. if (!options.rotatable && !options.scalable) {
  2757. options.checkOrientation = false;
  2758. } // Only IE10+ supports Typed Arrays
  2759. if (!options.checkOrientation || !window.ArrayBuffer) {
  2760. this.clone();
  2761. return;
  2762. } // Read ArrayBuffer from Data URL of JPEG images directly for better performance.
  2763. if (REGEXP_DATA_URL_JPEG.test(url)) {
  2764. this.read(dataURLToArrayBuffer(url));
  2765. return;
  2766. }
  2767. var xhr = new XMLHttpRequest();
  2768. var clone = this.clone.bind(this);
  2769. this.reloading = true;
  2770. this.xhr = xhr; // 1. Cross origin requests are only supported for protocol schemes:
  2771. // http, https, data, chrome, chrome-extension.
  2772. // 2. Access to XMLHttpRequest from a Data URL will be blocked by CORS policy
  2773. // in some browsers as IE11 and Safari.
  2774. xhr.onabort = clone;
  2775. xhr.onerror = clone;
  2776. xhr.ontimeout = clone;
  2777. xhr.onprogress = function () {
  2778. if (xhr.getResponseHeader('content-type') !== MIME_TYPE_JPEG) {
  2779. xhr.abort();
  2780. }
  2781. };
  2782. xhr.onload = function () {
  2783. _this.read(xhr.response);
  2784. };
  2785. xhr.onloadend = function () {
  2786. _this.reloading = false;
  2787. _this.xhr = null;
  2788. }; // Bust cache when there is a "crossOrigin" property to avoid browser cache error
  2789. if (options.checkCrossOrigin && isCrossOriginURL(url) && element.crossOrigin) {
  2790. url = addTimestamp(url);
  2791. }
  2792. xhr.open('GET', url);
  2793. xhr.responseType = 'arraybuffer';
  2794. xhr.withCredentials = element.crossOrigin === 'use-credentials';
  2795. xhr.send();
  2796. }
  2797. }, {
  2798. key: "read",
  2799. value: function read(arrayBuffer) {
  2800. var options = this.options,
  2801. imageData = this.imageData; // Reset the orientation value to its default value 1
  2802. // as some iOS browsers will render image with its orientation
  2803. var orientation = resetAndGetOrientation(arrayBuffer);
  2804. var rotate = 0;
  2805. var scaleX = 1;
  2806. var scaleY = 1;
  2807. if (orientation > 1) {
  2808. // Generate a new URL which has the default orientation value
  2809. this.url = arrayBufferToDataURL(arrayBuffer, MIME_TYPE_JPEG);
  2810. var _parseOrientation = parseOrientation(orientation);
  2811. rotate = _parseOrientation.rotate;
  2812. scaleX = _parseOrientation.scaleX;
  2813. scaleY = _parseOrientation.scaleY;
  2814. }
  2815. if (options.rotatable) {
  2816. imageData.rotate = rotate;
  2817. }
  2818. if (options.scalable) {
  2819. imageData.scaleX = scaleX;
  2820. imageData.scaleY = scaleY;
  2821. }
  2822. this.clone();
  2823. }
  2824. }, {
  2825. key: "clone",
  2826. value: function clone() {
  2827. var element = this.element,
  2828. url = this.url;
  2829. var crossOrigin;
  2830. var crossOriginUrl;
  2831. if (this.options.checkCrossOrigin && isCrossOriginURL(url)) {
  2832. crossOrigin = element.crossOrigin;
  2833. if (crossOrigin) {
  2834. crossOriginUrl = url;
  2835. } else {
  2836. crossOrigin = 'anonymous'; // Bust cache when there is not a "crossOrigin" property
  2837. crossOriginUrl = addTimestamp(url);
  2838. }
  2839. }
  2840. this.crossOrigin = crossOrigin;
  2841. this.crossOriginUrl = crossOriginUrl;
  2842. var image = document.createElement('img');
  2843. if (crossOrigin) {
  2844. image.crossOrigin = crossOrigin;
  2845. }
  2846. image.src = crossOriginUrl || url;
  2847. this.image = image;
  2848. image.onload = this.start.bind(this);
  2849. image.onerror = this.stop.bind(this);
  2850. addClass(image, CLASS_HIDE);
  2851. element.parentNode.insertBefore(image, element.nextSibling);
  2852. }
  2853. }, {
  2854. key: "start",
  2855. value: function start() {
  2856. var _this2 = this;
  2857. var image = this.isImg ? this.element : this.image;
  2858. image.onload = null;
  2859. image.onerror = null;
  2860. this.sizing = true;
  2861. var IS_SAFARI = WINDOW.navigator && /^(?:.(?!chrome|android))*safari/i.test(WINDOW.navigator.userAgent);
  2862. var done = function done(naturalWidth, naturalHeight) {
  2863. assign(_this2.imageData, {
  2864. naturalWidth: naturalWidth,
  2865. naturalHeight: naturalHeight,
  2866. aspectRatio: naturalWidth / naturalHeight
  2867. });
  2868. _this2.sizing = false;
  2869. _this2.sized = true;
  2870. _this2.build();
  2871. }; // Modern browsers (except Safari)
  2872. if (image.naturalWidth && !IS_SAFARI) {
  2873. done(image.naturalWidth, image.naturalHeight);
  2874. return;
  2875. }
  2876. var sizingImage = document.createElement('img');
  2877. var body = document.body || document.documentElement;
  2878. this.sizingImage = sizingImage;
  2879. sizingImage.onload = function () {
  2880. done(sizingImage.width, sizingImage.height);
  2881. if (!IS_SAFARI) {
  2882. body.removeChild(sizingImage);
  2883. }
  2884. };
  2885. sizingImage.src = image.src; // iOS Safari will convert the image automatically
  2886. // with its orientation once append it into DOM (#279)
  2887. if (!IS_SAFARI) {
  2888. sizingImage.style.cssText = 'left:0;' + 'max-height:none!important;' + 'max-width:none!important;' + 'min-height:0!important;' + 'min-width:0!important;' + 'opacity:0;' + 'position:absolute;' + 'top:0;' + 'z-index:-1;';
  2889. body.appendChild(sizingImage);
  2890. }
  2891. }
  2892. }, {
  2893. key: "stop",
  2894. value: function stop() {
  2895. var image = this.image;
  2896. image.onload = null;
  2897. image.onerror = null;
  2898. image.parentNode.removeChild(image);
  2899. this.image = null;
  2900. }
  2901. }, {
  2902. key: "build",
  2903. value: function build() {
  2904. if (!this.sized || this.ready) {
  2905. return;
  2906. }
  2907. var element = this.element,
  2908. options = this.options,
  2909. image = this.image; // Create cropper elements
  2910. var container = element.parentNode;
  2911. var template = document.createElement('div');
  2912. template.innerHTML = TEMPLATE;
  2913. var cropper = template.querySelector(".".concat(NAMESPACE, "-container"));
  2914. var canvas = cropper.querySelector(".".concat(NAMESPACE, "-canvas"));
  2915. var dragBox = cropper.querySelector(".".concat(NAMESPACE, "-drag-box"));
  2916. var cropBox = cropper.querySelector(".".concat(NAMESPACE, "-crop-box"));
  2917. var face = cropBox.querySelector(".".concat(NAMESPACE, "-face"));
  2918. this.container = container;
  2919. this.cropper = cropper;
  2920. this.canvas = canvas;
  2921. this.dragBox = dragBox;
  2922. this.cropBox = cropBox;
  2923. this.viewBox = cropper.querySelector(".".concat(NAMESPACE, "-view-box"));
  2924. this.face = face;
  2925. canvas.appendChild(image); // Hide the original image
  2926. addClass(element, CLASS_HIDDEN); // Inserts the cropper after to the current image
  2927. container.insertBefore(cropper, element.nextSibling); // Show the image if is hidden
  2928. if (!this.isImg) {
  2929. removeClass(image, CLASS_HIDE);
  2930. }
  2931. this.initPreview();
  2932. this.bind();
  2933. options.initialAspectRatio = Math.max(0, options.initialAspectRatio) || NaN;
  2934. options.aspectRatio = Math.max(0, options.aspectRatio) || NaN;
  2935. options.viewMode = Math.max(0, Math.min(3, Math.round(options.viewMode))) || 0;
  2936. addClass(cropBox, CLASS_HIDDEN);
  2937. if (!options.guides) {
  2938. addClass(cropBox.getElementsByClassName("".concat(NAMESPACE, "-dashed")), CLASS_HIDDEN);
  2939. }
  2940. if (!options.center) {
  2941. addClass(cropBox.getElementsByClassName("".concat(NAMESPACE, "-center")), CLASS_HIDDEN);
  2942. }
  2943. if (options.background) {
  2944. addClass(cropper, "".concat(NAMESPACE, "-bg"));
  2945. }
  2946. if (!options.highlight) {
  2947. addClass(face, CLASS_INVISIBLE);
  2948. }
  2949. if (options.cropBoxMovable) {
  2950. addClass(face, CLASS_MOVE);
  2951. setData(face, DATA_ACTION, ACTION_ALL);
  2952. }
  2953. if (!options.cropBoxResizable) {
  2954. addClass(cropBox.getElementsByClassName("".concat(NAMESPACE, "-line")), CLASS_HIDDEN);
  2955. addClass(cropBox.getElementsByClassName("".concat(NAMESPACE, "-point")), CLASS_HIDDEN);
  2956. }
  2957. this.render();
  2958. this.ready = true;
  2959. this.setDragMode(options.dragMode);
  2960. if (options.autoCrop) {
  2961. this.crop();
  2962. }
  2963. this.setData(options.data);
  2964. if (isFunction(options.ready)) {
  2965. addListener(element, EVENT_READY, options.ready, {
  2966. once: true
  2967. });
  2968. }
  2969. dispatchEvent(element, EVENT_READY);
  2970. }
  2971. }, {
  2972. key: "unbuild",
  2973. value: function unbuild() {
  2974. if (!this.ready) {
  2975. return;
  2976. }
  2977. this.ready = false;
  2978. this.unbind();
  2979. this.resetPreview();
  2980. this.cropper.parentNode.removeChild(this.cropper);
  2981. removeClass(this.element, CLASS_HIDDEN);
  2982. }
  2983. }, {
  2984. key: "uncreate",
  2985. value: function uncreate() {
  2986. if (this.ready) {
  2987. this.unbuild();
  2988. this.ready = false;
  2989. this.cropped = false;
  2990. } else if (this.sizing) {
  2991. this.sizingImage.onload = null;
  2992. this.sizing = false;
  2993. this.sized = false;
  2994. } else if (this.reloading) {
  2995. this.xhr.onabort = null;
  2996. this.xhr.abort();
  2997. } else if (this.image) {
  2998. this.stop();
  2999. }
  3000. }
  3001. /**
  3002. * Get the no conflict cropper class.
  3003. * @returns {Cropper} The cropper class.
  3004. */
  3005. }], [{
  3006. key: "noConflict",
  3007. value: function noConflict() {
  3008. window.Cropper = AnotherCropper;
  3009. return Cropper;
  3010. }
  3011. /**
  3012. * Change the default options.
  3013. * @param {Object} options - The new default options.
  3014. */
  3015. }, {
  3016. key: "setDefaults",
  3017. value: function setDefaults(options) {
  3018. assign(DEFAULTS, isPlainObject(options) && options);
  3019. }
  3020. }]);
  3021. return Cropper;
  3022. }();
  3023. assign(Cropper.prototype, render, preview, events, handlers, change, methods);
  3024. export default Cropper;