forked from CSE110-SP21/Lab5
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
71 lines (63 loc) · 2.71 KB
/
script.js
File metadata and controls
71 lines (63 loc) · 2.71 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// script.js
console.log("first");
var img = new Image(); // used to load image from <input> and draw to canvas
window.onload=function(){
img = document.getElementById("image-input").files[0];
console.log("onload");
};
console.log("after onload");
img.addEventListener('change', loadImage);
function loadImage(){
console.log("change");
img.src = URL.createObjectURL(this.files[0]);
console.log("changeload");
};
// Fires whenever the img object loads a new image (such as with img.src =)
img.addEventListener('load', () => {
// TODO
const canvas = document.getElementById("user-image");
const context = canvas.getContext('2d');
//CLEAR THE CANVAS CONTEXt
// context.clearRect(0, 0, canvas.width, canvas.height);
//fill with black
context.fillStyle = 'black';
context.fillRect(0,0,canvas.width,canvas.height);
//Draw the image
drawImage(img, startX, startY);
});
/**
* 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 }
}