繁体   English   中英

为什么笔画颜色没有变化?

[英]Why isn't the stroke color changing?

我希望为每个绘制的正方形改变颜色。 也许它与rgb有关? 但是我对这个真的很陌生,所以我真的不知道哈哈。

不知道还能尝试什么。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>IDK</title>
    <style>
      body {
        background-color:rgb(30, 30, 30);
        color:White;
      }
    </style>
  </head>
  <body>
    <h2>IDK</h2>
    <canvas id="minCanvas" width="600" height="600"></canvas>
    <script>
      var canvas = document.getElementById("minCanvas");
      var ctx = canvas.getContext("2d");
      var side=600;
      var x=0;
      var y=0;
      var a = 255;
      var b = 255;
      var c = 255;
      while (side>0) {
        ctx.strokeStyle = "rgb(a,b,c)";
        ctx.strokeRect(x, y, side, side);
        //ctx.stroke();
        a-=50;
        b-=10;
        c-=30;
        x+=5;
        y+=5;
        side-=10;
      }
    </script>
  </body>
</html>

您必须将变量显式插入到 rgb 字符串中:

ctx.strokeStyle = "rgb("+a+","+b+","+c+")"

如果支持,您还可以使用模板字符串:

ctx.strokeStyle = `rgb(${a},${b},${c})`

 var canvas = document.getElementById("minCanvas"); var ctx = canvas.getContext("2d"); var side=600; var x=0; var y=0; var a = 255; var b = 255; var c = 255; while (side>0) { ctx.strokeStyle = "rgb("+a+","+b+","+c+")"; ctx.strokeRect(x, y, side, side); //ctx.stroke(); a-=50; b-=10; c-=30; x+=5; y+=5; side-=10; }
 body { background-color:rgb(30, 30, 30); color:White; }
 <h2>IDK</h2> <canvas id="minCanvas" width="600" height="600"></canvas>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM