Clip path inset circle?

前端 未结 6 1294
北荒
北荒 2020-11-28 14:56

Is it possible to create an inset circle clip path so that the clip path would effectively cut a hole through the div in the center opposed to only showing the center?

6条回答
  •  忘掉有多难
    2020-11-28 15:04

    You can create hole in clip-path with this approach:

    let precision = 64;
    let radius = 25;
    let c = [...Array(precision)].map((_, i) => {
      let a = -i/(precision-1)*Math.PI*2;
      let x = Math.cos(a)*radius + 50;
      let y = Math.sin(a)*radius + 50;
      return `${x}% ${y}%`
    })
    
    document.querySelector('div').style.clipPath = 
     `polygon(100% 50%, 100% 100%, 0 100%, 0 0, 100% 0, 100% 50%, ${c.join(',')})`;
    div {
      background: blue;
      width: 200px;
      height: 200px;
    }

    or symply use resulting clip string:

    div {
          background: blue;
          width: 200px;
          height: 200px;
        }

提交回复
热议问题