DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> HTML基礎知識 >> HTML5詳解
用 Canvas 做一個會動的太陽系:公轉週期與漸層星球
編輯:HTML5詳解     

Canvas 搭配 setInterval 就能做出簡單的動畫。這個範例用「公轉週期」當作旋轉速度依據,把太陽與八顆行星畫成一個持續運轉的太陽系。

各行星的參數設定

每顆星球需要四個資訊:半徑、公轉週期、漸層的亮色與暗色。週期數值直接採用真實天文資料的相對比例,所以水星跑得快、海王星幾乎看不出在動。

星球變量名公轉週期亮色暗色
水星 MercuryMercury87.70#A69697#5c3e40
金星 VenusVenus224.71#c4bbac#1f1315
地球 EarthEarth365.224#78b1e8#050c12
火星 MarsMars686.98#cec9b6#76422d
木星 JupiterJupiter4332.589#c0a48e#322
土星 SaturnSaturn10759.5#f7f9e3#5c4533
天王星 UranusUranus30799.95#a7e1e5#19243a
海王星 NeptuneNeptune60152.95#0661b2#1E3b73

畫布與軌道

<canvas id="canvas" width="1000" height="1000" style="background-color: #000;"></canvas>

先畫出八條同心圓當作軌道參考線:

var canvas = document.getElementById("canvas");
var cxt = canvas.getContext("2d");

function DrawTrack() {
  for (var i = 0; i < 8; i++) {
    cxt.beginPath();
    cxt.arc(500, 500, (i + 1) * 50, 0, Math.PI * 2, false);
    cxt.closePath();
    cxt.strokeStyle = "#fff";
    cxt.stroke();
  }
}

這裡要注意 arc() 的最後兩個參數是「起止角度(弧度)」,一圈是 Math.PI * 2,不是 360。

用建構函數描述一顆星球

把星球的共同行為抽成一個 constructor,每顆行星再各自繼承並帶入自己的參數,這樣的結構最好擴充:

function DrawStar(x, y, radius, cycle, sColor, eColor) {
  this.x = x;               // 相對於太陽中心的座標
  this.y = y;
  this.radius = radius;     // 星球半徑
  this.sColor = sColor;     // 漸層起始色
  this.eColor = eColor;     // 漸層結束色
  this.color = null;        // 漸層物件
  this.time = 0;            // 已走過的「天數」
  this.cycle = cycle;       // 公轉週期

  this.draw = function () {
    cxt.save();
    cxt.translate(500, 500);                                   // 把原點移到太陽位置
    cxt.rotate(this.time * 360 / this.cycle * Math.PI / 180);  // 依週期換算角度
    cxt.beginPath();
    cxt.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
    cxt.closePath();
    this.color = cxt.createRadialGradient(this.x, this.y, 0, this.x, this.y, this.radius);
    this.color.addColorStop(0, this.sColor);
    this.color.addColorStop(1, this.eColor);
    cxt.fillStyle = this.color;
    cxt.fill();
    cxt.restore();
    this.time += 1;
  };
}

這段代碼的巧思在於「用 save/restore 包住 translate 與 rotate」:因為旋轉會影響整個座標系統,若不還原,下一顆星球就會被上一顆的旋轉帶著跑。

定義太陽與八顆行星

function Sun()     { DrawStar.call(this, 0, 0,   20, 0,        "#f00", "#f90"); }
function Mercury() { DrawStar.call(this, 0, -50, 10, 87.70,    "#A69697", "#5c3e40"); }
function Venus()   { DrawStar.call(this, 0, -100, 10, 224.71,  "#c4bbac", "#1f1315"); }
function Earth()   { DrawStar.call(this, 0, -150, 10, 365.224, "#78b1e8", "#050c12"); }
function Mars()    { DrawStar.call(this, 0, -200, 10, 686.98,  "#cec9b6", "#76422d"); }
function Jupiter() { DrawStar.call(this, 0, -250, 10, 4332.589,"#c0a48e", "#322"); }
function Saturn()  { DrawStar.call(this, 0, -300, 10, 10759.5, "#f7f9e3", "#5c4533"); }
function Uranus()  { DrawStar.call(this, 0, -350, 10, 30799.95,"#a7e1e5", "#19243a"); }
function Neptune() { DrawStar.call(this, 0, -400, 10, 60152.95,"#0661b2", "#1E3b73"); }

var s  = new Sun();
var m  = new Mercury();
var v  = new Venus();
var e  = new Earth();
var ma = new Mars();
var j  = new Jupiter();
var sa = new Saturn();
var ur = new Uranus();
var ne = new Neptune();

動畫迴圈

每一幀先把畫布清空、重畫軌道,再依序畫上所有星球。這裡用 setInterval 每 10 毫秒更新一次:

setInterval(function () {
  cxt.clearRect(0, 0, 1000, 1000);
  DrawTrack();
  s.draw();
  m.draw();
  v.draw();
  e.draw();
  ma.draw();
  j.draw();
  sa.draw();
  ur.draw();
  ne.draw();
}, 10);

可以再加強的地方

  • 用 requestAnimationFrame 取代 setInterval,畫面會更順,也比較省電。
  • 幫土星加上一圈 ellipse() 光環,視覺效果立刻升級。
  • 把 time 改成浮點數並乘上真實時間差,公轉速度就不會受裝置效能影響。

這個範例最值得學的不是天文,而是「用旋轉 + 平移來表達週期性運動」這個通用思路——時鐘、跑馬燈、齒輪,本質上都是同一招。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved