05-16 00:01
Notice
Recent Posts
Recent Comments
관리 메뉴

Scientific Computing & Data Science

[Programming / WebApp] Canvas Touch Gesture 예제 본문

Programming/Web App

[Programming / WebApp] Canvas Touch Gesture 예제

cinema4dr12 2016. 8. 3. 17:53

이번 글에서는 HTML5의 Canvas Element의 Touch Gesture를 통해 이미지를 이동하거나 확대(Pinch Zoom)하는 방법에 대하여 알아보고자 한다.

Canvas의 Image Touch Gesture를 위한 소스코드는 https://github.com/rombdn/img-touch-canvas를 이용하였으며, Touch가 안 되는 환경(Desktop without Touch Device)에서는 Mouse를 이용한 Drag를 통하여 이미지 이동이 가능하다.


다음 링크의 프로젝트 파일을 다운받아 테스트 할 수 있다:

img-touch-gesture.zip

img-touch-canvas.js

상기 Github 페이지에서 img-touch-canvas.js를 다운받는다.


Line 15 - 49에서는 ImgTouchCanvas의 개체 속성을 정의하고 있다.

Line 52 - 262에서는 ImgTouchCanvas의 프로토타입을 정의하고 있다.
animate는 애니메이션에 대한 초기화와 애니메이션 프레임 갱신을 위한 개체를 바인딩한다.

getsturePinchZoom는 두 점 이상의 터치를 통해 두 점을 벌려 Zoom In을 하거나 두 점을 오므려 Zoom Out을 할 수 있는 제스쳐를 정의한다.

doZoom은 제스쳐로 정의된 스케일에 따라 Zoom을 실행한다.

doMove는 터치로 오브젝트를 드래그함에 따라 오브젝트 Move를 실행한다.

setEventListeners는 touchStarttouchMove 등 각종 이벤트를 정의한다. 만약 터치를 인식할 수 없는 일반 Desktop PC의 경우, Key Code 입력을 통해 Zoom In/Out 및 마우스 드래그를 통해 오브젝트 이동을 정의한다.

Key Code는 브라우저에 따라 약간씩 다를 수 있으니, 다음 링크를 참고하도록 한다.

http://www.javascripter.net/faq/keycodes.htm


index.html

index.html 파일을 다음과 같이 작성하였다. Canvas 태그를 갖는 4개의 View를 구성하였고, Style은 base.css 파일에 정의하였으며, 각 Canvas에 대한 ID-Image 바인딩을 main.js에 정의하였다.


<html>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <link rel="stylesheet" href="./base.css">
  <title>Touch Gesture Test</title>
</head>

<body>
    <h1>Touch Gesture Test</h1>
    <hr>

    <div class="mycontainer" style="position:absolute; top: 70px; left:4px;">
        <canvas id="top-left-pane" class="mycanvas" style="width: 100%; height: 100%"></canvas>
    </div>

    <div class="mycontainer" style="position:absolute; top: 70px; left:330px;">
        <canvas id="top-right-pane" class="mycanvas" style="width: 100%; height: 100%"></canvas>
    </div>

    <div class="mycontainer" style="position:absolute; top: 240px; left:4px;">
        <canvas id="bottom-left-pane" class="mycanvas" style="width: 100%; height: 100%"></canvas>
    </div>

    <div class="mycontainer" style="position:absolute; top: 240px; left:330px;">
        <canvas id="bottom-right-pane" class="mycanvas" style="width: 100%; height: 100%"></canvas>
    </div>

    <script src="./jquery-3.0.0.min.js"></script>
    <script src="./img-touch-canvas.js"></script>
    <script src="./main.js"></script>
</body>

</html>


base.css

Style은 다음과 같이 정의하였다:


html {
    height: 100%;
    width: 100%;
    overflow: hidden;
    margin: 0px;
}

body {
    height: 100%;
    width: 100%;
    overflow: hidden;
    margin: 0px;
    color: #888888;
    background-color: black;
}

.mycontainer {
  border-style: solid;
  border-width: 2px;
  border-radius: 10px;
  border-color: #004852;
  background-color: #fff;
  width: 320px;
  height: 167px;
}

.mycanvas {
  border-style: solid;
  border-radius: 10px;
  border-width: 3px;
  border-color: #004852;
  background-color: #fff;
}


main.js

main.js에 다음과 같이 Canvas ID-Image 바인딩을 하였다.



Result

이제 작성된 코드의 결과를 테스트 해 보자.



Comments