cropper.js 63 KB

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