-
Notifications
You must be signed in to change notification settings - Fork 18
/
testImage.html
62 lines (56 loc) · 1.59 KB
/
testImage.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
<html>
<head>
<script type="text/javascript">
function setup() {
alert("setup");
var MAX_HEIGHT = 500;
var target = document.getElementById("drop-target"),
preview = document.getElementById("preview"),
canvas = document.getElementById("canvas");
var render = function(src){
var img = new Image();
img.onload = function(){
if(img.height > MAX_HEIGHT) {
img.width *= MAX_HEIGHT / img.height;
img.height = MAX_HEIGHT;
}
var ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, canvas.width, canvas.height);
preview.style.width = img.width + "px";
preview.style.height = img.height + "px";
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0, img.width, img.height);
};
img.src = src;
};
var readImage = function(imgFile){
if(!imgFile.type.match(/image.*/)){
console.log("The dropped file is not an image: ", imgFile.type);
return;
}
var reader = new FileReader();
reader.onload = function(e){
render(e.target.result);
};
reader.readAsDataURL(imgFile);
};
// DOMReady setup
target.addEventListener("dragover", function(e) {e.preventDefault();}, true);
target.addEventListener("drop", function(e){
e.preventDefault();
readImage(e.dataTransfer.files[0]);
}, true);
}
</script>
</head>
<body onload="setup()">
<p>Drag an image into the box below to see it automagically resized with canvas and JavaScript.</p>
<div id="preview-row">
<div id="drop-target">Drop image file here.</div>
<div id="preview">
<canvas id="canvas"></canvas>
</div>
</div>
</body>
</html>