在图片上高亮标注区域

<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>图片标注框</title>
  <style>
    .container {
      position: relative;
      width: 600px;
      height: 400px;
      margin: 50px auto;
    }

    .image {
      width: 100%;
      height: 100%;
      display: block;
    }

    .overlay {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: rgba(0, 0, 0, 0.9);
    }

    .annotation-box {
      position: absolute;
      width: 100px;
      height: 100px;
      border: 2px solid red;
      cursor: move;
      background-size: 600px 400px;
    }
  </style>
</head>

<body>
  <div class="container">
    <img src="https://fuss10.elemecdn.com/0/6f/e35ff375812e6b0020b6b4e8f9583jpeg.jpeg" class="image" alt="图片">
    <div class="overlay"></div>
    <div class="annotation-box"></div>
  </div>
  <script>
    const annotationBox = document.querySelector('.annotation-box');
    const container = document.querySelector('.container');
    let isDragging = false;
    let offsetX, offsetY;

    annotationBox.addEventListener('mousedown', (e) => {
      isDragging = true;
      offsetX = e.clientX - annotationBox.offsetLeft;
      offsetY = e.clientY - annotationBox.offsetTop;
    });

    document.addEventListener('mousemove', (e) => {
      if (!isDragging) return;
      let x = e.clientX - offsetX;
      let y = e.clientY - offsetY;
      x = Math.max(0, Math.min(container.clientWidth - annotationBox.clientWidth, x));
      y = Math.max(0, Math.min(container.clientHeight - annotationBox.clientHeight, y));
      annotationBox.style.left = x + 'px';
      annotationBox.style.top = y + 'px';
      annotationBox.style.backgroundImage = `url('https://fuss10.elemecdn.com/0/6f/e35ff375812e6b0020b6b4e8f9583jpeg.jpeg')`;
      annotationBox.style.backgroundSize = `${container.clientWidth}px ${container.clientHeight}px`;
      annotationBox.style.backgroundPosition = `-${x}px -${y}px`;
    });


    document.addEventListener('mouseup', () => {
      isDragging = false;
    });
  </script>
</body>

</html>

综合思路就是在原始图片上添加蒙层然后,在标注区域截取图片背景展示

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值