随笔 - 28  文章 - 2  评论 - 1  阅读 - 25021

canvas绘制

canvas

 <canvas id="canvas" ref="Canvas" />

绘制画布

复制代码
const canvas = ref<any | null>(null);

const draw = (value: string) => {
  // 创建 context 对象
  const ctx = canvas.value?.getContext('2d');

 // 设置画布宽高
  canvas.value.width = 200;
  canvas.value.height = 100;
  // 创建一个线性渐变
  const gradient = ctx.createLinearGradient(0,0,200,0);

  // 设置渐变颜色
  gradient.addColorStop(0, "red");
  gradient.addColorStop(1, "white");
  // 设置fillStyle为当前的渐变对象,并填充
  ctx.fillStyle = gradient;
  ctx.fillRect(10,10,250,40);

  // 设置字体样式和内容
  ctx.font="16px Arial";
  ctx.fillStyle = 'black';
  ctx.fillText("Hello World",8,10)
};
复制代码

展示效果

 其他方法:

fillText():在画布上绘制实心文本;

strokeText():在画布上绘制空心文本;

fillStyle():设置或返回用于填充绘画的颜色、渐变或模式

fillRect():绘制“被填充”的矩形.

 

posted on   blue_hl  阅读(57)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示