首頁 web前端 js教程 如何用html5 canvas達到等速運動

如何用html5 canvas達到等速運動

Oct 09, 2017 am 10:19 AM
canvas h5 html5

勻速運動:指的是物體在一條直線上運動,物體在任何相等時間間隔內通過的位移都是相等的。其實就是勻速直線運動,它的特徵是加速度為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=canvas高度的一半,以每幀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 canvas達到等速運動的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您聽不到任何人,如何修復音頻
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解鎖Myrise中的所有內容
4 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

HTML 中的表格邊框 HTML 中的表格邊框 Sep 04, 2024 pm 04:49 PM

HTML 表格邊框指南。在這裡,我們以 HTML 中的表格邊框為例,討論定義表格邊框的多種方法。

HTML 左邊距 HTML 左邊距 Sep 04, 2024 pm 04:48 PM

HTML 左邊距指南。在這裡,我們討論 HTML margin-left 的簡要概述及其範例及其程式碼實作。

HTML 中的巢狀表 HTML 中的巢狀表 Sep 04, 2024 pm 04:49 PM

這是 HTML 中巢狀表的指南。這裡我們討論如何在表中建立表格以及對應的範例。

HTML 表格佈局 HTML 表格佈局 Sep 04, 2024 pm 04:54 PM

HTML 表格佈局指南。在這裡,我們詳細討論 HTML 表格佈局的值以及範例和輸出。

HTML 輸入佔位符 HTML 輸入佔位符 Sep 04, 2024 pm 04:54 PM

HTML 輸入佔位符指南。在這裡,我們討論 HTML 輸入佔位符的範例以及程式碼和輸出。

HTML 有序列表 HTML 有序列表 Sep 04, 2024 pm 04:43 PM

HTML 有序列表指南。在這裡我們也分別討論了 HTML 有序列表和類型的介紹以及它們的範例

在 HTML 中移動文字 在 HTML 中移動文字 Sep 04, 2024 pm 04:45 PM

HTML 中的文字移動指南。在這裡我們討論一下marquee標籤如何使用語法和實作範例。

HTML onclick 按鈕 HTML onclick 按鈕 Sep 04, 2024 pm 04:49 PM

HTML onclick 按鈕指南。這裡我們分別討論它們的介紹、工作原理、範例以及各個事件中的onclick事件。

See all articles