Your code seems to work fine with some minor corrections.
I’ve introduced an additional variable i
in the loop to keep track of the current iteration and alternate between colors.
//Canvas constants
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');
const centerX = canvas.width / 2;
const centerY = canvas.height / 2;
const radius = 200;
//Slider
let slider = document.getElementById("myRange");
let bandWidth = document.getElementById("bandWidth");
bandWidth.innerHTML = slider.value;
function draw() {
bandWidth.innerHTML = this.value;
// console.log(this.value);
let input = this.value;
let circle = radius;
ctx.clearRect(0, 0, canvas.width, canvas.height)
for(let i = 0; input <= circle; circle -= input, i++){
// console.log(circle);
ctx.beginPath();
ctx.arc(centerX, centerY, circle, 0, 2 * Math.PI, false);
ctx.fillStyle = i % 2 === 0 ? 'red' : 'blue';
ctx.fill();
}
}
slider.oninput = draw;
draw.call(slider);
<canvas id="myCanvas" width="400" height="400" style="border: 2px solid black"></canvas>
<br>
<div>BandWidth:</div>
<!--Slider input-->
<!--onChnage will update the bandwith value-->
<input type="range" id="myRange" value="25" min="5" max="50" step="5">
<p>Current BandWith:
<span id="bandWidth"></span>
</p>