cropper.common.js 102 KB

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