javascript - 如何使用 JavaScript 和 canvas arc() 方法更改重叠圆的大小?

标签 javascript html for-loop canvas

我能够使用 canvas arch() 方法 stub 静态图像,并且我有一个 for 循环,但我坚持为什么 for 循环不从圆变量中减去所选的数字。我一直致力于如何创建一个 for 循环,当用户移动 slider 时,该循环会使圆圈改变大小并适本地交替颜色。 enter image description here

//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;

slider.oninput = function() {
bandWidth.innerHTML = this.value;
// console.log(this.value);
let input = this.value;
let circle = radius;


  for(input; input <= circle; circle -= input){
    //console.log(circle); 
    //I don't know why the loop does work correctly above 30...
    }
  }
  
//Figure out how to get the below in a for loop...
//Stubbed out what circles would look like at 25.
// the first circle has a band with of 25
//first circle
ctx.beginPath();
ctx.arc(centerX, centerY, radius, 0, 2 * Math.PI, false);
ctx.fillStyle = 'red';
ctx.fill();

//Second circle
ctx.beginPath();
ctx.arc(centerX, centerY, 175, 0, 2 * Math.PI, false);
ctx.fillStyle = 'blue';
ctx.fill();

//Third Circle
ctx.beginPath();
ctx.arc(centerX, centerY, 150, 0, 2 * Math.PI, false);
ctx.fillStyle = 'red';
ctx.fill();

//Fourth Circle
ctx.beginPath();
ctx.arc(centerX, centerY, 125, 0, 2 * Math.PI, false);
ctx.fillStyle = 'blue';
ctx.fill();

//Fifth
ctx.beginPath();
ctx.arc(centerX, centerY, 100, 0, 2 * Math.PI, false);
ctx.fillStyle = 'red';
ctx.fill();

//Sixth
ctx.beginPath();
ctx.arc(centerX, centerY, 75, 0, 2 * Math.PI, false);
ctx.fillStyle = 'blue';
ctx.fill();

//Seventh
ctx.beginPath();
ctx.arc(centerX, centerY, 50, 0, 2 * Math.PI, false);
ctx.fillStyle = 'red';
ctx.fill();

//Eigth
ctx.beginPath();
ctx.arc(centerX, centerY, 25, 0, 2 * Math.PI, false);
ctx.fillStyle = 'blue';
ctx.fill();
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>BullsEye</title>
</head>
<body>
  <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>
    
  <script src="bullsEye.js"></script>
</body>
</html>

最佳答案

您的代码似乎可以通过一些细微的修正正常工作。

我在循环中引入了一个附加变量 i 来跟踪当前迭代并在颜色之间交替。

//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>

关于javascript - 如何使用 JavaScript 和 canvas arc() 方法更改重叠圆的大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60273079/

相关文章:

javascript - 单击按钮后文本不会更改 - 表单、时间和全局变量

linux - 将行添加到文件末尾 - 使用 for 循环 - linux 脚本

r - 将元素添加到 R 中向量的开头

javascript - 如何重构这个 for 循环?检查标签 id 是否与数组中的标签匹配,然后更新其边框

php - 使用 PHP 和 Javascript 的 HTML 图像映射/用户可以选择颜色

javascript - 如何实现一个函数,当传入一个密集的字符串时返回,否则返回 false?

html - 使用标签和单选按钮作为 CSS 钩子(Hook)是否适合 HTML?

python - 使用 Xpath (HtmlXPathSelector) 获取父文本和子文本

javascript - 鼠标悬停时停止滑动并在鼠标移出时开始播放

javascript - 如何设置一个新类不适用于 Bootstrap 和 jQuery?