Advertisement
asimryu

canvas basic practice

Jul 10th, 2019
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.55 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <title>canvas</title>
  6.     <style>
  7.         canvas {
  8.             border: 1px solid #888;
  9.         }
  10.     </style>
  11. </head>
  12. <body>
  13.     <canvas id="mycanvas" width="500" height="500"></canvas>
  14.     <script>
  15.         var c = document.getElementById("mycanvas");
  16.         var x = c.width;
  17.         var y = c.height;
  18.         var xc = x/2;
  19.         var yc = y/2;
  20.         var ctx = c.getContext("2d");
  21.         ctx.fillStyle = "red";
  22.         ctx.fillRect(10,10,100,100);
  23.         ctx.fillStyle = "rgba(0,0,200,0.5)";
  24.         ctx.fillRect(50,50,100,100);
  25.         ctx.beginPath();
  26.         ctx.fillStyle = "rgba(0,200,0,0.5)";
  27.         ctx.arc(xc, yc, 200, 0, 2*Math.PI);
  28.         ctx.fill();
  29.         ctx.strokeStyle = "red";
  30.         ctx.stroke();
  31.         ctx.beginPath();
  32.         ctx.moveTo(100,250);
  33.         ctx.lineTo(400,250);
  34.         ctx.lineWidth = 10; //선 두께
  35.         ctx.stroke();
  36.         ctx.beginPath();
  37.         ctx.strokeStyle = "pink";
  38.         ctx.strokeRect(200,200,200,200);
  39.         ctx.clearRect(0,0,x,y); //지우기
  40.  
  41.         //파이그리기
  42.         ctx.beginPath();
  43.         ctx.moveTo(xc,yc);
  44.         ctx.fillStyle = "red";
  45.         ctx.arc(xc,yc,200,0,0.5*Math.PI);
  46.         ctx.moveTo(xc,yc);
  47.         ctx.fill();
  48.         ctx.beginPath();
  49.         ctx.moveTo(xc,yc);
  50.         ctx.fillStyle = "green";
  51.         ctx.arc(xc,yc,200,0.5*Math.PI,0.8*Math.PI);
  52.         ctx.moveTo(xc,yc);
  53.         ctx.fill();
  54.         ctx.beginPath();
  55.         ctx.moveTo(xc,yc);
  56.         ctx.fillStyle = "blue";
  57.         ctx.arc(xc,yc,200,0.8*Math.PI,1.5*Math.PI);
  58.         ctx.moveTo(xc,yc);
  59.         ctx.fill();
  60.         ctx.beginPath();
  61.         ctx.moveTo(xc,yc);
  62.         ctx.fillStyle = "yellow";
  63.         ctx.arc(xc,yc,200,1.5*Math.PI,2*Math.PI);
  64.         ctx.moveTo(xc,yc);
  65.         ctx.fill();
  66.     </script>
  67. </body>
  68. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement