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

}

相关推荐
用户11977079480731 分钟前
GitHub 电商开源项目分享:用 sharp 批量出饰品主图,试了裁剪、细节图和色彩配置文件三个坑
前端·node.js
deli0076 分钟前
地图定位为什么偏了 500 米?GPS 与火星坐标实测
前端
TK泰妞8 分钟前
节日促销前,TikTok 商品视频应该提前准备什么?
前端·人工智能·节日
CCIE-Yasuo19 分钟前
网络安全-学习web开发-列表、标签
前端·学习·web安全
小大宇23 分钟前
Vue知识点-ElementPlus-06 单选+多选+下拉+日期选择
前端·vue.js·elementui
用户114818678948425 分钟前
Nuxt 3 学习版 Demo:跟着做一遍,从跑起来到自己会改
前端
hiahiahia1231 小时前
Streaming Model Output
开发语言·前端
paopaokaka_luck1 小时前
基于springboot+vue的益暖微光公益服务平台(DeepSeek公益助手、善款信息追溯、志愿服务积分闭环、ECharts运营分析、活动状态自动流转)
java·前端·javascript·spring boot·mysql·架构·echarts
栀椩1 小时前
Electron 内嵌页面跨域问题(CORS/PNA)解决全记录
前端·javascript·electron
asaotomo1 小时前
截图识题、AI 讲解、笔记沉淀:Chrome 扩展体验
前端·chrome·笔记