Advertisement
metalx1000

HTML5 Load local file on to canvas

Nov 26th, 2016
711
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.   <title>Load Image</title>
  5.   <meta charset="utf-8">
  6.   <meta name="viewport" content="width=device-width, initial-scale=1">
  7.  
  8. </head>
  9. <body>
  10.  
  11.   <input type='file' name='img' size='65' id='uploadimage' />
  12.   <br>
  13.   <canvas id="canvas" width="500" height="500"></canvas>
  14.  
  15. </body>
  16.   <script>
  17.     document.getElementById("uploadimage").addEventListener("change", draw, false);
  18.  
  19.     function draw(ev) {
  20.       console.log(ev);
  21.       var ctx = document.getElementById('canvas').getContext('2d'),
  22.         img = new Image(),
  23.         f = document.getElementById("uploadimage").files[0],
  24.         url = window.URL || window.webkitURL,
  25.         src = url.createObjectURL(f);
  26.  
  27.       img.src = src;
  28.       img.onload = function() {
  29.         ctx.drawImage(img, 0, 0);
  30.         url.revokeObjectURL(src);
  31.       }
  32.     }
  33.  
  34.   </script>
  35.  
  36. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement