cropper.js 78 KB

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