css如何实现两个圆形重叠?

例如image.png
并把这个作为背景,在上边可以放置我们设置的标题和小图片

阅读 2.6k
2 个回答

index.html

<!doctype html>
<style>
  .test {
    background-image: paint(arc);
    width: 200px;
    height: 200px;
    border: 2px solid #eee;
  }
</style>
<div class="test">
  <h1>测试</h1>
</div>
<script>

  if ('paintWorklet' in CSS) {
    CSS.paintWorklet.addModule('demo1.js');
  } else {
    document.body.innerHTML = 'You need support for <a href="https://drafts.css-houdini.org/css-paint-api/">CSS Paint API</a> to view this demo :(';
  }
</script>

demo1.js

class arc {
  paint(ctx, geom, properties) {
    ctx.lineWidth = 2
    ctx.strokeStyle = 'cyan'

    ctx.beginPath();
    ctx.arc(geom.width, 0, geom.width, 0, 2 * Math.PI);
    ctx.stroke();

    ctx.beginPath();
    ctx.arc(0, 0, geom.width - 30, 0, 2 * Math.PI);
    ctx.fillStyle = '#fff'
    ctx.fill();

    ctx.beginPath();
    ctx.arc(0, 0, geom.width - 30, 0, 2 * Math.PI);
    ctx.stroke();
  }
}

registerPaint('arc', arc);

两种试一下,微调一下就行了
<!DOCTYPE html>
<html>
<head>
<style>
.cities {

background-color:black;
color:white;
margin:20px;
padding:20px;

}
.circle-1,
.circle-2 {
width: 150px;
height: 150px;
border-radius: 50%;
position: relative;
}

.circle-1 {
background-color: red;
}

.circle-2 {
background-color: green;
top: -75px;
left: 25px;
}
canvas {
max-width: 100%;
height: auto;
}
</style>
</head>

<body>
<canvas id="canvas"></canvas>
<div class="circle-1"></div><div class="circle-2"></div>

</body><script>
const canvas = document.getElementById("canvas");
const ctx = canvas.getContext("2d");

// 绘制红色圆形
ctx.beginPath();
ctx.arc(100, 100, 75, 0, Math.PI * 2);
ctx.fillStyle = "red";
ctx.fill();

// 绘制绿色圆形并实现重叠效果
ctx.beginPath();
ctx.arc(150, 150, 75, 0, Math.PI * 2);
ctx.fillStyle = "green";
ctx.globalCompositeOperation = "source-in";
ctx.fill();
</script>
</html>

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题