基本思路

<canvas>,HTML5新增元素,必须结合JS使用。见名知意,canvas就是画板的意思,可以在canvas上画画。css画三角形很简单,但是要画五角星呢,不妨试试canvas。

  1. 创建画布对象
    1. 指定画布大小
    2. 画布背景等设置
  2. 创建画笔
    1. 设置画笔(粗细、颜色...)
  3. 选择画法(空心、填充、直线、弧线、矩形、曲线...)
  4. 开始选取画图起始点
    1. 通过相应函数的参数进行设置

蒙版组合操作效果——>涂抹

笔记是在学习Vue3之后补的,所以会有Vue3的语法,不过无关大雅。可以参考:[(154条消息) 【vue3】使用canvas_vue3 canvas_46590928的博客-CSDN博客](https://blog.csdn.net/weixin_46590928/article/details/127358493#:~:text=在vue3中使用canvas 1 1、创建vite%2Bvue3项目并运行。 2,2、创建我们的canvas。 3 3、获取CanvasRenderingContext2D对象并绘制直线。 4 4、模板)

canva使用入门

1、canvas是html5中的一个标签。
新建一个html。并在body中加入canvas标签。

<body>
    <canvas height="600" width="600"></canvas>
</body>

此时canvas已经显示在画板中,只不过因为和body的颜色一样,所以看不出来。在head中加入css样式。这时我们就可以看到canvas了。

<style>
    canvas {
        border:1px solid;
    }
</style>

image.png

2、获取CanvasRenderingContext2D对象用于绘图。
先给canvas一个id属性,

<canvas id='canvas' height="600" width="600"></canvas>

获取

<script>
  const ctx=document.querySelector('#canvas').getContext('2d');
</script>

注意,script标签应该在body标签后(至少在canvas标签后),只有在canvas渲染后才能通过JavaScript代码获取到canvas中的CanvasRenderingContext2D对象。

3、使用JavaScript代码进行绘画。

<script>
  const ctx=document.querySelector('#canvas').getContext('2d');
  ctx.moveTo(100,100);
  ctx.lineTo(100,400);
  ctx.stroke();
</script>

该代码绘制了一条直线。
image.png

关于CanvasRenderingContext2D对象更多的绘制方法请参考官方文档。至少现在我们知道canvas是如何使用的了。

基本形状的绘制

Tip
  • 详尽的文档可参见:HTML Canvas 绘制 (w3school.com.cn)
  • 下面绘制扇形时使用了closePath,意思是将所有端点连接起来(一般是将终点和起点连接起来,形成一个闭合图形。只有图形闭合时,fill才能生效)。
  • 所有函数的参数不需要单位。(设置字体时,ctx.font=‘40px’,需要带单位,但确实不是函数的参数)

绘制折线

一个moveTo配合多个lineTo。可以通过lineWidth设置线宽,还可以设置两个端点和转折处的形状(使用lineCap和lineJoin)

// 使用moveTo,lineTo,lineWidth,lineCap,lineJoin
const drawCurvedLine = () => {
  ctx.moveTo(100, 100);
  ctx.lineTo(400, 100);
  ctx.lineTo(100, 400);
  ctx.lineTo(400, 400);
  ctx.lineCap = 'round';
  ctx.lineJoin = 'round';
  ctx.stroke();
}

绘制矩形

rect方法以及strokeRect和fillRect。效果等效:strokeRect=rect+stroke,fillRect=rect+stroke。

绘制方式:绘制边框,使用stroke,绘制填充,使用fill。strokeStyle可以设置边框颜色,fillStyle可以设置填充颜色。

// 使用rect,srokeStyle,stroke,fillStyle,fill
const drawStrokeRect = () => {
  ctx.rect(100, 100, 100, 100);
  ctx.strokeStyle = 'green';
  ctx.stroke();
}

const drawFillRect = () => {
  ctx.rect(300, 100, 100, 100);
  ctx.fillStyle = 'blue';
  ctx.fill();
}

将绘制一个绿色边框的矩形和蓝色的矩形。然而,当一同调用时,会发现变成了两个一模一样的矩形(绿色边框或者蓝色填充)。

属性作用域:解决上述问题,使用beginPath方法即可。beginPath将后面对于属性的设置隔离开来,以避免覆盖前面的属性。

// 这里还尝试了使用strokeRect和fillRect替代了rect、stroke、fill
const drawStrokeRect = () => {
    ctx.beginPath();
    ctx.strokeStyle='green';
    ctx.strokeRect(100,100,100,100);
}

const drawFillRect = () => {
  ctx.beginPath();
  ctx.fillStyle = 'blue';
  ctx.fillRect(300, 100, 100, 100);
}

绘制弧线

圆圈

ctx.beginPath();
ctx.arc(100,75,50,0,2*Math.PI);
ctx.stroke();

圆弧

ctx.beginPath();
ctx.arc(100,75,50,90/180*Math.PI,2*Math.PI);
ctx.stroke();

扇形

ctx.beginPath();
ctx.moveTo(100,75);
ctx.arc(100,75,50,90/180*Math.PI,2*Math.PI);
ctx.closePath();
ctx.fill();

圆环

  const RINGWIDTH = 10;

  ctx.beginPath();
  ctx.arc(100, 75, 90, 0, 2 * Math.PI);
  ctx.fill();
  ctx.beginPath();
  ctx.arc(100, 75, 90-2*RINGWIDTH, 0, 2 * Math.PI);
  ctx.fillStyle = 'white';
  ctx.fill();