英文:
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 = ["$100", "$10", "$25", "$250", "$30", "$1000"];
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("spin").addEventListener("click", spin);
function byte2Hex(n) {
var nybHexString = "0123456789ABCDEF";
return String(nybHexString.substr((n >> 4) & 0x0F, 1)) + nybHexString.substr(n & 0x0F, 1);
}
function RGB2Color(r, g, b) {
return '#' + 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("canvas");
if (canvas.getContext) {
var outsideRadius = 200;
var textRadius = 160;
var insideRadius = 125;
ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, 500, 500);
ctx.strokeStyle = "black";
ctx.lineWidth = 2;
ctx.font = 'bold 12px Helvetica, Arial';
for (var i = 0; i < 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 = "rgb(220,220,220)";
ctx.fillStyle = "black";
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 = "black";
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 >= spinTimeTotal) {
stopRotateWheel();
return;
}
var spinAngle = spinAngleStart - easeOut(spinTime, 0, spinAngleStart, spinTimeTotal);
startAngle += (spinAngle * Math.PI / 180);
drawRouletteWheel();
spinTimeout = setTimeout('rotateWheel()', 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 = 'bold 30px Helvetica, Arial';
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 -->
<input type="button" value="SPIN" style="float:left;background:#A07F3D;color:white;margin-left:2%" id='spin' />
<canvas id="canvas" width="500" height="500"></canvas>
<!-- 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;
}
英文:
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 = "#043F45";
// 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 >> 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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论