帮助中心
获取牛币
联系我们
关于我们
版权声明
素材牛首页
微博登录
QQ登录
微信扫码登录
注册
登录
新浪微博
QQ登陆
微信登录
热门搜索:
小程序
支付
Java
后台模板
上传
商城模板
jQuery
手机
Thinkphp
微信
Vue.js
首 页
网页特效
整站源码
PHP实例
网站模版
工具箱
常用代码
论坛
游戏源码
资源分享
当前位置:
首页
»
精选常用代码
» 模拟星空闪烁效果
模拟星空闪烁效果
浏览:715次
-
评论:0次
-
发布时间:2018-09-11
Html
Css
Js
* { margin:0; padding:0; } html,body { width:100%; height:100%; } #canvas { display:block; background-color:#040c11; }
(function(window) { if (window) { window.onload = function() { //获取canvas var canvas = document.getElementById('canvas'); //获取画布 var content = canvas.getContext('2d'); var cw = innerWidth, ch = innerHeight; var dotsArr = []; canvas.width = cw; canvas.height = ch; (function fn() { window.onresize = function() { cw = innerWidth; ch = innerHeight; canvas.width = cw; canvas.height = ch; } //接收一个指向自己的函数,在事件触发的时候执行 })(); function Dot() {}; Dot.prototype = { init: function() { this.w = rand(0, cw); this.h = rand(0, ch); this.r = rand(0.2, 1.5); this.speedX = rand(-1, 1); this.speedY = rand(-1, 1); this.color = 'white' }, draw: function() { content.fillStyle = this.color; content.beginPath(); content.arc(this.w, this.h, this.r, 0, Math.PI * 2); content.fill(); }, move: function() { this.w += this.speedX; this.h += this.speedY; if (this.w < 0 || this.w > cw) { this.speedX *= -1; } if (this.h < 0 || this.h > ch) { this.speedY *= -1; } this.draw(); }, shine: function() { this.r = rand(0.5, 1); this.draw(); } } //生成点对象 function createDots(num) { if (num) { for (var i = num; i > 0; i--) { var dots = new Dot(); dots.init(); dots.draw(); dotsArr.push(dots); } } } function Line() {}; Line.prototype = { //星星之间的连线 initStarLine: function() { this.colorStar = '#6699cc'; this.colorStop = '#9966cc'; }, //鼠标与星星之间的连线 initNewLine: function() { this.colorStar = '#6699cc'; this.colorStop = '#ff6666'; }, drawLine: function(ow, oh, nw, nh) { var dx = ow - nw; var dy = oh - nh; var d = Math.sqrt(dx * dx + dy * dy); if (d < 60) { var line = context.createLinearGradient(ow, oh, nw, nh); context.beginPath(); context.moveTo(ow, oh); //线开始点, context.lineTo(nw, nh); //线结束点 line.addColorStop(0, this.colorStar); line.addColorStop(1, this.colorStop); context.StrokeWidth = 1; context.strokeStyle = line; context.stroke(); context.restore(); } } } function rand(min, max) { return (Math.random() * (max - min)) + min; } createDots(400); // setTimeout(function(){ //递归调用setTimeout // content.clearRect(0,0,cw,ch); // for(var i of dotsArr){ // i.move(); // // for(var j=0;j
↑上面代码改变,会自动显示代码结果
jQuery调用版本:
1.11.3
<!doctype html><html><head><meta charset="utf-8"><title>模拟星空闪烁效果-www.sucainiu.com</title><script src="https://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script><style>
</style></head><body>
<script>
</script>
</body></html>
立即下载
收藏
积分说明:注册即送10牛币,每日签到可获得5牛币,成为VIP会员可永久免牛币下载!
充值积分
充值会员
更多说明»
代码描述:使用原生的js实现星空动画
常用的星星评分效果
文字单行滚动效果
讨论这个常用代码(0)
回答他人问题或分享心得会奖励牛币
〒_〒 居然一个评论都没有……
文明上网,理性发言! 😉 阿里云幸运券,
戳我领取
发表评论
提交回复
我的积分余额:
0
已下载次数:
715
所需牛币:
5
开始下载
牛币获取:
签到、评论、充值
» 在线充值
(10牛币=1元)
成为VIP可永久免积分下载全部常用代码
» 查看详情
VIP
客服
签到
充值