canvas基本操作

canvas基本操作

/**
 * @description: 初始化画布
 * @param {*} id 画布标签的id
 * @return {*} void
 */
initCanvas(id) {
  let canvas = document.getElementById(id);
  if (canvas.getContext) {
    //检测浏览器是否兼容
    let ctx = canvas.getContext("2d"); //你的canvas代码在这里
    return ctx;
  }
  return null;
},
/**
 * @description:绘制矩形
 * @param {*} x 起始x坐标
 * @param {*} y 起始y坐标
 * @param {*} r_width 矩形宽
 * @param {*} r_height 矩形高
 * @param {*} line_width 边框宽度
 * @return {*} void
 */
drawRect(x, y, r_width, r_height, line_width) {
  let ctx = this.initCanvas();
  ctx.fillStyle = "#2E81CE";
  ctx.strokeStyle = "#2E81CE";
  ctx.lineWidth = line_width;
  ctx.fillRect(x, y, r_width, r_height);
  ctx.strokeRect(x, y, r_width, r_height);
},
/**
 * @description:画圆
 * @param {*} x
 * @param {*} y
 * @param {*} radius 半径
 * @param {*} startAngle 开始弧度
 * @param {*} endAngle 结束弧度
 * @param {*} anticlockwise true顺时针绘制,false逆时针绘制
 * @return {*} void
 */
drawCircle(x, y, radius, startAngle, endAngle, anticlockwise) {
  let canvas = this.initCanvas();
  canvas.fillStyle = "#2E81CE";
  canvas.beginPath();
  canvas.arc(x, y, radius, startAngle, endAngle, anticlockwise);
  canvas.closePath();
  canvas.fill();
},
/**
 * @description: 画直线
 * @param {*} x 起始横坐标
 * @param {*} y 起始纵坐标
 * @param {*} x_end 终止横坐标
 * @param {*} y_end 终止纵坐标
 * @param {*} line_width 线的宽度
 * @return {*} void
 */
drawLine(x, y, x_end, y_end, line_width) {
  var canvas = this.initCanvas();
  canvas.strokeStyle = "red";
  canvas.lineWidth = line_width;
  canvas.beginPath();
  canvas.moveTo(x, y); //设置起点
  canvas.lineTo(x_end, y_end); //设置终点
  canvas.closePath();
  canvas.stroke(); //画线
},
posted @   沐雨辰沨  阅读(269)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示