cropper.common.js 98 KB

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