如何使用JavaScript在HTML画布中获取特定的背景颜色。

huangapple go评论78阅读模式
英文:

how to get specific background color in html canvas uisng javascript

问题

现在轮盘是彩虹颜色的,您想在画布中使用颜色 #043F45,每个格子中有这个颜色的浅色和深色。请问您如何实现这个目标?提前感谢您的回答。

英文:

i have a roulette made in html canvas using javascript, the roulette has rainbox color. the code is like below:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var options = [&quot;$100&quot;, &quot;$10&quot;, &quot;$25&quot;, &quot;$250&quot;, &quot;$30&quot;, &quot;$1000&quot;];

var startAngle = 0;
var arc = Math.PI / (options.length / 2);
var spinTimeout = null;

var spinArcStart = 10;
var spinTime = 0;
var spinTimeTotal = 0;

var ctx;

document.getElementById(&quot;spin&quot;).addEventListener(&quot;click&quot;, spin);

function byte2Hex(n) {
  var nybHexString = &quot;0123456789ABCDEF&quot;;
  return String(nybHexString.substr((n &gt;&gt; 4) &amp; 0x0F, 1)) + nybHexString.substr(n &amp; 0x0F, 1);
}

function RGB2Color(r, g, b) {
  return &#39;#&#39; + byte2Hex(r) + byte2Hex(g) + byte2Hex(b);
}

function getColor(item, maxitem) {
  var phase = 0;
  var center = 128;
  var width = 127;
  var frequency = Math.PI * 2 / maxitem;

  red = Math.sin(frequency * item + 2 + phase) * width + center;
  green = Math.sin(frequency * item + 0 + phase) * width + center;
  blue = Math.sin(frequency * item + 4 + phase) * width + center;

  return RGB2Color(red, green, blue);
}

function drawRouletteWheel() {
  var canvas = document.getElementById(&quot;canvas&quot;);
  if (canvas.getContext) {
    var outsideRadius = 200;
    var textRadius = 160;
    var insideRadius = 125;

    ctx = canvas.getContext(&quot;2d&quot;);
    ctx.clearRect(0, 0, 500, 500);

    ctx.strokeStyle = &quot;black&quot;;
    ctx.lineWidth = 2;

    ctx.font = &#39;bold 12px Helvetica, Arial&#39;;

    for (var i = 0; i &lt; options.length; i++) {
      var angle = startAngle + i * arc;
      //ctx.fillStyle = colors[i];
      ctx.fillStyle = getColor(i, options.length);

      ctx.beginPath();
      ctx.arc(250, 250, outsideRadius, angle, angle + arc, false);
      ctx.arc(250, 250, insideRadius, angle + arc, angle, true);
      ctx.stroke();
      ctx.fill();

      ctx.save();
      ctx.shadowOffsetX = -1;
      ctx.shadowOffsetY = -1;
      ctx.shadowBlur = 0;
      ctx.shadowColor = &quot;rgb(220,220,220)&quot;;
      ctx.fillStyle = &quot;black&quot;;
      ctx.translate(250 + Math.cos(angle + arc / 2) * textRadius,
        250 + Math.sin(angle + arc / 2) * textRadius);
      ctx.rotate(angle + arc / 2 + Math.PI / 2);
      var text = options[i];
      ctx.fillText(text, -ctx.measureText(text).width / 2, 0);
      ctx.restore();
    }

    //Arrow
    ctx.fillStyle = &quot;black&quot;;
    ctx.beginPath();
    ctx.moveTo(250 - 4, 250 - (outsideRadius + 5));
    ctx.lineTo(250 + 4, 250 - (outsideRadius + 5));
    ctx.lineTo(250 + 4, 250 - (outsideRadius - 5));
    ctx.lineTo(250 + 9, 250 - (outsideRadius - 5));
    ctx.lineTo(250 + 0, 250 - (outsideRadius - 13));
    ctx.lineTo(250 - 9, 250 - (outsideRadius - 5));
    ctx.lineTo(250 - 4, 250 - (outsideRadius - 5));
    ctx.lineTo(250 - 4, 250 - (outsideRadius + 5));
    ctx.fill();
  }
}

function spin() {
  spinAngleStart = Math.random() * 10 + 10;
  spinTime = 0;
  spinTimeTotal = Math.random() * 3 + 4 * 1000;
  rotateWheel();
}

function rotateWheel() {
  spinTime += 30;
  if (spinTime &gt;= spinTimeTotal) {
    stopRotateWheel();
    return;
  }
  var spinAngle = spinAngleStart - easeOut(spinTime, 0, spinAngleStart, spinTimeTotal);
  startAngle += (spinAngle * Math.PI / 180);
  drawRouletteWheel();
  spinTimeout = setTimeout(&#39;rotateWheel()&#39;, 30);
}

function stopRotateWheel() {
  clearTimeout(spinTimeout);
  var degrees = startAngle * 180 / Math.PI + 90;
  var arcd = arc * 180 / Math.PI;
  var index = Math.floor((360 - degrees % 360) / arcd);
  ctx.save();
  ctx.font = &#39;bold 30px Helvetica, Arial&#39;;
  var text = options[index]
  ctx.fillText(text, 250 - ctx.measureText(text).width / 2, 250 + 10);
  ctx.restore();
}

function easeOut(t, b, c, d) {
  var ts = (t /= d) * t;
  var tc = ts * t;
  return b + c * (tc + -3 * ts + 3 * t);
}

drawRouletteWheel();

<!-- language: lang-html -->

&lt;input type=&quot;button&quot; value=&quot;SPIN&quot; style=&quot;float:left;background:#A07F3D;color:white;margin-left:2%&quot; id=&#39;spin&#39; /&gt;
&lt;canvas id=&quot;canvas&quot; width=&quot;500&quot; height=&quot;500&quot;&gt;&lt;/canvas&gt;

<!-- end snippet -->

now the roulette is in rainbow color, instead i want to give this color #043F45 in canvas, the light and dark color of this color code in each box. can anyone please tell me how to accomplish this, thanks in advance

答案1

得分: 0

getColor函数现在根据项目索引直接返回指定颜色(#043F45)的浅色和深色阴影。shadeColor函数用于生成颜色的浅色和深色阴影。

function getColor(item, maxitem) {
    // 定义所需的颜色
    var colorCode = "#043F45";

    // 计算颜色的浅色和深色阴影
    var lightColor = shadeColor(colorCode, 0.3);
    var darkColor = shadeColor(colorCode, -0.3);

    // 根据项目索引获取适当的阴影
    var shade = item % 2 === 0 ? lightColor : darkColor;

    return shade;
}

// 生成给定颜色的浅色或深色阴影的函数
function shadeColor(color, percent) {
    var num = parseInt(color.slice(1), 16);
    var amt = Math.round(25.5 * percent); // 将调整值从2.55增加到25.5
    var R = (num >> 16) + amt;
    var G = (num >> 8 & 0x00FF) + amt;
    var B = (num & 0x0000FF) + amt;
    var newColor = "#" + (
      0x1000000 +
      (R < 255 ? (R < 1 ? 0 : R) : 255) * 0x10000 +
      (G < 255 ? (G < 1 ? 0 : G) : 255) * 0x100 +
      (B < 255 ? (B < 1 ? 0 : B) : 255)
    ).toString(16).slice(1);

    return newColor;
}

修改后的结果:
如何使用JavaScript在HTML画布中获取特定的背景颜色。

英文:

The getColor function now directly returns the light and dark shades of the specified color (#043F45) based on the item index. The shadeColor function is used to generate lighter and darker shades of the color.

function getColor(item, maxitem) {
    // Define the desired color
    var colorCode = &quot;#043F45&quot;;

    // Calculate the light and dark shades of the color
    var lightColor = shadeColor(colorCode, 0.3);
    var darkColor = shadeColor(colorCode, -0.3);

    // Get the appropriate shade based on the item index
    var shade = item % 2 === 0 ? lightColor : darkColor;

    return shade;
}

// Function to generate lighter or darker shade of a given color
function shadeColor(color, percent) {
    var num = parseInt(color.slice(1), 16);
    var amt = Math.round(25.5 * percent); // Increased adjustment value from 2.55 to 25.5
    var R = (num &gt;&gt; 16) + amt;
    var G = (num &gt;&gt; 8 &amp; 0x00FF) + amt;
    var B = (num &amp; 0x0000FF) + amt;
    var newColor = &quot;#&quot; + (
      0x1000000 +
      (R &lt; 255 ? (R &lt; 1 ? 0 : R) : 255) * 0x10000 +
      (G &lt; 255 ? (G &lt; 1 ? 0 : G) : 255) * 0x100 +
      (B &lt; 255 ? (B &lt; 1 ? 0 : B) : 255)
    ).toString(16).slice(1);

    return newColor;
}

The result after changes:
如何使用JavaScript在HTML画布中获取特定的背景颜色。

huangapple
  • 本文由 发表于 2023年6月26日 13:46:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553821.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定