vue canvas 鼠标进入绘制的图形区域高亮显示该区域

<template>
    <div style="width:700px;height:500px;">
      <canvas id="areaCanvas" style="border:1px solid red;width:100%;height:90%;"> 
      </canvas>
    </div>
</template>

<script>
  var inside = require('point-in-polygon');
  export default {
    data () {
      return {
        pointsArr:[[0,20],[30,20],[10,0]],
        collectionArr:[],

      }
    },
    mounted() {
      this.collectionArr.push(this.pointsArr);
      this.collectionArr.push([[20,30],[50,30],[50,50],[20,50]]);
      this.collectionArr.push([[60,3],[50,20],[70,40],[80,30],[70,20],[80,0]]);
      this.drawPolygons();


    },

    methods: {
      drawPolygons: function(){
        var canvas = document.getElementById('areaCanvas');
        if (null == canvas || !canvas.getContext) return;
        var ctx = canvas.getContext("2d");
        // 清除画布,准备绘制
       // ctx.clearRect(0, 0, canvas.width, canvas.height);
        this.drawPolygonByPoint(ctx,null);
        var _this = this;
        canvas.onmousemove = function (e) {
          ctx.clearRect(0, 0, canvas.width, canvas.height);
          var location = _this.getLocation(canvas, e.clientX, e.clientY);
          _this.drawPolygonByPoint(ctx, location);
        };
      },
      drawPolygonByPoint: function(ctx,location){
        this.collectionArr.map(pointsArr =>{
          if(location != null && inside(location, pointsArr) == true){
            //绘制高亮图形
            this.drawHighLightPolygon(ctx,pointsArr);
          }else{
            //绘制一般图形
            this.drawGeneralPolygon(ctx,pointsArr);
          }
        });
      },
      drawGeneralPolygon: function(ctx,pointsArr){
        ctx.beginPath();
        for(let i = 0; i < pointsArr.length; i++){
          if(i == 0){
            ctx.moveTo(pointsArr[i][0], pointsArr[i][1]);
          }else if(i < pointsArr.length - 1){
            ctx.lineTo(pointsArr[i][0], pointsArr[i][1]);
          }else{
            ctx.lineTo(pointsArr[i][0], pointsArr[i][1]);
            ctx.closePath();
            ctx.stroke();
          }
        }
      },
      drawHighLightPolygon: function(ctx,pointsArr){
        ctx.beginPath();
        for(let i = 0; i < pointsArr.length; i++){
          if(i == 0){
            ctx.moveTo(pointsArr[i][0], pointsArr[i][1]);
          }else if(i < pointsArr.length - 1){
            ctx.lineTo(pointsArr[i][0], pointsArr[i][1]);
          }else{
            ctx.lineTo(pointsArr[i][0], pointsArr[i][1]);
            ctx.fillStyle="#FF7F00";
            ctx.fill();
            ctx.closePath();
            ctx.stroke();
          }
        }
      },
      getLocation: function(canvas,x,y){
        var bbox = canvas.getBoundingClientRect();
        return [(x - bbox.left) * (canvas.width / bbox.width),(y - bbox.top) * (canvas.height / bbox.height)];
          /*
           * 此处不用下面两行是为了防止使用CSS和JS改变了canvas的高宽之后是表面积拉大而实际
           * 显示像素不变而造成的坐标获取不准的情况
          x: (x - bbox.left),
          y: (y - bbox.top)
          */
      },

    }
  }
</script>

<style>

</style>

npm install point-in-polygon

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值