【Web开发手礼】探索Web开发的魅力(九)-JavaScript(4)常用库

主要介绍了JavaScript的常用库,可以当作学习小笔记收藏一下!!!

文章目录


前言

主要介绍了JavaScript的常用库,可以当作学习小笔记收藏一下!!!


常用库

requestAnimationFrame

requestAnimationFrame(func)

该函数会在下次浏览器刷新页面之前执行一次,通常会用递归写法使其每秒执行60次func函数。调用时会传入一个参数,表示函数执行的时间戳,单位为毫秒。

let step = (timestamp) => {  // 每帧将div的宽度增加1像素
    let div = document.querySelector('div');
    div.style.width = div.clientWidth + 1 + 'px';
    requestAnimationFrame(step);
};

requestAnimationFrame(step);

与setTimeout和setInterval的区别:

  • requestAnimationFrame渲染动画的效果更好,性能更加。
    该函数可以保证每两次调用之间的时间间隔相同,但setTimeout与setInterval不能保证这点。setTmeout两次调用之间的间隔包含回调函数的执行时间;setInterval只能保证按固定时间间隔将回调函数压入栈中,但具体的执行时间间隔仍然受回调函数的执行时间影响。
  • 当页面在后台时,因为页面不再渲染,因此requestAnimationFrame不再执行。但setTimeout与setInterval函数会继续执行。 

Map与Set

 Map

Map 对象保存键值对。

  • 用for...of或者forEach可以按插入顺序遍历。
  • 键值可以为任意值,包括函数、对象或任意基本类型。

常用API:

  • set(key, value):插入键值对,如果key已存在,则会覆盖原有的value
  • get(key):查找关键字,如果不存在,返回undefined
  • size:返回键值对数量
  • has(key):返回是否包含关键字key
  • delete(key):删除关键字key
  • clear():删除所有元素
     

Set

 Set 对象允许你存储任何类型的唯一值,无论是原始值或者是对象引用。

  • 用for...of或者forEach可以按插入顺序遍历。

常用API:

  • add():添加元素
  • has():返回是否包含某个元素
  • size:返回元素数量
  • delete():删除某个元素
  • clear():删除所有元素 

 localStorage

可以在用户的浏览器上存储键值对。

常用API:

  • setItem(key, value):插入
  • getItem(key):查找
  • removeItem(key):删除
  • clear():清空

JSON

JSON对象用于序列化对象、数组、数值、字符串、布尔值和null。

常用API:

  • JSON.parse():将字符串解析成对象
  • JSON.stringify():将对象转化为字符串

日期

 返回值为整数的API,数值为1970-1-1 00:00:00 UTC(世界标准时间)到某个时刻所经过的毫秒数:

  • Date.now():返回现在时刻。
  • Date.parse("2022-04-15T15:30:00.000+08:00"):返回北京时间2022年4月15日 15:30:00的时刻。

与Date对象的实例相关的API:

  • new Date():返回现在时刻。
  • new Date("2022-04-15T15:30:00.000+08:00"):返回北京时间2022年4月15日 15:30:00的时刻。
  • 两个Date对象实例的差值为毫秒数
  • getDay():返回星期,0表示星期日,1-6表示星期一至星期六
  • getDate():返回日,数值为1-31
  • getMonth():返回月,数值为0-11
  • getFullYear():返回年份
  • getHours():返回小时
  • getMinutes():返回分钟
  • getSeconds():返回秒
  • getMilliseconds():返回毫秒 

 WebSocket

与服务器建立全双工连接。

常用API:

  • new WebSocket('ws://localhost:8080');:建立ws连接。
  • send():向服务器端发送一个字符串。一般用JSON将传入的对象序列化为字符串。
  • onopen:类似于onclick,当连接建立时触发。
  • onmessage:当从服务器端接收到消息时触发。
  • close():关闭连接。
  • onclose:当连接关闭后触发。

window

  •  window.open("https://www.acwing.com")在新标签栏中打开页面。
  • location.reload()刷新页面。
  • location.href = "https://www.acwing.com":在当前标签栏中打开页面。

Canvas 

 canvas教程

<canvas>是一个可以使用脚本 (通常为Javascript) 来绘制图形的 HTML 元素。例如,它可以用于绘制图表、制作图片构图或者制作简单的动画。 

示例:

鼠标追踪动画

<!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>Document</title>
    <script>
      var cn;
      //= document.getElementById('cw');
      var c;
      var u = 10;
      const m = {
        x: innerWidth / 2,
        y: innerHeight / 2,
      };
      window.onmousemove = function (e) {
        m.x = e.clientX;
        m.y = e.clientY;
      };
      function gc() {
        var s = "0123456789ABCDEF";
        var c = "#";
        for (var i = 0; i < 6; i++) {
          c += s[Math.ceil(Math.random() * 15)];
        }
        return c;
      }
      var a = [];
      window.onload = function myfunction() {
        cn = document.getElementById("cw");
        c = cn.getContext("2d");

        for (var i = 0; i < 10; i++) {
          var r = 30;
          var x = Math.random() * (innerWidth - 2 * r) + r;
          var y = Math.random() * (innerHeight - 2 * r) + r;
          var t = new ob(
            innerWidth / 2,
            innerHeight / 2,
            5,
            "red",
            Math.random() * 200 + 20,
            2,
          );
          a.push(t);
        }
        //cn.style.backgroundColor = "#700bc8";

        c.lineWidth = "2";
        c.globalAlpha = 0.5;
        resize();
        anim();
      };
      window.onresize = function () {
        resize();
      };
      function resize() {
        cn.height = innerHeight;
        cn.width = innerWidth;
        for (var i = 0; i < 101; i++) {
          var r = 30;
          var x = Math.random() * (innerWidth - 2 * r) + r;
          var y = Math.random() * (innerHeight - 2 * r) + r;
          a[i] = new ob(
            innerWidth / 2,
            innerHeight / 2,
            4,
            gc(),
            Math.random() * 200 + 20,
            0.02,
          );
        }
        //  a[0] = new ob(innerWidth / 2, innerHeight / 2, 40, "red", 0.05, 0.05);
        //a[0].dr();
      }
      function ob(x, y, r, cc, o, s) {
        this.x = x;
        this.y = y;
        this.r = r;
        this.cc = cc;
        this.theta = Math.random() * Math.PI * 2;
        this.s = s;
        this.o = o;
        this.t = Math.random() * 150;

        this.o = o;
        this.dr = function () {
          const ls = {
            x: this.x,
            y: this.y,
          };
          this.theta += this.s;
          this.x = m.x + Math.cos(this.theta) * this.t;
          this.y = m.y + Math.sin(this.theta) * this.t;
          c.beginPath();
          c.lineWidth = this.r;
          c.strokeStyle = this.cc;
          c.moveTo(ls.x, ls.y);
          c.lineTo(this.x, this.y);
          c.stroke();
          c.closePath();
        };
      }
      function anim() {
        requestAnimationFrame(anim);
        c.fillStyle = "rgba(0,0,0,0.05)";
        c.fillRect(0, 0, cn.width, cn.height);
        a.forEach(function (e, i) {
          e.dr();
        });
      }
    </script>
    <style>
      #cw {
        position: fixed;
        z-index: -1;
      }

      body {
        margin: 0;
        padding: 0;
        background-color: rgba(0, 0, 0, 0.05);
      }
    </style>
  </head>
  <body>
    <canvas id="cw"></canvas>
    qwerewr
  </body>
</html>


总结

主要介绍了JavaScript的常用库,可以当作学习小笔记收藏一下!!!

  • 14
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值