Advertisement
asimryu

canvas 그림 불러오고 그리고 저장하기

Dec 20th, 2017
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.31 KB | None | 0 0
  1.  
  2. <!DOCTYPE html>
  3. <html lang="ko">
  4. <head>
  5.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6.     <link rel="stylesheet" href="http://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
  7.     <script src="http://code.jquery.com/jquery-latest.js"></script>
  8.     <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  9.     <title>캔버스 그리고 저장하기</title>
  10.     <style type="text/css">
  11.         canvas {border:1px solid #000;}
  12.     </style>   
  13. </head>
  14. <body>
  15.     <form>
  16.         <input type="file" id="photo">
  17.     </form>
  18.     <canvas id="mycanvas" width="500" height="500"></canvas>
  19.     <div><a href="" onclick="saveCanvasToImage(this)">그림저장하기</a></div>
  20.     <script type="text/javascript">
  21.         var canvas, context;
  22.         var drawing = false;
  23.         var lastX, lastY;
  24.         var Size = 5;
  25.         window.onload = function(){
  26.             canvas = document.getElementById("mycanvas");
  27.             context = canvas.getContext("2d");            
  28.         }
  29.         window.onmousedown = mousedown;    
  30.         window.onmousemove = mousemove;
  31.         window.onmouseup = mouseup;
  32.         function mousedown(event){
  33.             var e = event || window.event;
  34.             drawing = true;
  35.             lastX = e.clientX;
  36.             lastY = e.clientY;
  37.             draw(e.clientX, e.clientY);    
  38.         }
  39.         function mousemove(event){
  40.             if(drawing){
  41.                 var e = event || window.event;
  42.                 draw(e.clientX, e.clientY);
  43.             }
  44.         }
  45.         function mouseup(event){
  46.             var e = event || window.event;
  47.             drawing = false;
  48.             draw(e.clientX, e.clientY);
  49.         }
  50.         function draw(x,y){
  51.             context.lineWidth = Size;
  52.             context.beginPath();
  53.             context.moveTo(lastX, lastY);
  54.             context.lineTo(x, y);
  55.             lastX = x;
  56.             lastY = y;
  57.             context.closePath();
  58.             context.strokeStyle = "#000000";
  59.             context.stroke();
  60.         }
  61.         function saveCanvasToImage(link){
  62.             var src = canvas.toDataURL("image/png")
  63.             link.download = "image.png";
  64.             link.href = src;
  65.         }
  66.  
  67.         $("#photo").change(function(){
  68.             photoLoad(this);
  69.         });
  70.         function photoLoad(photo){
  71.             if( ! photo.files ) return;
  72.             if( ! photo.files[0] ) return;
  73.             var reader = new FileReader();
  74.             reader.onload = function(event){
  75.                 var src = event.target.result;
  76.                 if( ! src ) return;
  77.                 var img = new Image();
  78.                 img.onload = function(){
  79.                     context.drawImage(img,0,0,500,500);
  80.                 };
  81.                 img.src = src;
  82.             };
  83.             reader.readAsDataURL(photo.files[0]);
  84.         }
  85.     </script>
  86. </body>
  87. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement