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

【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>圆角梅花五星旗 Draft2</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 RATIO=4;
const WIDTH=3*64*RATIO;
const HEIGHT=2*64*RATIO;// 舞台对象
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 = "red";//ctx.fillRect(-WIDTH/2,-HEIGHT/2,WIDTH,HEIGHT);//ctx.restore();var ct=createPt(0,0);// ct=center// #1 旗面ctx.save();    var w=WIDTH;var h=HEIGHT;drawRoundRect(ctx,ct.x,ct.y,w,h,h/8);ctx.clip();// 限制范围ctx.fillStyle = "rgb(235,24,33)";// 底色drawRect(ctx,ct.x,ct.y,w,h);ctx.fill();var r=h/4;ct=createPt(0,r*(1-Math.cos(Math.PI/5))/2);drawMeihuaIcon(ctx,ct.x,ct.y,r,Math.PI/10);// 梅花ctx.restore();// #2 玻璃光ctx.save();    ct=createPt(0,0);w-=2*h/100;h-=2*h/100;drawRoundRect(ctx,ct.x,ct.y,w,h,h*0.118);ctx.clip();var top=createPt2(ct.x,ct.y,h/2,-Math.PI/2);var bottom=createPt2(ct.x,ct.y,h/2,Math.PI/2);var gnt=ctx.createLinearGradient(top.x,top.y,bottom.x,bottom.y);gnt.addColorStop(0,"rgba(255,255,255,0.4)");gnt.addColorStop(0.1,"rgba(255,255,255,0.3)");gnt.addColorStop(0.2,"rgba(255,255,255,0.2)");gnt.addColorStop(0.3,"rgba(255,255,255,0.1)");gnt.addColorStop(0.4,"rgba(255,255,255,0.0)");gnt.addColorStop(1,"rgba(255,255,255,0.0)");ctx.fillStyle=gnt;drawRect(ctx,ct.x,ct.y,w,h);ctx.fill();ctx.restore();writeText(ctx,WIDTH/2-30,HEIGHT/2-5,"逆火制图","8px consolas","lightgrey");// 版权}
}/*----------------------------------------------------------
函数:用于绘制完整梅花标志,依赖drawMeihua函数
ctx:绘图上下文
x:轮廓中心横坐标
y:轮廓中心纵坐标
R:梅花中正五边形外接圆半径
initAngle:梅花初始旋转角
----------------------------------------------------------*/
function drawMeihuaIcon(ctx,x,y,R,initAngle){const ct=createPt(x,y);//#1 白边ctx.save();var r=R*1.00;ctx.fillStyle="white";drawMeihua(ctx,ct.x,ct.y,r,Math.PI/10);ctx.fill();ctx.restore();//#2 蓝底ctx.save();var ratio=0.85;var BLUE="rgb(61,71,203)";for(var i=0;i<5;i++){var theta=i*Math.PI*2/5+initAngle;        var p=createPt2(ct.x,ct.y,R*Math.cos(Math.PI/5),theta+Math.PI/5);var radius=R*Math.sin(Math.PI/5);drawSolidCircle(ctx,p.x,p.y,radius*ratio,BLUE);}drawSolidCircle(ctx,ct.x,ct.y,radius*ratio,BLUE);ctx.restore();//#3 花蕊ctx.save();var r=R*Math.cos(Math.PI/5);for(var i=0;i<5;i++){var theta=i*Math.PI*2/5-initAngle;        var a=createPt2(ct.x,ct.y,r,theta);var b=createPt2(ct.x,ct.y,r,theta+Math.PI*2/5);var xOffset=(b.x-a.x)/4;var yOffset=(b.y-a.y)/4;for(var j=0;j<4;j++){var p=createPt(a.x+j*xOffset,a.y+j*yOffset);ctx.strokeStyle="white";ctx.lineWidth=r*0.02;ctx.beginPath();ctx.moveTo(ct.x,ct.y);ctx.lineTo(p.x,p.y);ctx.stroke();if(j==0){ctx.fillStyle="rgb(250,238,38)";draw5Star(ctx,p.x,p.y,r*0.13);ctx.fill();}else{drawSolidCircle(ctx,p.x,p.y,r*0.05,"white");}}}ctx.restore();//#4 花心ctx.save();var r=R*0.1;drawSolidCircle(ctx,ct.x,ct.y,r,"white");ctx.restore();    
}/*----------------------------------------------------------
函数:用于绘制梅花轮廓
ctx:绘图上下文
x:轮廓中心横坐标
y:轮廓中心纵坐标
R:梅花中正五边形外接圆半径
initAngle:梅花初始旋转角
----------------------------------------------------------*/
function drawMeihua(ctx,x,y,R,initAngle){const ct=createPt(x,y);ctx.beginPath();for(var i=0;i<5;i++){var theta=i*Math.PI*2/5+initAngle;        var p=createPt2(ct.x,ct.y,R*Math.cos(Math.PI/5),theta+Math.PI/5);var radius=R*Math.sin(Math.PI/5);ctx.arc(p.x,p.y,radius,theta+Math.PI/5-Math.PI/2,theta+Math.PI/5+Math.PI/2,false);}
}/*--------------------------------------------------
基本函数:绘制标准正五角星轮廓,可描边,可填充
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:矩形中心纵坐标
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


文章转载自:

http://1Inv4BDk.qytpt.cn
http://Jv04Yiqs.qytpt.cn
http://yuk7HX4s.qytpt.cn
http://Vv2F3z2S.qytpt.cn
http://hFPa3NWv.qytpt.cn
http://KCAStfg8.qytpt.cn
http://tedvUG8I.qytpt.cn
http://EpCFqNzC.qytpt.cn
http://g5YBKE6D.qytpt.cn
http://eqIvpf7f.qytpt.cn
http://wEPVWwn9.qytpt.cn
http://icGCBieA.qytpt.cn
http://bozMfXus.qytpt.cn
http://rYeFPXNO.qytpt.cn
http://dTXpXhTL.qytpt.cn
http://q6BdXZbD.qytpt.cn
http://AKNLvgCJ.qytpt.cn
http://sixc3odD.qytpt.cn
http://F22WkVDK.qytpt.cn
http://30hac0NW.qytpt.cn
http://H5vfcVLX.qytpt.cn
http://nI2Fgv9O.qytpt.cn
http://pW6FyKni.qytpt.cn
http://HCyIXIfW.qytpt.cn
http://u4V3c6cd.qytpt.cn
http://ZXT9xuQh.qytpt.cn
http://xCFX0MyL.qytpt.cn
http://lYhSaWF9.qytpt.cn
http://bgjwFilN.qytpt.cn
http://CYq5Ijdb.qytpt.cn
http://www.dtcms.com/a/378587.html

相关文章:

  • 不同局域网远程桌面连接:设置让外网电脑直接windows自带远程桌面访问内网计算机,简单3步实现通用详细教程
  • set 认识及使用
  • 如何打造“高效、安全、精准、可持续”的智能化实验室?
  • 究竟什么时候用shared_ptr,什么时候用unique_ptr?
  • 前端抽象化,打破框架枷锁:react现代化项目中的思想体现
  • 基于开源AI智能名片、链动2+1模式与S2B2C商城小程序的流量运营与个人IP构建研究
  • gstreamer:创建组件、管道和总线,实现简单的播放器(Makefile,代码测试通过)
  • Kibana 双栈网络(Dual-Stack)支持能力评估
  • go 日志的分装和使用 Zap + lumberjack
  • 河北智算中心绿色能源占比多少?
  • 在能源互联网时代天硕工业级SSD固态硬盘为何更受青睐?
  • 关于rust的crates.io
  • 使用Rust实现服务配置/注册中心
  • C++ 类与对象(下):从构造函数到编译器优化深度解析
  • DNS 域名解析
  • EasyDSS重装系统后启动失败?解决RTMP推流平台EasyDss服务启动失败的详细步骤
  • 自动驾驶中的传感器技术45——Radar(6)
  • 第四章 Elasticsearch索引管理与查询优化
  • 拆分了解HashMap的数据结构
  • Sqlite“无法加载 DLL“e_sqlite3”: 找不到指定的模块”解决方法
  • 项目 PPT 卡壳?模型效果 + 训练数据展示模块直接填 ,451ppt.vip预制PPT也香
  • react-native项目通过华为OBS预签名url实现前端直传
  • Linux-> UDP 编程1
  • Pytest+requests进行接口自动化测试2.0(yaml)
  • 【容器使用】如何使用 docker 和 tar 命令来操作容器镜像
  • 科普:在Windows个人电脑上使用Docker的极简指南
  • 【面试场景题】电商订单系统分库分表方案设计
  • 微服务保护全攻略:从雪崩到 Sentinel 实战
  • springcloud二-Sentinel
  • Redis 持久化与高可用实践(RDB / AOF / Sentinel / Cluster 全解析)