cropper.esm.js 98 KB

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