cropper.js 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983
  1. /*!
  2. * Cropper v0.9.2
  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-04-18T04:35:01.500Z
  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. var $window = $(window),
  24. $document = $(document),
  25. location = window.location,
  26. // Constants
  27. CROPPER_NAMESPACE = '.cropper',
  28. CROPPER_PREVIEW = 'preview' + CROPPER_NAMESPACE,
  29. // RegExps
  30. REGEXP_DRAG_TYPES = /^(e|n|w|s|ne|nw|sw|se|all|crop|move|zoom)$/,
  31. // Classes
  32. CLASS_MODAL = 'cropper-modal',
  33. CLASS_HIDE = 'cropper-hide',
  34. CLASS_HIDDEN = 'cropper-hidden',
  35. CLASS_INVISIBLE = 'cropper-invisible',
  36. CLASS_MOVE = 'cropper-move',
  37. CLASS_CROP = 'cropper-crop',
  38. CLASS_DISABLED = 'cropper-disabled',
  39. CLASS_BG = 'cropper-bg',
  40. // Events
  41. EVENT_MOUSE_DOWN = 'mousedown touchstart',
  42. EVENT_MOUSE_MOVE = 'mousemove touchmove',
  43. EVENT_MOUSE_UP = 'mouseup mouseleave touchend touchleave touchcancel',
  44. EVENT_WHEEL = 'wheel mousewheel DOMMouseScroll',
  45. EVENT_DBLCLICK = 'dblclick',
  46. EVENT_RESIZE = 'resize' + CROPPER_NAMESPACE, // Bind to window with namespace
  47. EVENT_BUILD = 'build' + CROPPER_NAMESPACE,
  48. EVENT_BUILT = 'built' + CROPPER_NAMESPACE,
  49. EVENT_DRAG_START = 'dragstart' + CROPPER_NAMESPACE,
  50. EVENT_DRAG_MOVE = 'dragmove' + CROPPER_NAMESPACE,
  51. EVENT_DRAG_END = 'dragend' + CROPPER_NAMESPACE,
  52. EVENT_ZOOM_IN = 'zoomin' + CROPPER_NAMESPACE,
  53. EVENT_ZOOM_OUT = 'zoomout' + CROPPER_NAMESPACE,
  54. // Supports
  55. SUPPORT_CANVAS = $.isFunction($('<canvas>')[0].getContext),
  56. // Others
  57. sqrt = Math.sqrt,
  58. min = Math.min,
  59. max = Math.max,
  60. abs = Math.abs,
  61. sin = Math.sin,
  62. cos = Math.cos,
  63. num = parseFloat,
  64. // Prototype
  65. prototype = {};
  66. function isNumber(n) {
  67. return typeof n === 'number';
  68. }
  69. function isUndefined(n) {
  70. return typeof n === 'undefined';
  71. }
  72. function toArray(obj, offset) {
  73. var args = [];
  74. if (isNumber(offset)) { // It's necessary for IE8
  75. args.push(offset);
  76. }
  77. return args.slice.apply(obj, args);
  78. }
  79. // Custom proxy to avoid jQuery's guid
  80. function proxy(fn, context) {
  81. var args = toArray(arguments, 2);
  82. return function () {
  83. return fn.apply(context, args.concat(toArray(arguments)));
  84. };
  85. }
  86. function isCrossOriginURL(url) {
  87. var parts = url.match(/^(https?:)\/\/([^\:\/\?#]+):?(\d*)/i);
  88. return parts && (parts[1] !== location.protocol || parts[2] !== location.hostname || parts[3] !== location.port);
  89. }
  90. function addTimestamp(url) {
  91. var timestamp = 'timestamp=' + (new Date()).getTime();
  92. return (url + (url.indexOf('?') === -1 ? '?' : '&') + timestamp);
  93. }
  94. function inRange(source, target) {
  95. return target.left < 0 && source.width < (target.left + target.width) && target.top < 0 && source.height < (target.top + target.height);
  96. }
  97. function getRotateValue(degree) {
  98. return degree ? 'rotate(' + degree + 'deg)' : 'none';
  99. }
  100. function getRotatedSizes(data, reverse) {
  101. var deg = abs(data.degree) % 180,
  102. arc = (deg > 90 ? (180 - deg) : deg) * Math.PI / 180,
  103. sinArc = sin(arc),
  104. cosArc = cos(arc),
  105. width = data.width,
  106. height = data.height,
  107. aspectRatio = data.aspectRatio,
  108. newWidth,
  109. newHeight;
  110. if (!reverse) {
  111. newWidth = width * cosArc + height * sinArc;
  112. newHeight = width * sinArc + height * cosArc;
  113. } else {
  114. newWidth = width / (cosArc + sinArc / aspectRatio);
  115. newHeight = newWidth / aspectRatio;
  116. }
  117. return {
  118. width: newWidth,
  119. height: newHeight
  120. };
  121. }
  122. function getSourceCanvas(image, data) {
  123. var canvas = $('<canvas>')[0],
  124. context = canvas.getContext('2d');
  125. context.translate(0.5, 0.5);
  126. var width = data.naturalWidth,
  127. height = data.naturalHeight,
  128. rotate = data.rotate,
  129. rotated = getRotatedSizes({
  130. width: width,
  131. height: height,
  132. degree: rotate
  133. });
  134. if (rotate) {
  135. canvas.width = rotated.width;
  136. canvas.height = rotated.height;
  137. context.save();
  138. // context.translate(0.5, 0.5);
  139. context.translate(rotated.width / 2, rotated.height / 2);
  140. // context.translate(rotated.width / 2, rotated.height / 2);
  141. context.rotate(rotate * Math.PI / 180);
  142. context.drawImage(image, -width / 2, -height / 2, width, height);
  143. context.restore();
  144. } else {
  145. canvas.width = width;
  146. canvas.height = height;
  147. context.drawImage(image, 0, 0, width, height);
  148. }
  149. return canvas;
  150. }
  151. function Cropper(element, options) {
  152. this.$element = $(element);
  153. this.options = $.extend({}, Cropper.DEFAULTS, $.isPlainObject(options) && options);
  154. this.ready = false;
  155. this.built = false;
  156. this.rotated = false;
  157. this.cropped = false;
  158. this.disabled = false;
  159. this.canvas = null;
  160. this.cropBox = null;
  161. this.load();
  162. }
  163. prototype.load = function (url) {
  164. var options = this.options,
  165. $this = this.$element,
  166. crossOrigin,
  167. bustCacheUrl,
  168. buildEvent,
  169. $clone;
  170. if (!url) {
  171. if ($this.is('img')) {
  172. if (!$this.attr('src')) {
  173. return;
  174. }
  175. url = $this.prop('src');
  176. } else if ($this.is('canvas') && SUPPORT_CANVAS) {
  177. url = $this[0].toDataURL();
  178. }
  179. }
  180. if (!url) {
  181. return;
  182. }
  183. buildEvent = $.Event(EVENT_BUILD);
  184. $this.one(EVENT_BUILD, options.build).trigger(buildEvent); // Only trigger once
  185. if (buildEvent.isDefaultPrevented()) {
  186. return;
  187. }
  188. if (options.checkImageOrigin && isCrossOriginURL(url)) {
  189. crossOrigin = 'anonymous';
  190. if (!$this.prop('crossOrigin')) { // Only when there was not a "crossOrigin" property
  191. bustCacheUrl = addTimestamp(url); // Bust cache (#148)
  192. }
  193. }
  194. this.$clone = $clone = $('<img>');
  195. $clone.one('load', $.proxy(function () {
  196. var naturalWidth = $clone.prop('naturalWidth') || $clone.width(),
  197. naturalHeight = $clone.prop('naturalHeight') || $clone.height();
  198. this.image = {
  199. naturalWidth: naturalWidth,
  200. naturalHeight: naturalHeight,
  201. aspectRatio: naturalWidth / naturalHeight,
  202. rotate: 0
  203. };
  204. this.url = url;
  205. this.ready = true;
  206. this.build();
  207. }, this)).one('error', function () {
  208. $clone.remove();
  209. }).attr({
  210. src: bustCacheUrl || url,
  211. crossOrigin: crossOrigin
  212. });
  213. // Hide and insert into the document
  214. $clone.addClass(CLASS_HIDE).insertAfter($this);
  215. };
  216. prototype.build = function () {
  217. var $this = this.$element,
  218. $clone = this.$clone,
  219. options = this.options,
  220. $cropper,
  221. $cropBox;
  222. if (!this.ready) {
  223. return;
  224. }
  225. if (this.built) {
  226. this.unbuild();
  227. }
  228. // Create cropper elements
  229. this.$cropper = $cropper = $(Cropper.TEMPLATE);
  230. // Hide the original image
  231. $this.addClass(CLASS_HIDDEN);
  232. // Show the clone iamge
  233. $clone.removeClass(CLASS_HIDE);
  234. this.$container = $this.parent().append($cropper);
  235. this.$canvas = $cropper.find('.cropper-canvas').append($clone);
  236. this.$dragBox = $cropper.find('.cropper-drag-box');
  237. this.$cropBox = $cropBox = $cropper.find('.cropper-crop-box');
  238. this.$viewBox = $cropper.find('.cropper-view-box');
  239. this.addListeners();
  240. this.initPreview();
  241. // Format aspect ratio
  242. options.aspectRatio = num(options.aspectRatio) || NaN; // 0 -> NaN
  243. if (options.autoCrop) {
  244. this.cropped = true;
  245. if (options.modal) {
  246. this.$dragBox.addClass(CLASS_MODAL);
  247. }
  248. } else {
  249. $cropBox.addClass(CLASS_HIDDEN);
  250. }
  251. if (options.background) {
  252. $cropper.addClass(CLASS_BG);
  253. }
  254. if (!options.highlight) {
  255. $cropBox.find('.cropper-face').addClass(CLASS_INVISIBLE);
  256. }
  257. if (!options.guides) {
  258. $cropBox.find('.cropper-dashed').addClass(CLASS_HIDDEN);
  259. }
  260. if (!options.movable) {
  261. $cropBox.find('.cropper-face').data('drag', 'move');
  262. }
  263. if (!options.resizable) {
  264. $cropBox.find('.cropper-line, .cropper-point').addClass(CLASS_HIDDEN);
  265. }
  266. this.setDragMode(options.dragCrop ? 'crop' : 'move');
  267. this.built = true;
  268. this.render();
  269. $this.one(EVENT_BUILT, options.built).trigger(EVENT_BUILT); // Only trigger once
  270. };
  271. prototype.unbuild = function () {
  272. if (!this.built) {
  273. return;
  274. }
  275. this.built = false;
  276. this.container = null;
  277. this.canvas = null;
  278. this.cropBox = null; // This is necessary when replace
  279. this.removeListeners();
  280. this.resetPreview();
  281. this.$preview = null;
  282. this.$viewBox = null;
  283. this.$cropBox = null;
  284. this.$dragBox = null;
  285. this.$canvas = null;
  286. this.$container = null;
  287. this.$cropper.remove();
  288. this.$cropper = null;
  289. };
  290. $.extend(prototype, {
  291. render: function () {
  292. this.initContainer();
  293. this.initCanvas();
  294. this.initCropBox();
  295. this.renderCanvas();
  296. if (this.cropped) {
  297. this.renderCropBox();
  298. }
  299. },
  300. initContainer: function () {
  301. var $this = this.$element,
  302. $container = this.$container,
  303. $cropper = this.$cropper,
  304. options = this.options;
  305. $cropper.addClass(CLASS_HIDDEN);
  306. $this.removeClass(CLASS_HIDDEN);
  307. $cropper.css((this.container = {
  308. width: max($container.width(), num(options.minContainerWidth) || 200),
  309. height: max($container.height(), num(options.minContainerHeight) || 100)
  310. }));
  311. $this.addClass(CLASS_HIDDEN);
  312. $cropper.removeClass(CLASS_HIDDEN);
  313. },
  314. // image box (wrapper)
  315. initCanvas: function () {
  316. var container = this.container,
  317. containerWidth = container.width,
  318. containerHeight = container.height,
  319. image = this.image,
  320. aspectRatio = image.aspectRatio,
  321. canvas = {
  322. aspectRatio: aspectRatio,
  323. width: containerWidth,
  324. height: containerHeight
  325. };
  326. if (containerHeight * aspectRatio > containerWidth) {
  327. canvas.height = containerWidth / aspectRatio;
  328. } else {
  329. canvas.width = containerHeight * aspectRatio;
  330. }
  331. canvas.oldLeft = canvas.left = (containerWidth - canvas.width) / 2;
  332. canvas.oldTop = canvas.top = (containerHeight - canvas.height) / 2;
  333. this.canvas = canvas;
  334. this.limitCanvas(true, true);
  335. this.initialImage = $.extend({}, image);
  336. this.initialCanvas = $.extend({}, canvas);
  337. },
  338. limitCanvas: function (size, position) {
  339. var options = this.options,
  340. strict = options.strict,
  341. container = this.container,
  342. containerWidth = container.width,
  343. containerHeight = container.height,
  344. canvas = this.canvas,
  345. aspectRatio = canvas.aspectRatio,
  346. cropBox = this.cropBox,
  347. cropped = this.cropped && cropBox,
  348. minCanvasWidth,
  349. minCanvasHeight;
  350. if (size) {
  351. minCanvasWidth = num(options.minCanvasWidth) || 0;
  352. minCanvasHeight = num(options.minCanvasHeight) || 0;
  353. if (minCanvasWidth) {
  354. if (strict) {
  355. minCanvasWidth = max(cropped ? cropBox.width : containerWidth, minCanvasWidth);
  356. }
  357. minCanvasHeight = minCanvasWidth / aspectRatio;
  358. } else if (minCanvasHeight) {
  359. if (strict) {
  360. minCanvasHeight = max(cropped ? cropBox.height : containerHeight, minCanvasHeight);
  361. }
  362. minCanvasWidth = minCanvasHeight * aspectRatio;
  363. } else if (strict) {
  364. if (cropped) {
  365. minCanvasWidth = cropBox.width;
  366. minCanvasHeight = cropBox.height;
  367. if (minCanvasHeight * aspectRatio > minCanvasWidth) {
  368. minCanvasWidth = minCanvasHeight * aspectRatio;
  369. } else {
  370. minCanvasHeight = minCanvasWidth / aspectRatio;
  371. }
  372. } else {
  373. minCanvasWidth = containerWidth;
  374. minCanvasHeight = containerHeight;
  375. if (minCanvasHeight * aspectRatio > minCanvasWidth) {
  376. minCanvasHeight = minCanvasWidth / aspectRatio;
  377. } else {
  378. minCanvasWidth = minCanvasHeight * aspectRatio;
  379. }
  380. }
  381. }
  382. $.extend(canvas, {
  383. minWidth: minCanvasWidth,
  384. minHeight: minCanvasHeight,
  385. maxWidth: Infinity,
  386. maxHeight: Infinity
  387. });
  388. }
  389. if (position) {
  390. if (strict) {
  391. if (cropped) {
  392. canvas.minLeft = min(cropBox.left, (cropBox.left + cropBox.width) - canvas.width);
  393. canvas.minTop = min(cropBox.top, (cropBox.top + cropBox.height) - canvas.height);
  394. canvas.maxLeft = cropBox.left;
  395. canvas.maxTop = cropBox.top;
  396. } else {
  397. canvas.minLeft = min(0, containerWidth - canvas.width);
  398. canvas.minTop = min(0, containerHeight - canvas.height);
  399. canvas.maxLeft = max(0, containerWidth - canvas.width);
  400. canvas.maxTop = max(0, containerHeight - canvas.height);
  401. }
  402. } else {
  403. canvas.minLeft = -canvas.width;
  404. canvas.minTop = -canvas.height;
  405. canvas.maxLeft = containerWidth;
  406. canvas.maxTop = containerHeight;
  407. }
  408. }
  409. },
  410. renderCanvas: function (changed) {
  411. var options = this.options,
  412. canvas = this.canvas,
  413. image = this.image,
  414. aspectRatio,
  415. rotated;
  416. if (this.rotated) {
  417. this.rotated = false;
  418. // Computes rotatation sizes with image sizes
  419. rotated = getRotatedSizes({
  420. width: image.width,
  421. height: image.height,
  422. degree: image.rotate
  423. });
  424. aspectRatio = rotated.width / rotated.height;
  425. if (aspectRatio !== canvas.aspectRatio) {
  426. canvas.left -= (rotated.width - canvas.width) / 2;
  427. canvas.top -= (rotated.height - canvas.height) / 2;
  428. canvas.width = rotated.width;
  429. canvas.height = rotated.height;
  430. canvas.aspectRatio = aspectRatio;
  431. this.limitCanvas(true, false);
  432. }
  433. }
  434. if (canvas.width > canvas.maxWidth || canvas.width < canvas.minWidth) {
  435. canvas.left = canvas.oldLeft;
  436. }
  437. if (canvas.height > canvas.maxHeight || canvas.height < canvas.minHeight) {
  438. canvas.top = canvas.oldTop;
  439. }
  440. canvas.width = min(max(canvas.width, canvas.minWidth), canvas.maxWidth);
  441. canvas.height = min(max(canvas.height, canvas.minHeight), canvas.maxHeight);
  442. this.limitCanvas(false, true);
  443. canvas.oldLeft = canvas.left = min(max(canvas.left, canvas.minLeft), canvas.maxLeft);
  444. canvas.oldTop = canvas.top = min(max(canvas.top, canvas.minTop), canvas.maxTop);
  445. this.$canvas.css({
  446. width: canvas.width,
  447. height: canvas.height,
  448. left: canvas.left,
  449. top: canvas.top
  450. });
  451. this.renderImage();
  452. if (this.cropped && options.strict && !inRange(this.container, canvas)) {
  453. this.limitCropBox(true, true);
  454. }
  455. if (changed) {
  456. this.output();
  457. }
  458. },
  459. renderImage: function () {
  460. var canvas = this.canvas,
  461. image = this.image,
  462. reversed;
  463. if (image.rotate) {
  464. reversed = getRotatedSizes({
  465. width: canvas.width,
  466. height: canvas.height,
  467. degree: image.rotate,
  468. aspectRatio: image.aspectRatio
  469. }, true);
  470. }
  471. $.extend(image, reversed ? {
  472. width: reversed.width,
  473. height: reversed.height,
  474. left: (canvas.width - reversed.width) / 2,
  475. top: (canvas.height - reversed.height) / 2
  476. } : {
  477. width: canvas.width,
  478. height: canvas.height,
  479. left: 0,
  480. top: 0
  481. });
  482. this.$clone.css({
  483. width: image.width,
  484. height: image.height,
  485. marginLeft: image.left,
  486. marginTop: image.top,
  487. transform: getRotateValue(image.rotate)
  488. });
  489. },
  490. initCropBox: function () {
  491. var options = this.options,
  492. canvas = this.canvas,
  493. aspectRatio = options.aspectRatio,
  494. autoCropArea = num(options.autoCropArea) || 0.8,
  495. cropBox = {
  496. width: canvas.width,
  497. height: canvas.height
  498. };
  499. if (aspectRatio) {
  500. if (canvas.height * aspectRatio > canvas.width) {
  501. cropBox.height = cropBox.width / aspectRatio;
  502. } else {
  503. cropBox.width = cropBox.height * aspectRatio;
  504. }
  505. }
  506. this.cropBox = cropBox;
  507. this.limitCropBox(true, true);
  508. // Initialize auto crop area
  509. cropBox.width = min(max(cropBox.width, cropBox.minWidth), cropBox.maxWidth);
  510. cropBox.height = min(max(cropBox.height, cropBox.minHeight), cropBox.maxHeight);
  511. // The width of auto crop area must large than "minWidth", and the height too. (#164)
  512. cropBox.width = max(cropBox.minWidth, cropBox.width * autoCropArea);
  513. cropBox.height = max(cropBox.minHeight, cropBox.height * autoCropArea);
  514. cropBox.oldLeft = cropBox.left = canvas.left + (canvas.width - cropBox.width) / 2;
  515. cropBox.oldTop = cropBox.top = canvas.top + (canvas.height - cropBox.height) / 2;
  516. this.initialCropBox = $.extend({}, cropBox);
  517. },
  518. limitCropBox: function (size, position) {
  519. var options = this.options,
  520. strict = options.strict,
  521. container = this.container,
  522. containerWidth = container.width,
  523. containerHeight = container.height,
  524. canvas = this.canvas,
  525. cropBox = this.cropBox,
  526. aspectRatio = options.aspectRatio,
  527. minCropBoxWidth,
  528. minCropBoxHeight;
  529. if (size) {
  530. minCropBoxWidth = num(options.minCropBoxWidth) || 0;
  531. minCropBoxHeight = num(options.minCropBoxHeight) || 0;
  532. // min/maxCropBoxWidth/Height must less than conatiner width/height
  533. cropBox.minWidth = min(containerWidth, minCropBoxWidth);
  534. cropBox.minHeight = min(containerHeight, minCropBoxHeight);
  535. cropBox.maxWidth = min(containerWidth, strict ? canvas.width : containerWidth);
  536. cropBox.maxHeight = min(containerHeight, strict ? canvas.height : containerHeight);
  537. if (aspectRatio) {
  538. // compare crop box size with container first
  539. if (cropBox.maxHeight * aspectRatio > cropBox.maxWidth) {
  540. cropBox.minHeight = cropBox.minWidth / aspectRatio;
  541. cropBox.maxHeight = cropBox.maxWidth / aspectRatio;
  542. } else {
  543. cropBox.minWidth = cropBox.minHeight * aspectRatio;
  544. cropBox.maxWidth = cropBox.maxHeight * aspectRatio;
  545. }
  546. }
  547. // The "minWidth" must be less than "maxWidth", and the "minHeight" too.
  548. cropBox.minWidth = min(cropBox.maxWidth, cropBox.minWidth);
  549. cropBox.minHeight = min(cropBox.maxHeight, cropBox.minHeight);
  550. }
  551. if (position) {
  552. if (strict) {
  553. cropBox.minLeft = max(0, canvas.left);
  554. cropBox.minTop = max(0, canvas.top);
  555. cropBox.maxLeft = min(containerWidth, canvas.left + canvas.width) - cropBox.width;
  556. cropBox.maxTop = min(containerHeight, canvas.top + canvas.height) - cropBox.height;
  557. } else {
  558. cropBox.minLeft = 0;
  559. cropBox.minTop = 0;
  560. cropBox.maxLeft = containerWidth - cropBox.width;
  561. cropBox.maxTop = containerHeight - cropBox.height;
  562. }
  563. }
  564. },
  565. renderCropBox: function () {
  566. var options = this.options,
  567. container = this.container,
  568. containerWidth = container.width,
  569. containerHeight = container.height,
  570. $cropBox = this.$cropBox,
  571. cropBox = this.cropBox;
  572. if (cropBox.width > cropBox.maxWidth || cropBox.width < cropBox.minWidth) {
  573. cropBox.left = cropBox.oldLeft;
  574. }
  575. if (cropBox.height > cropBox.maxHeight || cropBox.height < cropBox.minHeight) {
  576. cropBox.top = cropBox.oldTop;
  577. }
  578. cropBox.width = min(max(cropBox.width, cropBox.minWidth), cropBox.maxWidth);
  579. cropBox.height = min(max(cropBox.height, cropBox.minHeight), cropBox.maxHeight);
  580. this.limitCropBox(false, true);
  581. cropBox.oldLeft = cropBox.left = min(max(cropBox.left, cropBox.minLeft), cropBox.maxLeft);
  582. cropBox.oldTop = cropBox.top = min(max(cropBox.top, cropBox.minTop), cropBox.maxTop);
  583. if (options.movable) {
  584. $cropBox.find('.cropper-face').data('drag', (cropBox.width === containerWidth && cropBox.height === containerHeight) ? 'move' : 'all');
  585. }
  586. $cropBox.css({
  587. width: cropBox.width,
  588. height: cropBox.height,
  589. left: cropBox.left,
  590. top: cropBox.top
  591. });
  592. if (this.cropped && options.strict && !inRange(container, this.canvas)) {
  593. this.limitCanvas(true, true);
  594. }
  595. if (!this.disabled) {
  596. this.output();
  597. }
  598. },
  599. output: function () {
  600. var options = this.options;
  601. this.preview();
  602. if (options.crop) {
  603. options.crop.call(this.$element, this.getData());
  604. }
  605. }
  606. });
  607. prototype.initPreview = function () {
  608. var url = this.url;
  609. this.$preview = $(this.options.preview);
  610. this.$viewBox.html('<img src="' + url + '">');
  611. // Override img element styles
  612. // Add `display:block` to avoid margin top issue (Occur only when margin-top <= -height)
  613. this.$preview.each(function () {
  614. var $this = $(this);
  615. $this.data(CROPPER_PREVIEW, {
  616. width: $this.width(),
  617. height: $this.height(),
  618. original: $this.html()
  619. }).html('<img src="' + url + '" style="display:block;width:100%;min-width:0!important;min-height:0!important;max-width:none!important;max-height:none!important;image-orientation: 0deg!important">');
  620. });
  621. };
  622. prototype.resetPreview = function () {
  623. this.$preview.each(function () {
  624. var $this = $(this);
  625. $this.html($this.data(CROPPER_PREVIEW).original).removeData(CROPPER_PREVIEW);
  626. });
  627. };
  628. prototype.preview = function () {
  629. var image = this.image,
  630. canvas = this.canvas,
  631. cropBox = this.cropBox,
  632. width = image.width,
  633. height = image.height,
  634. left = cropBox.left - canvas.left - image.left,
  635. top = cropBox.top - canvas.top - image.top,
  636. rotate = image.rotate;
  637. if (!this.cropped || this.disabled) {
  638. return;
  639. }
  640. this.$viewBox.find('img').css({
  641. width: width,
  642. height: height,
  643. marginLeft: -left,
  644. marginTop: -top,
  645. transform: getRotateValue(rotate)
  646. });
  647. this.$preview.each(function () {
  648. var $this = $(this),
  649. data = $this.data(CROPPER_PREVIEW),
  650. ratio = data.width / cropBox.width,
  651. newWidth = data.width,
  652. newHeight = cropBox.height * ratio;
  653. if (newHeight > data.height) {
  654. ratio = data.height / cropBox.height;
  655. newWidth = cropBox.width * ratio;
  656. newHeight = data.height;
  657. }
  658. $this.width(newWidth).height(newHeight).find('img').css({
  659. width: width * ratio,
  660. height: height * ratio,
  661. marginLeft: -left * ratio,
  662. marginTop: -top * ratio,
  663. transform: getRotateValue(rotate)
  664. });
  665. });
  666. };
  667. prototype.addListeners = function () {
  668. var options = this.options;
  669. this.$element.on(EVENT_DRAG_START, options.dragstart).on(EVENT_DRAG_MOVE, options.dragmove).on(EVENT_DRAG_END, options.dragend).on(EVENT_ZOOM_IN, options.zoomin).on(EVENT_ZOOM_OUT, options.zoomout);
  670. this.$cropper.on(EVENT_MOUSE_DOWN, $.proxy(this.dragstart, this)).on(EVENT_DBLCLICK, $.proxy(this.dblclick, this));
  671. if (options.zoomable && options.mouseWheelZoom) {
  672. this.$cropper.on(EVENT_WHEEL, $.proxy(this.wheel, this));
  673. }
  674. $document.on(EVENT_MOUSE_MOVE, (this._dragmove = proxy(this.dragmove, this))).on(EVENT_MOUSE_UP, (this._dragend = proxy(this.dragend, this)));
  675. if (options.responsive) {
  676. $window.on(EVENT_RESIZE, (this._resize = proxy(this.resize, this)));
  677. }
  678. };
  679. prototype.removeListeners = function () {
  680. var options = this.options;
  681. this.$element.off(EVENT_DRAG_START, options.dragstart).off(EVENT_DRAG_MOVE, options.dragmove).off(EVENT_DRAG_END, options.dragend).off(EVENT_ZOOM_IN, options.zoomin).off(EVENT_ZOOM_OUT, options.zoomout);
  682. this.$cropper.off(EVENT_MOUSE_DOWN, this.dragstart).off(EVENT_DBLCLICK, this.dblclick);
  683. if (options.zoomable && options.mouseWheelZoom) {
  684. this.$cropper.off(EVENT_WHEEL, this.wheel);
  685. }
  686. $document.off(EVENT_MOUSE_MOVE, this._dragmove).off(EVENT_MOUSE_UP, this._dragend);
  687. if (options.responsive) {
  688. $window.off(EVENT_RESIZE, this._resize);
  689. }
  690. };
  691. $.extend(prototype, {
  692. resize: function () {
  693. var $container = this.$container,
  694. container = this.container,
  695. canvasData,
  696. cropBoxData,
  697. ratio;
  698. if (this.disabled) {
  699. return;
  700. }
  701. ratio = $container.width() / container.width;
  702. if (ratio !== 1 || $container.height() !== container.height) {
  703. canvasData = this.getCanvasData();
  704. cropBoxData = this.getCropBoxData();
  705. this.render();
  706. this.setCanvasData($.each(canvasData, function (i, n) {
  707. canvasData[i] = n * ratio;
  708. }));
  709. this.setCropBoxData($.each(cropBoxData, function (i, n) {
  710. cropBoxData[i] = n * ratio;
  711. }));
  712. }
  713. },
  714. dblclick: function () {
  715. if (this.disabled) {
  716. return;
  717. }
  718. if (this.$dragBox.hasClass(CLASS_CROP)) {
  719. this.setDragMode('move');
  720. } else {
  721. this.setDragMode('crop');
  722. }
  723. },
  724. wheel: function (event) {
  725. var e = event.originalEvent,
  726. delta = 1;
  727. if (this.disabled) {
  728. return;
  729. }
  730. event.preventDefault();
  731. if (e.deltaY) {
  732. delta = e.deltaY > 0 ? 1 : -1;
  733. } else if (e.wheelDelta) {
  734. delta = -e.wheelDelta / 120;
  735. } else if (e.detail) {
  736. delta = e.detail > 0 ? 1 : -1;
  737. }
  738. this.zoom(-delta * 0.1);
  739. },
  740. dragstart: function (event) {
  741. var options = this.options,
  742. originalEvent = event.originalEvent,
  743. touches = originalEvent && originalEvent.touches,
  744. e = event,
  745. dragType,
  746. dragStartEvent,
  747. touchesLength;
  748. if (this.disabled) {
  749. return;
  750. }
  751. if (touches) {
  752. touchesLength = touches.length;
  753. if (touchesLength > 1) {
  754. if (options.zoomable && options.touchDragZoom && touchesLength === 2) {
  755. e = touches[1];
  756. this.startX2 = e.pageX;
  757. this.startY2 = e.pageY;
  758. dragType = 'zoom';
  759. } else {
  760. return;
  761. }
  762. }
  763. e = touches[0];
  764. }
  765. dragType = dragType || $(e.target).data('drag');
  766. if (REGEXP_DRAG_TYPES.test(dragType)) {
  767. event.preventDefault();
  768. dragStartEvent = $.Event(EVENT_DRAG_START, {
  769. originalEvent: originalEvent,
  770. dragType: dragType
  771. });
  772. this.$element.trigger(dragStartEvent);
  773. if (dragStartEvent.isDefaultPrevented()) {
  774. return;
  775. }
  776. this.dragType = dragType;
  777. this.cropping = false;
  778. this.startX = e.pageX;
  779. this.startY = e.pageY;
  780. if (dragType === 'crop') {
  781. this.cropping = true;
  782. this.$dragBox.addClass(CLASS_MODAL);
  783. }
  784. }
  785. },
  786. dragmove: function (event) {
  787. var options = this.options,
  788. originalEvent = event.originalEvent,
  789. touches = originalEvent && originalEvent.touches,
  790. e = event,
  791. dragType = this.dragType,
  792. dragMoveEvent,
  793. touchesLength;
  794. if (this.disabled) {
  795. return;
  796. }
  797. if (touches) {
  798. touchesLength = touches.length;
  799. if (touchesLength > 1) {
  800. if (options.zoomable && options.touchDragZoom && touchesLength === 2) {
  801. e = touches[1];
  802. this.endX2 = e.pageX;
  803. this.endY2 = e.pageY;
  804. } else {
  805. return;
  806. }
  807. }
  808. e = touches[0];
  809. }
  810. if (dragType) {
  811. event.preventDefault();
  812. dragMoveEvent = $.Event(EVENT_DRAG_MOVE, {
  813. originalEvent: originalEvent,
  814. dragType: dragType
  815. });
  816. this.$element.trigger(dragMoveEvent);
  817. if (dragMoveEvent.isDefaultPrevented()) {
  818. return;
  819. }
  820. this.endX = e.pageX;
  821. this.endY = e.pageY;
  822. this.change();
  823. }
  824. },
  825. dragend: function (event) {
  826. var dragType = this.dragType,
  827. dragEndEvent;
  828. if (this.disabled) {
  829. return;
  830. }
  831. if (dragType) {
  832. event.preventDefault();
  833. dragEndEvent = $.Event(EVENT_DRAG_END, {
  834. originalEvent: event.originalEvent,
  835. dragType: dragType
  836. });
  837. this.$element.trigger(dragEndEvent);
  838. if (dragEndEvent.isDefaultPrevented()) {
  839. return;
  840. }
  841. if (this.cropping) {
  842. this.cropping = false;
  843. this.$dragBox.toggleClass(CLASS_MODAL, this.cropped && this.options.modal);
  844. }
  845. this.dragType = '';
  846. }
  847. }
  848. });
  849. $.extend(prototype, {
  850. reset: function () {
  851. if (!this.built || this.disabled) {
  852. return;
  853. }
  854. this.image = $.extend({}, this.initialImage);
  855. this.canvas = $.extend({}, this.initialCanvas);
  856. this.renderCanvas();
  857. if (this.cropped) {
  858. this.cropBox = $.extend({}, this.initialCropBox);
  859. this.renderCropBox();
  860. }
  861. },
  862. clear: function () {
  863. if (!this.cropped || this.disabled) {
  864. return;
  865. }
  866. $.extend(this.cropBox, {
  867. left: 0,
  868. top: 0,
  869. width: 0,
  870. height: 0
  871. });
  872. this.cropped = false;
  873. this.renderCropBox();
  874. this.limitCanvas();
  875. this.renderCanvas(); // Render canvas after render crop box
  876. this.$dragBox.removeClass(CLASS_MODAL);
  877. this.$cropBox.addClass(CLASS_HIDDEN);
  878. },
  879. destroy: function () {
  880. var $this = this.$element;
  881. if (this.ready) {
  882. this.unbuild();
  883. $this.removeClass(CLASS_HIDDEN);
  884. } else {
  885. this.$clone.off('load').remove();
  886. }
  887. $this.removeData('cropper');
  888. },
  889. replace: function (url) {
  890. if (!this.disabled && url) {
  891. this.load(url);
  892. }
  893. },
  894. enable: function () {
  895. if (this.built) {
  896. this.disabled = false;
  897. this.$cropper.removeClass(CLASS_DISABLED);
  898. }
  899. },
  900. disable: function () {
  901. if (this.built) {
  902. this.disabled = true;
  903. this.$cropper.addClass(CLASS_DISABLED);
  904. }
  905. },
  906. move: function (offsetX, offsetY) {
  907. var canvas = this.canvas;
  908. if (this.built && !this.disabled && isNumber(offsetX) && isNumber(offsetY)) {
  909. canvas.left += offsetX;
  910. canvas.top += offsetY;
  911. this.renderCanvas(true);
  912. }
  913. },
  914. zoom: function (delta) {
  915. var canvas = this.canvas,
  916. zoomEvent,
  917. width,
  918. height;
  919. delta = num(delta);
  920. if (delta && this.built && !this.disabled && this.options.zoomable) {
  921. zoomEvent = delta > 0 ? $.Event(EVENT_ZOOM_IN) : $.Event(EVENT_ZOOM_OUT);
  922. this.$element.trigger(zoomEvent);
  923. if (zoomEvent.isDefaultPrevented()) {
  924. return;
  925. }
  926. delta = delta <= -1 ? 1 / (1 - delta) : delta <= 1 ? (1 + delta) : delta;
  927. width = canvas.width * delta;
  928. height = canvas.height * delta;
  929. canvas.left -= (width - canvas.width) / 2;
  930. canvas.top -= (height - canvas.height) / 2;
  931. canvas.width = width;
  932. canvas.height = height;
  933. this.renderCanvas(true);
  934. this.setDragMode('move');
  935. }
  936. },
  937. rotate: function (degree) {
  938. var image = this.image;
  939. degree = num(degree);
  940. if (degree && this.built && !this.disabled && this.options.rotatable) {
  941. image.rotate = (image.rotate + degree) % 360;
  942. this.rotated = true;
  943. this.renderCanvas(true);
  944. }
  945. },
  946. getData: function () {
  947. var cropBox = this.cropBox,
  948. canvas = this.canvas,
  949. image = this.image,
  950. ratio,
  951. data;
  952. if (this.built && this.cropped) {
  953. data = {
  954. x: cropBox.left - canvas.left,
  955. y: cropBox.top - canvas.top,
  956. width: cropBox.width,
  957. height: cropBox.height
  958. };
  959. ratio = image.width / image.naturalWidth;
  960. $.each(data, function (i, n) {
  961. n = n / ratio;
  962. data[i] = n;
  963. });
  964. } else {
  965. data = {
  966. x: 0,
  967. y: 0,
  968. width: 0,
  969. height: 0
  970. };
  971. }
  972. data.rotate = image.rotate;
  973. return data;
  974. },
  975. getContainerData: function () {
  976. return this.built ? this.container : {};
  977. },
  978. getImageData: function () {
  979. return this.ready ? this.image : {};
  980. },
  981. getCanvasData: function () {
  982. var canvas = this.canvas,
  983. data;
  984. if (this.built) {
  985. data = {
  986. left: canvas.left,
  987. top: canvas.top,
  988. width: canvas.width,
  989. height: canvas.height
  990. };
  991. }
  992. return data || {};
  993. },
  994. setCanvasData: function (data) {
  995. var canvas = this.canvas,
  996. aspectRatio = canvas.aspectRatio;
  997. if (this.built && !this.disabled && $.isPlainObject(data)) {
  998. if (isNumber(data.left)) {
  999. canvas.left = data.left;
  1000. }
  1001. if (isNumber(data.top)) {
  1002. canvas.top = data.top;
  1003. }
  1004. if (isNumber(data.width)) {
  1005. canvas.width = data.width;
  1006. canvas.height = data.width / aspectRatio;
  1007. } else if (isNumber(data.height)) {
  1008. canvas.height = data.height;
  1009. canvas.width = data.height * aspectRatio;
  1010. }
  1011. this.renderCanvas(true);
  1012. }
  1013. },
  1014. getCropBoxData: function () {
  1015. var cropBox = this.cropBox,
  1016. data;
  1017. if (this.built && this.cropped) {
  1018. data = {
  1019. left: cropBox.left,
  1020. top: cropBox.top,
  1021. width: cropBox.width,
  1022. height: cropBox.height
  1023. };
  1024. }
  1025. return data || {};
  1026. },
  1027. setCropBoxData: function (data) {
  1028. var cropBox = this.cropBox,
  1029. aspectRatio = this.options.aspectRatio;
  1030. if (this.built && this.cropped && !this.disabled && $.isPlainObject(data)) {
  1031. if (isNumber(data.left)) {
  1032. cropBox.left = data.left;
  1033. }
  1034. if (isNumber(data.top)) {
  1035. cropBox.top = data.top;
  1036. }
  1037. if (aspectRatio) {
  1038. if (isNumber(data.width)) {
  1039. cropBox.width = data.width;
  1040. cropBox.height = cropBox.width / aspectRatio;
  1041. } else if (isNumber(data.height)) {
  1042. cropBox.height = data.height;
  1043. cropBox.width = cropBox.height * aspectRatio;
  1044. }
  1045. } else {
  1046. if (isNumber(data.width)) {
  1047. cropBox.width = data.width;
  1048. }
  1049. if (isNumber(data.height)) {
  1050. cropBox.height = data.height;
  1051. }
  1052. }
  1053. this.renderCropBox();
  1054. }
  1055. },
  1056. getCroppedCanvas: function (options) {
  1057. var originalWidth,
  1058. originalHeight,
  1059. canvasWidth,
  1060. canvasHeight,
  1061. scaledWidth,
  1062. scaledHeight,
  1063. scaledRatio,
  1064. aspectRatio,
  1065. canvas,
  1066. context,
  1067. data;
  1068. if (!this.built || !this.cropped || !SUPPORT_CANVAS) {
  1069. return;
  1070. }
  1071. if (!$.isPlainObject(options)) {
  1072. options = {};
  1073. }
  1074. data = this.getData();
  1075. originalWidth = data.width;
  1076. originalHeight = data.height;
  1077. aspectRatio = originalWidth / originalHeight;
  1078. if ($.isPlainObject(options)) {
  1079. scaledWidth = options.width;
  1080. scaledHeight = options.height;
  1081. if (scaledWidth) {
  1082. scaledHeight = scaledWidth / aspectRatio;
  1083. scaledRatio = scaledWidth / originalWidth;
  1084. } else if (scaledHeight) {
  1085. scaledWidth = scaledHeight * aspectRatio;
  1086. scaledRatio = scaledHeight / originalHeight;
  1087. }
  1088. }
  1089. canvasWidth = scaledWidth || originalWidth;
  1090. canvasHeight = scaledHeight || originalHeight;
  1091. canvas = $('<canvas>')[0];
  1092. canvas.width = canvasWidth;
  1093. canvas.height = canvasHeight;
  1094. context = canvas.getContext('2d');
  1095. if (options.fillColor) {
  1096. context.fillStyle = options.fillColor;
  1097. context.fillRect(0, 0, canvasWidth, canvasHeight);
  1098. }
  1099. // https://developer.mozilla.org/en-US/docs/Web/API/CanvasRenderingContext2D.drawImage
  1100. context.drawImage.apply(context, (function () {
  1101. var source = getSourceCanvas(this.$clone[0], this.image),
  1102. sourceWidth = source.width,
  1103. sourceHeight = source.height,
  1104. args = [source],
  1105. srcX = data.x, // source canvas
  1106. srcY = data.y,
  1107. srcWidth,
  1108. srcHeight,
  1109. dstX, // destination canvas
  1110. dstY,
  1111. dstWidth,
  1112. dstHeight;
  1113. if (srcX <= -originalWidth || srcX > sourceWidth) {
  1114. srcX = srcWidth = dstX = dstWidth = 0;
  1115. } else if (srcX <= 0) {
  1116. dstX = -srcX;
  1117. srcX = 0;
  1118. srcWidth = dstWidth = min(sourceWidth, originalWidth + srcX);
  1119. } else if (srcX <= sourceWidth) {
  1120. dstX = 0;
  1121. srcWidth = dstWidth = min(originalWidth, sourceWidth - srcX);
  1122. }
  1123. if (srcWidth <= 0 || srcY <= -originalHeight || srcY > sourceHeight) {
  1124. srcY = srcHeight = dstY = dstHeight = 0;
  1125. } else if (srcY <= 0) {
  1126. dstY = -srcY;
  1127. srcY = 0;
  1128. srcHeight = dstHeight = min(sourceHeight, originalHeight + srcY);
  1129. } else if (srcY <= sourceHeight) {
  1130. dstY = 0;
  1131. srcHeight = dstHeight = min(originalHeight, sourceHeight - srcY);
  1132. }
  1133. args.push(srcX, srcY, srcWidth, srcHeight);
  1134. // Scale destination sizes
  1135. if (scaledRatio) {
  1136. dstX *= scaledRatio;
  1137. dstY *= scaledRatio;
  1138. dstWidth *= scaledRatio;
  1139. dstHeight *= scaledRatio;
  1140. }
  1141. // Avoid "IndexSizeError" in IE and Firefox
  1142. if (dstWidth > 0 && dstHeight > 0) {
  1143. args.push(dstX, dstY, dstWidth, dstHeight);
  1144. }
  1145. return args;
  1146. }).call(this));
  1147. return canvas;
  1148. },
  1149. setAspectRatio: function (aspectRatio) {
  1150. var options = this.options;
  1151. if (!this.disabled && !isUndefined(aspectRatio)) {
  1152. options.aspectRatio = num(aspectRatio) || NaN; // 0 -> NaN
  1153. if (this.built) {
  1154. this.initCropBox();
  1155. if (this.cropped) {
  1156. this.renderCropBox();
  1157. }
  1158. }
  1159. }
  1160. },
  1161. setDragMode: function (mode) {
  1162. var $dragBox = this.$dragBox,
  1163. cropable = false,
  1164. movable = false;
  1165. if (!this.ready || this.disabled) {
  1166. return;
  1167. }
  1168. switch (mode) {
  1169. case 'crop':
  1170. if (this.options.dragCrop) {
  1171. cropable = true;
  1172. $dragBox.data('drag', mode);
  1173. } else {
  1174. movable = true;
  1175. }
  1176. break;
  1177. case 'move':
  1178. movable = true;
  1179. $dragBox.data('drag', mode);
  1180. break;
  1181. default:
  1182. $dragBox.removeData('drag');
  1183. }
  1184. $dragBox.toggleClass(CLASS_CROP, cropable).toggleClass(CLASS_MOVE, movable);
  1185. }
  1186. });
  1187. prototype.change = function () {
  1188. var dragType = this.dragType,
  1189. options = this.options,
  1190. canvas = this.canvas,
  1191. container = this.container,
  1192. cropBox = this.cropBox,
  1193. width = cropBox.width,
  1194. height = cropBox.height,
  1195. left = cropBox.left,
  1196. top = cropBox.top,
  1197. right = left + width,
  1198. bottom = top + height,
  1199. minLeft = 0,
  1200. minTop = 0,
  1201. maxWidth = container.width,
  1202. maxHeight = container.height,
  1203. renderable = true,
  1204. aspectRatio = options.aspectRatio,
  1205. range = {
  1206. x: this.endX - this.startX,
  1207. y: this.endY - this.startY
  1208. },
  1209. offset;
  1210. if (options.strict) {
  1211. minLeft = cropBox.minLeft;
  1212. minTop = cropBox.minTop;
  1213. maxWidth = minLeft + min(container.width, canvas.width);
  1214. maxHeight = minTop + min(container.height, canvas.height);
  1215. }
  1216. if (aspectRatio) {
  1217. range.X = range.y * aspectRatio;
  1218. range.Y = range.x / aspectRatio;
  1219. }
  1220. switch (dragType) {
  1221. // Move cropBox
  1222. case 'all':
  1223. left += range.x;
  1224. top += range.y;
  1225. break;
  1226. // Resize cropBox
  1227. case 'e':
  1228. if (range.x >= 0 && (right >= maxWidth || aspectRatio && (top <= minTop || bottom >= maxHeight))) {
  1229. renderable = false;
  1230. break;
  1231. }
  1232. width += range.x;
  1233. if (aspectRatio) {
  1234. height = width / aspectRatio;
  1235. top -= range.Y / 2;
  1236. }
  1237. if (width < 0) {
  1238. dragType = 'w';
  1239. width = 0;
  1240. }
  1241. break;
  1242. case 'n':
  1243. if (range.y <= 0 && (top <= minTop || aspectRatio && (left <= minLeft || right >= maxWidth))) {
  1244. renderable = false;
  1245. break;
  1246. }
  1247. height -= range.y;
  1248. top += range.y;
  1249. if (aspectRatio) {
  1250. width = height * aspectRatio;
  1251. left += range.X / 2;
  1252. }
  1253. if (height < 0) {
  1254. dragType = 's';
  1255. height = 0;
  1256. }
  1257. break;
  1258. case 'w':
  1259. if (range.x <= 0 && (left <= minLeft || aspectRatio && (top <= minTop || bottom >= maxHeight))) {
  1260. renderable = false;
  1261. break;
  1262. }
  1263. width -= range.x;
  1264. left += range.x;
  1265. if (aspectRatio) {
  1266. height = width / aspectRatio;
  1267. top += range.Y / 2;
  1268. }
  1269. if (width < 0) {
  1270. dragType = 'e';
  1271. width = 0;
  1272. }
  1273. break;
  1274. case 's':
  1275. if (range.y >= 0 && (bottom >= maxHeight || aspectRatio && (left <= minLeft || right >= maxWidth))) {
  1276. renderable = false;
  1277. break;
  1278. }
  1279. height += range.y;
  1280. if (aspectRatio) {
  1281. width = height * aspectRatio;
  1282. left -= range.X / 2;
  1283. }
  1284. if (height < 0) {
  1285. dragType = 'n';
  1286. height = 0;
  1287. }
  1288. break;
  1289. case 'ne':
  1290. if (aspectRatio) {
  1291. if (range.y <= 0 && (top <= minTop || right >= maxWidth)) {
  1292. renderable = false;
  1293. break;
  1294. }
  1295. height -= range.y;
  1296. top += range.y;
  1297. width = height * aspectRatio;
  1298. } else {
  1299. if (range.x >= 0) {
  1300. if (right < maxWidth) {
  1301. width += range.x;
  1302. } else if (range.y <= 0 && top <= minTop) {
  1303. renderable = false;
  1304. }
  1305. } else {
  1306. width += range.x;
  1307. }
  1308. if (range.y <= 0) {
  1309. if (top > 0) {
  1310. height -= range.y;
  1311. top += range.y;
  1312. }
  1313. } else {
  1314. height -= range.y;
  1315. top += range.y;
  1316. }
  1317. }
  1318. if (width < 0 && height < 0) {
  1319. dragType = 'sw';
  1320. height = 0;
  1321. width = 0;
  1322. } else if (width < 0) {
  1323. dragType = 'nw';
  1324. width = 0;
  1325. } else if (height < 0) {
  1326. dragType = 'se';
  1327. height = 0;
  1328. }
  1329. break;
  1330. case 'nw':
  1331. if (aspectRatio) {
  1332. if (range.y <= 0 && (top <= minTop || left <= minLeft)) {
  1333. renderable = false;
  1334. break;
  1335. }
  1336. height -= range.y;
  1337. top += range.y;
  1338. width = height * aspectRatio;
  1339. left += range.X;
  1340. } else {
  1341. if (range.x <= 0) {
  1342. if (left > 0) {
  1343. width -= range.x;
  1344. left += range.x;
  1345. } else if (range.y <= 0 && top <= minTop) {
  1346. renderable = false;
  1347. }
  1348. } else {
  1349. width -= range.x;
  1350. left += range.x;
  1351. }
  1352. if (range.y <= 0) {
  1353. if (top > 0) {
  1354. height -= range.y;
  1355. top += range.y;
  1356. }
  1357. } else {
  1358. height -= range.y;
  1359. top += range.y;
  1360. }
  1361. }
  1362. if (width < 0 && height < 0) {
  1363. dragType = 'se';
  1364. height = 0;
  1365. width = 0;
  1366. } else if (width < 0) {
  1367. dragType = 'ne';
  1368. width = 0;
  1369. } else if (height < 0) {
  1370. dragType = 'sw';
  1371. height = 0;
  1372. }
  1373. break;
  1374. case 'sw':
  1375. if (aspectRatio) {
  1376. if (range.x <= 0 && (left <= minLeft || bottom >= maxHeight)) {
  1377. renderable = false;
  1378. break;
  1379. }
  1380. width -= range.x;
  1381. left += range.x;
  1382. height = width / aspectRatio;
  1383. } else {
  1384. if (range.x <= 0) {
  1385. if (left > 0) {
  1386. width -= range.x;
  1387. left += range.x;
  1388. } else if (range.y >= 0 && bottom >= maxHeight) {
  1389. renderable = false;
  1390. }
  1391. } else {
  1392. width -= range.x;
  1393. left += range.x;
  1394. }
  1395. if (range.y >= 0) {
  1396. if (bottom < maxHeight) {
  1397. height += range.y;
  1398. }
  1399. } else {
  1400. height += range.y;
  1401. }
  1402. }
  1403. if (width < 0 && height < 0) {
  1404. dragType = 'ne';
  1405. height = 0;
  1406. width = 0;
  1407. } else if (width < 0) {
  1408. dragType = 'se';
  1409. width = 0;
  1410. } else if (height < 0) {
  1411. dragType = 'nw';
  1412. height = 0;
  1413. }
  1414. break;
  1415. case 'se':
  1416. if (aspectRatio) {
  1417. if (range.x >= 0 && (right >= maxWidth || bottom >= maxHeight)) {
  1418. renderable = false;
  1419. break;
  1420. }
  1421. width += range.x;
  1422. height = width / aspectRatio;
  1423. } else {
  1424. if (range.x >= 0) {
  1425. if (right < maxWidth) {
  1426. width += range.x;
  1427. } else if (range.y >= 0 && bottom >= maxHeight) {
  1428. renderable = false;
  1429. }
  1430. } else {
  1431. width += range.x;
  1432. }
  1433. if (range.y >= 0) {
  1434. if (bottom < maxHeight) {
  1435. height += range.y;
  1436. }
  1437. } else {
  1438. height += range.y;
  1439. }
  1440. }
  1441. if (width < 0 && height < 0) {
  1442. dragType = 'nw';
  1443. height = 0;
  1444. width = 0;
  1445. } else if (width < 0) {
  1446. dragType = 'sw';
  1447. width = 0;
  1448. } else if (height < 0) {
  1449. dragType = 'ne';
  1450. height = 0;
  1451. }
  1452. break;
  1453. // Move image
  1454. case 'move':
  1455. canvas.left += range.x;
  1456. canvas.top += range.y;
  1457. this.renderCanvas(true);
  1458. renderable = false;
  1459. break;
  1460. // Scale image
  1461. case 'zoom':
  1462. this.zoom(function (x1, y1, x2, y2) {
  1463. var z1 = sqrt(x1 * x1 + y1 * y1),
  1464. z2 = sqrt(x2 * x2 + y2 * y2);
  1465. return (z2 - z1) / z1;
  1466. }(
  1467. abs(this.startX - this.startX2),
  1468. abs(this.startY - this.startY2),
  1469. abs(this.endX - this.endX2),
  1470. abs(this.endY - this.endY2)
  1471. ));
  1472. this.startX2 = this.endX2;
  1473. this.startY2 = this.endY2;
  1474. renderable = false;
  1475. break;
  1476. // Crop image
  1477. case 'crop':
  1478. if (range.x && range.y) {
  1479. offset = this.$cropper.offset();
  1480. left = this.startX - offset.left;
  1481. top = this.startY - offset.top;
  1482. width = cropBox.minWidth;
  1483. height = cropBox.minHeight;
  1484. if (range.x > 0) {
  1485. if (range.y > 0) {
  1486. dragType = 'se';
  1487. } else {
  1488. dragType = 'ne';
  1489. top -= height;
  1490. }
  1491. } else {
  1492. if (range.y > 0) {
  1493. dragType = 'sw';
  1494. left -= width;
  1495. } else {
  1496. dragType = 'nw';
  1497. left -= width;
  1498. top -= height;
  1499. }
  1500. }
  1501. // Show the cropBox if is hidden
  1502. if (!this.cropped) {
  1503. this.cropped = true;
  1504. this.$cropBox.removeClass(CLASS_HIDDEN);
  1505. }
  1506. }
  1507. break;
  1508. // No default
  1509. }
  1510. if (renderable) {
  1511. cropBox.width = width;
  1512. cropBox.height = height;
  1513. cropBox.left = left;
  1514. cropBox.top = top;
  1515. this.dragType = dragType;
  1516. this.renderCropBox();
  1517. }
  1518. // Override
  1519. this.startX = this.endX;
  1520. this.startY = this.endY;
  1521. };
  1522. $.extend(Cropper.prototype, prototype);
  1523. Cropper.DEFAULTS = {
  1524. // Defines the aspect ratio of the crop box
  1525. // Type: Number
  1526. aspectRatio: NaN,
  1527. // Defines the percentage of automatic cropping area when initializes
  1528. // Type: Number (Must large than 0 and less than 1)
  1529. autoCropArea: 0.8, // 80%
  1530. // Outputs the cropping results.
  1531. // Type: Function
  1532. crop: null,
  1533. // Add extra containers for previewing
  1534. // Type: String (jQuery selector)
  1535. preview: '',
  1536. // Toggles
  1537. strict: true, // strict mode, the image cannot zoom out less than the container
  1538. responsive: true, // Rebuild when resize the window
  1539. checkImageOrigin: true, // Check if the target image is cross origin
  1540. modal: true, // Show the black modal
  1541. guides: true, // Show the dashed lines for guiding
  1542. highlight: true, // Show the white modal to highlight the crop box
  1543. background: true, // Show the grid background
  1544. autoCrop: true, // Enable to crop the image automatically when initialize
  1545. dragCrop: true, // Enable to create new crop box by dragging over the image
  1546. movable: true, // Enable to move the crop box
  1547. resizable: true, // Enable to resize the crop box
  1548. rotatable: true, // Enable to rotate the image
  1549. zoomable: true, // Enable to zoom the image
  1550. touchDragZoom: true, // Enable to zoom the image by wheeling mouse
  1551. mouseWheelZoom: true, // Enable to zoom the image by dragging touch
  1552. // Dimensions
  1553. minCanvasWidth: 0,
  1554. minCanvasHeight: 0,
  1555. minCropBoxWidth: 0,
  1556. minCropBoxHeight: 0,
  1557. minContainerWidth: 200,
  1558. minContainerHeight: 100,
  1559. // Events
  1560. build: null, // Function
  1561. built: null, // Function
  1562. dragstart: null, // Function
  1563. dragmove: null, // Function
  1564. dragend: null, // Function
  1565. zoomin: null, // Function
  1566. zoomout: null // Function
  1567. };
  1568. Cropper.setDefaults = function (options) {
  1569. $.extend(Cropper.DEFAULTS, options);
  1570. };
  1571. // Use the string compressor: Strmin (https://github.com/fengyuanchen/strmin)
  1572. Cropper.TEMPLATE = (function (source, words) {
  1573. words = words.split(',');
  1574. return source.replace(/\d+/g, function (i) {
  1575. return words[i];
  1576. });
  1577. })('<0 6="5-container"><0 6="5-canvas"></0><0 6="5-2-9" 3-2="move"></0><0 6="5-crop-9"><1 6="5-view-9"></1><1 6="5-8 8-h"></1><1 6="5-8 8-v"></1><1 6="5-face" 3-2="all"></1><1 6="5-7 7-e" 3-2="e"></1><1 6="5-7 7-n" 3-2="n"></1><1 6="5-7 7-w" 3-2="w"></1><1 6="5-7 7-s" 3-2="s"></1><1 6="5-4 4-e" 3-2="e"></1><1 6="5-4 4-n" 3-2="n"></1><1 6="5-4 4-w" 3-2="w"></1><1 6="5-4 4-s" 3-2="s"></1><1 6="5-4 4-ne" 3-2="ne"></1><1 6="5-4 4-nw" 3-2="nw"></1><1 6="5-4 4-sw" 3-2="sw"></1><1 6="5-4 4-se" 3-2="se"></1></0></0>', 'div,span,drag,data,point,cropper,class,line,dashed,box');
  1578. /* Template source:
  1579. <div class="cropper-container">
  1580. <div class="cropper-canvas"></div>
  1581. <div class="cropper-drag-box" data-drag="move"></div>
  1582. <div class="cropper-crop-box">
  1583. <span class="cropper-view-box"></span>
  1584. <span class="cropper-dashed dashed-h"></span>
  1585. <span class="cropper-dashed dashed-v"></span>
  1586. <span class="cropper-face" data-drag="all"></span>
  1587. <span class="cropper-line line-e" data-drag="e"></span>
  1588. <span class="cropper-line line-n" data-drag="n"></span>
  1589. <span class="cropper-line line-w" data-drag="w"></span>
  1590. <span class="cropper-line line-s" data-drag="s"></span>
  1591. <span class="cropper-point point-e" data-drag="e"></span>
  1592. <span class="cropper-point point-n" data-drag="n"></span>
  1593. <span class="cropper-point point-w" data-drag="w"></span>
  1594. <span class="cropper-point point-s" data-drag="s"></span>
  1595. <span class="cropper-point point-ne" data-drag="ne"></span>
  1596. <span class="cropper-point point-nw" data-drag="nw"></span>
  1597. <span class="cropper-point point-sw" data-drag="sw"></span>
  1598. <span class="cropper-point point-se" data-drag="se"></span>
  1599. </div>
  1600. </div>
  1601. */
  1602. // Save the other cropper
  1603. Cropper.other = $.fn.cropper;
  1604. // Register as jQuery plugin
  1605. $.fn.cropper = function (options) {
  1606. var args = toArray(arguments, 1),
  1607. result;
  1608. this.each(function () {
  1609. var $this = $(this),
  1610. data = $this.data('cropper'),
  1611. fn;
  1612. if (!data) {
  1613. $this.data('cropper', (data = new Cropper(this, options)));
  1614. }
  1615. if (typeof options === 'string' && $.isFunction((fn = data[options]))) {
  1616. result = fn.apply(data, args);
  1617. }
  1618. });
  1619. return isUndefined(result) ? this : result;
  1620. };
  1621. $.fn.cropper.Constructor = Cropper;
  1622. $.fn.cropper.setDefaults = Cropper.setDefaults;
  1623. // No conflict
  1624. $.fn.cropper.noConflict = function () {
  1625. $.fn.cropper = Cropper.other;
  1626. return this;
  1627. };
  1628. });