【关键点】
三一渐变式光芒的实现。
【效果】
【代码】
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <head> <title>光芒四射</title> <style type="text/css"> .centerlize{ margin:0 auto; width:1200px; } </style> </head> <body οnlοad="init();"> <div class="centerlize"> <canvas id="myCanvas" width="10px" height="10px" style="border:1px dotted black;"> 如果看到这段文字说您的浏览器尚不支持HTML5 Canvas,请更换浏览器再试. </canvas> <img id="myImg" src="94.jpg" style="display:none;"/> </div> </body> </html> <script type="text/javascript"> <!-- /***************************************************************** * 将全体代码(从<!DOCTYPE到script>)拷贝下来,粘贴到文本编辑器中, * 另存为.html文件,再用chrome浏览器打开,就能看到实现效果。 ******************************************************************/ // canvas的绘图环境 var ctx; // 边长 const WIDTH=1000; const HEIGHT=500; // 舞台对象 var stage; //------------------------------- // 初始化 //------------------------------- function init(){ // 获得canvas对象 var canvas=document.getElementById('myCanvas'); canvas.width=WIDTH; canvas.height=HEIGHT; // 初始化canvas的绘图环境 ctx=canvas.getContext('2d'); ctx.translate(WIDTH/2,HEIGHT/2);// 原点平移到画布中央 // 准备 stage=new Stage(); stage.init(); // 开幕 animate(); } // 播放动画 function animate(){ stage.update(); stage.paintBg(ctx); stage.paintFg(ctx); // 循环 if(true){ window.requestAnimationFrame(animate); } } // 舞台类 function Stage(){ // 光芒的旋转角度 this.angle=0; // 下面两个量用来控制五个边依次闪亮 this.currLightPos=0; this.lightIdx=0; // 初始化 this.init=function(){ } // 更新 this.update=function(){ this.angle+=Math.PI/180; // 累计五百变一次 this.lightIdx++; this.currLightPos=Math.floor(this.lightIdx/100) if(this.lightIdx>500){ this.lightIdx=0; } } // 画背景 this.paintBg=function(ctx){ ctx.clearRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);// 清屏 var img=document.getElementById("myImg"); ctx.drawImage(img,0,0,667,500,-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT); } // 画前景 this.paintFg=function(ctx){ // 三一渐变式光芒四射效果 ctx.save(); ctx.rotate(this.angle); for(var i=0;i<90;i++){ var startAngle=Math.PI/3/15*i;// 起始角度 var endAngle=startAngle+Math.PI/3/15*1.15;// 中止角度 var alpha=Math.abs(Math.sin(3*startAngle/2))/4+0.75;// 透明度 ctx.beginPath(); ctx.moveTo(0, 0); ctx.arc(0, 0, 1000, startAngle, endAngle,false); ctx.closePath(); ctx.fillStyle = "rgba(153,217,234,"+alpha+")"; ctx.fill(); } ctx.restore(); // 画黄底五角星 draw5Star(ctx,0,0,90,"yellow"); // 画立体五角星 const R=80;// 五角星外角半径 const r=30;// 五角星内角半径 ctx.save(); ctx.rotate(Math.PI/10); for(let i=0;i<5;i++){ var alpha=i*2*Math.PI/5-Math.PI/5; var beta=i*2*Math.PI/5; // 画红色五片 var x1=R*Math.cos(alpha); var y1=R*Math.sin(alpha); var x2=r*Math.cos(beta); var y2=r*Math.sin(beta); ctx.beginPath(); ctx.moveTo(0,0); ctx.lineTo(x1,y1); ctx.lineTo(x2,y2); ctx.closePath(); if(this.currLightPos==i){ ctx.fillStyle="rgb(253,53,53)"; }else{ ctx.fillStyle="rgb(250,2,2)"; } ctx.fill(); // 画深红五片 var gama=(i)*2*Math.PI/5+Math.PI/5; var x3=R*Math.cos(gama); var y3=R*Math.sin(gama); ctx.beginPath(); ctx.moveTo(0,0); ctx.lineTo(x3,y3); ctx.lineTo(x2,y2); ctx.closePath(); if(this.currLightPos==i){ ctx.fillStyle="rgb(170,5,5)"; }else{ ctx.fillStyle="rgb(167,3,4)"; } ctx.fill(); } ctx.restore(); // 下方文字说明 ctx.save(); ctx.shadowColor = 'black'; ctx.shadowOffsetX = 0; ctx.shadowOffsetY = 1; ctx.shadowBlur = 2; ctx.textAlign="center"; ctx.textBaseLine="Middle"; ctx.font="40px 仿宋"; ctx.fillStyle="rgb(255,238,177)"; ctx.fillText('谁不欲光芒万丈千秋万代 谁甘愿此生如牛马代代永为奴',0,200); ctx.restore(); ctx.font="12px 仿宋"; ctx.fillStyle="white"; ctx.fillText('逆火原创',WIDTH/2-100,HEIGHT/2-20); } } // 画实心五角星的函数 function draw5Star(ctx,x,y,r,color){ ctx.save() ctx.translate(x-r,y-r); ctx.beginPath(); ctx.moveTo(r, 0); ctx.lineTo(r+Math.cos(Math.PI*3/10)*r, r+Math.sin(Math.PI*3/10)*r); ctx.lineTo(r-Math.cos(Math.PI*1/10)*r, r-Math.sin(Math.PI*1/10)*r); ctx.lineTo(r+Math.cos(Math.PI*1/10)*r, r-Math.sin(Math.PI*1/10)*r); ctx.lineTo(r-Math.cos(Math.PI*3/10)*r, r+Math.sin(Math.PI*3/10)*r); ctx.lineTo(r, 0); ctx.closePath(); ctx.fillStyle=color; ctx.fill(); ctx.restore(); } /*--------------------------------------------- 第一眼就看上的车,我们往往买不起; 第一眼就中意的人,她往往不会喜欢你; 你真正想要的,没有一样是可以轻易得到的; 对于穷人家孩子来说,想得着些许好的就得往死里奔; 对于周公子们来说,生下来就在终点线笑着喝茶等人。 ----------------------------------------------*/ //--> </script>