首页 > 其他分享 >【Canvas与徽章】庆祝建国75周年徽章

【Canvas与徽章】庆祝建国75周年徽章

时间:2024-09-25 23:21:43浏览次数:3  
标签:Canvas gnt addColorStop ctx rgb 75 徽章 var Math

【成图】

【Canvas与徽章】庆祝建国75周年徽章_徽章

【Canvas与徽章】庆祝建国75周年徽章_canvas_02

【Canvas与徽章】庆祝建国75周年徽章_徽章_03

【Canvas与徽章】庆祝建国75周年徽章_canvas_04

【代码】

<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>庆祝建国75周年</title>
     <style type="text/css">
     .centerlize{
        margin:0 auto;
        width:1200px;
     }
     </style>
     </head>

     <body onl oad="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;// 基准尺寸
        
        // 最外层竖向渐变外框
        ctx.save();
        var r=R*1.00;
        var gnt=ctx.createLinearGradient(0,-r,0,r);
        gnt.addColorStop(0,  "rgb(238,230,165)");
        gnt.addColorStop(0.25,"rgb(222,183,62)");
        gnt.addColorStop(0.5,"rgb(255,255,190)");
        gnt.addColorStop(0.6,"rgb(152,97,33)");
        gnt.addColorStop(0.8,"rgb(252,238,165)");
        gnt.addColorStop(1,  "rgb(217,173,74)");    
        ctx.fillStyle=gnt;
        ctx.beginPath();
        ctx.arc(0,0,r,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 上半圈渐变外框
        ctx.save();
        var ro=R*0.99;// outer radius
        var ri=R*0.93;// inner radius
        var gnt=ctx.createLinearGradient(0,-ro,0,0);
        gnt.addColorStop(0,  "rgb(252,247,183)");
        gnt.addColorStop(0.5,"rgb(231,192,91)");
        gnt.addColorStop(0.95,"rgb(220,208,156)");
        gnt.addColorStop(1,"rgb(207,195,145)");
        ctx.fillStyle=gnt;
        ctx.beginPath();
        ctx.moveTo(ri,0);
        ctx.lineTo(ro,0);
        ctx.arc(0,0,ro,0,-Math.PI,true);
        ctx.lineTo(-ri,0);
        ctx.arc(0,0,ri,-Math.PI,0,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 下半圈渐变外框
        ctx.save();
        var ro=R*0.99;// outer radius
        var ri=R*0.93;// inner radius
        var gnt=ctx.createLinearGradient(0,0,0,ro);
        gnt.addColorStop(0,  "rgb(209,197,145)");
        gnt.addColorStop(0.25,  "rgb(98,60,37)");
        gnt.addColorStop(0.75,"rgb(243,223,146)");
        gnt.addColorStop(1,"rgb(229,184,81)");
        ctx.fillStyle=gnt;
        ctx.beginPath();
        ctx.moveTo(ri,0);
        ctx.lineTo(ro,0);
        ctx.arc(0,0,ro,0,Math.PI,false);
        ctx.lineTo(-ri,0);
        ctx.arc(0,0,ri,Math.PI,0,true);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 细圈
        ctx.save();
        var r=R*0.93;
        ctx.fillStyle="rgb(217,173,74)";
        ctx.beginPath();
        ctx.arc(0,0,r,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 内层周年圈
        ctx.save();
        var r=R*0.92;
        var gnt=ctx.createLinearGradient(0,-r,0,r);
        gnt.addColorStop(0,  "rgb(254,253,189)");
        gnt.addColorStop(1,  "rgb(204,154,69)");    
        ctx.fillStyle=gnt;
        ctx.beginPath();
        ctx.arc(0,0,r,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 左右五星
        ctx.save();
        var r=R*0.75;
        var N=2;
        for(var i=0;i<N;i++){
            var theta=Math.PI*2/N*i;
            var a=createPt(r*Math.cos(theta),r*Math.sin(theta));
            ctx.fillStyle="rgb(68,41,11)";
            ctx.strokeStyle="gold";
            draw5Star(ctx,a.x,a.y,r/10);
            ctx.stroke();
            ctx.fill();
        }
        ctx.restore();

        // 上半圈文字
        var words="CELEBRATING";
        var arr=words.split("");
        const UP_ANGLE=Math.PI/3*2.2;// 上张角
        const UP_OFFSET=UP_ANGLE/(arr.length-1);
        var r=R*0.65;// 文字半径
        for(var i=0;i<arr.length;i++){
            var letter=arr[i];
            var theta=-Math.PI/2-UP_ANGLE/2+i*UP_OFFSET;
            var a=createPt(r*Math.cos(theta),r*Math.sin(theta));

            // 辅助线
            /*ctx.save();
            ctx.lineWidth=1;
            ctx.strokeStyle="black";
            ctx.beginPath();
            ctx.moveTo(0,0);
            ctx.lineTo(a.x,a.y);
            ctx.stroke();
            ctx.restore();*/

            ctx.save();
            ctx.translate(a.x,a.y);
            ctx.rotate(theta+Math.PI/2);
            ctx.scale(1,0.8);
            ctx.textBaseline="bottom";
            ctx.textAlign="center";
            ctx.font = r/3+"px Stencil Std";
            ctx.fillStyle="rgb(68,41,11)";
            ctx.fillText(letter,0,0);
            ctx.strokeStyle="gold";
            ctx.strokeText(letter,0,0);
            ctx.restore();
        }

        // 下半圈文字
        var words="aniversary";
        var arr=words.split("");
        const DOWN_ANGLE=Math.PI/3*2.0;// 下张角
        const DOWN_OFFSET=DOWN_ANGLE/(arr.length-1);
        var r=R*0.88;// 文字半径
        for(var i=0;i<arr.length;i++){
            var letter=arr[i];
            var theta=Math.PI/2+DOWN_ANGLE/2-i*DOWN_OFFSET;
            var a=createPt(r*Math.cos(theta),r*Math.sin(theta));

            // 辅助线
            /*ctx.save();
            ctx.lineWidth=1;
            ctx.strokeStyle="black";
            ctx.beginPath();
            ctx.moveTo(0,0);
            ctx.lineTo(a.x,a.y);
            ctx.stroke();
            ctx.restore();*/

            ctx.save();
            ctx.translate(a.x,a.y);
            ctx.rotate(theta-Math.PI/2);
            ctx.scale(1,0.8);
            ctx.textBaseline="bottom";
            ctx.textAlign="center";
            ctx.font = r/3.8+"px Stencil Std";
            ctx.fillStyle="rgb(68,41,11)";
            ctx.fillText(letter,0,0);
            ctx.strokeStyle="gold";
            ctx.strokeText(letter,0,0);
            ctx.restore();
        }

        // 细圈
        ctx.save();
        var r=R*0.61;
        ctx.fillStyle="rgb(250,239,174)";
        ctx.beginPath();
        ctx.arc(0,0,r,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 内层竖向渐变外框
        ctx.save();
        var r=R*0.60;
        var gnt=ctx.createLinearGradient(0,-r,0,r);
        gnt.addColorStop(0,  "rgb(204,154,67)");
        gnt.addColorStop(1,  "rgb(254,253,189)");    
        ctx.fillStyle=gnt;
        ctx.beginPath();
        ctx.arc(0,0,r,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 酱圈
        ctx.save();
        var r=R*0.55;
        ctx.fillStyle="rgb(68,41,11)";
        ctx.beginPath();
        ctx.arc(0,0,r,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 白底
        ctx.save();
        var r=R*0.54;
        ctx.fillStyle="white";
        ctx.beginPath();
        ctx.arc(0,0,r,0,Math.PI*2,false);
        ctx.closePath();
        ctx.fill();
        ctx.restore();

        // 十字纹
        ctx.save();
        r=R*0.54;
        var N=3600;
        for(var i=0;i<N;i++){
            var theta=Math.PI*2/N*i;
            var a=createPt(r*Math.cos(theta),r*Math.sin(theta));

            // 用锯齿函数实现周期变换,0.3为可调常数项
            var alpha=(((i)%600)/600);
            if(alpha>0.5){
                alpha=(1-alpha);
            }
            ctx.strokeStyle="rgba(188,187,189,"+alpha+")";// 控制直线的半透明度以调节明暗
            ctx.beginPath();
            ctx.moveTo(0,0);
            ctx.lineTo(a.x,a.y);
            ctx.stroke();
        }
        ctx.restore();

        // 75
        ctx.save();
        r=R*0.5;
        var x=-r/11,y=r/11*6;
        var word="75";
        ctx.textBaseline="bottom";
        ctx.textAlign="center";
        ctx.font = r/1.1+"px Stencil Std";
        ctx.fillStyle="rgb(68,41,11)";
        ctx.fillText(word,x,y);
        ctx.strokeStyle="gold";
        ctx.strokeText(word,x,y);
        ctx.restore();

        // th
        ctx.save();
        r=R*0.5;
        var x=r/1.6,y=-r/11;
        var word="th";
        ctx.textBaseline="bottom";
        ctx.textAlign="center";
        ctx.font = r/3.2+"px consolas";
        ctx.fillStyle="rgb(68,41,11)";
        ctx.fillText(word,x,y);
        ctx.strokeStyle="gold";
        ctx.strokeText(word,x,y);
        ctx.restore();

        writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","lightgrey");// 版权
    }
}

/*--------------------------------------------------
函数:绘制正五角星的推荐画法
ctx:绘图上下文
x:五角星中心横坐标
y:五角星中心纵坐标
R:五角星中心到顶点的距离
---------------------------------------------------*/
function draw5Star(ctx,x,y,R){
    var r=R*Math.sin(Math.PI/10)/Math.sin(Math.PI/10*7);
    
    var arr=[0,0,0,0,0,0,0,0,0,0];

    // 顶五点
    for(var i=0;i<5;i++){
        var theta=i*Math.PI/5*2-Math.PI/10;
        var x1=R*Math.cos(theta)+x;
        var y1=R*Math.sin(theta)+y;
        arr[i*2]=createPt(x1,y1);
    }

    // 内五点
    for(var i=0;i<5;i++){
        var theta=i*Math.PI/5*2+Math.PI/10;
        var x1=r*Math.cos(theta)+x;
        var y1=r*Math.sin(theta)+y;
        arr[i*2+1]=createPt(x1,y1);
    }

    ctx.beginPath();
    for(var i=0;i<arr.length;i++){
        ctx.lineTo(arr[i].x,arr[i].y);
    }
    ctx.closePath();
}

/*----------------------------------------------------------
函数:用于绘制实心圆,用途是标记点以辅助作图
ctx:绘图上下文
x:矩形中心横坐标
y:矩形中心纵坐标
r:圆半径
color:填充圆的颜色
----------------------------------------------------------*/
function drawSolidCircle(ctx,x,y,r,color){
    ctx.fillStyle=color;
    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

标签:Canvas,gnt,addColorStop,ctx,rgb,75,徽章,var,Math
From: https://blog.51cto.com/u_7726611/12112561

相关文章

  • 手写板 canvas
    <!doctypehtml><html><head><metacharset="utf-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge,chrome=1"><metaname="viewport"content="width=device-width">&......
  • Canvas简历编辑器-Monorepo+Rspack工程实践
    Canvas简历编辑器-Monorepo+Rspack工程实践 Canvas简历编辑器-Monorepo+Rspack工程实践在之前我们围绕Canvas聊了很多代码设计层面的东西,在这里我们聊一下工程实践。在之前的文中我也提到过,因为是本着学习的态度以及对技术的好奇心来做的,所以除了一些工具类的库例如 ArcoDe......
  • 基于SpringBoot+Vue的宠物领养系统的设计与实现-毕业设计源码33575
    目 录摘要1绪论1.1研究背景1.2 研究意义1.3论文结构与章节安排2 相关技术介绍2.1B/S结构2.2SpringBoot框架2.3MySQL数据库3系统分析3.1可行性分析3.2系统流程分析3.2.1数据新增流程3.2.2 数据删除流程3.3 系统功能分析3.3.1......
  • 计算机知识科普问答--15(71-75)
    文章目录71、操作系统中哪些操作会导致创建新进程?1.**用户登录**2.**启动新程序**3.**系统初始化**4.**父进程创建子进程**5.**执行批处理任务**6.**外部事件(定时器或设备驱动程序的请求)**7.**多线程环境中的线程创建**8.**操作系统命令或脚本的调......
  • 【Canvas与诗词】铁马冰河入梦来
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>金红圈铁马冰河入梦来</title><styletype="text/css&quo......
  • 【Unity】UI、背景和3D的Camera和Canvas设置
    目前存在需求背景是指定的图片,该图片始终显示在页面中,不会因场景的视角操控发生尺寸等变化;UI内容显示在页面最上层,同样不会因场景的视角操控发生尺寸等变化,但是当软件整个尺寸发生变化时,会跟随变化,UI内容会覆盖3D物体;3D物体可以随着相机视角的变化而变近变远等,3D物体上可能存在......
  • P5975 photo 题解
    Solution先离散化,记\(f(l,r,p)\)为覆盖了\([l,r]\)区间内纵坐标\(\gep\)的点最少矩形个数。则:\[f(l,r,p)=\min(f(l,r,p),f(l,mid,p)+f(mid+1,r,p))\]\[f(l,r,p)=\min(f(l,r,p),f(l,r,res)+1)\]令\(h\)为用面积为\(S\)的矩形去覆盖\([l,r]\)区间的高度,即\(S/(r......
  • 【Canvas技法】辐射式多道光影的实现
    【用余弦函数实现辐射四道光】成图:代码:constR=180;//基准尺寸//十字纹ctx.save();r=R;varN=3600;for(vari=0;i<N;i++){vartheta=Math.PI*2/N*i;vara=createPt(r*Math.cos(theta),r*Math.s......
  • 【Canvas与诗词】木兰辞节选
    【成图】【代码】<!DOCTYPEhtml><htmllang="utf-8"><metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/><head><title>金边钢底徽章</title><styletype="text/css"&g......
  • SAE ARP 4754A 标准详解
    一、引言1.1SAEARP4754A的背景随着航空技术的不断发展,现代民航飞机的系统变得越来越复杂。为了确保这些系统的安全性和可靠性,国际上制定了一系列的航空系统开发标准。SAEARP4754A是这些标准中的重要一部分,专注于指导航空系统的开发过程,特别是对于复杂集成系统的设......