forked from nadagehad01/Lab5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
53 lines (46 loc) · 2.31 KB
/
script.js
File metadata and controls
53 lines (46 loc) · 2.31 KB
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
// script.js
const img = new Image(); // used to load image from <input> and draw to canvas
// Fires whenever the img object loads a new image (such as with img.src =)
img.addEventListener('load', () => {
// TODO
// Some helpful tips:
// - Fill the whole Canvas with black first to add borders on non-square images, then draw on top
// - Clear the form when a new image is selected
// - If you draw the image to canvas here, it will update as soon as a new image is selected
});
/**
* Takes in the dimensions of the canvas and the new image, then calculates the new
* dimensions of the image so that it fits perfectly into the Canvas and maintains aspect ratio
* @param {number} canvasWidth Width of the canvas element to insert image into
* @param {number} canvasHeight Height of the canvas element to insert image into
* @param {number} imageWidth Width of the new user submitted image
* @param {number} imageHeight Height of the new user submitted image
* @returns {Object} An object containing four properties: The newly calculated width and height,
* and also the starting X and starting Y coordinate to be used when you draw the new image to the
* Canvas. These coordinates align with the top left of the image.
*/
function getDimmensions(canvasWidth, canvasHeight, imageWidth, imageHeight) {
let aspectRatio, height, width, startX, startY;
// Get the aspect ratio, used so the picture always fits inside the canvas
aspectRatio = imageWidth / imageHeight;
// If the apsect ratio is less than 1 it's a verical image
if (aspectRatio < 1) {
// Height is the max possible given the canvas
height = canvasHeight;
// Width is then proportional given the height and aspect ratio
width = canvasHeight * aspectRatio;
// Start the Y at the top since it's max height, but center the width
startY = 0;
startX = (canvasWidth - width) / 2;
// This is for horizontal images now
} else {
// Width is the maximum width possible given the canvas
width = canvasWidth;
// Height is then proportional given the width and aspect ratio
height = canvasWidth / aspectRatio;
// Start the X at the very left since it's max width, but center the height
startX = 0;
startY = (canvasHeight - height) / 2;
}
return { 'width': width, 'height': height, 'startX': startX, 'startY': startY }
}