【Canvas与艺术】辐射状糖果色 Happy New Year 2025
【成图】
【代码】
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <head> <title>糖果色Happy New Year 2025 Draft3</title> <style type="text/css"> .centerlize{ margin:0 auto; width:1200px; } </style> </head> <body onload="init();"> <div class="centerlize"> <canvas id="myCanvas" width="12px" height="12px" style="border:1px dotted black;"> 如果看到这段文字说您的浏览器尚不支持HTML5 Canvas,请更换浏览器再试. </canvas> <img id="myImg" src="732.jpg" style="display:none;"/> </div> </body> </html> <script type="text/javascript"> <!-- /***************************************************************** * 将全体代码(从<!DOCTYPE到script>)拷贝下来,粘贴到文本编辑器中, * 另存为.html文件,再用chrome浏览器打开,就能看到实现效果。 ******************************************************************/ // canvas的绘图环境 var ctx; // 高宽 const WIDTH=512; const HEIGHT=512; // 舞台对象 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){ //sleep(100); window.requestAnimationFrame(animate); } } // 舞台类 function Stage(){ // 初始化 this.init=function(){ } // 更新 this.update=function(){ } // 画背景 this.paintBg=function(ctx){ ctx.clearRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);// 清屏 } // 画前景 this.paintFg=function(ctx){ // 底色 //ctx.save(); //ctx.fillStyle = "white"; //ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT); //ctx.restore(); const R=210;//基准尺寸 // 第1圈 ctx.save(); var r=R*1.00; ctx.fillStyle="rgb(202,34,49)"; drawRoundRect(ctx,0,0,2*r,2*r,r/6); ctx.fill(); ctx.restore(); // 第2圈 ctx.save(); var r=R*0.98; drawRoundRect(ctx,0,0,2*r,2*r,r/6-0.015*R); ctx.clip(); var N=13*4; var part=Math.PI*2/N; for(var i=0;i<N;i++){ var theta=part*i-part/2; var startAngle=theta; var endAngle=theta+part; var l=r*Math.sqrt(2); var start=createPt(l*Math.cos(startAngle),l*Math.sin(startAngle)); var end=createPt(l*Math.cos(endAngle),l*Math.sin(endAngle)); ctx.fillStyle=(i%2==0)?"rgb(252,232,145)":"rgb(252,189,13)"; ctx.beginPath(); ctx.moveTo(0,0); ctx.lineTo(start.x,start.y); ctx.arc(0,0,l,startAngle,endAngle,false); ctx.closePath(); ctx.fill(); } ctx.restore(); // 第3圈 阴影 ctx.save(); var r=R*1.00; drawRoundRect(ctx,0,0,2*r,2*r,r/6); ctx.clip(); r=R*0.80; var l=r; var angle=-Math.PI/4; var a=createPt(l*Math.cos(angle),l*Math.sin(angle)); l=1.5*R; angle=Math.PI/4; var d=createPt(a.x+l*Math.cos(angle),a.y+l*Math.sin(angle)); l=r; angle=Math.PI/4*3; var b=createPt(l*Math.cos(angle),l*Math.sin(angle)); l=1.5*R; angle=Math.PI/4; var c=createPt(b.x+l*Math.cos(angle),b.y+l*Math.sin(angle)); ctx.fillStyle="rgba(36,36,36,0.3)"; ctx.beginPath(); ctx.moveTo(a.x,a.y); ctx.lineTo(b.x,b.y); ctx.lineTo(c.x,c.y); ctx.lineTo(d.x,d.y); ctx.closePath(); ctx.fill(); ctx.restore(); // 第4圈 波浪圈 ctx.save(); var r=R*0.80; ctx.fillStyle="white"; drawWavedCircle(ctx,0,0,r,r/50,288); ctx.fill(); ctx.restore(); // 第5圈 ctx.save(); var r=R*0.70; drawSolidCircle(ctx,0,0,r,"rgb(244,113,147)"); ctx.restore(); // 第6圈 渐变背景色 ctx.save(); var r=R*0.68; var gnt=ctx.createLinearGradient(-r,-r,r,r); gnt.addColorStop(0,"rgb(244,113,147)"); gnt.addColorStop(1,"rgb(239,166,183)"); drawSolidCircle(ctx,0,0,r,gnt); ctx.restore(); // 彩旗 ctx.save(); var r=R*0.68; var y=-r*0.8; var W=r*0.45*2; var N=8; var w=W/N; var h=w*0.5; var colors=["rgb(234,48,27)","rgb(141,55,30)","rgb(233,101,114)","rgb(238,236,3)", "rgb(141,55,30)","rgb(233,101,114)","rgb(238,236,3)","rgb(234,48,27)"]; for(var i=0;i<N;i++){ ctx.fillStyle=colors[i]; drawRect(ctx,-3.5*w+i*w,y,w,h); ctx.fill(); drawSolidCircle(ctx,-3.5*w+i*w,y+h/2,w/2,colors[i]); } ctx.restore(); // 白点上 ctx.save(); var r=R*0.68; var y=-r*0.6; var W=r*0.70*2; var N=24; var w=W/N; var r=w*0.2; for(var i=0;i<=N;i++){ drawSolidCircle(ctx,-W/2+i*w,y,r,"white"); } ctx.restore(); // 白点下 ctx.save(); var r=R*0.68; var y=-r*0.3; var W=r*0.70*2; var N=24; var w=W/N; var r=w*0.2; for(var i=0;i<=N;i++){ drawSolidCircle(ctx,-W/2+i*w,y,r,"white"); } ctx.restore(); // Happy new year ctx.save(); var r=R*0.68; var start=createPt(0,-r*0.36); var sz=R*0.1; printWords(ctx,start.x,start.y,sz,R/210*2,"HAPPY NEW YEAR"); ctx.restore(); // 2025 ctx.save(); var r=R*0.68; var w=0.6*2*r/8; var sz=R*0.3; var a=createPt(-3*w,r*0.2); printWords2(ctx,a.x,a.y,sz,R/210*6,"2","rgb(245,124,31)","white"); var b=createPt(-1*w,r*0.2); printWords2(ctx,b.x,b.y,sz,R/210*6,"0","rgb(225,41,91)","white"); var c=createPt(+1*w,r*0.2); printWords2(ctx,c.x,c.y,sz,R/210*6,"2","rgb(90,172,222)","white"); var d=createPt(+3*w,r*0.2); printWords2(ctx,d.x,d.y,sz,R/210*6,"5","rgb(242,77,72)","white"); ctx.restore(); // 美元 ctx.save(); var r=R*0.6; var startAngle=-0; var endAngle=Math.PI; var N=10; var part=(endAngle-startAngle)/N; for(var i=0;i<=N;i++){ var theta=i*part+startAngle; var a=createPt(r*Math.cos(theta),r*Math.sin(theta)); //drawSolidCircle(ctx,a.x,a.y,3,"red"); var w=r/4; var h=w/2.3; ctx.save(); ctx.translate(a.x,a.y); ctx.rotate(Math.PI/2+theta); drawDollar(ctx,0,0,w,h); ctx.restore(); } // WIN ctx.save(); var r=R*0.68; var w=0.6*2*r/8; var sz=R*0.3; var a=createPt(0,r*0.7); printWords2(ctx,a.x,a.y,sz,R/210*6,"WIN","rgb(245,0,0)","white"); ctx.restore(); writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","lightgrey");// 版权 } } /*---------------------------------------------------------- 函数:用于绘制绿色美元 ctx:绘图上下文 x:美元矩形中心横坐标 y:美元矩形中心纵坐标 width:美元矩形宽 height:美元矩形高 ----------------------------------------------------------*/ function drawDollar(ctx,x,y,width,height){ const LIGHT_GREEN="rgb(195,228,175)"; const DARK_GREEN="rgb(121,179,95)"; ctx.save(); // 外圈 ctx.fillStyle=LIGHT_GREEN; drawRect(ctx,x,y,width,height); ctx.fill(); const T=width/16;// T:thickness // 中圈 ctx.fillStyle=DARK_GREEN; drawRect(ctx,x,y,width-T,height-T); ctx.fill(); // 内圈 ctx.fillStyle=LIGHT_GREEN; drawRect4RoundCorner(ctx,x,y,width-T*2,height-T*2,width/16); ctx.fill(); // 两圆 drawSolidCircle(ctx,x-width/4,y,width/20,DARK_GREEN); drawSolidCircle(ctx,x+width/4,y,width/20,DARK_GREEN); // 中椭圆 ctx.fillStyle=DARK_GREEN; ctx.ellipse(x, y, height/2*0.8*0.5, height/2*0.6, 0, 0, 2*Math.PI,false); ctx.fill(); // 中文字 var sz=width*0.25; writeText(ctx,x,y+width*0.145,"$",sz+"px consolas",LIGHT_GREEN); ctx.restore(); } /*---------------------------------------------------------- 函数:用于绘制内凹圆角矩形,即四角各切去一个四分之一圆 ctx:绘图上下文 x:矩形中心横坐标 y:矩形中心纵坐标 width:矩形宽 height:矩形高 cl:cornerLength 角边长 ----------------------------------------------------------*/ function drawRect4RoundCorner(ctx,x,y,width,height,cl){ ctx.beginPath(); ctx.moveTo(x+width/2,y); ctx.lineTo(x+width/2,y-height/2+cl); ctx.arc(x+width/2,y-height/2,cl,Math.PI/2,Math.PI,false); ctx.lineTo(x-width/2+cl,y-height/2); ctx.arc(x-width/2,y-height/2,cl,0,Math.PI/2,false); ctx.lineTo(x-width/2,y+height/2-cl); ctx.arc(x-width/2,y+height/2,cl,-Math.PI/2,0,false); ctx.lineTo(x+width/2-cl,y+height/2); ctx.arc(x+width/2,y+height/2,cl,Math.PI,-Math.PI/2,false); ctx.closePath(); } // 输出文字并描边 function printWords2(ctx,x,y,size,lineWidth,words,fillColor,strokeColor){ ctx.textBaseline="bottom"; ctx.textAlign="center"; ctx.font = size+"px Bahnschrift SemiBold"; ctx.lineWidth=lineWidth; ctx.strokeStyle=strokeColor; ctx.strokeText(words,x,y); ctx.fillStyle=fillColor; ctx.fillText(words,x,y); } // 输出文字并描边 function printWords(ctx,x,y,size,lineWidth,words){ ctx.textBaseline="bottom"; ctx.textAlign="center"; ctx.font = size+"px Bahnschrift SemiBold"; ctx.lineWidth=lineWidth; ctx.strokeStyle="white"; ctx.strokeText(words,x,y); ctx.fillStyle="rgb(245,50,30)"; ctx.fillText(words,x,y); } /*---------------------------------------------------------- 函数:用于绘制波浪形圆轮廓 ctx:绘图上下文 x:圆中心横坐标 y:圆中心纵坐标 radius:圆半径 waveHeight:波浪高,一般取半径的几分之一 waveCount:波浪个数,一般取4的整倍数 ----------------------------------------------------------*/ function drawWavedCircle(ctx,x,y,radius,waveHeight,waveCount){ var arr=[]; const n=waveCount; const LEN=n+2;// 数组长度比浪头峰谷数多两个以在绘图时形成闭环 for(var i=0;i<LEN;i++){ var theta=i*Math.PI*2/n; var r=radius+Math.sin(Math.PI/2*i)*waveHeight;// 造成涨落 var pt={}; pt.x=r*Math.cos(theta)+x; pt.y=r*Math.sin(theta)+y; arr.push(pt); } ctx.beginPath(); for(var i=0;i<arr.length-2;i+=2){ var pt1=arr[i]; var pt2=arr[i+1];// 控制点 var pt3=arr[i+2]; ctx.lineTo(pt1.x,pt1.y); ctx.quadraticCurveTo(pt2.x,pt2.y,pt3.x,pt3.y); } ctx.closePath(); } /*---------------------------------------------------------- 函数:用于绘制矩形 ctx:绘图上下文 x:矩形中心横坐标 y:矩形中心纵坐标 width:矩形宽 height:矩形高 ----------------------------------------------------------*/ function drawRect(ctx,x,y,width,height){ ctx.beginPath(); ctx.moveTo(x-width/2,y-height/2); ctx.lineTo(x+width/2,y-height/2); ctx.lineTo(x+width/2,y+height/2); ctx.lineTo(x-width/2,y+height/2); ctx.closePath(); } /*---------------------------------------------------------- 函数:用于绘制圆角矩形 ctx:绘图上下文 x:矩形中心横坐标 y:矩形中心纵坐标 width:矩形宽 height:矩形高 radius:圆角半径 ----------------------------------------------------------*/ function drawRoundRect(ctx,x,y,width,height,radius){ ctx.beginPath(); ctx.moveTo(x-width/2+radius,y-height/2); ctx.lineTo(x+width/2-radius,y-height/2); ctx.arcTo(x+width/2,y-height/2,x+width/2,y-height/2+radius,radius); ctx.lineTo(x+width/2,y-height/2+radius); ctx.lineTo(x+width/2,y+height/2-radius); ctx.arcTo(x+width/2,y+height/2,x+width/2-radius,y+height/2,radius); ctx.lineTo(x+width/2-radius,y+height/2); ctx.lineTo(x-width/2+radius,y+height/2); ctx.arcTo(x-width/2,y+height/2,x-width/2,y+height/2-radius,radius); ctx.lineTo(x-width/2,y+height/2-radius); ctx.lineTo(x-width/2,y-height/2+radius); ctx.arcTo(x-width/2,y-height/2,x-width/2+radius,y-height/2,radius); ctx.closePath(); } /*---------------------------------------------------------- 函数:用于绘制实心圆 ctx:绘图上下文 x:矩形中心横坐标 y:矩形中心纵坐标 r:圆半径 style:填充圆的方案 ----------------------------------------------------------*/ function drawSolidCircle(ctx,x,y,r,style){ ctx.fillStyle=style; ctx.beginPath(); ctx.arc(x,y,r,0,Math.PI*2,false); ctx.closePath(); ctx.fill(); } /*---------------------------------------------------------- 函数:创建一个二维坐标点 x:横坐标 y:纵坐标 Pt即Point ----------------------------------------------------------*/ function createPt(x,y){ var retval={}; retval.x=x; retval.y=y; return retval; } /*---------------------------------------------------------- 函数:延时若干毫秒 milliseconds:毫秒数 ----------------------------------------------------------*/ function sleep(milliSeconds) { const date = Date.now(); let currDate = null; while (currDate - date < milliSeconds) { currDate = Date.now(); } } /*---------------------------------------------------------- 函数:书写文字 ctx:绘图上下文 x:横坐标 y:纵坐标 text:文字 font:字体 color:颜色 ----------------------------------------------------------*/ function writeText(ctx,x,y,text,font,color){ ctx.save(); ctx.textBaseline="bottom"; ctx.textAlign="center"; ctx.font = font; ctx.fillStyle=color; ctx.fillText(text,x,y); ctx.restore(); } /*------------------------------------------------------------- 为什么说大一新生开学揭开了太多家庭的无奈和辛酸? 还记得一年我作为志愿者接待新生,看到有的家长西装革履,开宾利来; 有的家长穿解放鞋,提蛇皮口袋来。 他们问的问题也各不相同,有人问“学生可不可以开车?”“学校有没有 咖啡馆和健身房?”,也有人问“学费最晚可以延迟到几号交?”“助学 贷款怎么办理?” --------------------------------------------------------------*/ //--> </script>
END
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步