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();

}

相关推荐
用户6600676685392 分钟前
深入解析JavaScript数组:从内存原理到高效遍历实践
前端·javascript
有点笨的蛋4 分钟前
CSS 定位彻底搞懂:五种 position 的真实差异与最佳实践
前端·css
液态不合群11 分钟前
数字化转型改变了什么?从技术底层到业务本质的深度重构
前端·人工智能·低代码·重构
qiao若huan喜12 分钟前
9、webgl 基本概念 + 复合变换 + 平面内容复习
前端·javascript·信息可视化·webgl
梦幻通灵27 分钟前
Edge浏览器好用插件【持续更新】
前端·edge
sTone8737531 分钟前
Chrome devtools二次开发准备:获取源码和编译
前端·google
龙泉寺天下行走35 分钟前
[Powershell入门教程]第4天:模块、脚本编写、错误处理与 .NET 集成
java·服务器·前端
晴天丨42 分钟前
Vite:下一代前端构建工具深度解析与实践指南
前端
多来哈米1 小时前
Jenkins配置vue前端项目(最简单的操作)
运维·前端·jenkins
一只叁木Meow1 小时前
Vue scoped CSS 与 Element Plus Drawer 样式失效问题深度解析
前端