cropper.esm.js 105 KB

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