asimryu

canvas 그리기 예제 1

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