美文网首页
canvas-(1) :展示矩形框

canvas-(1) :展示矩形框

作者: 小北呀_ | 来源:发表于2020-08-08 10:03 被阅读0次
<template>
  <div class="box">
    <!-- 
      
      只是:让canvas展示出矩形框,
      

      注意:
      1.  Canvas标签包涵两个东西,一个是元素本身,另一个是元素绘图表面,他们都有各自的尺寸大小默认300*150

          1.通过css设置canvas大小,只是设置了 canvas元素本身
          2.在canvas标签内直接定义width和height,是同时定义元素本身大小和元素绘图表面大小。
          3.通过JS来定义与在canvas标签内定义效果是一样的。
     
      
        所以要设置元素绘图表面 宽高 需要:
        const canvas = document.getElementById('myCanvas')
        const bg = document.getElementsByClassName('bg')[0] 
        canvas.width = bg.clientWidth
        canvas.height = bg.clientHeight
        


      2.canvas本身画图片背景失败,所以现在是图片和canvas 定位,这样显示


     -->
    <div class="bg"></div>
    <canvas id="myCanvas" width="500px" height="500px"></canvas>
  </div>
</template>
<script>
  export default {
    data () {
      return {
      }
    },
    mounted() {
      var canvas=document.getElementById('myCanvas');
      var ctx=canvas.getContext('2d');
     
      // 画矩形
      ctx.fillStyle='pink';
      ctx.fillRect(20,20,180,180); //(x,y,width,height)
      // 画1矩形边框
      ctx.strokeStyle = "blue";
      ctx.strokeRect(20,20,180,180);

      // 画2矩形边框
      ctx.strokeStyle = "red";
      ctx.strokeRect(100,250,180,180);

      // 画3矩形边框 -126  
      ctx.strokeStyle = "#fff";
      ctx.strokeRect(0,499,137,-126);
      

    }
  }
</script>
<style scoped>
.box{
  width: 100%;
  height: 100%;
  position: relative;
}
.bg{
  width: 500px;
  height: 500px;
  background: url('../assets/123.png');
  background-position: center;
  background-size: cover;
  position: absolute;
  top: 0;
  left: 0;
  /* z-index: 10; */
}
canvas{
  /* z-index: 120; */
  position: absolute;
  top: 0;
  left: 0;
}

</style>



相关文章

网友评论

      本文标题:canvas-(1) :展示矩形框

      本文链接:https://www.haomeiwen.com/subject/jtgvrktx.html