I tried change the coordinates in css, but the level does not stick to the tube and it also breaks the look on image.
i need that gradation was like this:
100 – full, 75 – average, 50 – half, 25 – Low, 0 – Empty
document.getElementsByName('water-level')[0].addEventListener('change', updateWaterLevel);
function updateWaterLevel() {
var fractionFull = this.value / 100;
var dy = (1 - fractionFull) * 150;
document.getElementById("tubeLiquid").setAttribute("transform", "translate(0," + dy +")");
}
.item {
width: 150px;
height: 150px;
}
#tubeLiquid {
background-color: #74ccf4;
clip-path: polygon(40% 24%, 38% 28%, 38% 32%, 38% 86%, 38% 91%, 40% 94%, 44% 96%, 57% 96%, 60% 94%, 62% 91%, 62% 86%, 62% 33%, 62% 29%, 60% 25%, 59% 24%);
}
#bottleMask{
background-color: #FFFFFF;
clip-path: polygon(40% 24%, 38% 28%, 38% 32%, 38% 86%, 38% 91%, 40% 94%, 44% 96%, 57% 96%, 60% 94%, 62% 91%, 62% 86%, 62% 33%, 62% 29%, 60% 25%, 59% 24%);
}
<input type="number" value="" name="water-level" />
<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/HGFQYTj/bottle.png" />
<path id="tubeLiquid" fill="#74ccf4" d="M0,0h300v300H0V0z" clip-path="url(#liquidClip)" mask="url(#bottleMask)" />
</svg>
</div>
2
Answers
As explained in my previous answer, your current clip-path/mask structure is slightly complicated.
Actually, only the css clip-path has an effect on your graphic.
Currently you’re just moving your progress/fill bar via
translate()
.Your clip-path has relative units and will also move:
CSS clip-path: Add a transparent
<rect>
You can circumvent the aforementioned scaling issue by adding a
<rect>
filling the entire viewBox:Native SVG
<clipPath>
This approach has the advantage, you can draw/position your progress elements as they would appear at 100% progress.
Now you can change their height relative to 100%:
The new height of the progress bar would be
But we also need to calculate a new y value to align the progress bar to the bottom
<line>
as progress elementUsing a
<line>
element for the "tubeLiquid" element can simplify the javaScript function even further as it allows us to set thepathLength
attribute to 100.In the example below, an
<input type="range">
determines the percentage values of a linear-gradient.Details are commented in example