Advertisement
metalx1000

HTML5 Load local file on to canvas with jQuery

Nov 26th, 2016
673
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.   <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  8.   <script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
  9.   <script src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
  10.  
  11.   <script>
  12.     $(document).ready(function(){
  13.       $("#uploadimage").on("change", draw);
  14.     });
  15.  
  16.     function draw(ev) {
  17.       console.log(ev);
  18.       var ctx = $('#canvas')[0].getContext('2d'),
  19.         img = new Image(),
  20.         f = $("#uploadimage")[0].files[0],
  21.         url = window.URL || window.webkitURL,
  22.         src = url.createObjectURL(f);
  23.  
  24.       img.src = src;
  25.       img.onload = function() {
  26.         ctx.drawImage(img, 0, 0);
  27.         url.revokeObjectURL(src);
  28.       }
  29.     }
  30.  
  31.   </script>
  32. </head>
  33. <body>
  34.  
  35. <div class="container">
  36.   <input type='file' name='img' size='65' id='uploadimage' />
  37.   <canvas id="canvas" width="500" height="500"></canvas>
  38. </div>
  39.  
  40. </body>
  41. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement