【Canvas与化学】圆角方形白底红绿蓝同心三色环碳元素图标

【成图】

120*120的图标:

大小图:

【代码】

复制代码
<!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=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();
        ctx.shadowOffsetX=R/210*4; // 阴影
        ctx.shadowOffsetY=R/210*4; 
        ctx.shadowColor="lightgrey";
        ctx.shadowBlur=R/210*4;
        var r=R*1.00;
        var gnt=ctx.createLinearGradient(-r,-r,r,r);
        gnt.addColorStop(0,"rgb(254,254,254)");
        gnt.addColorStop(1,"rgb(242,242,242)");

        ctx.lineWidth=R/210/2;
        ctx.strokeStyle="lightgrey";
        ctx.fillStyle=gnt;
        drawRoundRect(ctx,0,0,2*r,2*r,r/8);
        ctx.fill();
        ctx.stroke();
        ctx.restore();

        // 第2圈
        ctx.save();
        var r=R*0.96;
        var gnt=ctx.createLinearGradient(-r,-r,r,r);
        gnt.addColorStop(0,"rgb(230,231,233)");
        gnt.addColorStop(1,"rgb(253,253,253)");
        ctx.fillStyle=gnt;
        drawRoundRect(ctx,0,0,2*r,2*r,r/8-R*0.04);
        ctx.fill();
        ctx.restore();

        // 红圈
        ctx.save();
        var rOut=R*0.60;
        var rIn=R*0.52;
        var theta=-Math.PI/6;
        var startColor="rgb(244,1,7)";
        var midColor="rgb(251,41,78)";
        var endColor="rgb(255,71,146)";
        drawColoredCloop(ctx,0,0,rIn,rOut,theta,startColor,midColor,endColor);
        ctx.restore();

        // 绿圈
        ctx.save();
        var rOut=R*0.50;
        var rIn=R*0.42;
        var theta=-Math.PI/6;
        var startColor="rgb(9,196,17)";
        var midColor="rgb(107,227,51)";
        var endColor="rgb(189,253,79)";
        drawColoredCloop(ctx,0,0,rIn,rOut,theta,startColor,midColor,endColor);
        ctx.restore();

        // 蓝圈
        ctx.save();
        var rOut=R*0.40;
        var rIn=R*0.32;
        var theta=-Math.PI/6;
        var startColor="rgb(2,178,225)";
        var midColor="rgb(0,219,199)";
        var endColor="rgb(0,251,178)";
        drawColoredCloop(ctx,0,0,rIn,rOut,theta,startColor,midColor,endColor);
        ctx.restore();

        var fontColor="rgb(164,164,164)";

        // 6/12
        ctx.save();
        var r=R*0.93;
        ctx.textBaseline="middle";
        ctx.textAlign="center";
        ctx.font = r*0.1+"px consolas";
        ctx.fillStyle=fontColor;
        ctx.fillText("6/12",-r*0.75,-r*0.75);
        ctx.restore();

        // [He]2s22p2
        ctx.save();
        var r=R*0.93;
        ctx.textBaseline="middle";
        ctx.textAlign="center";
        ctx.font = r*0.1+"px consolas";
        ctx.fillStyle=fontColor;
        ctx.fillText("1s2 2s2 2p2",r*0.55,-r*0.75);
        ctx.restore();
        
        // Carbon
        ctx.save();
        var r=R*0.93;
        ctx.textBaseline="middle";
        ctx.textAlign="center";
        ctx.font = r*0.15+"px consolas";
        ctx.fillStyle="black";
        ctx.fillText("Carbon",0,r*0.85);
        ctx.restore();

        // non-metal
        /*ctx.save();
        var r=R*0.93;
        ctx.textBaseline="middle";
        ctx.textAlign="center";
        ctx.font = r*0.15+"px consolas";
        ctx.fillStyle=fontColor;
        ctx.fillText("non-metal",0,r*0.90);
        ctx.restore();  */  
                
        writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","lightgrey");// 版权
    }
}

/*----------------------------------------------------------
函数:用于绘制三彩C环
ctx:绘图上下文
x:C环圆心心横坐标
y:C环圆心纵坐标
rIn:C环内径
rOut:C环外径
theta:启闭角度
startColor:启端颜色
midColor:中端颜色
endColor:终端颜色
----------------------------------------------------------*/
function drawColoredCloop(ctx,x,y,rIn,rOut,theta,startColor,midColor,endColor){
    ctx.save();
    ctx.translate(x,y);

    var startAngle=-theta;// 下半圈
    var endAngle=Math.PI;
    var a=createPt(rOut*Math.cos(startAngle),rOut*Math.sin(startAngle));
    var b=createPt(rOut*Math.cos(endAngle),rOut*Math.sin(endAngle));
    var gnt=ctx.createLinearGradient(a.x,a.y,b.x,b.y);
    gnt.addColorStop(0,startColor);
    gnt.addColorStop(1,midColor);
    ctx.fillStyle=gnt;
    drawFan(ctx,0,0,rIn,rOut,startAngle,endAngle);
    ctx.fill();

    var rMid=rIn/2+rOut/2;// 开头圆
    var d=createPt(rMid*Math.cos(startAngle),rMid*Math.sin(startAngle));
    drawSolidCircle(ctx,d.x,d.y,rOut/2-rIn/2,startColor);

    startAngle=Math.PI;// 上半圈
    endAngle=theta;
    var a=createPt(rOut*Math.cos(startAngle),rOut*Math.sin(startAngle));
    var b=createPt(rOut*Math.cos(endAngle),rOut*Math.sin(endAngle));
    var gnt=ctx.createLinearGradient(a.x,a.y,b.x,b.y);
    gnt.addColorStop(0,midColor);
    gnt.addColorStop(1,endColor);
    ctx.fillStyle=gnt;
    drawFan(ctx,0,0,rIn,rOut,startAngle,endAngle);
    ctx.fill();

    ctx.fillStyle=midColor;// 补缝圈
    drawFan(ctx,0,0,rIn,rOut,startAngle-Math.PI/20,startAngle+Math.PI/20);
    ctx.fill();

    var rMid=rIn/2+rOut/2;// 收尾圆
    var c=createPt(rMid*Math.cos(endAngle),rMid*Math.sin(endAngle));
    drawSolidCircle(ctx,c.x,c.y,rOut/2-rIn/2,endColor);

    ctx.restore();
}

/*----------------------------------------------------------
函数:用于绘制扇形
ctx:绘图上下文
x:扇形圆心心横坐标
y:扇形圆心纵坐标
rIn:扇形内径
rOut:扇形外径
startAngle:起始角度
endAngle:中止角度
----------------------------------------------------------*/
function drawFan(ctx,x,y,rIn,rOut,startAngle,endAngle){
    var a=createPt(x+rIn*Math.cos(startAngle),y+rIn*Math.sin(startAngle));
    var b=createPt(x+rOut*Math.cos(startAngle),y+rOut*Math.sin(startAngle));
    var c=createPt(x+rOut*Math.cos(endAngle),y+rOut*Math.sin(endAngle));
    var d=createPt(x+rIn*Math.cos(endAngle),y+rIn*Math.sin(endAngle));

    ctx.beginPath();
    ctx.moveTo(a.x,a.y);
    ctx.lineTo(b.x,b.y);
    ctx.arc(0,0,rOut,startAngle,endAngle,false);
    ctx.lineTo(c.x,c.y);
    ctx.lineTo(d.x,d.y);
    ctx.arc(0,0,rIn,endAngle,startAngle,true);
    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

posted @   逆火狂飙  阅读(273)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2014-09-07 Acer商祺x4610安装及使用
生当作人杰 死亦为鬼雄 至今思项羽 不肯过江东
点击右上角即可分享
微信分享提示