勉強したことのメモ

Webエンジニア / プログラマが勉強したことのメモ。

円形のプログレスバーをprogressbar.jsで実装する方法

   2024/04/15  JavaScript

円形のプログレスバーを実装したかった。CSSだったり画像を用いたりする方法もあったけど、その辺りは知識があまり無いのでJavaScriptもしくはjQueryで何とかしたい。調べてみたところprogressbar.jsというのが良さげ。以下に実装方法をメモ。

 

サンプル

https://taitan916.info/sample/progressbar/

 

公式サイト

https://kimmobrunfeldt.github.io/progressbar.js/

 

CDNでの呼び出し方法

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<script src="https://cdnjs.cloudflare.com/ajax/libs/progressbar.js/1.1.0/progressbar.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/progressbar.js/1.1.0/progressbar.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/progressbar.js/1.1.0/progressbar.min.js"></script>

異なるバージョンを使用したい場合は以下から探す。

https://cdnjs.com/libraries/progressbar.js/1.1.0

 

ソースコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>progressbar.jsサンプル</title>
<style>
#progressbar{
width: 100px;
height: 100px;
position: relative;
}
</style>
</head>
<body>
<div id="progressbar"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/progressbar.js/1.1.0/progressbar.min.js"></script>
<script>
var bar = new ProgressBar.Circle(progressbar, {
color: '#aaa',
strokeWidth: 4,
trailWidth: 1,
easing: 'easeInOut',
duration: 5000, //100%になるまでの秒数
text: {
autoStyleContainer: false
},
from: { color: '#aaa', width: 1 },
to: { color: '#333', width: 4 },
step: function(state, circle) {
circle.path.setAttribute('stroke', state.color);
circle.path.setAttribute('stroke-width', state.width);
var value = Math.round(circle.value() * 100);
if( value === 0 ){
circle.setText('');
}else{
circle.setText(value + '%');
}
}
});
bar.text.style.fontSize = '1rem';
bar.animate(1.0);
</script>
</body>
</html>
<html lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>progressbar.jsサンプル</title> <style> #progressbar{ width: 100px; height: 100px; position: relative; } </style> </head> <body> <div id="progressbar"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/progressbar.js/1.1.0/progressbar.min.js"></script> <script> var bar = new ProgressBar.Circle(progressbar, { color: '#aaa', strokeWidth: 4, trailWidth: 1, easing: 'easeInOut', duration: 5000, //100%になるまでの秒数 text: { autoStyleContainer: false }, from: { color: '#aaa', width: 1 }, to: { color: '#333', width: 4 }, step: function(state, circle) { circle.path.setAttribute('stroke', state.color); circle.path.setAttribute('stroke-width', state.width); var value = Math.round(circle.value() * 100); if( value === 0 ){ circle.setText(''); }else{ circle.setText(value + '%'); } } }); bar.text.style.fontSize = '1rem'; bar.animate(1.0); </script> </body> </html>
<html lang="ja">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>progressbar.jsサンプル</title>
<style>
#progressbar{
    width: 100px;
    height: 100px;
    position: relative;
}
</style>
</head>
<body>

    <div id="progressbar"></div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/progressbar.js/1.1.0/progressbar.min.js"></script>
    <script>
    var bar = new ProgressBar.Circle(progressbar, {
        color: '#aaa',
        strokeWidth: 4,
        trailWidth: 1,
        easing: 'easeInOut',
        duration: 5000, //100%になるまでの秒数
        text: {
            autoStyleContainer: false
        },
        from: { color: '#aaa', width: 1 },
        to: { color: '#333', width: 4 },
        step: function(state, circle) {
            circle.path.setAttribute('stroke', state.color);
            circle.path.setAttribute('stroke-width', state.width);
            var value = Math.round(circle.value() * 100);
            if( value === 0 ){
                circle.setText('');
            }else{
                circle.setText(value + '%');
            }
        }
    });
    bar.text.style.fontSize = '1rem';
    bar.animate(1.0);
    </script>
</body>
</html>

circle.setText部分で円形内のテキストを変更できる。

 

所感

本プラグインは円形以外にも四角、三角のプログレスバー等も実装できる模様。また、公式サイトにはそれぞれサンプルが用意されているので助かった。

 - JavaScript

  関連記事

JavaScriptにて電子書籍や漫画ビューア用のライブラリ「ToraViewer」の利用方法

電子書籍や漫画ビューア用を設置したい。条件としてはレスポンシブ対応していること。 ...

サイトをダークモード対応させる「Darkmode.js」ライブラリの利用方法

サイトをダークモード対応させる場合、CSSで色々指定しないといけないと思っていた ...

formで複数選択可能なセレクトメニュー(プルダウン)を実装する方法(select2)

フォームで複数選択可能なセレクトボックス(プルダウン)を実装したい。ただHTML ...

パスワードの強度を判定できる「zxcvbn」ライブラリの使用方法

アカウントを作成するようなページで偶にパスワードの強度を判定してメーターで表示さ ...

ローソク足のグラフをJavaScriptで描写する方法(highstock)

株価か仮想通貨などの値動き(チャート)を図表とするローソク足のグラフを何らかの方 ...

S