svg 로 다각형 그리기



html)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" 
          content="width=device-width, height=device-height, 
                     minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0">
    <title>Insert title here</title>
</head>
 
<body>
    <?xml version="1.0" ?>
    <svg xmlns="http://www.w3.org/2000/svg" width="300px" height="400px" version="1.1">
        <!-- 
        polygon : 선의 끝부분이 연결된다
            points : 띄어쓰기로 점들을 지정한다
         -->    
        <polygon points="50,50 200,50 250,100 200,150 50,150" 
                 style="fill:orange; stroke:green; stroke-width:5;" />
        
        <!-- 
        polyline : 선의 끝부분이 연결되지 않는다         
            points : 띄어쓰기로 점들을 지정한다
         -->
        <polyline points="50,200 200,200 250,250 200,300 50,300" 
                 style="fill:orange; stroke:green; stroke-width:5;" />
    </svg>
</body>
</html>
cs



결과

'IT > - 프로그래밍' 카테고리의 다른 글

HTML5/CSS3 svg 로 글자쓰기  (0) 2017.07.22
HTML5/CSS3 svg 로 path 요소로 선긋기  (0) 2017.07.21
HTML5/CSS3 svg 로 선그리기  (0) 2017.07.19
HTML5/CSS3 svg 로 타원 그리기  (0) 2017.07.18
HTML5/CSS3 svg 로 원 그리기  (0) 2017.07.17

svg 로 선그리기



html)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Insert title here</title>
</head>
 
<body>
    <?xml version="1.0" ?>
    <svg xmlns="http://www.w3.org/2000/svg" width="300px" height="400px" version="1.1">
        <!-- 
        x1 : 시작점 x좌표
        y1 : 시작점 y좌표
        x2 : 끝점 x좌표
        y2 : 끝점 y좌표
         -->
        <line x1="50" y1="100" x2="250" y2="100"
              style="stroke:green; stroke-width:5; stroke-opacity:0.5;" />
    </svg>
</body>
</html>
cs



결과

svg 로 타원 그리기



html)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" 
          content="width=device-width, height=device-height, 
                     minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0">
    <title>Insert title here</title>
</head>
 
<body>
    <?xml version="1.0" ?>
    <svg xmlns="http://www.w3.org/2000/svg" width="300px" height="400px" version="1.1">
        <!-- 
        cx : 중점 x좌표
        cy : 중점 y좌표
        rx : 가로 반지름 크기
        ry : 세로 반지름 크기
         -->
        <ellipse cx="100" cy="100" rx="75" ry="50"
                 style="fill:orange; stroke:green; stroke-width:3;" />
    </svg>
</body>
</html>
cs



결과

svg 로 원 그리기



html)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" 
          content="width=device-width, height=device-height, 
                     minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0">
    <title>Insert title here</title>
</head>
 
<body>
    <?xml version="1.0"?>
    <svg xmlns="http://www.w3.org/2000/svg" width="300px" height="400px" version="1.1">
        <!-- 
        circle : 원
            cx : 중심점 x
            cy : 중심점 y
            r : 반지름
        -->
        <circle cx="150" cy="150" r="100" 
                style="fill:orange; stroke:#000; stroke-width:3;" />
    </svg>
</body>
</html>
cs



결과

svg 로 사각형 그리기



html)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" 
          content="width=device-width, height=device-height, 
                     minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0">
    <title>Insert title here</title>
</head>
 
<body>
    <?xml version="1.0"?>
    <svg xmlns="http://www.w3.org/2000/svg" width="300px" height="400px" version="1.1">
        <!-- 
        rect : 사각형
            x : 가로 길이
            y : 세로 길이
            rx, ry : 모서리 라운드
            fill : 배경색
            fill-opacity : 투명도
            stroke : 선색
            stroke-width : 선두께
            stroke-opacity : 선투명도
         --> 
        <rect x="10" y="10" rx="10" ry="10"
              width="100" height="100" 
              style="fill:#ff0000; fill-opacity:0.5; stroke:#000; stroke-width:3;" />
    </svg>
</body>
</html>
cs



결과

canvas 클리핑(오려내기)



html)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" 
          content="width=device-width, height=device-height, 
                     minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0">
    <title>Insert title here</title>
    <script type="text/javascript">
    window.onload = function(){
        var canvas = document.getElementById("canvas");
        if(canvas.getContext){
            var draw = canvas.getContext("2d");
            
            draw.beginPath();
            draw.arc(1501501000, Math.PI*2true);
            draw.lineWidth = 5;
            draw.stroke();
            //clip : 그린 도형 만큼 잘라내서 보여준다
            draw.clip();
            
            var img = new Image();
            img.src = "images/testimage.jpg";
            img.onload = function(){
                draw.drawImage(img, 5050250250);    
            }
        }
    }
    </script>
</head>
 
<body>
    <canvas id="canvas" width="300px" height="400px"></canvas>
</body>
</html>
cs



결과

canvas 도형의 이동, 회전, 확대



html)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" 
          content="width=device-width, height=device-height, 
                     minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0">
    <title>Insert title here</title>
    <script type="text/javascript">
    window.onload = function(){
        var canvas = document.getElementById("canvas");
        if(canvas.getContext){
            var draw = canvas.getContext("2d");
            
            draw.beginPath();
            draw.fillStyle = "green";
            //translate(이동할 x거리, 이동할 y거리) : 기준점 이동
            draw.translate(100,0);
            //rotate(deg) : 시계방향 회전
            draw.rotate(Math.PI*0.25);
            //scale(가로비율, 세로비율) : 확대/축소
            draw.scale(2,1);
            draw.fillRect(0,0,100,100);
        }
    }
    </script>
</head>
 
<body>
    <canvas id="canvas" width="300px" height="400px"></canvas>
</body>
</html>
cs



결과

canvas의 save, restore : 설정 저장,복구



html)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" 
          content="width=device-width, height=device-height, 
                     minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0">
    <title>Insert title here</title>
    <script type="text/javascript">
    window.onload = function(){
        var canvas = document.getElementById("canvas");
        if(canvas.getContext){
            var draw = canvas.getContext("2d");
            
            draw.lineWidth = 5;
            draw.strokeStyle = "red";
            
            //save : 시점 저장
            draw.save();    
            draw.beginPath();
            draw.moveTo(50,50);
            draw.lineTo(250,50);
            draw.lineWidth = 10;
            draw.strokeStyle = "green";
            draw.stroke();
            //restore : 저장하기 전으로 돌아가기
            draw.restore();
            
            draw.beginPath();
            draw.moveTo(50,100);
            draw.lineTo(250,100);
            draw.stroke();    //저장하기전 속성이 적용된다
        }
    }
    </script>
</head>
 
<body>
    <canvas id="canvas" width="300px" height="400px"></canvas>
</body>
</html>
cs



결과

canvas 에 텍스트 그리기



html)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" 
          content="width=device-width, height=device-height, 
                     minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0">
    <title>Insert title here</title>
    <script type="text/javascript">
    window.onload = function(){
        var canvas = document.getElementById("canvas");
        if(canvas.getContext){
            var draw = canvas.getContext("2d");
            
            //font = "스타일 폰트크기 폰트"
            draw.font = "bold 40px sans-serif";
            
            
            //fillStyle : 텍스트 배경색
            draw.fillStyle = "green";
            //fillText(텍스트, 기준점x, 기준점y) : 채워진 텍스트
            draw.fillText("Hello Canvas"5,50);
            
            //strokeStyle : 텍스트 선색
            draw.strokeStyle = "blue";
            //lineWidth : 선 굵기
            draw.lineWidth = 2;
            //textAlign : 가로정렬 (left, center, right)
            //textBaseline : 세로정렬 (top, middle, bottom)
            draw.textAlign = "center";
            //strokeText(텍스트, 기준점x, 기준점y) : 선 텍스트
            draw.strokeText("Hello Canvas"5,100);
        }
    }
    </script>
</head>
 
<body>
    <canvas id="canvas" width="300px" height="400px"></canvas>
</body>
</html>
cs



결과

canvas 에 이미지 띄우기



html)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" 
          content="width=device-width, height=device-height, 
                     minimum-scale=1.0, maximum-scale=1.0, initial-scale=1.0">
    <title>Insert title here</title>
    <script type="text/javascript">
    window.onload = function(){
        var canvas = document.getElementById("canvas");
        if(canvas.getContext){
            var draw = canvas.getContext("2d");
            
            var img = new Image();
            img.src = "images/testimage.jpg";
            img.onload = function(){
                //이미지의 원하는 부분만 잘라서 그리기
                //drawImage(이미지객체, 
                //        이미지의 왼위 부분x, 이미지의 왼위 부분y, 이미지의 원하는 가로크기, 이미지의 원하는 세로크기,
                //        사각형 부분x, 사각형 부분y, 가로크기, 세로크기)
                //draw.drawImage(img, 100,100, 300,300, 50,50, 250,300);
                
                //전체 이미지 그리기
                //drawImage(이미지객체, 사각형 왼위 x, 사각형 왼위 y, 가로크기, 세로크기)
                draw.drawImage(img, 50,50250,300);
            }
        }
    }
    </script>
</head>
 
<body>
    <canvas id="canvas" width="300px" height="400px"></canvas>
</body>
</html>
cs



결과

+ Recent posts