Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
592 views
in Technique[技术] by (71.8m points)

animation - Custom SVG progress bar fill

In its simplest form, I want to make a loading page like this website.

I want to use a custom SVG logo (that I have made in illustrator) and horizontally fill the logo as the page loads.

Like a SVG clip mask progress bar (or something).

Please, please help me!

Thanks, Jon

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The simplest way to do this is with a gradient fill.

<linearGradient id="progress">
   <stop id="stop1" offset="0" stop-color="black"/>
   <stop id="stop2" offset="0" stop-color="grey"/>
</linearGradient>

You just need to change the value of offset on both <stop> elements to be the percentage you want - either 0->1 or "0%"->"100%". For example:

An example function might be:

function setProgress(amt)
{
  amt = (amt < 0) ? 0 : (amt > 1) ? 1 : amt;
  document.getElementById("stop1").setAttribute("offset", amt);
  document.getElementById("stop2").setAttribute("offset", amt);
}

This approach works for any SVG element, whether it be text, as in the demo below, or a complicated logo made of paths.

function setProgress(amt)
{
  amt = (amt < 0) ? 0 : (amt > 1) ? 1 : amt;
  document.getElementById("stop1").setAttribute("offset", amt);
  document.getElementById("stop2").setAttribute("offset", amt);
}

  
// Simple test of setProgress().
// We step up from 0 to 1 using timeouts
val = 0;
doTimeout();

function doTimeout() {
  setProgress(val);
  val += 0.01;
  if (val <= 1) {
    setTimeout(doTimeout, 30);
  }
}
text {
  font: 'Times Roman', serif;
  font-size: 125px;
  fill: url(#progress);
}
<svg width="650" height="150">
  <defs>
    <linearGradient id="progress">
      <stop id="stop1" offset="0" stop-color="black"/>
      <stop id="stop2" offset="0" stop-color="grey"/>
    </linearGradient>
  </defs>

  <text x="20" y="120">TILL JANZ</text>
</svg>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...