[趣味代码] 全员恶人闪电文字特效
作者:精品下载站 日期:2018-10-15 04:02:36 浏览:2445 分类:站长帮
效果图:
代码演示:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>闪电绘制文字动画特效</title> <style> .page-thunder-to-text { position: relative; overflow: hidden; } .page-thunder-to-text canvas { display: block; } .page-thunder-to-text input { position: absolute; bottom:50px; left:0; right:0; display: block; outline: none; background-color: rgba(38,50,56,0.2); color:#ffffff; border: none; width:50%; min-width:500px; max-width:100%; margin:auto; height:60px; line-height:60px; font-size:40px; padding:020px; } .page-thunder-to-text input:hover,.page-thunder-to-text input:focus { border:1px solid rgba(38,50,56,0.6); } .page-thunder-to-text input::-webkit-input-placeholder { color: rgba(255,255,255,0.1); } </style> </head> <body> <div class="page page-thunder-to-text"> <input id="input" type="text" maxlength="24" placeholder="星城哈哈 - 一个有温度的资源网站"> <canvas id="canvas"></canvas> </div> <script> let canvas, ctx, w, h, thunder, text, particles, input; functionThunder(options){ options = options ||{}; this.lifespan = options.lifespan ||Math.round(Math.random()*10+10); this.maxlife =this.lifespan; this.color = options.color ||'#fefefe'; this.glow = options.glow ||'#2323fe'; this.x = options.x ||Math.random()* w; this.y = options.y ||Math.random()* h; this.width = options.width ||2; this.direct = options.direct ||Math.random()*Math.PI *2; this.max = options.max ||Math.round(Math.random()*10+20); this.segments =[...newArray(this.max)].map(()=>{ return{ direct:this.direct +(Math.PI *Math.random()*0.2-0.1), length:Math.random()*20+80, change:Math.random()*0.04-0.02 }; }); this.update =function(index, array){ this.segments.forEach(s =>{(s.direct += s.change)&&Math.random()>0.96&&(s.change *=-1)}); (this.lifespan >0&&this.lifespan--)||this.remove(index, array); } this.render =function(ctx){ if(this.lifespan <=0)return; ctx.beginPath(); ctx.globalAlpha =this.lifespan /this.maxlife; ctx.strokeStyle =this.color; ctx.lineWidth =this.width; ctx.shadowBlur =32; ctx.shadowColor =this.glow; ctx.moveTo(this.x,this.y); let prev ={ x:this.x, y:this.y }; this.segments.forEach(s =>{ const x = prev.x +Math.cos(s.direct)* s.length; const y = prev.y +Math.sin(s.direct)* s.length; prev ={ x: x, y: y }; ctx.lineTo(x, y); }); ctx.stroke(); ctx.closePath(); ctx.shadowBlur =0; const strength =Math.random()*80+40; const light = ctx.createRadialGradient(this.x,this.y,0,this.x,this.y, strength); light.addColorStop(0,'rgba(250, 200, 50, 0.6)'); light.addColorStop(0.1,'rgba(250, 200, 50, 0.2)'); light.addColorStop(0.4,'rgba(250, 200, 50, 0.06)'); light.addColorStop(0.65,'rgba(250, 200, 50, 0.01)'); light.addColorStop(0.8,'rgba(250, 200, 50, 0)'); ctx.beginPath(); ctx.fillStyle = light; ctx.arc(this.x,this.y, strength,0,Math.PI *2); ctx.fill(); ctx.closePath(); } this.remove =function(index, array){ array.splice(index,1); } } functionSpark(options){ options = options ||{}; this.x = options.x || w *0.5; this.y = options.y || h *0.5; this.v = options.v ||{ direct:Math.random()*Math.PI *2, weight:Math.random()*14+2, friction:0.88}; this.a = options.a ||{ change:Math.random()*0.4-0.2, min:this.v.direct -Math.PI *0.4, max:this.v.direct +Math.PI *0.4}; this.g = options.g ||{ direct:Math.PI *0.5+(Math.random()*0.4-0.2), weight:Math.random()*0.25+0.25}; this.width = options.width ||Math.random()*3; this.lifespan = options.lifespan ||Math.round(Math.random()*20+40); this.maxlife =this.lifespan; this.color = options.color ||'#feca32'; this.prev ={ x:this.x, y:this.y }; this.update =function(index, array){ this.prev ={ x:this.x, y:this.y }; this.x +=Math.cos(this.v.direct)*this.v.weight; this.x +=Math.cos(this.g.direct)*this.g.weight; this.y +=Math.sin(this.v.direct)*this.v.weight; this.y +=Math.sin(this.g.direct)*this.g.weight; this.v.weight >0.2&&(this.v.weight *=this.v.friction); this.v.direct +=this.a.change; (this.v.direct >this.a.max ||this.v.direct <this.a.min)&&(this.a.change *=-1); this.lifespan >0&&this.lifespan--; this.lifespan <=0&&this.remove(index, array); } this.render =function(ctx){ if(this.lifespan <=0)return; ctx.beginPath(); ctx.globalAlpha =this.lifespan /this.maxlife; ctx.strokeStyle =this.color; ctx.lineWidth =this.width; ctx.moveTo(this.x,this.y); ctx.lineTo(this.prev.x,this.prev.y); ctx.stroke(); ctx.closePath(); } this.remove =function(index, array){ array.splice(index,1); } } functionParticles(options){ options = options ||{}; this.max = options.max ||Math.round(Math.random()*10+10); this.sparks =[...newArray(this.max)].map(()=>newSpark(options)); this.update =function(){ this.sparks.forEach((s, i)=> s.update(i,this.sparks)); } this.render =function(ctx){ this.sparks.forEach(s => s.render(ctx)); } } functionText(options){ options = options ||{}; const pool = document.createElement('canvas'); const buffer = pool.getContext('2d'); pool.width = w; buffer.fillStyle ='#000000'; buffer.fillRect(0,0, pool.width, pool.height); this.size = options.size ||100; this.copy =(options.copy ||`Hello!`)+' '; this.color = options.color ||'#cd96fe'; this.delay = options.delay ||5; this.basedelay =this.delay; buffer.font =`${this.size}px ComicSans MS`; this.bound = buffer.measureText(this.copy); this.bound.height =this.size *1.5; this.x = options.x || w *0.5-this.bound.width *0.5; this.y = options.y || h *0.5-this.size *0.5; buffer.strokeStyle =this.color; buffer.strokeText(this.copy,0,this.bound.height *0.8); this.data = buffer.getImageData(0,0,this.bound.width,this.bound.height); this.index =0; this.update =function(){ if(this.index >=this.bound.width){ this.index =0; return; } const data =this.data.data; for(let i =this.index *4; i < data.length; i +=(4*this.data.width)){ const bitmap = data[i]+ data[i +1]+ data[i +2]+ data[i +3]; if(bitmap >255&&Math.random()>0.96){ const x =this.x +this.index; const y =this.y +(i /this.bound.width /4); thunder.push(newThunder({ x: x, y: y })); Math.random()>0.5&& particles.push(newParticles({ x: x, y: y })); } } if(this.delay--<0){ this.index++; this.delay +=this.basedelay; } } this.render =function(ctx){ ctx.putImageData(this.data,this.x,this.y,0,0,this.index,this.bound.height); } } function loop(){ update(); render(); requestAnimationFrame(loop); } function update(){ text.update(); thunder.forEach((l, i)=> l.update(i, thunder)); particles.forEach(p => p.update()); } function render(){ ctx.globalCompositeOperation ='source-over'; ctx.globalAlpha =1; ctx.fillStyle ='#000000'; ctx.fillRect(0,0, w, h); // ctx.globalCompositeOperation ='screen'; text.render(ctx); thunder.forEach(l => l.render(ctx)); particles.forEach(p => p.render(ctx)); } (function(){ // canvas = document.getElementById('canvas'); input = document.getElementById('input'); ctx = canvas.getContext('2d'); w = window.innerWidth; h = window.innerHeight; canvas.width = w; canvas.height = h; thunder =[]; particles =[]; // text =newText({ copy:'Anh Yêu Em!' }); canvas.addEventListener('click',(e)=>{ const x = e.clientX; const y = e.clientY; thunder.push(newThunder({ x: x, y: y })); particles.push(newParticles({ x: x, y: y })); }); let cb =0; input.addEventListener('keyup',(e)=>{ clearTimeout(cb); cb = setTimeout(()=>{ text =newText({ copy: input.value }); },300); }); // loop(); })() </script> <div style="text-align:center;margin:50px 0; font:normal 14px/24px 'MicroSoft YaHei';"> <p></p> <p></p> </div> </body> </html>
猜你还喜欢
- 03-12 [建站系列] 如何轻松搭建专业企业邮箱:从域名到收发邮件的完整指南
- 03-12 [建站系列] Cloudflare R2个人免费图床:如何设置和使用Cloudflare R2图床
- 06-04 [站长技术] 如何开启WordPress Multisite多站点网络
- 03-29 [环境测试] Hexo部署GitHub Pages
- 03-22 [源码设置] 如何设置Xiuno BBS URL-Rewrite(伪静态设定)
- 03-06 [建站交流] PicGo + smms 构建图床
- 11-18 [emlog技巧] Emlog非插件显示评论者IP属地
- 11-09 [网站维护] WordPress 后台速度慢?加快仪表板速度的 15 种方法
- 11-09 [WordPress插件] 10 个最好用的 WordPress 聊天机器人插件(免费和付费)
- 11-09 [WordPress开发] 探索 WordPress 6.3 中的增强样板(Patterns)
- 11-09 [网站维护] 无需插件即可优化 WordPress 速度的 12 种策略
- 11-09 [网站安全] WordPress 安全统计:WordPress 到底有多安全?
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[短剧合集] 2025年06月2日 精选+付费短剧推荐39部
[软件合集] 25年6月2日 精选软件18个
[软件合集] 25年6月1日 精选软件15个
[短剧合集] 2025年06月1日 精选+付费短剧推荐59部
[短剧] 2025年05月31日 精选+付费短剧推荐58部
[软件合集] 25年5月31日 精选软件66个
[电影] 黄沙漫天(2025) 4K.EDRMAX.杜比全景声 / 4K杜比视界/杜比全景声
[风口福利] 短视频红利新风口!炬焰创作者平台重磅激励来袭
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[美图] 2W美女个美女小姐姐,饱眼福
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag