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

}

相关推荐
前端太佬1 分钟前
小程序登录与授权功能全解析:从原理到设计的实战指南 (2025年最新规范实践版)
前端·微信·微信小程序
一抓掉一大把6 分钟前
elementui日历显示红点及根据日程范围判断是否有红点
前端·javascript·elementui
前端.火鸡7 分钟前
使用wavesurferJs实现录音音波效果
开发语言·前端·javascript
anyup14 分钟前
借助 Trae 从 0 到 1 实现海外地图渲染(leaflet + OSM)
前端·数据可视化·trae
六边形66618 分钟前
JavaScript 中本地对象、内置对象、宿主对象的区别与作用
前端·javascript
snakeshe101022 分钟前
深入解析React Hooks:useState与useReducer的区别与实现
前端
程序员韩立26 分钟前
现代全栈开发:Next.js与Node.js实战指南
前端·后端
航Hang*29 分钟前
WEBSTORM前端 —— 第2章:CSS —— 第2节:文字控制属性与CSS特性
前端·css·css3·html5·webstorm
小桥风满袖31 分钟前
Three.js-硬要自学系列18 (模型边界线、几何体顶点颜色、网格模型颜色渐变)
前端·css·three.js
JHC00000031 分钟前
DrissionPage 请求一次换一个代理(不重启chrome)
前端·chrome