-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
83 lines (77 loc) · 1.79 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en-us">
<head>
<meta charset="UTF-8">
<title>Img2canvas2img by getsetbro</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body{margin:0; font:15px/1 sans-serif;}
body *{box-sizing:border-box;}
header{
background-color: #888;
padding:11px;
}
h1{
margin:0;
}
section{
text-align: center;
}
input,canvas,button,img{
display: block;
margin:20px auto;
border:2px solid #ccc;
background-color: #eee
}
canvas{
height: 200px
}
</style>
</head>
<body>
<header>
<h1>app: img -> canvas -> img</h1>
</header>
<section>
STEP 1.
<input type="file" id="fileUpload" accept="image/*" />
CANVAS
<canvas id="canvas"></canvas>
STEP 2.
<button id="imageSaver">Save image</button>
IMAGE
<img src="" alt="" id="newImage"/>
</section>
<!-- <script src="//code.jquery.com/jquery-latest.min.js"></script>-->
<script>
function el(id){// Get elem by ID
return document.getElementById(id);
}
var canvas = el("canvas");
var ctx = canvas.getContext("2d");
function readImage() {
if ( this.files && this.files[0] ) {
var FR = new FileReader();
FR.onload = function(e) {
var img = new Image();
img.onload = function() {
canvas.width = img.width;
canvas.height = img.height;
//ctx.scale(.3,.3);
ctx.drawImage(img, 0, 0);
};
img.src = e.target.result;
};
FR.readAsDataURL( this.files[0] );
}
}
el("fileUpload").addEventListener("change", readImage, false);
function saveImage(e) {
var fullQuality = canvas.toDataURL("image/jpeg", 1.0);
var halfQuality = canvas.toDataURL("image/jpeg", 0.5);
el("newImage").src = halfQuality;
}
el("imageSaver").addEventListener('click', saveImage, false);
</script>
</body>
</html>