Open local image in canvas

前端 未结 1 862
广开言路
广开言路 2020-12-14 03:25

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() {
    va         


        
相关标签:
1条回答
  • 2020-12-14 03:26

    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.

    0 讨论(0)
提交回复
热议问题