【Canvas与图标】积分图标
【成图】
120*120的png图标
大小图:
【代码】
<!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <head> <title>微积分图标 Draft1</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> </div> </body> </html> <script type="text/javascript"> <!-- /***************************************************************** * 将全体代码(从<!DOCTYPE到script>)拷贝下来,粘贴到文本编辑器中, * 另存为.html文件,再用chrome浏览器打开,就能看到实现效果。 ******************************************************************/ // canvas的绘图环境 var ctx; // 高宽 const WIDTH=120; const HEIGHT=120; // 舞台对象 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 = "black"; //ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT); ctx.restore(); const R=53;//基准尺寸 var ct=createPt(0,0);// ct=center // #1 ctx.save(); var r=R*1.00; ctx.fillStyle="rgb(37,54,64)"; drawRoundPolygon(ctx,ct.x,ct.y,6,r,r/8,Math.PI/6); ctx.fill(); ctx.restore(); // #2 ctx.save(); var r=R*0.95; ctx.fillStyle="white"; drawRoundPolygon(ctx,ct.x,ct.y,6,r,r/8-r*0.05,Math.PI/6); ctx.fill(); ctx.restore(); // #3 ctx.save(); var r=R*0.90; ctx.fillStyle="rgb(37,54,64)"; drawRoundPolygon(ctx,ct.x,ct.y,6,r,r/8-r*0.05,Math.PI/6); ctx.fill(); ctx.restore(); // #4 ctx.save(); var r=R*0.85; ctx.fillStyle="white"; drawRoundPolygon(ctx,ct.x,ct.y,6,r,r/8-r*0.05,Math.PI/6); ctx.fill(); ctx.restore(); // #5 积分符号拉长的S ctx.save(); var r=R*0.60; var radius=r/4; var a=createPt2(ct.x,ct.y,r-radius,-Math.PI/2); var b=createPt2(ct.x,ct.y,r-radius,+Math.PI/2); ctx.lineWidth=R/220*24; ctx.lineCap="round"; ctx.strokeStyle="rgb(37,54,64)"; ctx.beginPath(); ctx.arc(a.x+radius,a.y,radius,0,Math.PI,true); ctx.lineTo(a.x,a.y); ctx.lineTo(b.x,b.y); ctx.arc(b.x-radius,b.y,radius,0,Math.PI,false); ctx.stroke(); ctx.restore(); //writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","lightgrey");// 版权 } } /*---------------------------------------------------------- 函数:用于绘制圆角多边形,使用贝塞尔二次曲线形成圆角效果, 适用于五边以上图形 ctx:绘图上下文 x:多边形中心横坐标 y:多边形中心纵坐标 n:多边形边数 R:多边形半径 r:圆角半径 initAngle:初始角度 ----------------------------------------------------------*/ function drawRoundPolygon(ctx,x,y,n,R,r,initAngle){ // 取点 const N=n;// 多边形边数 const ROUND=r;// 圆角半径 var arr=[];// 放顶点三点的数组 for(var i=0;i<N;i++){ var theta=Math.PI*2/N*i+initAngle; var r=R; var a=createPt(x+r*Math.cos(theta),y+r*Math.sin(theta)); r=ROUND*Math.tan(Math.PI/N); var angle=theta+Math.PI/2+Math.PI/N; var c=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle)); r=ROUND/Math.sqrt(3); angle=theta-Math.PI/2-Math.PI/N; var b=createPt(a.x+r*Math.cos(angle),a.y+r*Math.sin(angle)); var arrInner=[b,a,c]; arr.push(arrInner); } // 绘图 ctx.beginPath(); for(var i=0;i<arr.length;i++){ var b=arr[i][0]; var a=arr[i][1]; var c=arr[i][2]; ctx.lineTo(b.x,b.y); ctx.quadraticCurveTo(a.x,a.y,c.x,c.y); ctx.lineTo(c.x,c.y); } 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:矩形中心纵坐标 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:矩形中心纵坐标 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(); } /*---------------------------------------------------------- 基础函数:创建一个二维坐标点 baseX:基准点横坐标 baseY:基准点纵坐标 radius:当前点到基准点的距离 theta:当前点到基准点的角度 Pt即Point ----------------------------------------------------------*/ function createPt2(baseX,baseY,radius,theta){ var retval={}; retval.x=baseX+radius*Math.cos(theta); retval.y=baseY+radius*Math.sin(theta); return retval; } /*---------------------------------------------------------- 基础函数:创建一个二维坐标点 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