Blog Content

    티스토리 뷰

    HTML5 - 09.canvas : 그림판 1차 코드

    반응형
    HTML5 Canvas를 이용하여 그림판을 만들고, 호스팅 서비스에 올려서 모바일로 직접 실행했는데 잘됩니다.

    잘되긴 하는데...모바일에 있는 기본 인터넷 브라우저로 열어서 보니 드래그가 안되고...;;;

    네이버 웹을 통해서 접속하여 확인하니 잘 됩니다...;;;

    (컴퓨터에 익스플로러, 파이어폭스, 크롬에서는 잘 됩니다.)

    이유가 먼지...아시는분 답변 부탁드립니다.

     ex08.Gradation.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
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
        <meta charset="euc-kr" /> 
        <meta name="viewport" content="width=device-width, initial-scale=1" />
    </head>
    <body>
        <table border="1" style="width:500px; height:500px;">
            <tr style="height:80px;">
                <td style="background-color:red; cursor:pointer;" onclick="fn_Color('red');">빨강색</td>
                <td style="background-color:green; cursor:pointer;" onclick="fn_Color('green');">녹색</td>
                <td style="background-color:blue; cursor:pointer;" onclick="fn_Color('blue');">파랑색</td>
                <td style="background-color:yellow; cursor:pointer;" onclick="fn_Color('yellow');">노랑색</td>
                <td style="background-color:pink; cursor:pointer;" onclick="fn_Color('pink');">핑크색</td>
                <td style="background-color:white; cursor:pointer;" onclick="fn_Color('white');">삼각형</td>
                <td style="background-color:white; cursor:pointer;" onclick="fn_Color('sketch');">드래그</td>
                
            </tr>
            <tr style="width:100%; height:420px;">
                <td colspan="5">
                    <canvas id="can1" width="500" height="420" 
                        style="background-color:yellow;cursor:crosshair;">
                    </canvas>
     
                    <script type="text/javascript">
                        var canvas = document.getElementById("can1");
                        var cv = canvas.getContext("2d");
                        var event_Chk;
                        var x, y;
     
                        var state_Coloe = "rgb(0, 0, 0)";
                        var eraser = false;
     
                        function fn_Clear() {
                            cv.clearRect(0, 0, canvas.width, canvas.height);
                        };
     
                        function fn_Color(color) {
                            switch (color)
                            {
                                case "red":
                                    state_Coloe = "rgb(255, 0, 0)";
                                    break;
                                case "green":
                                    state_Coloe = "rgb(29, 219, 22)";
                                    break;
                                case "blue":
                                    state_Coloe = "rgb(1, 0, 255)";
                                    break;
                                case "yellow":
                                    state_Coloe = "rgb(255, 187, 0)";
                                    break;
                                case "pink":
                                    state_Coloe = "rgb(255, 0, 127)";
                                    break;
                                case "white":
                                    fn_Triangle();
                                    break;
                                case "sketch":
                                    fn_Sketch();
                                    break;
                            }
                            alert(state_Coloe);
                        };
     
                        function fn_Sketch() {
                            canvas.onmousedown = function (event) {
                                var x = event.x;
                                var y = event.y;
     
                                cv.beginPath();
     
                                cv.moveTo(x, y);
                                cv.stroke();
                                cv.lineWidth = 3;
                            };
     
                            canvas.onmouseup = function (event) {
                                var x = event.x;
                                var y = event.y;
     
                                cv.lineTo(x, y);
                                cv.stroke();
                                cv.lineWidth = 3;
     
                                cv.closePath();
                            };
                        };
     
                        function fn_Triangle() {
                            cv.beginPath(); //패스를 초기화
     
                            cv.moveTo(50, 0);      //시작위치 X,Y좌표
                            cv.lineTo(0, 50);      //선 그리기
                            cv.lineTo(100, 50);    //선 그리기
     
                            cv.closePath();         //그리기 종료
                            cv.fillStyle = "rgb(0,0,255)";
                            cv.fill();              //삼각형 그리기 렌더링
                        };
     
                        canvas.addEventListener('mousedown'function (event) {
                            event_Chk = true;
     
                            var rect = canvas.getBoundingClientRect();
     
                            x = event.clientX - rect.left;
                            y = event.clientY - rect.top;
     
                            cv.beginPath();
                            cv.moveTo(x, y);
                            cv.strokeStyle = state_Coloe;
                        }, false);
     
                        canvas.addEventListener('mousemove'function (event) {
     
                            if (event_Chk == true) {
                                var rect = canvas.getBoundingClientRect();
     
                                x = event.clientX - rect.left;
                                y = event.clientY - rect.top;
     
                                cv.lineTo(x, y);
                                cv.stroke();
                            }
                        }, false);
     
                        canvas.onmouseup = function () {
                            cv.closePath();
                            event_Chk = false;
                        };
                    </script>
                </td>
            </tr>
            <tr>
                <td style="cursor:pointer;" onclick="fn_Clear();">전체 삭제</td>
                <td></td>
                <td></td>
            </tr>
        </table>
    </body>
    </html>
     

    결과 화면입니다.

    컴퓨터에서 한 결과 이미지

     


    모바일 - 네이버 웹에서 실행 결과

    반응형

    Comments