使用canvas绘制基础线条和图片,文字等

<div class="box" >
    <canvas id="cont" width="1200px" height="900px"
    >您的浏览器版本过低,请升级浏览器</canvas
  >

如果画线条的画总共有7步

  /*canvas只能使用原生js来操作*/
        /*
        1、获取画布
        2、获取画布上下文
        3、开始一条路径
        4、确定起始点
        5、确定结束点
        6、着色
        7、结束路径
        */
        //第一步 获取dom元素
        let canvas = document.querySelector('#cont');
        //第二步 获取画布上下文  就是让你有能画上去的能力
        let ctx = canvas.getContext('2d');
        //第三步 开始一条路径
        ctx.beginPath();
        //第四步 确定起始点
        ctx.moveTo(100, 100);
        //第五步 确定结束点
        ctx.lineTo(400, 400);
        //第六步 开始上颜色
        ctx.stroke();
        //第七步 关闭路径
        ctx.closePath();

如果你画完一条线还有东西要画,第七步不用写

画图片和线条

 //第一步 获取dom元素
        let canvas = document.querySelector("#cont");
   //第二步 获取画布上下文  就是让你有能画上去的能力
        let ctx = canvas.getContext("2d");
          // 这是自己写的图片的方法
          //图片参数 1位置和宽高 2. 图片路径 3,文字参数,不需要文字不传
      imgFn([drawer.x, drawer.y, drawer.w, drawer.h], "./img/1.png", [
          "xx变压器",
          0,
          drawer.h + 20,
        ]);
            // 绘制图片的方法
        function imgFn(arr, str, textArr) {
          var img = new Image(); //创建img元素
          img.onload = function () {
            // 参数 1:要绘制的 img   参数 2、3:绘制的 img 在 canvas 中的坐标 参数4,5是width,height
            ctx.drawImage(img, ...arr);
          };
          img.src = str; //设置图片源地址
          // 如果需要文字   //画文字
          if (textArr) {
            ctx.font = "20px 微软雅黑";
            ctx.fillText(...textArr);
          }
        }


画线条

  drawLine(
          drawer.w + 10,
          drawer.h / 2,
          drawer.w + 10 + 250,
          drawer.h / 2,
          "#000",
          1,
          [result[0].lineName, drawer.w + 100, drawer.h / 2 - 10]
        );
 /**
         * 画直线
         * @Pparam x1   起始位置x
         * @Pparam y1   起始位置y
         * @Pparam x2   结束位置x
         * @Pparam y2   结束位置y
         * @Pparam color    线颜色
         * @Pparam width    线宽度
         */

        function drawLine(x1, y1, x2, y2, color, width, textArr) {
          ctx.beginPath();
          ctx.moveTo(x1, y1);
          ctx.lineTo(x2, y2);
          ctx.strokeStyle = color;
          ctx.lineWidth = width;
          ctx.stroke();
          ctx.closePath();
          if (textArr) {
            ctx.font = "20px 宋体";
            ctx.fontWeight = "400";
            ctx.fillText(...textArr);
          }
        }
    }


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 中可以使用 HTML5 的 Canvas API 来实现在页面上画画,可以通过在 Vue 组件中添加 canvas 元素,并在 mounted 钩子函数中获取其 2D 上下文来实现。 以下是一个实现在页面上画画的示例代码: ```html <template> <div> <canvas ref="canvas" @mousedown="startDrawing" @mousemove="draw" @mouseup="stopDrawing"></canvas> </div> </template> <script> export default { name: 'DrawCanvas', data() { return { isDrawing: false, lastX: 0, lastY: 0, ctx: null, } }, mounted() { this.ctx = this.$refs.canvas.getContext('2d'); }, methods: { startDrawing(e) { this.isDrawing = true; this.lastX = e.offsetX; this.lastY = e.offsetY; }, draw(e) { if (!this.isDrawing) return; this.ctx.beginPath(); this.ctx.moveTo(this.lastX, this.lastY); this.ctx.lineTo(e.offsetX, e.offsetY); this.ctx.stroke(); this.lastX = e.offsetX; this.lastY = e.offsetY; }, stopDrawing() { this.isDrawing = false; }, }, } </script> <style scoped> canvas { width: 100%; height: 100%; border: 1px solid #000; } </style> ``` 上面的代码中,我们在组件中添加了一个 canvas 元素,并为其添加了三个事件监听器,分别是 @mousedown、@mousemove 和 @mouseup,用于开始、绘制和停止绘制画画。 在 mounted 钩子函数中,我们获取了 canvas 元素的 2D 上下文,并将其保存在组件的 this.ctx 属性中。 在组件的 methods 中,我们定义了三个方法,分别是 startDrawing、draw 和 stopDrawing。 startDrawing 方法用于在鼠标按下时记录当前鼠标的位置,并将 isDrawing 属性设置为 true。 draw 方法用于在鼠标移动时绘制线条,如果 isDrawing 为 true,则使用 moveTo 和 lineTo 方法绘制一条线段,并将 lastX 和 lastY 属性更新为当前鼠标的位置。 stopDrawing 方法用于在鼠标松开时将 isDrawing 属性设置为 false,停止绘制画画。 最后,我们在组件的 template 中使用 ref 属性来获取 canvas 元素,并为其添加样式。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值