Gathering detailed insights and metrics for canvas-circular-countdown
Gathering detailed insights and metrics for canvas-circular-countdown
Gathering detailed insights and metrics for canvas-circular-countdown
Gathering detailed insights and metrics for canvas-circular-countdown
Draw a configurable circular canvas countdown timer.
npm install canvas-circular-countdown
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
17 Stars
126 Commits
8 Forks
2 Watchers
2 Branches
2 Contributors
Updated on Mar 24, 2025
Latest Version
1.8.0
Package Id
canvas-circular-countdown@1.8.0
Unpacked Size
86.41 kB
Size
13.11 kB
File Count
15
NPM Version
8.15.0
Node Version
18.7.0
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Draw a configurable circular canvas countdown timer.
Check here for a live demo.
NOTE: Depends on window.requestAnimationFrame
. If your environment does not support it, you can polyfill.
1$ npm install canvas-circular-countdown --save
The library is exported in UMD, CommonJS, and ESM formats. You can import it the following ways:
1import CanvasCircularCountdown from 'canvas-circular-countdown';
1const CanvasCircularCountdown = require('canvas-circular-countdown').default;
1<script src="https://unpkg.com/canvas-circular-countdown"></script>
1new CanvasCircularCountdown(element, [options], [onTimerRunning])
Param | Type | Description |
---|---|---|
element | HTMLElement | (Required) The element that the countdown is drawn to. If is a canvas element, the countdown will be drawn on it; otherwise a canvas element will be created and appended to element . |
options | Object | (Optional) Options that can be overriden by user. See below for more details about each option. |
onTimerRunning | Function | (Optional) Function to be executed while timer is running. Parameters passed by include the percentage remaining, an object containing the remaining and elapsed time and the CanvasCircularCountdown instance. |
Param | Type | Default | Description |
---|---|---|---|
duration | Number | 60 * 1000 | The timer's duration in milliseconds. Throws TypeError if the provided value is not a Number or is NaN . |
elapsedTime | Number | 0 | The time that has elapsed in milliseconds. Throws TypeError if the provided value is not a Number or is NaN . |
throttle | Number | undefined | Throttle duration in milliseconds. Must be a number lower or equal to the duration option. If provided, it limits the number of times the canvas is drawn in the given period, therefore the number of times the callback function onTimerRunning can be called. You can use it if you perform heavy tasks inside the onTimerRunning callback function to improve performance. Always prefer small numbers, eg. 250, etc |
clockwise | Boolean | false | Determines the direction of the progress ring. By default the direction is counterclockwise. |
radius | Number | 150 | The radius of the circular countdown in pixels. |
progressBarWidth | Number | 15 | The circular progress bar in pixels. |
progressBarOffset | Number | 5 | The number of pixels that will be left between the edges of the progress bar and the rest of the circle. |
circleBackgroundColor | String | "#ffffff" | The background color of the main circle. |
emptyProgressBarBackgroundColor | String | "#dddddd" | The background color of the progress bar when is empty. |
filledProgressBarBackgroundColor 1 | String|Function | "#00bfeb" | The background color of the progress bar when is filled. |
captionText 1 | String|Function | undefined | The text to be displayed as caption inside the countdown circle. By default if it is left as undefined and showCaption is set to true, the remaining percentage will be displayed. |
captionColor 1 | String|Function | "#343a40" | The foreground color of the caption string. |
captionFont 1 | String|Function | "20px sans-serif" | The text style of the caption string. Check here for available values. |
showCaption 1 | Boolean|Function | true | Whether the caption text inside the countdown circle will be displayed or not. |
draw | Function | undefined | A function that exposes CanvasRenderingContext2D to allow free drawing on the canvas element. The function is called with 2 arguments. The first argument is the CanvasRenderingContext2D and the second is an object with information like the canvas width/height, the remaining percentage and an object containing the remaining and elapsed time. |
1 If it is a function, the remaining percentage and an object containing the remaining and elapsed time are passed as parameters and it should return the appropriate type for each option. For example, for showCaption
should return a boolean (true or false), but for captionColor
should return a string. Useful when you need to change some options' values depending on the remaining percentage or the remaining/elapsed time.
Start the timer. If the timer has been already started, the timer will just resume.
1start() => CanvasCircularCountdown
Stop/Pause the timer.
1stop() => CanvasCircularCountdown
Resets the timer to initial specified duration
extracting the elapsedTime
if provided.
1reset() => CanvasCircularCountdown
Change the styles of the circular countdown at any time while te timer running.
Param | Type | Default | Description |
---|---|---|---|
options | Object | {} | Any of the options provided above can be changed apart from the duration , elapsedTime and throttle options. |
1style(options = {}) => CanvasCircularCountdown
Set the timer's duration (in milliseconds), at any time, even when the timer is running.
Param | Type | Default | Description |
---|---|---|---|
time | Number | - | The timer's duration in milliseconds. Throws TypeError if the provided value is not a Number or is NaN . |
1setDuration(time) => CanvasCircularCountdown
Set the timer's elapsed time (in milliseconds), at any time, even when the timer is running.
Param | Type | Default | Description |
---|---|---|---|
time | Number | - | The timer's elapsed time in milliseconds. Throws TypeError if the provided value is not a Number or is NaN . |
1setElapsedTime(time) => CanvasCircularCountdown
Create a new instance of CanvasCircularCountdown
with the default configuration and immediately start the countdown timer. Pause the countdown after 5 seconds have passed.
1<canvas id="countdown-canvas"></canvas>
1new CanvasCircularCountdown(document.getElementById('countdown-canvas'), (percentage, time, instance) => {
2 if (time.elapsed >= 5000) {
3 instance.stop();
4 }
5}).start();
Same as the above example, but with custom configuration.
1<canvas id="countdown-canvas"></canvas>
1new CanvasCircularCountdown(document.getElementById('countdown-canvas'), { 2 duration: 30 * 1000, 3 radius: 200, 4 progressBarWidth: 20, 5 progressBarOffset: 0, 6 circleBackgroundColor: '#f5f5f5', 7 emptyProgressBarBackgroundColor: '#b9c1c7', 8 filledProgressBarBackgroundColor: '#17a2b8', 9 captionColor: '#6c757d', 10 captionFont: '22px serif', 11 showCaption: true 12}, (percentage, time, instance) => { 13 if (time.elapsed >= 5000 ) { 14 instance.stop(); 15 } 16}).start();
1<canvas id="countdown-canvas"></canvas>
1const pickColorByPercentage = (percentage, time) => { 2 switch (true) { 3 case percentage >= 75: 4 return '#28a745'; // green 5 case percentage >= 50 && percentage < 75: 6 return '#17a2b8'; // blue 7 case percentage >= 25 && percentage < 50: 8 return '#ffc107'; // orange 9 default: 10 return '#dc3545'; // red 11 } 12} 13 14new CanvasCircularCountdown(document.getElementById('countdown-canvas'), { 15 filledProgressBarBackgroundColor: pickColorByPercentage, 16 captionColor: pickColorByPercentage 17}).start();
1<div id="countdown-container"> 2 <canvas id="countdown-canvas"></canvas> 3</div>
1#countdown-container { 2 width: 100%; 3 max-width: 500px; 4}
1const containerEl = document.getElementById('countdown-container');
2const countdownEl = document.getElementById('countdown-canvas');
3
4const countdown = new CanvasCircularCountdown(countdownEl, {
5 radius: containerEl.getBoundingClientRect().width / 2
6}).start();
7
8let resizeTimeout;
9
10window.addEventListener('resize', () => {
11 clearTimeout(resizeTimeout);
12
13 resizeTimeout = setTimeout(() => {
14 countdownEl.style({
15 radius: containerEl.getBoundingClientRect().width / 2
16 });
17 }, 250);
1<canvas id="countdown-canvas"></canvas>
1new CanvasCircularCountdown(document.getElementById('countdown-canvas'), { 2 captionText: percentage => { 3 if (percentage <= 25) { 4 return 'Time is running out!'; 5 } 6 7 return 'There is time. Don\'t worry!'; 8 } 9}).start();
1<canvas id="countdown-canvas"></canvas>
1new CanvasCircularCountdown(document.getElementById('countdown-canvas'), { 2 draw: (ctx, opts) => { 3 // Draw a in the centre of the canvas element, 4 // with radius being 1/4 of the canvas width. 5 ctx.save(); 6 ctx.beginPath(); 7 ctx.arc(opts.width / 2, opts.height / 2, opts.width / 4, 0, 4 * Math.PI, false); 8 ctx.lineWidth = 5; 9 ctx.strokeStyle = opts.percentage >= 50 ? '#008000' : '#ff0000'; // change color according to percentage 10 ctx.stroke(); 11 ctx.restore(); 12 } 13}).start();
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 1/8 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
12 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More