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



결과

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");
            
            //조절점이 1개
            draw.beginPath();
            draw.moveTo(50,50);
            //quadraticCurveTo(조절점x, 조절점y, 선의 끝점x, 선의 끝점y)
            draw.quadraticCurveTo(15020025050);
            draw.lineWidth = 10;
            draw.strokeStyle = "green";
            draw.stroke();
            
            
            //조절점이 2개
            draw.beginPath();
            draw.moveTo(50,200);
            //bezierCurveTo(조절점1x, 조절점1y, 조절점2x, 조절점2y, 선의 끝점x, 선의 끝점y)
            draw.bezierCurveTo(100,300200,100250,200);
            draw.lineWidth = 10;
            draw.strokeStyle = "blue";
            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
<!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();
            //arc(중심점x, 중심점y, 반지름, 시작각도, 끝각도, 방향)
            //true : 반시계 방향 , false : 시계 방향
            draw.arc(100100500, Math.PI*2true);
            draw.lineWidth = 10;
            draw.strokeStyle = "#00f";
            draw.stroke();
        }
    }
    </script>
</head>
 
<body>
    <canvas id="canvas" width="300px" height="300px"></canvas>
</body>
</html>
cs



결과

canvas 로 line 그리기



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
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Insert title here</title>
    <script type="text/javascript">
    window.onload = function(){
        /* 
        beginPath : 그리기 시작
        moveTo(x, y) : 선이 시작될 좌표
        lineTo(x, y) : 선이 이동할 좌표
        lineWidth : 선 두께
        lineCap : 선의 끝타입 (butt | round | square)
        lineJoin : 선의 연결부분 (miter | round | bevel)
         */
        var canvas = document.getElementById("canvas");
        if(canvas.getContext){
            var draw = canvas.getContext("2d");
            
            draw.beginPath();
            draw.moveTo(100100);
            draw.lineTo(200100);
            draw.lineTo(200200);
            draw.lineWidth = 10;
            draw.lineCap = "round";
            draw.lineJoin = "round";
            draw.strokeStyle = "green";
            draw.stroke();
        }
    }
    </script>
</head>
 
<body>
    <canvas id="canvas" width="300px" height="300px"></canvas>
</body>
</html>
cs



결과

audio : 사운드 파일 재생하기


 

MP3 

Wav 

Ogg 

IE 

파이어폭스 

크롬 

사파리 

오페라 



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>
    <!-- 
    autoplay : 자동으로 오디오 재생
    controls : 컨트롤바 나타냄
    loop : 반복재생
    src : 경로
    autobuffer : 사용자가 플레이하기전 미리 다운로드 할지 여부
     -->
    <audio controls>
        <source src="images/sound.MP3" type="audio/mpeg">
        <p>이 브라우저는 audio 요소를 지원하지 않습니다.</p>
    </audio>
</body>
</html>
cs



결과

video : 동영상 파일 재생하기


 

MP4 

WebM 

OGV 

IE 

파이어폭스 

크롬 

사파리 

오페라 




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
<!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>
    <!-- 
    autoplay : 자동으로 비디오 실행
    controls : 컨트롤바 나타남
    width : 가로크기
    height : 세로크기
    loop : 반복재생
    poster : 비디오가 시작되기전 보여질 이미지
    src : 경로
    autobuffer : 사용자가 플레이하기전 미리 다운로드 할지 여부
     -->
    <video width="300px" height="300px" controls>
        <source src="images/example.mov" type="video/webm">
        <p>이 브라우저는 video 요소를 지원하지 않습니다.</p>
    </video>
</body>
</html>
cs



결과

embed : url로 된 동영상 재생하기



html)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!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>
    <embed src="https://www.youtube.com/embed/NjcDxJxYzAU?ecver=2" width="300px" height="300px">
</body>
</html>
cs



결과

유튜브 동영상이 재생된다

+ Recent posts