I have an image of a bottle and try fill this bottle of some color
I found the fill coordinates, but my background inside of bottle doesn’t filled
.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>
2
Answers
The closest solution I found is this i didnt fill the whole bottle but you just need to change some of the coordinates in polygons i dont have that knowledge about shapes and polygons.
You don’t need an additional
<mask>
.Due to the shape of the bottle, you could use a rounded
<rect>
as clip-path:Draw your clip-path and fill-scale/progress bar shapes first – without clipping anything – to find the right coordinates and dimensions.
Then you can apply, the clip-path.
I’ve used a
<line>
element for the "tubeLiquid" element as it allows us to set thepathLength
attribute to 100.We can easily change the current fill value by updating the
stroke-dasharray
attribute:You’ll find a lot of examples on SO using this method for all kinds of dynamic gauges/progress bars or animated pie charts like "Set svg progress bar percentage in javascript"