js实现创意时钟时间显示效果代码

代码语言:html

所属分类:其他

代码描述:js实现创意时钟时间显示效果代码

代码标签: 创意 时钟 时间

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

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

<head>

    <meta charset="UTF-8">




    <style>
        html, body {
          margin: 0;
          padding: 0;
          overflow: hidden;
        }
        
        canvas {
          width: 100%;
          height: 100%;
        }
    </style>



</head>

<body>
    <canvas id="render"></canvas>


    <script>
        const canvas = document.getElementById("render");
        const context = canvas.getContext('2d');
        
        const toggleTime = 300; // 300ms
        
        class Easing {
          static easeInOutQuad(x) {
            return x < 0.5 ? 2 * x * x : 1 - Math.pow(-2 * x + 2, 2) / 2;
          }
        
          static easeOutBounce(x) {
            const n1 = 7.5625;
            const d1 = 2.75;
        
            if (x < 1 / d1) {
              return n1 * x * x;
            } else if (x < 2 / d1) {
              return n1 * (x -= 1.5 / d1) * x + 0.75;
            } else if (x < 2.5 / d1) {
              return n1 * (x -= 2.25 / d1) * x + 0.9375;
            } else {
              return n1 * (x -= 2.625 / d1) * x + 0.984375;
            }
          }
        
          static easeInOutBounce(x) {
            return x < 0.5 ?
            (1 - Easing.easeOutBounce(1 - 2 * x)) / 2 :
            (1 + Easing.easeOutBounce(2 * x - 1)) / 2;
          }
        
          static easeInOutCirc(x) {
            return x < 0.5 ?
            (1 - Math.sqrt(1 - Math.pow(2 * x, 2))) / 2 :
            (Math.sqrt(1 - Math.pow(-2 * x + 2, 2)) + 1) / 2;
          }}
        
        
        class RGB {
          static clamp(value, min, max) {
            if (value < min)
            return min;
            if (value > max)
            return max;
            return value;
          }
        
          constructor(r, g, b) {
            this.r = RGB.clamp(r, 0, 255);
            this.g = RGB.clamp(g, 0, 255);
            this.b = RGB.clamp(b, 0, 255);
          }
        
          toString() {
            return `rgb(${this.r}, ${this.g}, ${this.b})`;
          }
        
          clone() {
            return new RGB(this.r, this.g, this.b);
          }}
        ;
        
        class RGBA extends RGB {
          constructor(r, g, b, a = 1) {
            super(r, g, b);
            this.a = RGB.clamp(a, 0, 1);
          }
        
          toString() {
            return `rgba(${this.r}, ${this.g}, ${this.b}, ${this.a})`;
          }
        
          clone() {
            return new RGBA(this.r, this.g, this.b, this.a);
          }}
        ;
        
        class Annotation {
          constructor(text, side, color) {
            this.text = text;
            this.side = side;
            this.color = color.clone();
          }
        
          render(context, position, size) {
            context.save();
        
            context.font = size / 2 + "px sans-serif";
        
            if (this.side === "right" || this.side === "left") {
              context.fillStyle = this.color.toString();
              context.textBaseline = "middle";
              context.textAlign = this.side === "right" ? "left" : "right";
              context.fillText(this.text, position.x + (this.side === "right" ? size : -size), position.y);
            } else if (this.side === "top" || this.side === "bottom") {
              context.translate(position.x, position.y);
              context.rotate(-(Math.PI / 2));
              context.translate(-position.x, -position.y);
              context.fillStyle = this.color.toString();
              context.textBaseline = "middle";
              context.textAlign = this.side === "top" ? "left" : "right";
              context.fillText(this.text, position.x + (this.side === "top" ? size : -size), position.y);
            }
        
            context.restore();
          }}
        ;
        
        class BinaryNumber {
          constructor(value, valueWidth, color, index, total, annotations) {
            this.value = [];
            this.old = [];
            this.cols = [];
            this.anno = annotations || {};
        
            for (let i = 0; i < valueWidth; ++i) {
              this.value.push(0);
              this.old.push(0);
              this.cols.push(color.clone());
            }
        
            this.valueWidth = valueWidth;
            this.setValue(value);
        
            this.bitSize = 30;
            this.bitSpacing = 10;
            this.color = color;
            this.index = index;
            this.total = total;
            this.width = total * this.bitSize + (total - 1) * this.bitSpacing;
            this.height = this.value.length * this.bitSize + (this.value.length - 1) * this.bitSpacing;
            this.offset = this.index * this.bitSize + this.index * this.bitSpacing;
            this.future = 0;
            this.past = 0;
          }
        
          setValue(value) {
            this.old = [...this.value];
            this.value = [...value.toString(2)].map(v => +v);
            this.future = new Date().getTime() + toggleTime;
        
            if (this.value.length > this.valueWidth)
            throw TypeError("invalid value width provided");
        
            for (let i = this.value.length; i < this.valueWidth; ++i)
            this.value.unshift(0);
        
            if (this.value.length !== this.old.length || this.value.length != this.cols.length)
            throw TypeError("catastrophic failure");
          }
        
          step() {
            let now = new Date().getTime();
            let delta = 1;
        
            if (now < this.future) {
              delta = (this.future - now) / toggleTime;
            } else {
              return;
            }
        
            for (let index in this.value) {
              let value = this.value[index];
              let old = this.old[index];
              let color = this.cols[index];
        
              if (value === old) {
                color.a = value === 1 ? 1 : 0;
                continue;
              }
        
              delta = Easing.easeInOutQuad(delta);
        
              color.a = value > old ? 1 - delta : delta;
            }
          }
        
          render(context) {
      .........完整代码请登录后点击上方下载按钮下载查看

网友评论0