-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy paths-blond.js
68 lines (57 loc) · 1.98 KB
/
s-blond.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
var sImgLoad = document.getElementById('s-imgLoad');
sImgLoad.addEventListener('change', sHandleImage, false);
var sDownloadButton = document.getElementById('s-download');
var sDownloadStyle = document.getElementById('s-download-style');
var sAlbum = new Image();
sAlbum.src = "blond_sm.jpg";
function sLoadFrank() {
var sCanvas = document.getElementById('s-blond-canvas');
if (sCanvas.getContext) {
var sctx = sCanvas.getContext('2d');
sAlbum.onload = function() {
sctx.drawImage(sAlbum,0,0,300,300,0,0,300,300);
}
} else {
console.log('no way');
}
}
sLoadFrank();
function sHandleImage(e) {
var sCanvas = document.getElementById('s-blond-canvas');
if (sCanvas.getContext) {
var sctx = sCanvas.getContext('2d');
sDownloadStyle.classList.add('active');
var reader = new FileReader();
reader.onload = function(event){
var img = new Image();
img.onload = function() {
var width = img.width;
var height = img.height;
var cropWidth = ((img.width/2) - (img.height/3));
var cropHeight = ((img.height/2) - (img.width*0.75));
sCanvas.width = 300;
sCanvas.height = 300;
if (height > width*1.5) { //tall photos
var newHeight = width*1.5;
sctx.drawImage(sAlbum,0,0);
sctx.drawImage(img,0,cropHeight,width,newHeight,80,50,140,210);
} else { //wide or square photos
var newWidth = height/1.5;
sctx.drawImage(sAlbum,0,0);
sctx.drawImage(img,cropWidth,0,newWidth,height,80,50,140,210);
}
}
img.src = event.target.result;
}
reader.readAsDataURL(e.target.files[0]);
}
}
// Download canvas as image
function sDownloadCanvas(link, canvasId, filename) {
link.href = document.getElementById(canvasId).toDataURL();
link.download = filename;
}
sDownloadButton.addEventListener('click', function() {
console.log('button clicked');
sDownloadCanvas(this, 's-blond-canvas', 'blond.png');
}, false);