Canvas入门(二)Canvas图形

二、Canvas图形

1.栅格

在我们开始画图之前,我们需要了解一下画布栅格(canvas grid)以及坐标空间。如下图所示,canvas 元素默认被网格所覆盖。通常来说网格中的一个单元相当于 canvas 元素中的一像素。栅格的起点为左上角(坐标为(0,0))。所有元素的位置都相对于原点定位。所以图中蓝色方形左上角的坐标为距离左边(X 轴)x 像素,距离上边(Y 轴)y 像素(坐标为(x,y))。在课程的最后我们会平移原点到不同的坐标上,旋转网格以及缩放。现在我们还是使用原来的设置。

栅格

2.绘制矩形

2.1 绘制矩形方法

​ canvas 提供了三种方法绘制矩形:

  • //绘制填充矩形方法 fillRect(x, y, width, height)
    
  • //绘制描边矩形 strokeRect(x, y, width, height)
    
  • //绘制清理矩形方法 clearRect(x, y, width, height)
    

​ 上面提供的方法之中每一个都包含了相同的参数。x 与 y 指定了在 canvas 画布上所绘制的矩形的左上角(相对于原点)的坐标。width 和 height 设置矩形的尺寸。

2.2 矩形例子
<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=\, initial-scale=1.0">
    <title>矩形</title>
    <style>
        body {
            margin: 0;
            overflow: hidden;
        }
        
        #canvas {
            background: antiquewhite;
        }
    </style>
</head>

<body>
    <canvas id="canvas"></canvas>
    <script>
        const canvas = document.getElementById('canvas');
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;

        //画笔
        const ctx = canvas.getContext('2d');

        //描边宽度
        ctx.lineWidth = 20;

        //描边颜色
        ctx.strokeStyle = 'red';

        //填充矩形方法 fillRect(x, y, w, h);
        ctx.fillRect(200, 100, 400, 200);

        //描边矩形 strokeRect(x, y, w, h)
        ctx.strokeRect(200, 100, 400, 200);

        //描边线
        ctx.lineWidth = 3;
        ctx.strokeStyle = '#000';
        ctx.strokeRect(200, 100, 400, 200);

        //清理矩形方法 clearRect(x, y, w, h)
        // ctx.clearRect(200, 100, 400, 200);
    </script>
</body>

</html>

​ 该例子输出如下图所示:

在这里插入图片描述

3.绘制路径

3.1 绘制路径的步骤
  1. 首先,你需要创建路径起始点。
  2. 然后你使用画图命令去画出路径。
  3. 之后你把路径封闭。
  4. 一旦路径生成,你就能通过描边或填充路径区域来渲染图形。

以下是所要用到的函数:

  • beginPath()
    

    新建一条路径,生成之后,图形绘制命令被指向到路径上生成路径。

  • closePath()
    

    闭合路径之后图形绘制命令又重新指向到上下文中。

  • stroke()
    

    通过线条来绘制图形轮廓。

  • fill()
    

    通过填充路径的内容区域生成实心的图形。

3.2 绘制三角形
/*绘制三角形(直线):lineTo(x,y); */
ctx.beginPath();
ctx.moveTo(100, 100);
ctx.lineTo(400, 100);
ctx.lineTo(400, 300);
ctx.closePath();
ctx.stroke();
ctx.fill();
3.3 绘制圆弧与切线圆弧
/* 圆弧 arc(x,y,半径,开始弧度,结束弧度,方向)*/
ctx.beginPath();
ctx.arc(400, 400, 200, 0, Math.PI * 3 / 2);
ctx.moveTo(600, 600);
ctx.arc(400, 600, 200, 0, Math.PI * 3 / 2);
ctx.stroke();

/*切线圆弧:arcTo(x1,y1,x2,y2,半径)*/
ctx.beginPath();
ctx.moveTo(100, 100);
ctx.lineTo(400, 100);
ctx.lineTo(400, 300);
ctx.stroke();

ctx.beginPath();
ctx.moveTo(100, 100);
ctx.arcTo(400, 100, 400, 300, 100);
ctx.stroke();

3.4 绘制贝塞尔曲线
/*二次贝塞尔曲线:quadraCurveTo(cpx1,cpy1,x,y)*/
ctx.beginPath();
ctx.moveTo(100, 100);
ctx.lineTo(400, 100);
ctx.lineTo(400, 400);
ctx.stroke();

ctx.beginPath();
ctx.moveTo(100, 100);
ctx.quadraticCurveTo(400, 100, 400, 400);
ctx.stroke();


/*三次贝塞尔曲线:bezierCurveTo(cpx1,cpy1,cpx2,cpy2,x,y)*/
ctx.beginPath();
ctx.moveTo(100, 100);
ctx.lineTo(400, 100);
ctx.lineTo(400, 400);
ctx.lineTo(700, 400);
ctx.stroke();

ctx.beginPath();
ctx.moveTo(100, 100);
ctx.bezierCurveTo(400, 0, 400, 400, 700, 400);
ctx.stroke();
3.5 绘制矩形
ctx.beginPath();
ctx.rect(100, 100, 400, 200);
ctx.rect(100, 400, 400, 200);
ctx.stroke();
3.6 案例1-机器人

机器人设计图如下:

在这里插入图片描述

绘制机器人整体代码:

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>机器人</title>
    <style>
        body {
            margin: 0;
            padding: 0;
            overflow: hidden;
        }
        
        #canvas {
            background: antiquewhite;
        }
    </style>
</head>

<body>
    <canvas id="canvas"></canvas>
    <script>
        //画布
        const canvas = document.getElementById('canvas');
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;

        //画笔
        const ctx = canvas.getContext('2d');

        //颜色
        //填充色
        ctx.fillStyle = 'red';

        //画笔宽度
        ctx.lineWidth = 40;

        //面部(填充矩形)
        //填充方法 fillRect(x, y, w, h);
        ctx.fillRect(50, 250, 400, 200);

        //面部轮廓()
        //填充方法 strokeRect(x, y, w, h);
        ctx.strokeRect(50, 250, 400, 200);

        //眼罩
        //填充方法 clearRect(x, y, w, h);
        ctx.clearRect(50, 300, 400, 60);

        //嘴巴
        //直线:lineTo(x, y);
        ctx.beginPath();
        ctx.moveTo(150, 400);
        ctx.lineTo(350, 400);
        ctx.stroke();

        //眼睛
        //圆弧arc(x,y,半径,开始弧度,结束弧度,方向)

        //左眼
        ctx.beginPath();
        ctx.arc(150, 330, 20, 0, Math.PI * 2);

        //右眼
        ctx.moveTo(350 - 20, 340)
        ctx.arc(350, 340, 20, Math.PI, 0);
        ctx.fill();

        //天线
        // bezierCurveTo(cpx1,cpy1,cpx2,cpy2,x,y)
        //左天线
        ctx.beginPath();
        ctx.moveTo(50, 50);
        ctx.bezierCurveTo(150, 50, 150, 250, 250, 250);
        //右天线
        ctx.moveTo(450, 50);
        ctx.bezierCurveTo(350, 50, 350, 250, 250, 250);
        ctx.stroke();

    </script>
</body>

</html>
3.7 案例2-水滴

相较于上一个机器人的简单拼凑,下图这个水滴是对图形的入微级操作,因为它是用多个图形拼成了一个子路径。

在这里插入图片描述

绘制水滴完整代码

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>水滴</title>
    <style>
        html {
            height: 100%;
            overflow: hidden
        }
        
        body {
            height: 100%;
            margin: 0;
        }
        
        #canvas {
            background: antiquewhite;
        }
    </style>
</head>

<body>
    <canvas id="canvas"></canvas>
    <script>
        const canvas = document.getElementById('canvas');
        //canvas充满窗口
        canvas.width = window.innerWidth;
        canvas.height = window.innerHeight;
        //画笔
        const ctx = canvas.getContext('2d');

        /*
        三次贝塞尔曲线:bezierCurverTo(cpx1,cpy1,cpx2,cpy2,x,y)
        圆弧:arc(x,y,半径,开始弧度,结束弧度,方向)
        */
        ctx.fillStyle = '#00acec';

        //位移画布
        ctx.translate(canvas.width / 2, canvas.height / 2);

        //开始新路径
        ctx.beginPath();

        //指定起点,建立子路径
        ctx.moveTo(0, 0);

        //绘制二次贝塞尔曲线
        ctx.quadraticCurveTo(50, -50, 50, -100);

        //绘制圆弧
        ctx.arc(
            0, -100,
            50,
            0, Math.PI,
            true
        );

        //绘制二次贝塞尔曲线
        ctx.quadraticCurveTo(-50, -50,
            0, 0
        );

        //显示路径
        ctx.stroke();
        ctx.fill();
    </script>
</body>

</html>

.height / 2);

    //开始新路径
    ctx.beginPath();

    //指定起点,建立子路径
    ctx.moveTo(0, 0);

    //绘制二次贝塞尔曲线
    ctx.quadraticCurveTo(50, -50, 50, -100);

    //绘制圆弧
    ctx.arc(
        0, -100,
        50,
        0, Math.PI,
        true
    );

    //绘制二次贝塞尔曲线
    ctx.quadraticCurveTo(-50, -50,
        0, 0
    );

    //显示路径
    ctx.stroke();
    ctx.fill();
</script>
```
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
嗨!欢迎来到 Vue Canvas 入门。Vue 是一个流行的 JavaScript 框架,而 Canvas 是一个 HTML5 元素,用于在网页上绘制图形。结合 Vue 和 Canvas,你可以创建动态的图形和交互性应用程序。下面是一个简单的入门示例。 首先,确保你已经安装了 Vue。你可以使用 Vue CLI 来创建一个新的 Vue 项目。 ```shell npm install -g @vue/cli vue create vue-canvas-demo cd vue-canvas-demo npm run serve ``` 接下来,我们将在 Vue 组件中创建一个 Canvas 元素。在 `src/components/Canvas.vue` 文件中添加以下代码: ```html <template> <canvas ref="canvas"></canvas> </template> <script> export default { mounted() { this.draw(); }, methods: { draw() { const canvas = this.$refs.canvas; const ctx = canvas.getContext('2d'); // 在画布上绘制一个矩形 ctx.fillStyle = 'red'; ctx.fillRect(10, 10, 100, 100); } } }; </script> ``` 这是一个简单的 Vue 组件,它在页面上创建了一个红色的矩形。我们使用 `mounted` 钩子来在组件加载后调用 `draw` 方法。在 `draw` 方法中,我们获取 Canvas 元素的引用并获取 2D 上下文对象 (`ctx`),然后使用 `fillRect` 方法绘制一个红色的矩形。 最后,在 `src/App.vue` 文件中引入并使用我们刚刚创建的 Canvas 组件: ```html <template> <div id="app"> <Canvas /> </div> </template> <script> import Canvas from './components/Canvas.vue'; export default { components: { Canvas } }; </script> ``` 现在,可以启动开发服务器并在浏览器中查看效果: ```shell npm run serve ``` 这只是一个简单的入门示例,你可以根据需要在 Canvas 中绘制更复杂的图形和添加交互性。希望对你有所帮助!如果你有任何问题,请随时问我。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值