MCPcopy Create free account
hub / github.com/TruthHun/BookStack / getSourceCanvas

Function getSourceCanvas

static/cropper/2.3.4/cropper.js:222–286  ·  view source on GitHub ↗
(image, data)

Source from the content-addressed store, hash-verified

220 }
221
222 function getSourceCanvas(image, data) {
223 var canvas = $('<canvas>')[0];
224 var context = canvas.getContext('2d');
225 var dstX = 0;
226 var dstY = 0;
227 var dstWidth = data.naturalWidth;
228 var dstHeight = data.naturalHeight;
229 var rotate = data.rotate;
230 var scaleX = data.scaleX;
231 var scaleY = data.scaleY;
232 var scalable = isNumber(scaleX) && isNumber(scaleY) && (scaleX !== 1 || scaleY !== 1);
233 var rotatable = isNumber(rotate) && rotate !== 0;
234 var advanced = rotatable || scalable;
235 var canvasWidth = dstWidth * abs(scaleX || 1);
236 var canvasHeight = dstHeight * abs(scaleY || 1);
237 var translateX;
238 var translateY;
239 var rotated;
240
241 if (scalable) {
242 translateX = canvasWidth / 2;
243 translateY = canvasHeight / 2;
244 }
245
246 if (rotatable) {
247 rotated = getRotatedSizes({
248 width: canvasWidth,
249 height: canvasHeight,
250 degree: rotate
251 });
252
253 canvasWidth = rotated.width;
254 canvasHeight = rotated.height;
255 translateX = canvasWidth / 2;
256 translateY = canvasHeight / 2;
257 }
258
259 canvas.width = canvasWidth;
260 canvas.height = canvasHeight;
261
262 if (advanced) {
263 dstX = -dstWidth / 2;
264 dstY = -dstHeight / 2;
265
266 context.save();
267 context.translate(translateX, translateY);
268 }
269
270 // Rotate should come first before scale as in the "getTransform" function
271 if (rotatable) {
272 context.rotate(rotate * Math.PI / 180);
273 }
274
275 if (scalable) {
276 context.scale(scaleX, scaleY);
277 }
278
279 context.drawImage(image, floor(dstX), floor(dstY), floor(dstWidth), floor(dstHeight));

Callers 1

cropper.jsFile · 0.85

Calls 3

getRotatedSizesFunction · 0.85
isNumberFunction · 0.70
$Function · 0.50

Tested by

no test coverage detected