cropper.js 98 KB

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