-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
225 lines (175 loc) · 6.49 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
window.utils = {
debounce: (func, delay) => {
let lastTimeout = window.performance.now();
let queued = 0;
let consuming = false;
function queueConsumer() {
// console.log('debounce worker state', { queued, delay, latency: window.performance.now() - lastTimeout });
lastTimeout = window.performance.now();
if (queued === 0) {
consuming = false;
return;
}
queued = 0;
setTimeout(queueConsumer, delay);
func(); // so the debounce doesn't get affected by the latency of the wrapped function
}
return () => {
if (consuming) {
queued++;
return;
}
consuming = true;
setTimeout(queueConsumer, delay);
func(); // so the debounce doesn't get affected by the latency of the wrapped function
}
},
timed: (func) => {
const start = window.performance.now();
let result = null;
let exception = null;
try {
result = func();
} catch (e) {
exception = e;
}
console.log(`### timed ${func.name}: ${window.performance.now() - start}`);
if (exception) throw exception;
return result;
},
find: (selector) => document.querySelector(selector),
findAll: (selector) => document.querySelectorAll(selector),
hide: function hide(elementOrSelector) {
if (!elementOrSelector.nodeType) {
elementOrSelector = this.find(elementOrSelector);
}
elementOrSelector.classList.add("hidden");
},
reveal: function reveal(elementOrSelector) {
if (!elementOrSelector.nodeType) {
elementOrSelector = this.find(elementOrSelector);
}
elementOrSelector.classList.remove("hidden");
},
addSlider: ({ containerDiv, sliderId, label, onUpdate, startingValue = 0.5 }) => {
const div = document.createElement('div');
div.id = sliderId;
div.classList.add('dragdealer');
div.innerHTML = `<div class="handle bar slider-button">${label}</div>`
containerDiv.appendChild(div);
return new Dragdealer(sliderId, { x: startingValue, animationCallback: (x, y) => onUpdate(x) });
},
getPositionForCursor: function(element, e) {
if (['mousedown', 'mousemove', 'mouseup'].includes(e.type)) {
const box = element.getBoundingClientRect();
return {
x: e.x - box.left,
y: e.y - box.top
};
}
},
getPositionForTouchMove: function(element, event) {
const touchEvent = event.targetTouches[0];
const box = element.getBoundingClientRect();
return {
x: touchEvent.clientX - box.left,
y: touchEvent.clientY - box.top
};
}
};
// var { sizeRatio, top, left } = vars;
// var vertOffset = canvas.height * sizeRatio;
// var vertRange = canvas.height + vertOffset;
// var horizontalOffset = canvas.width * sizeRatio;
// var horizontalRange = canvas.width + horizontalOffset;
// var x = left * horizontalRange;
// var y = top * vertRange;
// var ctx = canvas.getContext("2d");
// ctx.globalCompositeOperation = "source-over"; // layer on top
// console.log('drawImage', image, x - horizontalOffset, y - vertOffset, image.width * sizeRatio, image.height * sizeRatio);
// ctx.drawImage(image, x - horizontalOffset, y - vertOffset, image.width * sizeRatio, image.height * sizeRatio);
(() => {
function boundedRectangle(width, height, widthToHeightRatio) {
const ratio = width / height;
if (ratio == widthToHeightRatio) {
return { width, height };
}
// source is wider than destination
if (widthToHeightRatio > ratio) {
return { width, height: width / widthToHeightRatio };
}
// source is taller than destination
return {
height,
width: height * widthToHeightRatio
};
}
function resizeAndOffsetImage(image, destinationCanvas, { sizeRatio, top, left }) {
const {
height,
width
} = boundedRectangle(destinationCanvas.width, destinationCanvas.height, image.width / image.height);
const finalHeight = height * sizeRatio;
const finalWidth = width * sizeRatio;
const horizontalRange = (destinationCanvas.width + finalWidth);
const vertRange = (destinationCanvas.height + finalHeight);
const x = left * horizontalRange;
const y = top * vertRange;
const ctx = destinationCanvas.getContext("2d");
ctx.globalCompositeOperation = "source-over"; // layer on top
ctx.drawImage(image, x - finalWidth, y - finalHeight, finalWidth, finalHeight);
return destinationCanvas;
}
function flipImage(imgElement) {
const overlayCanvas = document.createElement('canvas');
overlayCanvas.width = imgElement.width;
overlayCanvas.height = imgElement.height;
const overlayCtx = overlayCanvas.getContext("2d");
overlayCtx.translate(imgElement.width, 0);
overlayCtx.scale(-1, 1);
overlayCtx.drawImage(imgElement, 0, 0);
return overlayCanvas;
}
function calculateRotation(sliderRotationValue) {
const angle = (sliderRotationValue - 0.5) * 360; // normalize 0 - 1.0 to -0.5 - 0.5
let radianAngle = angle * Math.PI / 180;
return radianAngle;
}
function calculateRotatedImageSize(oWidth, oHeight, radianAngle) {
let adjustedRadianAngle = Math.abs(radianAngle);
if (adjustedRadianAngle > Math.PI / 2) {
adjustedRadianAngle = Math.PI - adjustedRadianAngle;
}
return {
adjustedRadianAngle: adjustedRadianAngle,
width: oWidth * Math.cos(adjustedRadianAngle) + oHeight * Math.sin(adjustedRadianAngle),
height: oWidth * Math.sin(adjustedRadianAngle) + oHeight * Math.cos(adjustedRadianAngle)
}
}
function rotateImage(imgElement, sliderRotationValue) {
const overlayCanvas = document.createElement('canvas');
const overlayCtx = overlayCanvas.getContext("2d");
if (sliderRotationValue === 0.5) {
overlayCanvas.width = imgElement.width;
overlayCanvas.height = imgElement.height;
overlayCtx.drawImage(imgElement, 0, 0);
return overlayCanvas;
}
const radianAngle = calculateRotation(sliderRotationValue);
const { width, height } = calculateRotatedImageSize(imgElement.width, imgElement.height, radianAngle);
overlayCanvas.width = width;
overlayCanvas.height = height;
overlayCtx.translate(overlayCanvas.width / 2, overlayCanvas.height / 2);
overlayCtx.rotate(radianAngle);
overlayCtx.drawImage(imgElement, -imgElement.width / 2, -imgElement.height / 2);
return overlayCanvas;
}
window.imageUtils = {
resizeAndOffsetImage,
flipImage,
calculateRotation,
calculateRotatedImageSize,
rotateImage,
boundedRectangle,
};
})();