HTML5画布绘制实心圆、三角形、五边形、五角星

场景

纯原生canvas绘制,可以自定义位置、颜色等信息。
实现

以下封装成方法,调用后传参即可。图形两角间边距暂时写死,可以自行加减参数来调整。

1.实心圆

function circular(x, y, radius, fillColor, strokeColor){

ctx.beginPath();//开始绘制

ctx.arc(x, y, radius, 0, 2 * Math.PI); //arc 的意思是"弧"

ctx.fillStyle = fillColor; //设置填充颜色

ctx.fill(); //开始填充

ctx.strokeStyle = strokeColor; //将线条颜色设置为蓝色

ctx.setLineDash([]);

ctx.stroke(); //stroke() 方法默认颜色是黑色(如果没有上面一行,则会是黑色)。

}

2.三角形

function triangle(x, y, fillColor){

ctx.beginPath();

ctx.moveTo(x, y); //上顶点

ctx.lineTo(x - 23, y + 22); //左顶点

ctx.lineTo(x + 8, y + 15); //右顶点

ctx.stroke();

ctx.fillStyle = fillColor;

ctx.fill();

}

3.五边形

function pentagon(x, y, fillColor){

ctx.beginPath();

ctx.moveTo(x, y); //顶点

ctx.lineTo(x - 15, y + 10); //最左

ctx.lineTo(x - 8, y + 25); //左下

ctx.lineTo(x + 8, y + 25); //右下

ctx.lineTo(x + 15, y + 10); //最右

ctx.closePath();

ctx.fillStyle = fillColor;

ctx.fill();

}

4.五角星

function pointedStar(x, y, angle){

ctx.beginPath();

ctx.rotate(angle*Math.PI/180); //旋转角度 输入number

ctx.moveTo(x, y);

ctx.lineTo(x + 10, y + 30);

ctx.lineTo(x - 15, y + 11);

ctx.lineTo(x + 15, y + 11);

ctx.lineTo(x - 10, y + 30);

ctx.closePath();

ctx.fillStyle = 'black';

ctx.strokeStyle = "rgb(250, 252, 211)";

ctx.stroke();

ctx.fill();

ctx.restore();

}

相关推荐
EndingCoder1 小时前
React从基础入门到高级实战:React 实战项目 - 项目三:实时聊天应用
前端·react.js·架构·前端框架
阿阳微客2 小时前
Steam 搬砖项目深度拆解:从抵触到真香的转型之路
前端·笔记·学习·游戏
德育处主任Pro3 小时前
『React』Fragment的用法及简写形式
前端·javascript·react.js
CodeBlossom3 小时前
javaweb -html -CSS
前端·javascript·html
打小就很皮...4 小时前
HBuilder 发行Android(apk包)全流程指南
前端·javascript·微信小程序
集成显卡5 小时前
PlayWright | 初识微软出品的 WEB 应用自动化测试框架
前端·chrome·测试工具·microsoft·自动化·edge浏览器
前端小趴菜056 小时前
React - 组件通信
前端·react.js·前端框架
Amy_cx6 小时前
在表单输入框按回车页面刷新的问题
前端·elementui
dancing9996 小时前
cocos3.X的oops框架oops-plugin-excel-to-json改进兼容多表单导出功能
前端·javascript·typescript·游戏程序
后海 0_o7 小时前
2025前端微服务 - 无界 的实战应用
前端·微服务·架构