Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open local image in canvas

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' />
like image 882
Winns Avatar asked Apr 18 '12 12:04

Winns


People also ask

How do I open an image in canvas?

Go to your Canvas Course In your page, click on the embed image button (1) to quickly choose and upload an image saved on your computer. Click on the drop down arrow to the right of the embed image button to access images in your Course and User Files (2). A popup will appear to choose your image.

How do I find an image URL in canvas?

To get the image data URL of the canvas, we can use the toDataURL() method of the canvas object which converts the canvas drawing into a 64 bit encoded PNG URL. If you'd like for the image data URL to be in the jpeg format, you can pass image/jpeg as the first argument in the toDataURL() method.


1 Answers

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.

like image 86
Phil Parsons Avatar answered Oct 19 '22 18:10

Phil Parsons