Cropper.js 129 KB

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