iT邦幫忙

2021 iThome 鐵人賽

DAY 4
1
Modern Web

JavaScript 嗨起來用 JS 做動畫 ε= ᕕ( ᐛ )ᕗ系列 第 4

Day04 - 微調讓昨天的輪盤轉起來

今天微調讓整個輪盤轉動


wheelCanvas = document.getElementById('wheelCanvas')
ctx = wheelCanvas.getContext('2d');
var ww = wheelCanvas.width = window.innerWidth;
var wh = wheelCanvas.height = window.innerHeight;


var t = 0;
function draw(){
    t++;

    ctx.fillStyle = '#faf7f8';
    ctx.fillRect(0, 0, ww, wh);

    ctx.fillStyle = '#fcd2dc';
    ctx.arc(ww/2 , wh/2, 300, 0, Math.PI * 2);
    ctx.fill();

    ctx.save();
        // 將初始座標 ww/2 , wh/2 設為新座標 0,0 的所在地 
        ctx.translate(ww/2 , wh/2);

        // 座標軸依照時間旋轉
        ctx.rotate(Math.PI * 2 * t / 50);

        ctx.beginPath();
            // 記得要移動到等等要畫的圓心
            ctx.moveTo(0,0)

            // 畫六分之一個圓
            ctx.arc(0, 0, 300, 0, Math.PI * 2 / 6);
            ctx.fillStyle = '#1460db';
            ctx.fill();
        ctx.closePath();

        ctx.beginPath();
            // 記得要移動到等等要畫的圓心
            ctx.moveTo(0,0)

            // 畫六分之一個圓
            ctx.arc(0, 0, 300, Math.PI * 2 / 6, Math.PI * 2 / 6 * 2);
            ctx.fillStyle = '#f72d5c';
            ctx.fill();
        ctx.closePath();
    ctx.restore();
    requestAnimationFrame(draw)
};

draw()

可惡今天時間花太少,明天讓輪盤有按鈕可以控制開始動,與停止動


若內容有出入都在麻煩提點了ε= ᕕ( ᐛ )ᕗ


上一篇
Day03 - 複習 canvas translate 與 rotate
下一篇
Day05 - 讓輪子依隨機速度轉動並漸慢停下來
系列文
JavaScript 嗨起來用 JS 做動畫 ε= ᕕ( ᐛ )ᕗ17
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言