HTML5 캔버스를 사용하여 균일한 모션을 얻는 방법

一个新手
풀어 주다: 2017-10-09 10:19:35
원래의
2048명이 탐색했습니다.

등속 운동: 직선으로 움직이는 물체를 말하며, 동일한 시간 간격에서 물체의 변위는 동일합니다. 실제로 등속선운동은 가속도가 0이 되는 것이 특징입니다. 정의에 따르면 동일한 시간간격 내에서는 속도의 크기와 방향이 동일하다는 것을 알 수 있습니다.


<head>
    <meta charset=&#39;utf-8&#39; />
    <style>
        #canvas {
            border: 1px dashed #aaa;
        }
    </style>
    <script>
        window.onload = function () {
            var oCanvas = document.querySelector("#canvas"),
                oGc = oCanvas.getContext(&#39;2d&#39;),
                width = oCanvas.width, height = oCanvas.height,
                x = 0;
            function drawBall( x, y, cxt ){
                cxt.fillStyle = &#39;#09f&#39;;
                cxt.beginPath();
                cxt.arc( x, y, 20, 0, 2 * Math.PI );
                cxt.closePath();
                cxt.fill();
            }
            ( function linear(){
                oGc.clearRect( 0, 0, width, height );
                drawBall( x, height / 2, oGc );
                x += 2;
                console.log( x );
                requestAnimationFrame( linear );
            } )();
        }
    </script>
</head>
<body>
    <canvas id="canvas" width="1200" height="600"></canvas>
</body>
로그인 후 복사

위의 예에서는 반경이 20px인 작은 공이 x=0, y=캔버스 높이의 절반에서 프레임당 2px의 속도로 오른쪽으로 균일하게 이동할 수 있습니다.

우리는 작은 공을 캡슐화할 수 있습니다. 공을 개체로 변환:

ball.js 파일:

function Ball( x, y, r, color ){
    this.x = x || 0;
    this.y = y || 0;
    this.radius = r || 20;
    this.color = color || &#39;#09f&#39;;
}
Ball.prototype = {
    constructor : Ball,
    stroke : function( cxt ){
        cxt.strokeStyle = this.color;
        cxt.beginPath();
        cxt.arc( this.x, this.y, this.radius, 0, 2 * Math.PI );
        cxt.closePath();
        cxt.stroke();
    },
    fill : function( cxt ){
        cxt.fillStyle = this.color;
        cxt.beginPath();
        cxt.arc( this.x, this.y, this.radius, 0, 2 * Math.PI );
        cxt.closePath();
        cxt.fill();
    }
}
로그인 후 복사

이 공 개체는 위치 반경과 색상을 사용자 정의할 수 있으며 두 가지 렌더링 방법(획 및 채우기)을 지원합니다.


<head>
    <meta charset=&#39;utf-8&#39; />
    <style>
        #canvas {
            border: 1px dashed #aaa;
        }
    </style>
    <script src="./ball.js"></script>
    <script>
        window.onload = function () {
            var oCanvas = document.querySelector("#canvas"),
                oGc = oCanvas.getContext(&#39;2d&#39;),
                width = oCanvas.width, height = oCanvas.height,
                ball = new Ball( 0, height / 2 );
            (function linear() {
                oGc.clearRect(0, 0, width, height);
                ball.fill( oGc );
                ball.x += 2;
                requestAnimationFrame(linear);
            })();
        }
    </script>
</head>

<body>
    <canvas id="canvas" width="1200" height="600"></canvas>
</body>
로그인 후 복사

균일한 슬래시 이동:


<head>
    <meta charset=&#39;utf-8&#39; />
    <style>
        #canvas {
            border: 1px dashed #aaa;
        }
    </style>
    <script src="./ball.js"></script>
    <script>
        window.onload = function () {
            var oCanvas = document.querySelector("#canvas"),
                oGc = oCanvas.getContext(&#39;2d&#39;),
                width = oCanvas.width, height = oCanvas.height,
                ball = new Ball( 0, height );
            (function linear() {
                oGc.clearRect(0, 0, width, height);
                ball.fill( oGc );
                ball.x += 2;
                ball.y -= 1;
                requestAnimationFrame(linear);
            })();
        }
    </script>
</head>

<body>
    <canvas id="canvas" width="1200" height="600"></canvas>
</body>
로그인 후 복사

모든 방향의 등속 운동(속도 분해)

<head>
    <meta charset=&#39;utf-8&#39; />
    <style>
        #canvas {
            border: 1px dashed #aaa;
        }
    </style>
    <script src="./ball.js"></script>
    <script>
        window.onload = function () {
            var oCanvas = document.querySelector("#canvas"),
                oGc = oCanvas.getContext(&#39;2d&#39;),
                width = oCanvas.width, height = oCanvas.height,
                ball = new Ball( 0, 0 ),
                speed = 5,
                vx = speed * Math.cos( 10 * Math.PI / 180 ),
                vy = speed * Math.sin( 10 * Math.PI / 180 );
                
            (function linear() {
                oGc.clearRect(0, 0, width, height);
                ball.fill( oGc );
                ball.x += vx;
                ball.y += vy;
                requestAnimationFrame(linear);
            })();
        }
    </script>
</head>
<body>
    <canvas id="canvas" width="1200" height="600"></canvas>
</body>
로그인 후 복사

위 내용은 HTML5 캔버스를 사용하여 균일한 모션을 얻는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!