HTML5 캔버스를 사용하여 균일한 모션을 얻는 방법
등속 운동: 직선으로 움직이는 물체를 말하며, 동일한 시간 간격에서 물체의 변위는 동일합니다. 실제로 등속선운동은 가속도가 0이 되는 것이 특징입니다. 정의에 따르면 동일한 시간간격 내에서는 속도의 크기와 방향이 동일하다는 것을 알 수 있습니다.
<head> <meta charset='utf-8' /> <style> #canvas { border: 1px dashed #aaa; } </style> <script> window.onload = function () { var oCanvas = document.querySelector("#canvas"), oGc = oCanvas.getContext('2d'), width = oCanvas.width, height = oCanvas.height, x = 0; function drawBall( x, y, cxt ){ cxt.fillStyle = '#09f'; 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 || '#09f'; } 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='utf-8' /> <style> #canvas { border: 1px dashed #aaa; } </style> <script src="./ball.js"></script> <script> window.onload = function () { var oCanvas = document.querySelector("#canvas"), oGc = oCanvas.getContext('2d'), 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='utf-8' /> <style> #canvas { border: 1px dashed #aaa; } </style> <script src="./ball.js"></script> <script> window.onload = function () { var oCanvas = document.querySelector("#canvas"), oGc = oCanvas.getContext('2d'), 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='utf-8' /> <style> #canvas { border: 1px dashed #aaa; } </style> <script src="./ball.js"></script> <script> window.onload = function () { var oCanvas = document.querySelector("#canvas"), oGc = oCanvas.getContext('2d'), 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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

핫 AI 도구

Undresser.AI Undress
사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover
사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool
무료로 이미지를 벗다

Clothoff.io
AI 옷 제거제

AI Hentai Generator
AI Hentai를 무료로 생성하십시오.

인기 기사

뜨거운 도구

메모장++7.3.1
사용하기 쉬운 무료 코드 편집기

SublimeText3 중국어 버전
중국어 버전, 사용하기 매우 쉽습니다.

스튜디오 13.0.1 보내기
강력한 PHP 통합 개발 환경

드림위버 CS6
시각적 웹 개발 도구

SublimeText3 Mac 버전
신 수준의 코드 편집 소프트웨어(SublimeText3)

뜨거운 주제











HTML의 테이블 테두리 안내. 여기에서는 HTML의 테이블 테두리 예제를 사용하여 테이블 테두리를 정의하는 여러 가지 방법을 논의합니다.

HTML 여백-왼쪽 안내. 여기에서는 HTML margin-left에 대한 간략한 개요와 코드 구현과 함께 예제를 논의합니다.

HTML의 Nested Table에 대한 안내입니다. 여기에서는 각 예와 함께 테이블 내에 테이블을 만드는 방법을 설명합니다.

HTML 테이블 레이아웃 안내. 여기에서는 HTML 테이블 레이아웃의 값에 대해 예제 및 출력 n 세부 사항과 함께 논의합니다.

HTML 순서 목록에 대한 안내입니다. 여기서는 HTML Ordered 목록 및 유형에 대한 소개와 각각의 예에 대해서도 설명합니다.

HTML 입력 자리 표시자 안내. 여기서는 코드 및 출력과 함께 HTML 입력 자리 표시자의 예를 논의합니다.

HTML에서 텍스트 이동 안내. 여기서는 Marquee 태그가 구문과 함께 작동하는 방식과 구현할 예제에 대해 소개합니다.

HTML onclick 버튼에 대한 안내입니다. 여기에서는 각각의 소개, 작업, 예제 및 다양한 이벤트의 onclick 이벤트에 대해 설명합니다.
