cropper.js 126 KB

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