cropper.common.js 100 KB

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