欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript canvas動(dòng)畫實(shí)現(xiàn)時(shí)鐘效果

 更新時(shí)間:2020年02月10日 15:48:57   作者:crazy的藍(lán)色夢想  
這篇文章主要為大家詳細(xì)介紹了JavaScript canvas動(dòng)畫實(shí)現(xiàn)時(shí)鐘效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了canvas動(dòng)畫實(shí)現(xiàn)時(shí)鐘效果的具體代碼,供大家參考,具體內(nèi)容如下

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>時(shí)鐘特效</title>
</head>
<body>
  <canvas width="150" height="150" id="canvas"></canvas>
</body>
</html>
<script>
  clock();// 顯示
  setInterval(clock,1000);// 每一秒重繪一次,達(dá)到轉(zhuǎn)動(dòng)效果
  function clock(){
    var now = new Date();// 得到當(dāng)前日期與時(shí)間
    var second = now.getSeconds(),
      min = now.getMinutes(),
      hour = now.getHours();// 得到時(shí)分秒
      hour = hour > 12?hour-12:hour;
    var canvas = document.getElementById('canvas');
    var ctx = canvas.getContext('2d');
    ctx.clearRect(0,0,150,150);// 初始化畫布
    ctx.save();
    ctx.translate(75,75);// 平移坐標(biāo)原點(diǎn)
    ctx.scale(0.4,0.4);//縮放效果
    ctx.rotate(-Math.PI/2);// 將x軸旋轉(zhuǎn)-90
    ctx.strokeStyle = 'black';
    ctx.fillStyle = 'black';
    ctx.lineWidth = 8;
    ctx.lineCap = 'round';

    // 顯示時(shí)針刻度
    ctx.save();
    ctx.beginPath();
    for(var i = 0;i<12;i++)
    {
      ctx.rotate(Math.PI/6);
      ctx.moveTo(100,0);
      ctx.lineTo(120,0);
    }
    ctx.stroke();
    ctx.closePath();
    ctx.restore();// 恢復(fù)
    ctx.save();
    
    // 顯示秒針刻度
    ctx.beginPath();
    ctx.lineWidth = 5;
    for(var i = 0;i < 60; i++)
    {
      if(i % 5 != 0)
      {
        ctx.moveTo(117,0);
        ctx.lineTo(120,0);
      }
      ctx.rotate(Math.PI/30);// 轉(zhuǎn)6度
    }
    ctx.stroke();
    ctx.closePath();
    ctx.restore();// 恢復(fù)
    ctx.save();

    // 繪制時(shí)針
    ctx.beginPath();
    ctx.rotate((Math.PI / 6)*hour + (Math.PI/360)*min + (Math.PI /21600)*second)//時(shí)針當(dāng)前指向的位置
    ctx.lineWidth = 14;
    ctx.moveTo(-20,0);
    ctx.lineTo(75,0);
    ctx.stroke();
    ctx.closePath();
    ctx.restore();//恢復(fù)
    ctx.save();

    // 繪制分針
    ctx.beginPath();
    ctx.strokeStyle = 'black';
    ctx.lineWidth = 10;
    ctx.rotate((Math.PI/30)*min + (Math.PI/1800)*second);// 分針當(dāng)前的位置
    ctx.moveTo(-28,0);
    ctx.lineTo(102,0);
    ctx.stroke();
    ctx.closePath();
    ctx.restore();//恢復(fù)
    ctx.save();


    // 繪制秒針
    ctx.beginPath();
    ctx.rotate(Math.PI/30*second);
    ctx.strokeStyle = '#D40000';
    ctx.lineWidth = 6;
    ctx.moveTo(-30,0);
    ctx.lineTo(83,0);
    ctx.stroke();
    ctx.closePath();
    ctx.restore();//恢復(fù)
    ctx.save();

    //繪制表框
    ctx.beginPath();
    ctx.lineWidth = 4;
    ctx.strokeStyle = '#325Fa2';
    ctx.arc(0,0,142,0,Math.PI*2,true);//半徑142
    ctx.stroke();
    ctx.closePath();
    ctx.restore()//恢復(fù)
    ctx.restore()//恢復(fù)


  }
</script>

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論