canvas模拟二进制时钟走动效果代码

代码语言:html

所属分类:动画

代码描述:canvas模拟二进制时钟走动效果代码

代码标签: 时钟 走动 效果

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >

<head>

  <meta charset="UTF-8">

  
  
<style>
body, html {
  width: 100%;
  height: 100%;
}

body {
  margin: 0;
  position: relative;
}

canvas {
  margin: 0;
  position: absolute;
}
</style>


</head>

<body translate="no" >
  <canvas id="clock-face"></canvas>
<canvas id="clock-hands"></canvas>

  
      <script>
window.onload = function () {
  const cf = document.getElementById("clock-face"),
  cf$ = cf.getContext("2d"),
  ch = document.getElementById("clock-hands"),
  ch$ = ch.getContext("2d"),
  // firstColor = '#f44242',
  // secondColor = '#223235',
  // background = '#0f0f0f',
  // handsColor = '#ffffff', // red blue black theme

  // firstColor = '#ff00a5',
  // secondColor = '#2b103a',
  // background = '#0f0f0f',
  // handsColor = '#0087ff', //cold purple theme

  firstColor = "#17b9dc",
  secondColor = "#223235",
  background = "#0f0f0f",
  handsColor = "#ffffff", //dark theme
  sixtieth = 2 * Math.PI / 60,
  twelfth = sixtieth * 5;

  let w = cf.width = ch.width = window.innerWidth,
  h = cf.height = ch.height = window.innerHeight,
  shortestSide = w <= h ? w : h,
  i,
  r,
  v,
  currentAngle,
  handsWidth,
  time,
  secondsAngle,
  minutesAngle,
  hoursAngle,
  timeElapsed,
  last,
  clockRadius = shortestSide / 2 - shortestSide / 20;

  function drawface() {
    cf$.fillStyle = background;
    cf$.fillRect(0, 0, cf$.canvas.width, cf$.canvas.height);

    for (r = 1; r <= 6; r++) {
      cf$.fillStyle = firstColor;
      v = Math.pow(2, r - 1);

      for (i = 0; i < 60; i++) {
        currentAngle = i * sixtieth - Math.PI / 2;

        if (i % v === 0) {
          cf$.fillStyle = cf$.fillStyle === firstColor ?
          secondColor :
          firstColor;
        }

        cf$.beginPath();
        cf$.arc(
        cf$.canvas.width / 2 +
        Math.cos(currentAngle) * (clockRadius - r * clockRadius / 15),
        cf$.canvas.height / 2 +
        Math.sin(currentAngle) * (clockRadius - r * clockRadius / 15),
        clockRadius / 40,
        0,
        Math.PI * 2);

        cf$.fill();
      }
    }

    for (r = 1; r <= 4; r++) {
      cf$.fillStyle = firstColor;
      v = Math.pow(2, r - 1);

      for (i = 0; i < 12; i++) {
        currentAngle = i * twelfth - Math.PI / 2;

        if (i % v === 0) {
          cf$.fillStyle = cf$.fillStyle === firstColor ?
          secondColor :
          firstColor;
        }

        cf$.beginPath();
        cf$.arc(
        cf$.canvas.width / 2 +
        Math.cos(currentAngle) * (clockRadius / 1.8 - r * clockRadius / 11),
        cf$.canvas.height / 2 +
        Math.sin(currentAngle) * (clockRadius / 1.8 - r * clockRadius / 11),
        clock.........完整代码请登录后点击上方下载按钮下载查看

网友评论0