cropper.js 108 KB

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