Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
127
rated 0 times [  130] [ 3]  / answers: 1 / hits: 20264  / 12 Years ago, wed, april 18, 2012, 12:00:00

i'm trying to make an javascript image color picker.
It is possible to open local image in canvas, without uploading it to server ?



function draw() {
var ctx = document.getElementById('canvas').getContext('2d');
var img = new Image();
img.onload = function(){
ctx.drawImage(img,0,0);
}

img.src = $('#uploadimage').val();
}

<input type='file' name='img' size='65' id='uploadimage' />

More From » jquery

 Answers
18

Not supported in all browser (IE and Opera AFAIK) but you could get a data URI using the File API



function draw() {
var ctx = document.getElementById('canvas').getContext('2d')
, img = new Image()
, f = document.getElementById(uploadimage).files[0]
, url = window.URL || window.webkitURL
, src = url.createObjectURL(f);

img.src = src;
img.onload = function(){
ctx.drawImage(img,0,0);
url.revokeObjectURL(src);
}
}

<input type='file' name='img' size='65' id='uploadimage' />


I added a fiddle here as an example.


[#86168] Tuesday, April 17, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
malkajillc

Total Points: 652
Total Questions: 107
Total Answers: 98

Location: Finland
Member since Sat, Nov 6, 2021
3 Years ago
malkajillc questions
;