cropper.js 108 KB

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