cropper.js 65 KB

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