Hi guys!
I am working on
application based on canvas element which allows you to apply Edge Detection effect on image.
Successfully doing that i wanted to add
FileReader or
FIle API so user can browse his own image from disk.
So the problem is, lets say i'm using File API (if someone knows the reason FileReader is better let me know pls),i don't know how to address e.target.result to global variable chosenimage so i can call fuction draw again and re-draw on canvas.
Here is my code without filereader or file api,so if you can post how it should look with file api(or fileReader)
Looking forward to any kind of replies
Code:
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Aplikacija Edge Detection</title>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<title>Aplikacija</title>
<meta name="description" content="" />
<meta name="author" content="marin" />
<meta name="viewport" content="width=device-width; initial-scale=1.0" />
<noscript>Skripte se ne mogu pokrenuti u vasem pregledniku</noscript>
<script type="text/javascript" src="raphael.js"></script>
<script>
var image =new Image();
var chosenimage = undefined;
function draw() {
var c=document.getElementById('canvas1');
var cxt=c.getContext('2d');
var image = new Image();
image.src="pipes.jpg";
cxt.drawImage(image,0,0);
}
function init() {
image.onload = edge;
image.src="pipes.jpg"; }
function edge() {
var canvas = document.getElementById('canvas1');
var context = canvas.getContext('2d');
context.drawImage(image, 0, 0);
// get the image data to manipulate
var input = context.getImageData(0, 0, canvas.width, canvas.height);
// get an empty slate to put the data into
var output = context.createImageData(canvas.width, canvas.height);
var w = input.width, h = input.height;
var inputData = input.data;
var outputData = output.data;
//EDGE DETECTION
for (var y = 1; y < h-1; y += 1) {
for (var x = 1; x < w-1; x += 1) {
for (var c = 0; c < 3; c += 1) {
var i = (y*w + x)*4 + c;
outputData[i] = 127 + -inputData[i - w*4 - 4] - inputData[i - w*4] - inputData[i - w*4 + 4] +
-inputData[i - 4] + 8*inputData[i] - inputData[i + 4] +
-inputData[i + w*4 - 4] - inputData[i + w*4] - inputData[i + w*4 + 4];
}
outputData[(y*w + x)*4 + 3] = 255; // alpha
}
}
context.putImageData(output, 0, 0);
}
</script>
<style type="text/css">some long css code here </style>
</head>
<body>
<script type="text/javascript">
window.onload = draw ;
</script>
<div id="okvir" style="display: block; margin-left: 100px; margin-top: 35px;">
<canvas id="canvas1" width="650" height="530" >Your browser does not support canvas.
</canvas>
<div id="edge">
<button onclick="init()" >Edge detection</button>
</div>
<div id="unos">
<input type="file" id="uploadImage" name="myphoto" onchange="loadImageFile();" >
</div>
</div>
</body>
</html>