搜尋

首頁  >  問答  >  主體

我怎麼才能使用clipPath為瓶子填充顏色

我有一個瓶子的圖像,並嘗試用某種顏色填滿這個瓶子

我找到了填充座標,但瓶子內的背景未填滿

.item {
  width: 150px;
  height: 150px;
}

#tubeLiquid {
  background-color: #74ccf4;
  clip-path: polygon(45% 19%, 41% 22%, 41% 29%, 41% 36%, 41% 43%, 41% 50%, 41% 57%, 41% 63%, 41% 70%, 41% 77%, 41% 83%, 41% 90%, 43% 93%, 50% 92%, 56% 93%, 60% 88%, 59% 81%, 60% 75%, 60% 68%, 60% 61%, 59% 55%, 59% 48%, 59% 42%, 59% 35%, 59% 29%, 58% 23%, 54% 19%);
}

#bottleMask{
  background-color: #FFFFFF;
  clip-path: polygon(45% 19%, 41% 22%, 41% 29%, 41% 36%, 41% 43%, 41% 50%, 41% 57%, 41% 63%, 41% 70%, 41% 77%, 41% 83%, 41% 90%, 43% 93%, 50% 92%, 56% 93%, 60% 88%, 59% 81%, 60% 75%, 60% 68%, 60% 61%, 59% 55%, 59% 48%, 59% 42%, 59% 35%, 59% 29%, 58% 23%, 54% 19%);
}
<div class="item">
  <svg viewBox="0 0 300 300">
    <image
      width="300"
      clip-path="url(#bottleMask)"
      xlink:href="https://i.ibb.co/Mhfwtxp/image-removebg-preview-1.png"
    ></image>
    <defs>
      <mask id="bottleMask"></mask>
    </defs>
    <g id="maskedLiquid" mask="url(#bottleMask)">
      <path id="tubeLiquid" fill="#74ccf4"></path>
    </g>
  </svg>
</div>

P粉690200856P粉690200856441 天前530

全部回覆(2)我來回復

  • P粉343141633

    P粉3431416332023-09-10 14:35:30

    您不需要額外的
    由於瓶子的形狀,您可以使用圓形 作為剪輯路徑:

    waterLevel.addEventListener('input', e => {
      let value = +e.currentTarget.value
      tubeLiquid.setAttribute('stroke-dasharray', `${value} 100`)
    })
    svg {
      width: 20%;
    }
    <h3>Draw clip-path</h3>
    <svg viewBox="0 0 300 300">
        <image width="300" href="https://i.ibb.co/Mhfwtxp/image-removebg-preview-1.png" />
        <!-- test clip path -->
        <rect x="115" y="55" width="69" height="235" rx="20" ry="20" fill="red" />
      </svg>
    
    <h3>Draw liquid fill</h3>
    <svg viewBox="0 0 300 300">
        <image width="300" href="https://i.ibb.co/Mhfwtxp/image-removebg-preview-1.png" />
        <line x1="149.5" y1="290" x2="149.5" y2="50" stroke="green" stroke-width="70" />
      </svg>
    
    <h3>Apply clip path</h3>
    <p><input type="range" min="0" max="100" value="100" id="waterLevel" name="water-level" /></p>
    <svg viewBox="0 0 300 300">
        <clipPath id="clip">
              <rect x="115" y="55" width="69" height="235" rx="20" ry="20" fill="red" />
        </clipPath>
        <image width="300" href="https://i.ibb.co/Mhfwtxp/image-removebg-preview-1.png" />
        <line id="tubeLiquid" clip-path="url(#clip)" pathLength="100" stroke-dasharray="100 100" x1="149.5" y1="290" x2="149.5" y2="50" stroke="#74ccf4" stroke-width="80" />
      </svg>

    首先繪製剪輯路徑和填充比例/進度條形狀 - 不剪輯任何內容 - 以找到正確的座標和尺寸。

    然後您可以套用剪輯路徑。
    我為「tubeLiquid」元素使用了 元素,因為它允許我們將 pathLength 屬性設為 100。

    我們可以透過更新 lines-dasharray 屬性輕鬆更改目前填滿值:

    // show 100 %
    stroke-dasharray="100 100"
    
    // show 50 %
    stroke-dasharray="50 100"

    您會發現很多關於使用此方法實現各種動態儀表/進度條或動畫餅圖的範例,例如「在JavaScript 中設定svg 進度條百分比」

    回覆
    0
  • P粉593649715

    P粉5936497152023-09-10 11:34:50

    我發現的最接近的解決方案是我沒有裝滿整個瓶子,但你只需要更改多邊形中的一些座標,我不了解形狀和多邊形。

    <style>
    .item {
      width: 150px;
      height: 150px;
    }
    
    #tubeLiquid {
      background-color: #74ccf4;
      clip-path: polygon(45% 19%, 41% 22%, 41% 29%, 41% 36%, 41% 43%, 41% 50%, 41% 57%, 41% 63%, 41% 70%, 41% 77%, 41% 83%, 41% 90%, 43% 93%, 50% 92%, 56% 93%, 60% 88%, 59% 81%, 60% 75%, 60% 68%, 60% 61%, 59% 55%, 59% 48%, 59% 42%, 59% 35%, 59% 29%, 58% 23%, 54% 19%);
    }
    
    #bottleMask{
      background-color: #FFFFFF;
      clip-path: polygon(45% 19%, 41% 22%, 41% 29%, 41% 36%, 41% 43%, 41% 50%, 41% 57%, 41% 63%, 41% 70%, 41% 77%, 41% 83%, 41% 90%, 43% 93%, 50% 92%, 56% 93%, 60% 88%, 59% 81%, 60% 75%, 60% 68%, 60% 61%, 59% 55%, 59% 48%, 59% 42%, 59% 35%, 59% 29%, 58% 23%, 54% 19%);
    }</style>
    
    <div class="item">
      <svg viewBox="0 0 300 300">
        <defs>
          <mask id="bottleMask" fill="white">
            <path d="M0,0h300v300H0V0z M89,68h122v147H89V68z" />
          </mask>
          <clipPath id="liquidClip">
            <path d="M89,68h122v147H89V68z" />
          </clipPath>
        </defs>
        <image width="300" xlink:href="https://i.ibb.co/Mhfwtxp/image-removebg-preview-1.png" />
        <path id="tubeLiquid" fill="#74ccf4" d="M0,0h300v300H0V0z" clip-path="url(#liquidClip)" mask="url(#bottleMask)" />
      </svg>
    </div>

    回覆
    0
  • 取消回覆