svg+js实现mask图片遮罩参数调节操控效果代码

代码语言:html

所属分类:其他

代码描述:svg+js实现mask图片遮罩参数调节操控效果代码

代码标签: svg js mask 图片 遮罩 参数 调节 操控

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开

<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  
  
  
<style>
body {
  font-family: "Helvetica Neue", Arial, sans-serif;
  background-color: #f3f4f6;
  color: #333;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  margin: 0;
}

.controls {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
  background-color: #fff;
  padding: 15px;
  border-radius: 8px;
  box-shadow: 0 8px 15px rgba(0, 0, 0, 0.1);
  margin-bottom: 30px;
  width: 80%;
}

.controls label {
  font-size: 14px;
  font-weight: bold;
  margin-bottom: 5px;
}

.controls div {
  display: flex;
  flex-direction: column;
}

.controls select,
.controls input[type=range] {
  padding: 5px;
  border: 1px solid #ccc;
  border-radius: 5px;
  transition: all 0.2s ease-in-out;
  width: 100%;
}

.demo-container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 90%;
  max-width: 800px;
  height: auto;
  padding: 20px;
  background-color: #e0e0e0;
  border-radius: 10px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  overflow: hidden;
}

.masked-element {
  width: 100%;
  height: auto;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #333;
  transition: mask-position 0.3s ease, mask-size 0.3s ease;
  position: relative;
  cursor: grab;
}

.masked-element.dragging {
  cursor: grabbing;
}

.masked-element img {
  width: 100%;
  height: auto;
  max-width: 100%;
  object-fit: cover;
}

p {
  max-width: 50vw;
  font-size: 14px;
}

@media (max-width: 768px) {
  .controls {
    width: 90%;
    grid-template-columns: 1fr;
  }

  .demo-container {
    width: 100%;
  }

  p {
    max-width: 100%;
    margin: 1rem;
  }
}
@media (min-width: 769px) and (max-width: 1024px) {
  .controls {
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
  }

  .demo-container {
    width: 90%;
  }

  p {
    max-width: 80%;
    margin: 1rem;
  }
}
@media (min-width: 1025px) {
  .controls {
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    width: 70%;
  }

  .demo-container {
    width: 70%;
  }

  p {
    margin: 1rem;
  }
}
</style>


  
  
</head>

<body translate="no">
  <p>
  This demo allows users to explore CSS masking by adjusting the position, size, and repetition of a mask applied to an image. Users can drag the mask within the bounds of the image or fine-tune its position using range sliders. The demo highlights how CSS mask-image, mask-position, and mask-size properties can create visually striking effects by revealing portions of an underlying image through a custom shape. Whether you’re adjusting a single mask or experimenting with repeating patterns, this tool provides an intuitive way to experiment with CSS masking techniques interactively.
</p>
<div class="controls">
  <div>
    <label for="maskImage">Mask Image:</label><select id="maskImage" onchange="updateMask()">
      <option value="">
        Select an Image to start
      </option>
      <option value="//repo.bfw.wiki/bfwrepo/images/mask/compass.webp">
        Compass
      </option>
      <option value="//repo.bfw.wiki/bfwrepo/images/mask/canoe.webp">
        Canoe
      </option>
      <option value="//repo.bfw.wiki/bfwrepo/images/mask/hiker.webp">
        Hiker
      </option>
    </select>
  </div>
  <div>
    <label for="maskPosition">.........完整代码请登录后点击上方下载按钮下载查看

网友评论0