使用canvas绘制四个角的指定样式

使用canvas绘制四个角的指定样式

codepen中js代码地址
在这里插入图片描述

1. 实现思路

项目中通过Vue进行封装一个公用的组件形式, 将内容divcanvas放在一个父盒子中, 通过定位实现视觉效果

<template>
  <div class="ai-capability-video-bgc">
    <canvas
      class="ai-capanbility-canvas"
      :id="canvasId"
      :width="canvasWidth"
      :height="canvasHeight"
      v-if="canvasFlag"
    ></canvas>
    <div class="main-box">
      <slot></slot>
    </div>
  </div>
</template>
  • id是每次自动生成的,就是为了防止出现重复的id
created(){
	this.canvasId = `ai-capanbility-canvas_${+new Date()}_${Math.ceil(Math.random() * 1000000)}`
}
  • v-if="canvasFlag"是为了获取到父盒子的宽高之后再渲染
  • JS完整代码
data() {
  return {
    canvasId: '',
    canvasWidth: 0,
    canvasHeight: 0,
    canvasFlag: false
  }
},
created() {
  this.canvasId = `ai-capanbility-canvas_${+new Date()}_${Math.ceil(
    Math.random() * 1000000
  )}`
},
mounted() {
  this.initDom()
},
methods: {
  initDom() {
    // 获取宽高
    const { clientWidth, clientHeight } = document.querySelector(
      '.ai-capability-video-bgc'
    )
    this.canvasWidth = clientWidth
    this.canvasHeight = clientHeight
    this.canvasFlag = true

    this.$nextTick(() => {
      // 设置每次划线的长度
      const canvasWidth = 6
      // 两倍划线的长度
      const doubleCanvasWidth = canvasWidth * 2
      // 根据元素的 宽 - 划线的长度
      const calcWidthByOne = clientWidth - canvasWidth
      // 根据元素的 宽 - 划线的长度 * 2
      const calcWidthBySceond = clientWidth - doubleCanvasWidth

      // 根据元素的 高 - 划线的长度
      const calcHeightByOne = clientHeight - canvasWidth
      // 根据元素的 高 - 划线的长度 * 2
      const calcHeightBySceond = clientHeight - doubleCanvasWidth

      const c = document.getElementById(this.canvasId)
      const ctx = c.getContext('2d')
      ctx.moveTo(0, doubleCanvasWidth)
      ctx.lineTo(0, canvasWidth)
      ctx.lineTo(canvasWidth, 0)
      ctx.lineTo(doubleCanvasWidth, 0)

      ctx.moveTo(calcWidthBySceond, 0)
      ctx.lineTo(calcWidthByOne, 0)
      ctx.lineTo(clientWidth, canvasWidth)
      ctx.lineTo(clientWidth, doubleCanvasWidth)

      ctx.moveTo(clientWidth, calcHeightBySceond)
      ctx.lineTo(clientWidth, calcHeightByOne)
      ctx.lineTo(calcWidthByOne, clientHeight)
      ctx.lineTo(calcWidthBySceond, clientHeight)

      ctx.moveTo(doubleCanvasWidth, clientHeight)
      ctx.lineTo(canvasWidth, clientHeight)
      ctx.lineTo(0, calcHeightByOne)
      ctx.lineTo(0, calcHeightBySceond)
      ctx.strokeStyle = '#2DFFFE'
      ctx.lineWidth = 3
      ctx.stroke()
    })
  }
}

css部分

.ai-capability-video-bgc {
  position: relative;
  height: 100%;
  width: 100%;
  .ai-capanbility-canvas {
    position: absolute;
    z-index: 10;
    height: 100%;
    width: 100%;
  }
  .main-box {
    height: 100%;
    width: 100%;
    
  }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值