Advertisement
asimryu

canvas 그리고 저장하기

Dec 20th, 2017
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.80 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. </head>
  11. <body>
  12.     <style type="text/css">
  13.         canvas {border:1px solid #000;}
  14.     </style>
  15.     <canvas id="mycanvas" width="500" height="500"></canvas>
  16.     <div><a href="" onclick="saveCanvasToImage(this)">그림저장하기</a></div>
  17.     <script type="text/javascript">
  18.         var canvas, context;
  19.         var drawing = false;
  20.         var lastX, lastY;
  21.         var Size = 5;
  22.         window.onload = function(){
  23.             canvas = document.getElementById("mycanvas");
  24.             context = canvas.getContext("2d");            
  25.         }
  26.         window.onmousedown = mousedown;    
  27.         window.onmousemove = mousemove;
  28.         window.onmouseup = mouseup;
  29.         function mousedown(event){
  30.             var e = event || window.event;
  31.             drawing = true;
  32.             lastX = e.clientX;
  33.             lastY = e.clientY;
  34.             draw(e.clientX, e.clientY);    
  35.         }
  36.         function mousemove(event){
  37.             if(drawing){
  38.                 var e = event || window.event;
  39.                 draw(e.clientX, e.clientY);
  40.             }
  41.         }
  42.         function mouseup(event){
  43.             var e = event || window.event;
  44.             drawing = false;
  45.             draw(e.clientX, e.clientY);
  46.         }
  47.         function draw(x,y){
  48.             context.lineWidth = Size;
  49.             context.beginPath();
  50.             context.moveTo(lastX, lastY);
  51.             context.lineTo(x, y);
  52.             lastX = x;
  53.             lastY = y;
  54.             context.closePath();
  55.             context.strokeStyle = "#000000";
  56.             context.stroke();
  57.         }
  58.         function saveCanvasToImage(link){
  59.             var src = canvas.toDataURL("image/png")
  60.             link.download = "image.png";
  61.             link.href = src;
  62.         }
  63.     </script>
  64. </body>
  65. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement