Нет описания

cropper.js 90KB

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