cropper.js 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005
  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. });