This content originally appeared on Envato Tuts+ Tutorials and was authored by Monty Shokeen
Nothing on the web happens instantly. The only difference is in the time it takes for a process to complete. Some processes can happen in a few milliseconds, while others can take up to several seconds or minutes. For example, you might be editing a very large image uploaded by your users, and this process can take some time. In such cases, it is a good idea to let the visitors know that the website is not stuck somewhere but it is actually working on your image and making some progress.
One of the most common ways to show readers how much a process has progressed is to use progress bars. In this tutorial, you will learn how to use the ProgressBar.js library to create different progress bars with simple and complex shapes.
Creating a Basic Progress Bar
Once you have included the library in your project, creating a progress bar using this library is easy. ProgressBar.js is supported in all major browsers, including IE9+, which means that you can use it in any website you are creating with confidence. You can get the latest version of the library from GitHub or directly use a CDN link to add it in your project.
To avoid any unexpected behavior, please make sure that the container of the progress bar has the same aspect ratio as the progress bar. In the case of a circle, the aspect ratio of the container should be 1:1 because the width will be equal to the height. In the case of a semicircle, the aspect ratio of the container should be 2:1 because the width will be double the height. Similarly, in the case of a simple line, the container should have an aspect ratio of 100:strokeWidth for the line.
When creating progress bars with a line, circle, or semicircle, you can simply use the ProgressBar.Shape()
method to create the progress bar. In this case, the Shape
can be a Circle
, Line
, or SemiCircle
. You can pass two parameters to the Shape()
method. The first parameter is a selector or DOM node to identify the container of the progress bar. The second parameter is an object with key-value pairs which determine the appearance of the progress bar.
You can specify the color of the progress bar using the color
property. Any progress bar that you create will have a dark gray color by default. The thickness of the progress bar can be specified using the strokeWidth
property. You should keep in mind that the width here is not in pixels but in terms of a percentage of the canvas size. For instance, if the canvas is 200px wide, a strokeWidth
value of 5 will create a line which is 10px thick.
Besides the main progress bar, the library also allows you to draw a trailing line which will show readers the path on which the progress bar will move. The color of the trail line can be specified using the trailColor
property, and its width can be specified using the trailWidth
property. Just like strokeWidth
, the trailWidth
property also computes the width in percentage terms.
The total time taken by the progress bar to go from its initial state to its final state can be specified using the duration
property. By default, a progress bar will complete its animation in 800 milliseconds.
You can use the easing
property to specify how a progress bar should move during the animation. All progress bars will move with a linear
speed by default. To make the animation more appealing, you can set this value to something else like easeIn
, easeOut
, easeInOut
, or bounce
.
After specifying the initial parameter values, you can animate the progress bars using the animate()
method. This parameter accepts three parameters. The first parameter is the amount up to which you want to animate the progress line. The two other parameters are optional. The second parameter can be used to override any animation property values that you set during initialization. The third parameter is a callback function to do something else once the animation ends.
In the following example, I have created three different progress bars using all the properties we have discussed so far.
var lineBar = new ProgressBar.Line('#line-container', { color: 'orange', strokeWidth: 2, trailWidth: 0.5 }); lineBar.animate(1, { duration: 1000 }); var circleBar = new ProgressBar.Circle('#circle-container', { color: 'white', strokeWidth: 2, trailWidth: 10, trailColor: 'black', easing: 'easeInOut' }); circleBar.animate(0.75, { duration: 1500 }); var semiBar = new ProgressBar.SemiCircle('#semi-container', { color: 'violet', strokeWidth: 2, trailWidth: 0.5, easing: 'bounce' }); semiBar.animate(1, { duration: 3000 });
Animating Text Values With the Progress Bar
The only thing that changes with the animation of the progress bars in the above example is their length. However, ProgressBar.js also allows you to change other physical attributes like the width and color of the stroking line. In such cases, you will have to specify the initial values for the progress bar inside the from
parameter and the final values inside the to
parameter when initializing the progress bars.
You can also tell the library to create an accompanying text element with the progress bar to show some textual information to your users. The text can be anything from a static value to a numerical value indicating the progress of the animation. The text
parameter will accept an object as its value.
This object can have a value
parameter to specify the initial text to be shown inside the element. You can also provide a class name to be added to the text element using the className
parameter. If you want to apply some inline styles to the text element, you can specify them all as a value of the style
parameter. All the default styles can be removed by setting the value of style
to null
. It is important to remember that the default values only apply if you have not set a custom value for any CSS property inside style
.
The value inside the text element will stay the same during the whole animation if you don't update it yourself. Luckily, ProgressBar.js also provides a step
parameter which can be used to define a function to be called with each animation step. Since this function will be called multiple times each second, you need to be careful with its use and keep the calculations inside it simple.
var lineBar = new ProgressBar.Line("#line-container", { strokeWidth: 4, trailWidth: 0.5, from: { color: "#FF9900" }, to: { color: "#00FF99" }, text: { value: '0', className: 'progress-text', style: { color: 'black', position: 'absolute', top: '-30px', padding: 0, margin: 0, transform: null } }, step: (state, shape) => { shape.path.setAttribute("stroke", state.color); shape.setText(Math.round(shape.value() * 100) + ' %'); } }); lineBar.animate(1, { duration: 4000 }); var circleBar = new ProgressBar.Circle("#circle-container", { color: "white", strokeWidth: 2, trailWidth: 25, trailColor: "black", easing: "easeInOut", from: { color: "#FF9900", width: 1 }, to: { color: "#FF0099", width: 25 }, text: { value: '0', className: 'progress-text', style: { color: 'black', position: 'absolute', top: '45%', left: '42%', padding: 0, margin: 0, transform: null } }, step: (state, shape) => { shape.path.setAttribute("stroke", state.color); shape.path.setAttribute("stroke-width", state.width); shape.setText(Math.round(shape.value() * 100) + ' %'); } }); circleBar.animate(0.75, { duration: 1500 }); var semiBar = new ProgressBar.SemiCircle("#semi-container", { color: "violet", strokeWidth: 2, trailWidth: 8, trailColor: "black", easing: "bounce", from: { color: "#FF0099", width: 1 }, to: { color: "#FF9900", width: 2 }, text: { value: '0', className: 'progress-text', style: { color: 'black', position: 'absolute', top: '45%', left: '50%', padding: 0, margin: 0, transform: null } }, step: (state, shape) => { shape.path.setAttribute("stroke", state.color); shape.path.setAttribute("stroke-width", state.width); shape.setText(Math.round(shape.value() * 100) + ' %'); } }); semiBar.animate(0.75, { duration: 2000 });
Creating Progress Bars With Custom Shapes
Sometimes, you might want to create progress bars with different shapes that match the overall theme of your website. ProgressBar.js allows you to create progress bars with custom shapes using the Path()
method. This method works like Shape()
but provides fewer parameters to customize the progress bar animation. You can still provide a duration
and easing
value for the animation. If you want to animate the color and width of the stroke used for drawing the custom path, you can do so inside the from
and to
parameters.
The library does not provide any way to draw a trail for the custom path, as it did for simple lines and circles. However, you can create the trail yourself fairly easily. In the following example, I have created a triangular progress bar using the Path()
method.
Before writing the JavaScript code, we will have to define our custom SVG path in HTML. Here is the code I used to create a simple triangle:
<svg xmlns="https://www.w3.org/2000/svg" version="1.1" x="0" y="0" width="300" height="300" viewBox="0 0 300 300"> <path d="M 50 50 L 200 100 L 200 300 z" fill="none" stroke="#ddd" stroke-width="1"/> <path id="triangle" d="M 50 50 L 200 100 L 200 300 z" fill="none" stroke="blue" stroke-width="5"/> </svg>
You might have noticed that I created two different path elements. The first path has a light gray color which acts like the trail we saw with simple progress bars in the previous section. The second path is the one that we animate with our code. We have given it an id
which is used to identify it in the JavaScript code below.
var path = new ProgressBar.Path("#triangle", { duration: 6000, from: { color: "#ff0000", width: 2 }, to: { color: "#0099ff", width: 10 }, strokeWidth: 4, easing: "easeInOut", step: (state, shape) => { shape.path.setAttribute("stroke", state.color); shape.path.setAttribute("stroke-width", state.width); } }); path.animate(1);
Using Progress Bars on Webpages
Progress bars are meant to indicate the progress of a process to users. You can use them for a variety of tasks like image upload, indicating password strength, editing images, processing a large table etc.
In this section, we will create a simple form with a textarea
element to input some text. We are supposed to write at least 100 characters inside it and the progress bar will gradually turn from red to green. The markup will look something like this:
<form action="" class="form-example"> <textarea name="answer" class="answer" placeholder="Keep writing till you are at 100 characters..."></textarea> </form> <div id="circle-container"></div>
Nothing fancy here, just a simple form and a div
that will contain our circular progress bar.
Lets begin by creating an instance of our circular progress bar with the following code:
const circleBar = new ProgressBar.Circle("#circle-container", { from: {color: "#ff0000"}, to: {color: "#008800"}, strokeWidth: 8, trailWidth: 1, trailColor: "#666", easing: "easeInOut", step: (state, shape) => { shape.path.setAttribute("stroke", state.color); } });
We use the from
and to
parameters to specify that the progress bar should originally be red and ultimately turn green. Keep in mind that simply setting the values for from
and to
won't result in any color change. The change in the color of the progress bar is accomplished with the use of step
parameter where we set the value of stroke
attribute.
Now, lets write some code to attach a keydown
event listener to the textarea
element. We will get the length of text content inside the element and divide it by the minimum length that we want the text content to be. In our case, we just want to count till 100 characters. So, we divide by 100.
Finally, we use the animate()
method and pass the value of progress
to it along with animation duration.
const textArea = document.querySelector('form textarea'); textArea.addEventListener('keydown', (event) => { let content = event.target.value; const min_length = 100; let progress = content.length/min_length; if(progress > 1) { progress = 1; } circleBar.animate(progress, { duration: 50 }); });
This completes our integration of the progress bar with the textarea
element. You can play around with the code inside the following demo:
For practice, you should consider integrating a progress bar with a simple password checker.
Final Thoughts
As you saw in this tutorial, ProgressBar.js allows you to easily create different kinds of progress bars with ease. It also gives you the option to animate different attributes of the progress bar like its width and color.
Not only that, but you can also use this library to change the value of an accompanying text element in order to show the progress in textual form. This tutorial covers everything that you need to know to create simple progress bars. However, you can go through the documentation to learn more about the library.
If there is anything that you would like me to clarify in this tutorial, feel free to let me know in the comments.
Post thumbnail created with OpenAI DALL-E.
This content originally appeared on Envato Tuts+ Tutorials and was authored by Monty Shokeen
Monty Shokeen | Sciencx (2018-02-24T11:57:11+00:00) Creating Stylish and Responsive Progress Bars Using ProgressBar.js. Retrieved from https://www.scien.cx/2018/02/24/creating-stylish-and-responsive-progress-bars-using-progressbar-js/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.