cropper.esm.js 100 KB

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