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



결과

canvas 에 그라디언트 적용하기



html5)

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
42
43
44
45
46
47
<!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.rect(50,50,200,100);            
            //선형 그라디언트 생성
            //createLinearGradient(시작점x, 시작점y, 끝점x, 끝점y)
            var gradient = draw.createLinearGradient(50,100,250,100);
            gradient.addColorStop(0"red");
            gradient.addColorStop(0.5"green");
            gradient.addColorStop(1"blue");
            //그라디언트로 채우기
            draw.fillStyle = gradient;
            draw.fill();
            
            
            draw.beginPath();
            draw.rect(50,200,200,100);            
            //원형 그라디언트 생성
            //createRadialGradient(시작원 중심점x, 시작원 중심점y, 시작원 반지름, 끝원 중심점x, 끝원 중심점 y, 끝원 반지름)
            var gradient = draw.createRadialGradient(150,250,0,150,250,100);
            gradient.addColorStop(0"red");
            gradient.addColorStop(0.5"green");
            gradient.addColorStop(1"blue");
            //그라디언트로 채우기
            draw.fillStyle = gradient;
            draw.fill();
        }
    }
    </script>
</head>
 
<body>
    <canvas id="canvas" width="300px" height="400px"></canvas>
</body>
</html>
cs



결과

canvas 의 clearRect : 해당하는 영역만큼 지운다



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
<!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();
            //rect(왼쪽위 x, 왼쪽위 y, 가로크기, 세로크기)
            draw.rect(50,50,200,200);
            draw.lineWidth = 10;
            draw.strokeStyle = "green";
            draw.fillStyle = "#6799FF";
            //fill : 채우기
            draw.fill();
            //stroke : 선그리기
            draw.stroke();
            
            
            draw.beginPath();
            //clearRect(왼쪽위 x, 왼쪽위 y, 가로크기, 세로크기) : 해당하는 영역만큼 지운다
            draw.clearRect(100,100,100,100);
            draw.fill();
        }
    }
    </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
<!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();
            //rect(왼쪽위 x, 왼쪽위 y, 가로크기, 세로크기)
            draw.rect(50,50,200,200);
            draw.lineWidth = 10;
            draw.strokeStyle = "green";
            draw.fillStyle = "#6799FF";
            //fill : 채우기
            draw.fill();
            //stroke : 선그리기
            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
<!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.moveTo(50,50);
            draw.lineTo(250,50);
            draw.lineTo(150,200);
            //closePath : 마지막 지점과 처음 지점을 연결
            draw.closePath();
            draw.lineWidth = 10;
            draw.strokeStyle = "green";
            draw.stroke();
        }
    }
    </script>
</head>
 
<body>
    <canvas id="canvas" width="300px" height="400px"></canvas>
</body>
</html>
cs



결과

+ Recent posts