annotate src/uploadcare/croppr.js @ 3:b016e4b7c8da default tip

add read_me
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 12 Jul 2025 12:51:36 -0600
parents 8f4df159f06b
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 /**
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2 * Croppr.js
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 * https://github.com/jamesssooi/Croppr.js
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
4 *
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
5 * A JavaScript image cropper that's lightweight, awesome, and has
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 * zero dependencies.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 *
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 * (C) 2017 James Ooi. Released under the MIT License.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 */
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 (function (global, factory) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
12 typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 typeof define === 'function' && define.amd ? define(factory) :
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 (global.Croppr = factory());
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 }(this, (function () { 'use strict';
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
16
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 (function () {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 var lastTime = 0;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 var vendors = ['ms', 'moz', 'webkit', 'o'];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 for (var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 window.requestAnimationFrame = window[vendors[x] + 'RequestAnimationFrame'];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22 window.cancelAnimationFrame = window[vendors[x] + 'CancelAnimationFrame'] || window[vendors[x] + 'CancelRequestAnimationFrame'];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
24 if (!window.requestAnimationFrame) window.requestAnimationFrame = function (callback, element) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 var currTime = new Date().getTime();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 var timeToCall = Math.max(0, 16 - (currTime - lastTime));
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 var id = window.setTimeout(function () {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
28 callback(currTime + timeToCall);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29 }, timeToCall);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 lastTime = currTime + timeToCall;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 return id;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32 };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33 if (!window.cancelAnimationFrame) window.cancelAnimationFrame = function (id) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 clearTimeout(id);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35 };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36 })();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 (function () {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 if (typeof window.CustomEvent === "function") return false;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 function CustomEvent(event, params) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 params = params || { bubbles: false, cancelable: false, detail: undefined };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 var evt = document.createEvent('CustomEvent');
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 return evt;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 CustomEvent.prototype = window.Event.prototype;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 window.CustomEvent = CustomEvent;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 })();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48 (function (window) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
49 try {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 new CustomEvent('test');
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
51 return false;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 } catch (e) {}
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 function MouseEvent(eventType, params) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 params = params || { bubbles: false, cancelable: false };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 var mouseEvent = document.createEvent('MouseEvent');
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 mouseEvent.initMouseEvent(eventType, params.bubbles, params.cancelable, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
57 return mouseEvent;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
59 MouseEvent.prototype = Event.prototype;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 window.MouseEvent = MouseEvent;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 })(window);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 var classCallCheck = function (instance, Constructor) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 if (!(instance instanceof Constructor)) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 throw new TypeError("Cannot call a class as a function");
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
66 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
67 };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
68
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
69 var createClass = function () {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
70 function defineProperties(target, props) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
71 for (var i = 0; i < props.length; i++) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
72 var descriptor = props[i];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
73 descriptor.enumerable = descriptor.enumerable || false;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
74 descriptor.configurable = true;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 if ("value" in descriptor) descriptor.writable = true;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 Object.defineProperty(target, descriptor.key, descriptor);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 return function (Constructor, protoProps, staticProps) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
81 if (protoProps) defineProperties(Constructor.prototype, protoProps);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 if (staticProps) defineProperties(Constructor, staticProps);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 return Constructor;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 }();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
89
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 var get = function get(object, property, receiver) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
94 if (object === null) object = Function.prototype;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 var desc = Object.getOwnPropertyDescriptor(object, property);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
97 if (desc === undefined) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98 var parent = Object.getPrototypeOf(object);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
99
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
100 if (parent === null) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
101 return undefined;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
102 } else {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
103 return get(parent, property, receiver);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
104 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
105 } else if ("value" in desc) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 return desc.value;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 } else {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108 var getter = desc.get;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
109
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
110 if (getter === undefined) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
111 return undefined;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114 return getter.call(receiver);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
115 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116 };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
118 var inherits = function (subClass, superClass) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119 if (typeof superClass !== "function" && superClass !== null) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120 throw new TypeError("Super expression must either be null or a function, not " + typeof superClass);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
121 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
122
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
123 subClass.prototype = Object.create(superClass && superClass.prototype, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
124 constructor: {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125 value: subClass,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
126 enumerable: false,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
127 writable: true,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
128 configurable: true
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
129 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
130 });
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
131 if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
132 };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
133
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
134
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
135
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
136
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
137
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
138
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
139
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
140
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
141
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
142
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
143
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
144 var possibleConstructorReturn = function (self, call) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
145 if (!self) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
146 throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
147 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
148
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
149 return call && (typeof call === "object" || typeof call === "function") ? call : self;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
150 };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
151
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
152
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
153
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
154
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
155
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
156 var slicedToArray = function () {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
157 function sliceIterator(arr, i) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
158 var _arr = [];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
159 var _n = true;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
160 var _d = false;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
161 var _e = undefined;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
162
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
163 try {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
164 for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
165 _arr.push(_s.value);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
166
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
167 if (i && _arr.length === i) break;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
168 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
169 } catch (err) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
170 _d = true;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
171 _e = err;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
172 } finally {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
173 try {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
174 if (!_n && _i["return"]) _i["return"]();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
175 } finally {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
176 if (_d) throw _e;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
177 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
178 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
179
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
180 return _arr;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
181 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
182
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
183 return function (arr, i) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
184 if (Array.isArray(arr)) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
185 return arr;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
186 } else if (Symbol.iterator in Object(arr)) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
187 return sliceIterator(arr, i);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
188 } else {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
189 throw new TypeError("Invalid attempt to destructure non-iterable instance");
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
190 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
191 };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
192 }();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
193
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
194 var Handle =
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
195 /**
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
196 * Creates a new Handle instance.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
197 * @constructor
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
198 * @param {Array} position The x and y ratio position of the handle
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
199 * within the crop region. Accepts a value between 0 to 1 in the order
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
200 * of [X, Y].
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
201 * @param {Array} constraints Define the side of the crop region that
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
202 * is to be affected by this handle. Accepts a value of 0 or 1 in the
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
203 * order of [TOP, RIGHT, BOTTOM, LEFT].
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
204 * @param {String} cursor The CSS cursor of this handle.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
205 * @param {Element} eventBus The element to dispatch events to.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
206 */
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
207 function Handle(position, constraints, cursor, eventBus) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
208 classCallCheck(this, Handle);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
209 var self = this;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
210 this.position = position;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
211 this.constraints = constraints;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
212 this.cursor = cursor;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
213 this.eventBus = eventBus;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
214 this.el = document.createElement('div');
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
215 this.el.className = 'croppr-handle';
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
216 this.el.style.cursor = cursor;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
217 this.el.addEventListener('mousedown', onMouseDown);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
218 function onMouseDown(e) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
219 e.stopPropagation();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
220 document.addEventListener('mouseup', onMouseUp);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
221 document.addEventListener('mousemove', onMouseMove);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
222 self.eventBus.dispatchEvent(new CustomEvent('handlestart', {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
223 detail: { handle: self }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
224 }));
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
225 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
226 function onMouseUp(e) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
227 e.stopPropagation();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
228 document.removeEventListener('mouseup', onMouseUp);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
229 document.removeEventListener('mousemove', onMouseMove);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
230 self.eventBus.dispatchEvent(new CustomEvent('handleend', {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
231 detail: { handle: self }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
232 }));
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
233 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
234 function onMouseMove(e) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
235 e.stopPropagation();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
236 self.eventBus.dispatchEvent(new CustomEvent('handlemove', {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
237 detail: { mouseX: e.clientX, mouseY: e.clientY }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
238 }));
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
239 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
240 };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
241
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
242 var Box = function () {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
243 /**
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
244 * Creates a new Box instance.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
245 * @constructor
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
246 * @param {Number} x1
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
247 * @param {Number} y1
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
248 * @param {Number} x2
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
249 * @param {Number} y2
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
250 */
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
251 function Box(x1, y1, x2, y2) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
252 classCallCheck(this, Box);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
253 this.x1 = x1;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
254 this.y1 = y1;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
255 this.x2 = x2;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
256 this.y2 = y2;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
257 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
258 /**
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
259 * Sets the new dimensions of the box.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
260 * @param {Number} x1
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
261 * @param {Number} y1
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
262 * @param {Number} x2
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
263 * @param {Number} y2
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
264 */
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
265 createClass(Box, [{
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
266 key: 'set',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
267 value: function set$$1() {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
268 var x1 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
269 var y1 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
270 var x2 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
271 var y2 = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
272 this.x1 = x1 == null ? this.x1 : x1;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
273 this.y1 = y1 == null ? this.y1 : y1;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
274 this.x2 = x2 == null ? this.x2 : x2;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
275 this.y2 = y2 == null ? this.y2 : y2;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
276 return this;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
277 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
278 /**
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
279 * Calculates the width of the box.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
280 * @returns {Number}
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
281 */
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
282 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
283 key: 'width',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
284 value: function width() {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
285 return Math.abs(this.x2 - this.x1);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
286 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
287 /**
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
288 * Calculates the height of the box.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
289 * @returns {Number}
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
290 */
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
291 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
292 key: 'height',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
293 value: function height() {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
294 return Math.abs(this.y2 - this.y1);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
295 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
296 /**
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
297 * Resizes the box to a new size.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
298 * @param {Number} newWidth
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
299 * @param {Number} newHeight
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
300 * @param {Array} [origin] The origin point to resize from.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
301 * Defaults to [0, 0] (top left).
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
302 */
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
303 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
304 key: 'resize',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
305 value: function resize(newWidth, newHeight) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
306 var origin = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [0, 0];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
307 var fromX = this.x1 + this.width() * origin[0];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
308 var fromY = this.y1 + this.height() * origin[1];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
309 this.x1 = fromX - newWidth * origin[0];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
310 this.y1 = fromY - newHeight * origin[1];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
311 this.x2 = this.x1 + newWidth;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
312 this.y2 = this.y1 + newHeight;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
313 return this;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
314 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
315 /**
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
316 * Scale the box by a factor.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
317 * @param {Number} factor
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
318 * @param {Array} [origin] The origin point to resize from.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
319 * Defaults to [0, 0] (top left).
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
320 */
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
321 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
322 key: 'scale',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
323 value: function scale(factor) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
324 var origin = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [0, 0];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
325 var newWidth = this.width() * factor;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
326 var newHeight = this.height() * factor;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
327 this.resize(newWidth, newHeight, origin);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
328 return this;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
329 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
330 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
331 key: 'move',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
332 value: function move() {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
333 var x = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
334 var y = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
335 var width = this.width();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
336 var height = this.height();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
337 x = x === null ? this.x1 : x;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
338 y = y === null ? this.y1 : y;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
339 this.x1 = x;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
340 this.y1 = y;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
341 this.x2 = x + width;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
342 this.y2 = y + height;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
343 return this;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
344 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
345 /**
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
346 * Get relative x and y coordinates of a given point within the box.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
347 * @param {Array} point The x and y ratio position within the box.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
348 * @returns {Array} The x and y coordinates [x, y].
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
349 */
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
350 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
351 key: 'getRelativePoint',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
352 value: function getRelativePoint() {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
353 var point = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [0, 0];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
354 var x = this.width() * point[0];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
355 var y = this.height() * point[1];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
356 return [x, y];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
357 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
358 /**
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
359 * Get absolute x and y coordinates of a given point within the box.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
360 * @param {Array} point The x and y ratio position within the box.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
361 * @returns {Array} The x and y coordinates [x, y].
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
362 */
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
363 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
364 key: 'getAbsolutePoint',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
365 value: function getAbsolutePoint() {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
366 var point = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [0, 0];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
367 var x = this.x1 + this.width() * point[0];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
368 var y = this.y1 + this.height() * point[1];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
369 return [x, y];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
370 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
371 /**
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
372 * Constrain the box to a fixed ratio.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
373 * @param {Number} ratio
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
374 * @param {Array} [origin] The origin point to resize from.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
375 * Defaults to [0, 0] (top left).
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
376 * @param {String} [grow] The axis to grow to maintain the ratio.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
377 * Defaults to 'height'.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
378 */
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
379 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
380 key: 'constrainToRatio',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
381 value: function constrainToRatio(ratio) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
382 var origin = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [0, 0];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
383 var grow = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'height';
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
384 if (ratio === null) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
385 return;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
386 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
387 var width = this.width();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
388 var height = this.height();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
389 switch (grow) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
390 case 'height':
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
391 this.resize(this.width(), this.width() * ratio, origin);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
392 break;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
393 case 'width':
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
394 this.resize(this.height() * 1 / ratio, this.height(), origin);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
395 break;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
396 default:
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
397 this.resize(this.width(), this.width() * ratio, origin);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
398 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
399 return this;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
400 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
401 /**
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
402 * Constrain the box within a boundary.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
403 * @param {Number} boundaryWidth
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
404 * @param {Number} boundaryHeight
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
405 * @param {Array} [origin] The origin point to resize from.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
406 * Defaults to [0, 0] (top left).
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
407 */
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
408 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
409 key: 'constrainToBoundary',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
410 value: function constrainToBoundary(boundaryWidth, boundaryHeight) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
411 var origin = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [0, 0];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
412 var _getAbsolutePoint = this.getAbsolutePoint(origin),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
413 _getAbsolutePoint2 = slicedToArray(_getAbsolutePoint, 2),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
414 originX = _getAbsolutePoint2[0],
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
415 originY = _getAbsolutePoint2[1];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
416 var maxIfLeft = originX;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
417 var maxIfTop = originY;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
418 var maxIfRight = boundaryWidth - originX;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
419 var maxIfBottom = boundaryHeight - originY;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
420 var directionX = -2 * origin[0] + 1;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
421 var directionY = -2 * origin[1] + 1;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
422 var maxWidth = null,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
423 maxHeight = null;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
424 switch (directionX) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
425 case -1:
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
426 maxWidth = maxIfLeft;break;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
427 case 0:
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
428 maxWidth = Math.min(maxIfLeft, maxIfRight) * 2;break;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
429 case +1:
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
430 maxWidth = maxIfRight;break;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
431 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
432 switch (directionY) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
433 case -1:
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
434 maxHeight = maxIfTop;break;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
435 case 0:
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
436 maxHeight = Math.min(maxIfTop, maxIfBottom) * 2;break;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
437 case +1:
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
438 maxHeight = maxIfBottom;break;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
439 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
440 if (this.width() > maxWidth) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
441 var factor = maxWidth / this.width();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
442 this.scale(factor, origin);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
443 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
444 if (this.height() > maxHeight) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
445 var _factor = maxHeight / this.height();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
446 this.scale(_factor, origin);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
447 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
448 return this;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
449 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
450 /**
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
451 * Constrain the box to a maximum/minimum size.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
452 * @param {Number} [maxWidth]
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
453 * @param {Number} [maxHeight]
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
454 * @param {Number} [minWidth]
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
455 * @param {Number} [minHeight]
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
456 * @param {Array} [origin] The origin point to resize from.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
457 * Defaults to [0, 0] (top left).
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
458 * @param {Number} [ratio] Ratio to maintain.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
459 */
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
460 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
461 key: 'constrainToSize',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
462 value: function constrainToSize() {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
463 var maxWidth = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
464 var maxHeight = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
465 var minWidth = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : null;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
466 var minHeight = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
467 var origin = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : [0, 0];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
468 var ratio = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : null;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
469 if (ratio) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
470 if (ratio > 1) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
471 maxWidth = maxHeight * 1 / ratio;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
472 minHeight = minHeight * ratio;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
473 } else if (ratio < 1) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
474 maxHeight = maxWidth * ratio;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
475 minWidth = minHeight * 1 / ratio;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
476 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
477 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
478 if (maxWidth && this.width() > maxWidth) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
479 var newWidth = maxWidth,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
480 newHeight = ratio === null ? this.height() : maxHeight;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
481 this.resize(newWidth, newHeight, origin);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
482 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
483 if (maxHeight && this.height() > maxHeight) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
484 var _newWidth = ratio === null ? this.width() : maxWidth,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
485 _newHeight = maxHeight;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
486 this.resize(_newWidth, _newHeight, origin);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
487 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
488 if (minWidth && this.width() < minWidth) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
489 var _newWidth2 = minWidth,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
490 _newHeight2 = ratio === null ? this.height() : minHeight;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
491 this.resize(_newWidth2, _newHeight2, origin);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
492 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
493 if (minHeight && this.height() < minHeight) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
494 var _newWidth3 = ratio === null ? this.width() : minWidth,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
495 _newHeight3 = minHeight;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
496 this.resize(_newWidth3, _newHeight3, origin);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
497 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
498 return this;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
499 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
500 }]);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
501 return Box;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
502 }();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
503
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
504 /**
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
505 * Binds an element's touch events to be simulated as mouse events.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
506 * @param {Element} element
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
507 */
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
508 function enableTouch(element) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
509 element.addEventListener('touchstart', simulateMouseEvent);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
510 element.addEventListener('touchend', simulateMouseEvent);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
511 element.addEventListener('touchmove', simulateMouseEvent);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
512 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
513 /**
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
514 * Translates a touch event to a mouse event.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
515 * @param {Event} e
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
516 */
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
517 function simulateMouseEvent(e) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
518 e.preventDefault();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
519 var touch = e.changedTouches[0];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
520 var eventMap = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
521 'touchstart': 'mousedown',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
522 'touchmove': 'mousemove',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
523 'touchend': 'mouseup'
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
524 };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
525 touch.target.dispatchEvent(new MouseEvent(eventMap[e.type], {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
526 bubbles: true,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
527 cancelable: true,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
528 view: window,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
529 clientX: touch.clientX,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
530 clientY: touch.clientY,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
531 screenX: touch.screenX,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
532 screenY: touch.screenY
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
533 }));
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
534 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
535
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
536 /**
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
537 * Define a list of handles to create.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
538 *
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
539 * @property {Array} position - The x and y ratio position of the handle within
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
540 * the crop region. Accepts a value between 0 to 1 in the order of [X, Y].
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
541 * @property {Array} constraints - Define the side of the crop region that is to
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
542 * be affected by this handle. Accepts a value of 0 or 1 in the order of
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
543 * [TOP, RIGHT, BOTTOM, LEFT].
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
544 * @property {String} cursor - The CSS cursor of this handle.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
545 */
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
546 var HANDLES = [{ position: [0.0, 0.0], constraints: [1, 0, 0, 1], cursor: 'nw-resize' }, { position: [0.5, 0.0], constraints: [1, 0, 0, 0], cursor: 'n-resize' }, { position: [1.0, 0.0], constraints: [1, 1, 0, 0], cursor: 'ne-resize' }, { position: [1.0, 0.5], constraints: [0, 1, 0, 0], cursor: 'e-resize' }, { position: [1.0, 1.0], constraints: [0, 1, 1, 0], cursor: 'se-resize' }, { position: [0.5, 1.0], constraints: [0, 0, 1, 0], cursor: 's-resize' }, { position: [0.0, 1.0], constraints: [0, 0, 1, 1], cursor: 'sw-resize' }, { position: [0.0, 0.5], constraints: [0, 0, 0, 1], cursor: 'w-resize' }];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
547 var CropprCore = function () {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
548 function CropprCore(element, options) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
549 var _this = this;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
550 var deferred = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
551 classCallCheck(this, CropprCore);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
552 this.options = CropprCore.parseOptions(options || {});
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
553 if (!element.nodeName) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
554 element = document.querySelector(element);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
555 if (element == null) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
556 throw 'Unable to find element.';
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
557 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
558 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
559 if (!element.getAttribute('src')) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
560 throw 'Image src not provided.';
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
561 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
562 this._initialized = false;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
563 this._restore = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
564 parent: element.parentNode,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
565 element: element
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
566 };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
567 if (!deferred) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
568 if (element.width === 0 || element.height === 0) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
569 element.onload = function () {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
570 _this.initialize(element);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
571 };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
572 } else {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
573 this.initialize(element);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
574 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
575 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
576 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
577 createClass(CropprCore, [{
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
578 key: 'initialize',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
579 value: function initialize(element) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
580 this.createDOM(element);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
581 this.options.convertToPixels(this.cropperEl);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
582 this.attachHandlerEvents();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
583 this.attachRegionEvents();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
584 this.attachOverlayEvents();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
585 this.box = this.initializeBox(this.options);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
586 this.redraw();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
587 this._initialized = true;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
588 if (this.options.onInitialize !== null) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
589 this.options.onInitialize(this);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
590 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
591 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
592 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
593 key: 'createDOM',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
594 value: function createDOM(targetEl) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
595 this.containerEl = document.createElement('div');
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
596 this.containerEl.className = 'croppr-container';
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
597 this.eventBus = this.containerEl;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
598 enableTouch(this.containerEl);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
599 this.cropperEl = document.createElement('div');
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
600 this.cropperEl.className = 'croppr';
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
601 this.imageEl = document.createElement('img');
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
602 this.imageEl.setAttribute('src', targetEl.getAttribute('src'));
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
603 this.imageEl.setAttribute('alt', targetEl.getAttribute('alt'));
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
604 this.imageEl.className = 'croppr-image';
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
605 this.imageClippedEl = this.imageEl.cloneNode();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
606 this.imageClippedEl.className = 'croppr-imageClipped';
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
607 this.regionEl = document.createElement('div');
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
608 this.regionEl.className = 'croppr-region';
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
609 this.overlayEl = document.createElement('div');
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
610 this.overlayEl.className = 'croppr-overlay';
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
611 var handleContainerEl = document.createElement('div');
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
612 handleContainerEl.className = 'croppr-handleContainer';
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
613 this.handles = [];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
614 for (var i = 0; i < HANDLES.length; i++) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
615 var handle = new Handle(HANDLES[i].position, HANDLES[i].constraints, HANDLES[i].cursor, this.eventBus);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
616 this.handles.push(handle);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
617 handleContainerEl.appendChild(handle.el);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
618 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
619 this.cropperEl.appendChild(this.imageEl);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
620 this.cropperEl.appendChild(this.imageClippedEl);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
621 this.cropperEl.appendChild(this.regionEl);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
622 this.cropperEl.appendChild(this.overlayEl);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
623 this.cropperEl.appendChild(handleContainerEl);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
624 this.containerEl.appendChild(this.cropperEl);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
625 targetEl.parentElement.replaceChild(this.containerEl, targetEl);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
626 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
627 /**
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
628 * Changes the image src.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
629 * @param {String} src
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
630 */
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
631 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
632 key: 'setImage',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
633 value: function setImage(src) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
634 var _this2 = this;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
635 this.imageEl.onload = function () {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
636 _this2.box = _this2.initializeBox(_this2.options);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
637 _this2.redraw();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
638 };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
639 this.imageEl.src = src;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
640 this.imageClippedEl.src = src;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
641 return this;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
642 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
643 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
644 key: 'destroy',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
645 value: function destroy() {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
646 this._restore.parent.replaceChild(this._restore.element, this.containerEl);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
647 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
648 /**
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
649 * Create a new box region with a set of options.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
650 * @param {Object} opts The options.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
651 * @returns {Box}
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
652 */
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
653 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
654 key: 'initializeBox',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
655 value: function initializeBox(opts) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
656 var width = opts.startSize.width;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
657 var height = opts.startSize.height;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
658 var box = new Box(0, 0, width, height);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
659 box.constrainToRatio(opts.aspectRatio, [0.5, 0.5]);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
660 var min = opts.minSize;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
661 var max = opts.maxSize;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
662 box.constrainToSize(max.width, max.height, min.width, min.height, [0.5, 0.5], opts.aspectRatio);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
663 var parentWidth = this.cropperEl.offsetWidth;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
664 var parentHeight = this.cropperEl.offsetHeight;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
665 box.constrainToBoundary(parentWidth, parentHeight, [0.5, 0.5]);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
666 var x = this.cropperEl.offsetWidth / 2 - box.width() / 2;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
667 var y = this.cropperEl.offsetHeight / 2 - box.height() / 2;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
668 box.move(x, y);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
669 return box;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
670 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
671 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
672 key: 'redraw',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
673 value: function redraw() {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
674 var _this3 = this;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
675 var width = Math.round(this.box.width()),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
676 height = Math.round(this.box.height()),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
677 x1 = Math.round(this.box.x1),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
678 y1 = Math.round(this.box.y1),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
679 x2 = Math.round(this.box.x2),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
680 y2 = Math.round(this.box.y2);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
681 window.requestAnimationFrame(function () {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
682 _this3.regionEl.style.transform = 'translate(' + x1 + 'px, ' + y1 + 'px)';
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
683 _this3.regionEl.style.width = width + 'px';
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
684 _this3.regionEl.style.height = height + 'px';
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
685 _this3.imageClippedEl.style.clip = 'rect(' + y1 + 'px, ' + x2 + 'px, ' + y2 + 'px, ' + x1 + 'px)';
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
686 var center = _this3.box.getAbsolutePoint([.5, .5]);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
687 var xSign = center[0] - _this3.cropperEl.offsetWidth / 2 >> 31;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
688 var ySign = center[1] - _this3.cropperEl.offsetHeight / 2 >> 31;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
689 var quadrant = (xSign ^ ySign) + ySign + ySign + 4;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
690 var foregroundHandleIndex = -2 * quadrant + 8;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
691 for (var i = 0; i < _this3.handles.length; i++) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
692 var handle = _this3.handles[i];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
693 var handleWidth = handle.el.offsetWidth;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
694 var handleHeight = handle.el.offsetHeight;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
695 var left = x1 + width * handle.position[0] - handleWidth / 2;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
696 var top = y1 + height * handle.position[1] - handleHeight / 2;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
697 handle.el.style.transform = 'translate(' + Math.round(left) + 'px, ' + Math.round(top) + 'px)';
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
698 handle.el.style.zIndex = foregroundHandleIndex == i ? 5 : 4;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
699 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
700 });
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
701 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
702 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
703 key: 'attachHandlerEvents',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
704 value: function attachHandlerEvents() {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
705 var eventBus = this.eventBus;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
706 eventBus.addEventListener('handlestart', this.onHandleMoveStart.bind(this));
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
707 eventBus.addEventListener('handlemove', this.onHandleMoveMoving.bind(this));
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
708 eventBus.addEventListener('handleend', this.onHandleMoveEnd.bind(this));
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
709 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
710 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
711 key: 'attachRegionEvents',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
712 value: function attachRegionEvents() {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
713 var eventBus = this.eventBus;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
714 var self = this;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
715 this.regionEl.addEventListener('mousedown', onMouseDown);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
716 eventBus.addEventListener('regionstart', this.onRegionMoveStart.bind(this));
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
717 eventBus.addEventListener('regionmove', this.onRegionMoveMoving.bind(this));
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
718 eventBus.addEventListener('regionend', this.onRegionMoveEnd.bind(this));
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
719 function onMouseDown(e) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
720 e.stopPropagation();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
721 document.addEventListener('mouseup', onMouseUp);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
722 document.addEventListener('mousemove', onMouseMove);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
723 eventBus.dispatchEvent(new CustomEvent('regionstart', {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
724 detail: { mouseX: e.clientX, mouseY: e.clientY }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
725 }));
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
726 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
727 function onMouseMove(e) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
728 e.stopPropagation();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
729 eventBus.dispatchEvent(new CustomEvent('regionmove', {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
730 detail: { mouseX: e.clientX, mouseY: e.clientY }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
731 }));
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
732 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
733 function onMouseUp(e) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
734 e.stopPropagation();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
735 document.removeEventListener('mouseup', onMouseUp);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
736 document.removeEventListener('mousemove', onMouseMove);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
737 eventBus.dispatchEvent(new CustomEvent('regionend', {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
738 detail: { mouseX: e.clientX, mouseY: e.clientY }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
739 }));
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
740 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
741 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
742 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
743 key: 'attachOverlayEvents',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
744 value: function attachOverlayEvents() {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
745 var SOUTHEAST_HANDLE_IDX = 4;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
746 var self = this;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
747 var tmpBox = null;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
748 this.overlayEl.addEventListener('mousedown', onMouseDown);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
749 function onMouseDown(e) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
750 e.stopPropagation();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
751 document.addEventListener('mouseup', onMouseUp);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
752 document.addEventListener('mousemove', onMouseMove);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
753 var container = self.cropperEl.getBoundingClientRect();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
754 var mouseX = e.clientX - container.left;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
755 var mouseY = e.clientY - container.top;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
756 tmpBox = self.box;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
757 self.box = new Box(mouseX, mouseY, mouseX + 1, mouseY + 1);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
758 self.eventBus.dispatchEvent(new CustomEvent('handlestart', {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
759 detail: { handle: self.handles[SOUTHEAST_HANDLE_IDX] }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
760 }));
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
761 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
762 function onMouseMove(e) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
763 e.stopPropagation();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
764 self.eventBus.dispatchEvent(new CustomEvent('handlemove', {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
765 detail: { mouseX: e.clientX, mouseY: e.clientY }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
766 }));
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
767 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
768 function onMouseUp(e) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
769 e.stopPropagation();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
770 document.removeEventListener('mouseup', onMouseUp);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
771 document.removeEventListener('mousemove', onMouseMove);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
772 if (self.box.width() === 1 && self.box.height() === 1) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
773 self.box = tmpBox;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
774 return;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
775 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
776 self.eventBus.dispatchEvent(new CustomEvent('handleend', {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
777 detail: { mouseX: e.clientX, mouseY: e.clientY }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
778 }));
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
779 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
780 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
781 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
782 key: 'onHandleMoveStart',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
783 value: function onHandleMoveStart(e) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
784 var handle = e.detail.handle;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
785 var originPoint = [1 - handle.position[0], 1 - handle.position[1]];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
786 var _box$getAbsolutePoint = this.box.getAbsolutePoint(originPoint),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
787 _box$getAbsolutePoint2 = slicedToArray(_box$getAbsolutePoint, 2),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
788 originX = _box$getAbsolutePoint2[0],
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
789 originY = _box$getAbsolutePoint2[1];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
790 this.activeHandle = { handle: handle, originPoint: originPoint, originX: originX, originY: originY };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
791 if (this.options.onCropStart !== null) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
792 this.options.onCropStart(this.getValue());
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
793 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
794 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
795 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
796 key: 'onHandleMoveMoving',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
797 value: function onHandleMoveMoving(e) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
798 var _e$detail = e.detail,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
799 mouseX = _e$detail.mouseX,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
800 mouseY = _e$detail.mouseY;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
801 var container = this.cropperEl.getBoundingClientRect();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
802 mouseX = mouseX - container.left;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
803 mouseY = mouseY - container.top;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
804 if (mouseX < 0) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
805 mouseX = 0;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
806 } else if (mouseX > container.width) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
807 mouseX = container.width;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
808 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
809 if (mouseY < 0) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
810 mouseY = 0;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
811 } else if (mouseY > container.height) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
812 mouseY = container.height;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
813 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
814 var origin = this.activeHandle.originPoint.slice();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
815 var originX = this.activeHandle.originX;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
816 var originY = this.activeHandle.originY;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
817 var handle = this.activeHandle.handle;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
818 var TOP_MOVABLE = handle.constraints[0] === 1;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
819 var RIGHT_MOVABLE = handle.constraints[1] === 1;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
820 var BOTTOM_MOVABLE = handle.constraints[2] === 1;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
821 var LEFT_MOVABLE = handle.constraints[3] === 1;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
822 var MULTI_AXIS = (LEFT_MOVABLE || RIGHT_MOVABLE) && (TOP_MOVABLE || BOTTOM_MOVABLE);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
823 var x1 = LEFT_MOVABLE || RIGHT_MOVABLE ? originX : this.box.x1;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
824 var x2 = LEFT_MOVABLE || RIGHT_MOVABLE ? originX : this.box.x2;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
825 var y1 = TOP_MOVABLE || BOTTOM_MOVABLE ? originY : this.box.y1;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
826 var y2 = TOP_MOVABLE || BOTTOM_MOVABLE ? originY : this.box.y2;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
827 x1 = LEFT_MOVABLE ? mouseX : x1;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
828 x2 = RIGHT_MOVABLE ? mouseX : x2;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
829 y1 = TOP_MOVABLE ? mouseY : y1;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
830 y2 = BOTTOM_MOVABLE ? mouseY : y2;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
831 var isFlippedX = false,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
832 isFlippedY = false;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
833 if (LEFT_MOVABLE || RIGHT_MOVABLE) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
834 isFlippedX = LEFT_MOVABLE ? mouseX > originX : mouseX < originX;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
835 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
836 if (TOP_MOVABLE || BOTTOM_MOVABLE) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
837 isFlippedY = TOP_MOVABLE ? mouseY > originY : mouseY < originY;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
838 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
839 if (isFlippedX) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
840 var tmp = x1;x1 = x2;x2 = tmp;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
841 origin[0] = 1 - origin[0];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
842 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
843 if (isFlippedY) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
844 var _tmp = y1;y1 = y2;y2 = _tmp;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
845 origin[1] = 1 - origin[1];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
846 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
847 var box = new Box(x1, y1, x2, y2);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
848 if (this.options.aspectRatio) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
849 var ratio = this.options.aspectRatio;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
850 var isVerticalMovement = false;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
851 if (MULTI_AXIS) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
852 isVerticalMovement = mouseY > box.y1 + ratio * box.width() || mouseY < box.y2 - ratio * box.width();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
853 } else if (TOP_MOVABLE || BOTTOM_MOVABLE) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
854 isVerticalMovement = true;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
855 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
856 var ratioMode = isVerticalMovement ? 'width' : 'height';
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
857 box.constrainToRatio(ratio, origin, ratioMode);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
858 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
859 var min = this.options.minSize;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
860 var max = this.options.maxSize;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
861 box.constrainToSize(max.width, max.height, min.width, min.height, origin, this.options.aspectRatio);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
862 var parentWidth = this.cropperEl.offsetWidth;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
863 var parentHeight = this.cropperEl.offsetHeight;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
864 box.constrainToBoundary(parentWidth, parentHeight, origin);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
865 this.box = box;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
866 this.redraw();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
867 if (this.options.onCropMove !== null) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
868 this.options.onCropMove(this.getValue());
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
869 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
870 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
871 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
872 key: 'onHandleMoveEnd',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
873 value: function onHandleMoveEnd(e) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
874 if (this.options.onCropEnd !== null) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
875 this.options.onCropEnd(this.getValue());
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
876 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
877 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
878 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
879 key: 'onRegionMoveStart',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
880 value: function onRegionMoveStart(e) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
881 var _e$detail2 = e.detail,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
882 mouseX = _e$detail2.mouseX,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
883 mouseY = _e$detail2.mouseY;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
884 var container = this.cropperEl.getBoundingClientRect();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
885 mouseX = mouseX - container.left;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
886 mouseY = mouseY - container.top;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
887 this.currentMove = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
888 offsetX: mouseX - this.box.x1,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
889 offsetY: mouseY - this.box.y1
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
890 };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
891 if (this.options.onCropStart !== null) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
892 this.options.onCropStart(this.getValue());
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
893 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
894 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
895 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
896 key: 'onRegionMoveMoving',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
897 value: function onRegionMoveMoving(e) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
898 var _e$detail3 = e.detail,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
899 mouseX = _e$detail3.mouseX,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
900 mouseY = _e$detail3.mouseY;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
901 var _currentMove = this.currentMove,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
902 offsetX = _currentMove.offsetX,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
903 offsetY = _currentMove.offsetY;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
904 var container = this.cropperEl.getBoundingClientRect();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
905 mouseX = mouseX - container.left;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
906 mouseY = mouseY - container.top;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
907 this.box.move(mouseX - offsetX, mouseY - offsetY);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
908 if (this.box.x1 < 0) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
909 this.box.move(0, null);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
910 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
911 if (this.box.x2 > container.width) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
912 this.box.move(container.width - this.box.width(), null);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
913 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
914 if (this.box.y1 < 0) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
915 this.box.move(null, 0);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
916 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
917 if (this.box.y2 > container.height) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
918 this.box.move(null, container.height - this.box.height());
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
919 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
920 this.redraw();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
921 if (this.options.onCropMove !== null) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
922 this.options.onCropMove(this.getValue());
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
923 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
924 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
925 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
926 key: 'onRegionMoveEnd',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
927 value: function onRegionMoveEnd(e) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
928 if (this.options.onCropEnd !== null) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
929 this.options.onCropEnd(this.getValue());
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
930 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
931 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
932 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
933 key: 'getValue',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
934 value: function getValue() {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
935 var mode = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
936 if (mode === null) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
937 mode = this.options.returnMode;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
938 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
939 if (mode == 'real') {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
940 var actualWidth = this.imageEl.naturalWidth;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
941 var actualHeight = this.imageEl.naturalHeight;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
942 var _imageEl$getBoundingC = this.imageEl.getBoundingClientRect(),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
943 elementWidth = _imageEl$getBoundingC.width,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
944 elementHeight = _imageEl$getBoundingC.height;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
945 var factorX = actualWidth / elementWidth;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
946 var factorY = actualHeight / elementHeight;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
947 return {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
948 x: Math.round(this.box.x1 * factorX),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
949 y: Math.round(this.box.y1 * factorY),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
950 width: Math.round(this.box.width() * factorX),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
951 height: Math.round(this.box.height() * factorY)
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
952 };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
953 } else if (mode == 'ratio') {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
954 var _imageEl$getBoundingC2 = this.imageEl.getBoundingClientRect(),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
955 _elementWidth = _imageEl$getBoundingC2.width,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
956 _elementHeight = _imageEl$getBoundingC2.height;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
957 return {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
958 x: round(this.box.x1 / _elementWidth, 3),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
959 y: round(this.box.y1 / _elementHeight, 3),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
960 width: round(this.box.width() / _elementWidth, 3),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
961 height: round(this.box.height() / _elementHeight, 3)
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
962 };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
963 } else if (mode == 'raw') {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
964 return {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
965 x: Math.round(this.box.x1),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
966 y: Math.round(this.box.y1),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
967 width: Math.round(this.box.width()),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
968 height: Math.round(this.box.height())
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
969 };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
970 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
971 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
972 }], [{
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
973 key: 'parseOptions',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
974 value: function parseOptions(opts) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
975 var defaults$$1 = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
976 aspectRatio: null,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
977 maxSize: { width: null, height: null },
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
978 minSize: { width: null, height: null },
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
979 startSize: { width: 100, height: 100, unit: '%' },
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
980 returnMode: 'real',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
981 onInitialize: null,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
982 onCropStart: null,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
983 onCropMove: null,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
984 onCropEnd: null
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
985 };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
986 var aspectRatio = null;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
987 if (opts.aspectRatio !== undefined) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
988 if (typeof opts.aspectRatio === 'number') {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
989 aspectRatio = opts.aspectRatio;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
990 } else if (opts.aspectRatio instanceof Array) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
991 aspectRatio = opts.aspectRatio[1] / opts.aspectRatio[0];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
992 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
993 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
994 var maxSize = null;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
995 if (opts.maxSize !== undefined && opts.maxSize !== null) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
996 maxSize = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
997 width: opts.maxSize[0] || null,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
998 height: opts.maxSize[1] || null,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
999 unit: opts.maxSize[2] || 'px'
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1000 };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1001 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1002 var minSize = null;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1003 if (opts.minSize !== undefined && opts.minSize !== null) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1004 minSize = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1005 width: opts.minSize[0] || null,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1006 height: opts.minSize[1] || null,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1007 unit: opts.minSize[2] || 'px'
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1008 };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1009 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1010 var startSize = null;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1011 if (opts.startSize !== undefined && opts.startSize !== null) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1012 startSize = {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1013 width: opts.startSize[0] || null,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1014 height: opts.startSize[1] || null,
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1015 unit: opts.startSize[2] || '%'
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1016 };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1017 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1018 var onInitialize = null;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1019 if (typeof opts.onInitialize === 'function') {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1020 onInitialize = opts.onInitialize;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1021 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1022 var onCropStart = null;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1023 if (typeof opts.onCropStart === 'function') {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1024 onCropStart = opts.onCropStart;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1025 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1026 var onCropEnd = null;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1027 if (typeof opts.onCropEnd === 'function') {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1028 onCropEnd = opts.onCropEnd;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1029 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1030 var onCropMove = null;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1031 if (typeof opts.onUpdate === 'function') {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1032 console.warn('Croppr.js: `onUpdate` is deprecated and will be removed in the next major release. Please use `onCropMove` or `onCropEnd` instead.');
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1033 onCropMove = opts.onUpdate;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1034 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1035 if (typeof opts.onCropMove === 'function') {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1036 onCropMove = opts.onCropMove;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1037 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1038 var returnMode = null;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1039 if (opts.returnMode !== undefined) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1040 var s = opts.returnMode.toLowerCase();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1041 if (['real', 'ratio', 'raw'].indexOf(s) === -1) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1042 throw "Invalid return mode.";
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1043 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1044 returnMode = s;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1045 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1046 var convertToPixels = function convertToPixels(container) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1047 var width = container.offsetWidth;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1048 var height = container.offsetHeight;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1049 var sizeKeys = ['maxSize', 'minSize', 'startSize'];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1050 for (var i = 0; i < sizeKeys.length; i++) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1051 var key = sizeKeys[i];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1052 if (this[key] !== null) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1053 if (this[key].unit == '%') {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1054 if (this[key].width !== null) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1055 this[key].width = this[key].width / 100 * width;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1056 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1057 if (this[key].height !== null) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1058 this[key].height = this[key].height / 100 * height;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1059 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1060 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1061 delete this[key].unit;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1062 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1063 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1064 };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1065 var defaultValue = function defaultValue(v, d) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1066 return v !== null ? v : d;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1067 };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1068 return {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1069 aspectRatio: defaultValue(aspectRatio, defaults$$1.aspectRatio),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1070 maxSize: defaultValue(maxSize, defaults$$1.maxSize),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1071 minSize: defaultValue(minSize, defaults$$1.minSize),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1072 startSize: defaultValue(startSize, defaults$$1.startSize),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1073 returnMode: defaultValue(returnMode, defaults$$1.returnMode),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1074 onInitialize: defaultValue(onInitialize, defaults$$1.onInitialize),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1075 onCropStart: defaultValue(onCropStart, defaults$$1.onCropStart),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1076 onCropMove: defaultValue(onCropMove, defaults$$1.onCropMove),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1077 onCropEnd: defaultValue(onCropEnd, defaults$$1.onCropEnd),
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1078 convertToPixels: convertToPixels
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1079 };
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1080 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1081 }]);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1082 return CropprCore;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1083 }();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1084 function round(value, decimals) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1085 return Number(Math.round(value + 'e' + decimals) + 'e-' + decimals);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1086 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1087
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1088 var Croppr$1 = function (_CropprCore) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1089 inherits(Croppr, _CropprCore);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1090 /**
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1091 * @constructor
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1092 * Calls the CropprCore's constructor.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1093 */
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1094 function Croppr(element, options) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1095 var _deferred = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1096 classCallCheck(this, Croppr);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1097 return possibleConstructorReturn(this, (Croppr.__proto__ || Object.getPrototypeOf(Croppr)).call(this, element, options, _deferred));
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1098 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1099 /**
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1100 * Gets the value of the crop region.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1101 * @param {String} [mode] Which mode of calculation to use: 'real', 'ratio' or
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1102 * 'raw'.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1103 */
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1104 createClass(Croppr, [{
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1105 key: 'getValue',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1106 value: function getValue(mode) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1107 return get(Croppr.prototype.__proto__ || Object.getPrototypeOf(Croppr.prototype), 'getValue', this).call(this, mode);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1108 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1109 /**
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1110 * Changes the image src.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1111 * @param {String} src
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1112 */
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1113 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1114 key: 'setImage',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1115 value: function setImage(src) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1116 return get(Croppr.prototype.__proto__ || Object.getPrototypeOf(Croppr.prototype), 'setImage', this).call(this, src);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1117 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1118 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1119 key: 'destroy',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1120 value: function destroy() {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1121 return get(Croppr.prototype.__proto__ || Object.getPrototypeOf(Croppr.prototype), 'destroy', this).call(this);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1122 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1123 /**
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1124 * Moves the crop region to a specified coordinate.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1125 * @param {Number} x
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1126 * @param {Number} y
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1127 */
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1128 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1129 key: 'moveTo',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1130 value: function moveTo(x, y) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1131 this.box.move(x, y);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1132 this.redraw();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1133 if (this.options.onCropEnd !== null) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1134 this.options.onCropEnd(this.getValue());
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1135 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1136 return this;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1137 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1138 /**
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1139 * Resizes the crop region to a specified width and height.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1140 * @param {Number} width
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1141 * @param {Number} height
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1142 * @param {Array} origin The origin point to resize from.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1143 * Defaults to [0.5, 0.5] (center).
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1144 */
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1145 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1146 key: 'resizeTo',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1147 value: function resizeTo(width, height) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1148 var origin = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [.5, .5];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1149 this.box.resize(width, height, origin);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1150 this.redraw();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1151 if (this.options.onCropEnd !== null) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1152 this.options.onCropEnd(this.getValue());
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1153 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1154 return this;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1155 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1156 /**
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1157 * Scale the crop region by a factor.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1158 * @param {Number} factor
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1159 * @param {Array} origin The origin point to resize from.
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1160 * Defaults to [0.5, 0.5] (center).
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1161 */
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1162 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1163 key: 'scaleBy',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1164 value: function scaleBy(factor) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1165 var origin = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [.5, .5];
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1166 this.box.scale(factor, origin);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1167 this.redraw();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1168 if (this.options.onCropEnd !== null) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1169 this.options.onCropEnd(this.getValue());
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1170 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1171 return this;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1172 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1173 }, {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1174 key: 'reset',
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1175 value: function reset() {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1176 this.box = this.initializeBox(this.options);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1177 this.redraw();
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1178 if (this.options.onCropEnd !== null) {
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1179 this.options.onCropEnd(this.getValue());
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1180 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1181 return this;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1182 }
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1183 }]);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1184 return Croppr;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1185 }(CropprCore);
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1186
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1187 return Croppr$1;
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1188
8f4df159f06b start public repo
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1189 })));