当前位置: 首页 > ai >正文

【Canvas与徽章】中国制造金色玻璃光徽章

【成图】

1

2

3

4

5

【代码】

<!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=220;//基准尺寸var center=createPt(0,0);// #1 底圆ctx.save();    var r=R*1.00;drawSolidCircle(ctx,center.x,center.y,r,"rgb(107,77,60)");        ctx.restore();// #2 右上左下渐变圈ctx.save();    var r=R*0.99;var a=createPt2(center.x,center.y,r,-Math.PI/3);var b=createPt2(center.x,center.y,r,Math.PI/3*2);var gnt=ctx.createLinearGradient(a.x,a.y,b.x,b.y);gnt.addColorStop(0,"rgb(251,251,222)");gnt.addColorStop(0.25,"rgb(244,217,136)");gnt.addColorStop(0.5,"rgb(190,137,34)");gnt.addColorStop(0.75,"rgb(102,64,20)");gnt.addColorStop(1,"rgb(107,77,60)");drawSolidCircle(ctx,center.x,center.y,r,gnt);ctx.restore();// #3 凸型渐变圈ctx.save();    var r=R*0.99;var a=createPt2(center.x,center.y,r,-Math.PI/3);var b=createPt2(center.x,center.y,r,Math.PI/3*2);var c=createPt2(center.x,center.y,r,Math.PI/6);var d=createPt2(center.x,center.y,(Math.sqrt(2)-1)*r,Math.PI+Math.PI/6);var gnt=ctx.createLinearGradient(a.x,a.y,b.x,b.y);gnt.addColorStop(0,"rgb(145,115,63)");gnt.addColorStop(0.25,"rgb(196,167,102)");gnt.addColorStop(0.5,"rgb(211,161,73)");gnt.addColorStop(0.75,"rgb(102,64,20)");gnt.addColorStop(1,"rgb(107,77,60)");ctx.fillStyle=gnt;ctx.beginPath();ctx.arc(center.x,center.y,r,-Math.PI/3,Math.PI/3*2,false);ctx.arc(c.x,c.y,r*Math.sqrt(2),Math.PI-Math.PI/12,-Math.PI/2-Math.PI/12,false);ctx.fill();ctx.restore();// #4 上下渐变圈ctx.save();    var r=R*0.93;var a=createPt2(center.x,center.y,r,-Math.PI/2);var b=createPt2(center.x,center.y,r,Math.PI/2);var gnt=ctx.createLinearGradient(a.x,a.y,b.x,b.y);gnt.addColorStop(0,"rgb(183,135,40)");gnt.addColorStop(0.25,"rgb(209,165,92)");gnt.addColorStop(0.5,"rgb(249,219,147)");gnt.addColorStop(0.75,"rgb(247,237,193)");gnt.addColorStop(1,"rgb(250,251,220)");drawSolidCircle(ctx,center.x,center.y,r,gnt);ctx.restore();// #5 上下渐变圈ctx.save();    var r=R*0.90;var a=createPt2(center.x,center.y,r,-Math.PI/2);var b=createPt2(center.x,center.y,r,Math.PI/2);var gnt=ctx.createLinearGradient(a.x,a.y,b.x,b.y);gnt.addColorStop(0,"rgb(255,250,217)");gnt.addColorStop(0.25,"rgb(245,238,192)");gnt.addColorStop(0.5,"rgb(242,221,140)");gnt.addColorStop(0.75,"rgb(211,170,84)");gnt.addColorStop(1,"rgb(196,139,53)");drawSolidCircle(ctx,center.x,center.y,r,gnt);ctx.restore();// #6 上部文字“MADE IN CHINA”ctx.save();    var r=R*0.70;var words="MADE IN CHINA";var arr=words.split("");   var N=arr.length;var startAngle=Math.PI/2*3-Math.PI/3.7;var endAngle=Math.PI/2*3+Math.PI/3.7;var part=(endAngle-startAngle)/N;for(var i=0;i<arr.length;i++){var theta=startAngle+part*(i+1);var a=createPt(r*Math.cos(theta),r*Math.sin(theta));ctx.save();            ctx.translate(a.x,a.y);            ctx.rotate(theta+Math.PI/2);ctx.scale(1,1.3);writeText(ctx,0,0,arr[i],r*0.2+"px Noto Sans SC Black","rgb(43,23,9)");ctx.restore();}ctx.restore();// #7 下圈文字ctx.save();var r=0.87*R;  var words="中国制造";var arr=words.split("");   var N=arr.length;var startAngle=Math.PI/3*2+Math.PI/15;var endAngle=Math.PI/3;var part=(endAngle-startAngle)/N;for(var i=0;i<arr.length;i++){var theta=startAngle+part*(i+1);var a=createPt(r*Math.cos(theta),r*Math.sin(theta));ctx.save();            ctx.translate(a.x,a.y);            ctx.rotate(theta+Math.PI/2*3);ctx.scale(1,1.15);writeText(ctx,0,0,arr[i],r*0.175+"px 黑体","rgb(43,23,9)");ctx.restore();}ctx.restore();// #8 上下渐变圈ctx.save();    var r=R*0.67;var a=createPt2(center.x,center.y,r,-Math.PI/2);var b=createPt2(center.x,center.y,r,Math.PI/2);var gnt=ctx.createLinearGradient(a.x,a.y,b.x,b.y);gnt.addColorStop(0,"rgb(83,44,9)");gnt.addColorStop(1,"rgb(227,176,61)");drawSolidCircle(ctx,center.x,center.y,r,gnt);ctx.restore();// #9 国旗外圈ctx.save();    var r=R*0.64;drawSolidCircle(ctx,center.x,center.y,r,"rgb(107,77,60)");ctx.restore();// #10 国旗ctx.save();    var r=R*0.63;var a=createPt2(center.x,center.y,r,-Math.PI/2);// 旗面var b=createPt2(center.x,center.y,r,Math.PI/2);var gnt=ctx.createLinearGradient(a.x,a.y,b.x,b.y);gnt.addColorStop(0,"rgb(224,58,71)");gnt.addColorStop(0.5,"rgb(169,26,35)");gnt.addColorStop(1,"rgb(101,18,22)");drawSolidCircle(ctx,center.x,center.y,r,gnt);var radius=r*3.5;// 五星var w=radius/30;//小格宽度var h=radius/20;// 小格高度// 诸星属性数组var arr=[{"x":-10*w,"y":-5*h,"r":3*w,"theta":0},// 大星{"x":-5*w,"y":-8*h,"r":1*w,"theta":Math.atan(5*w/3/h)-Math.PI/10*2},// 最上小星{"x":-3*w,"y":-6*h,"r":1*w,"theta":Math.atan(7*w/1/h)-Math.PI/10*2},// 第2小星{"x":-3*w,"y":-3*h,"r":1*w,"theta":0},// 第3小星{"x":-5*w,"y":-1*h,"r":1*w,"theta":Math.atan(4*w/5/h)+Math.PI/2-Math.PI/10*2},// 第4小星];// 绘制五星for(var i=0;i<arr.length;i++){var star=arr[i];ctx.save();ctx.fillStyle="yellow";ctx.translate(r*0.7*1+star.x,r*0.6*1+star.y);ctx.rotate(star.theta);draw5Star(ctx,0,0,star.r);ctx.fill();ctx.restore();}ctx.restore();// #8 月形玻璃光ctx.save();    var r=R*0.63;var center=createPt(0,0);var c=createPt2(center.x,center.y,r,Math.PI/6);var b=createPt2(center.x,center.y,r,Math.PI/2+Math.PI/6);var a=createPt2(center.x,center.y,r,-Math.PI/3);var gnt=ctx.createLinearGradient(0,-r,0,b.y);gnt.addColorStop(0,"rgba(255,255,255,0.4)");gnt.addColorStop(0.4,"rgba(255,255,255,0.1)");gnt.addColorStop(1,"rgba(255,255,255,0.0)");ctx.fillStyle=gnt;ctx.beginPath();ctx.moveTo(a.x,a.y);ctx.arc(center.x,center.y,r,-Math.PI/3,Math.PI/2+Math.PI/6,true);ctx.arc(c.x,c.y,r*Math.sqrt(2),Math.PI-Math.PI/12,-Math.PI/2-Math.PI/12,false);ctx.fill();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:圆半径
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();
}/*----------------------------------------------------------
基本函数:用于绘制矩形
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();
}/*----------------------------------------------------------
基本函数:创建一个二维坐标点
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

http://www.xdnf.cn/news/18520.html

相关文章:

  • 生成模型 | 扩散模型损失函数公式推导
  • 复杂工况漏检率↓79%!陌讯多模态融合算法在智慧能源设备检测的落地实践
  • Python 版本与 package 版本兼容性检查方法
  • 【Linux系列】macOS(MacBook)上获取 MAC 地址
  • 内网穿透教程
  • React学习(十三)
  • Java 泛型 T、E、K、V、?、S、U、V
  • week4-[字符数组]字符统计
  • 详细介绍将 AList 搭建 WebDav 添加到 PotPlayer 专辑 的方法
  • 基于Python与Tkinter的校园点餐系统设计与实现
  • 单片机的输出模式推挽和开漏如何选择呢?
  • [新启航]白光干涉仪与激光干涉仪的区别及应用解析
  • 【typenum】 24 去除尾部零的特性(private.rs片段)
  • MERGE 语句在 Delta Lake 中的原子更新原理
  • nodejs 集成mongodb实现增删改查
  • Kubernetes相关问题集(四)
  • 什么是正态分布
  • B.30.01.1-Java并发编程及电商场景应用
  • Socket 编程预备
  • 软件测试从入门到精通:通用知识点+APP专项实战
  • 使用Screenpipe+本地大模型实现私人助手Agent
  • 某电器5G智慧工厂网络建设全解析
  • Linux学习:信号的保存
  • TypeReference 泛型的使用场景及具体使用流程
  • GEO优化服务商:AI时代数字经济的新引擎——解码行业发展与技术创新实践
  • 【Spring Boot】集成Redis超详细指南 Redis在Spring Boot中的应用场景
  • kubernetes-dashboard使用http不登录
  • 【卷积神经网络详解与实例】1——计算机中的图像原理
  • 卓伊凡的开源战略与PHP-SG16加密技术深度解析-sg加密技术详解-卓伊凡
  • pixijs基础学习