博客
关于我
跟随鼠标移动的星星✩直接在页面引用✧✧✧
阅读量:358 次
发布时间:2019-03-04

本文共 1722 字,大约阅读时间需要 5 分钟。

效果✩:

在这里插入图片描述

直接在你的页面引用方法✩:

1.新建一个名为 canvasStar.js的文件:

在这里插入图片描述

2.右键打开该 js 文件,把下面代码复制进去:

window.addEventListener('load',()=>{       var canvas = document.querySelector("#canvas");    var ctx = canvas.getContext("2d");          window.onresize=resizeCanvas;    function resizeCanvas(){          canvas.width=window.innerWidth;       canvas.height=window.innerHeight;    }       resizeCanvas();     canvas.style.cssText = `    position: fixed;    z-index: 1000;    pointer-events: none;    `    //    var arr = [];    var colours =["#ffff00","#66ffff","#3399ff","#99ff00","#ff9900"];        //    window.addEventListener('mousemove', e=>{                           arr.push({                   x:e.clientX,                y:e.clientY,                r:Math.random()*0.5+1.5,                td:Math.random()*4-2,                dx:Math.random()*2-1,                dy:Math.random()*1+1,                rot: Math.random()*90+90,                color: colours[Math.floor(Math.random()*colours.length)]            });               })    //    function star(x,y,r,l,rot){          ctx.beginPath();        for(let i=0;i<5;i++){                       ctx.lineTo(Math.cos((18 + i*72 -rot)*Math.PI/180)*r+x,           -Math.sin((18 + i*72 - rot)*Math.PI/180)*r+y);           ctx.lineTo(Math.cos((54+i*72-rot)*Math.PI/180)*l+x               ,-Math.sin((54+i*72 -rot)*Math.PI/180)*l+y);                     }        ctx.closePath();       }    //    function draw(){           for(let i=0;i
{ ctx.clearRect(0,0,canvas.width,canvas.height); draw(); update(); },20)})

3. 在你想要的显示效果的页面插入该js文件:

如在这引入,src写上文件在的路径:

在这里插入图片描述

4.在body里添该加标签:

如:

在这里插入图片描述
以上就完成了~

总结:

这样在你的网页就可以直接看到跟随鼠标移动的小星星效果了,如果需要解释代码意思和制作过程的留言,我可以下次出篇文章详细说说怎么做~

在这里插入图片描述

其它文章:

…等

转载地址:http://rtir.baihongyu.com/

你可能感兴趣的文章
MYSQL从入门到精通(一)
查看>>
MYSQL从入门到精通(二)
查看>>
mysql以下日期函数正确的_mysql 日期函数
查看>>
mysql以服务方式运行
查看>>
mysql优化--索引原理
查看>>
MySQL优化之BTree索引使用规则
查看>>
MySQL优化之推荐使用规范
查看>>
Webpack Critical CSS 提取与内联教程
查看>>
mysql优化概述(范式.索引.定位慢查询)
查看>>
MySQL优化的一些需要注意的地方
查看>>
mysql优化相关
查看>>
MySql优化系列-优化版造数据(存储过程+函数+修改存储引擎)-2
查看>>
MySql优化系列-进阶版造数据(load data statment)-3
查看>>
MySql优化系列-造数据(存储过程+函数)-1
查看>>
MySQL优化配置详解
查看>>
Mysql优化高级篇(全)
查看>>
mysql会员求积分_MySql-统计所有会员的最高前10次的积分和
查看>>
mysql会对联合索性排序优化_MySQL索引优化实战
查看>>
MySQL作为服务端的配置过程与实际案例
查看>>