【问题标题】:How to allow user to choose a colour for data being input in canvas 2d如何允许用户为在画布 2d 中输入的数据选择颜色
【发布时间】:2020-10-22 04:15:27
【问题描述】:

我有生成条形图的代码,用户可以在其中选择条形的值。目前,这些条的颜色已被硬编码为在红色和蓝色之间交替。我需要帮助的是如何允许用户在设置值时选择每个条的颜色。任何建议将不胜感激。

这是我到目前为止所得到的;

<html>
<head>
<script>

var barVals = [];

function draw() {

  var canvas = document.getElementById("canvas");
  var ctx = canvas.getContext("2d");

  // calculate highest bar value (used to scale the rest)
  var highest = 0;
  for (var b = 0; b < barVals.length; b++) {
    if (barVals[b] > highest)
      highest = barVals[b];
  }

  // we have 8 horizontal lines so calculate an appropriate scale

  var lineSpacing = 1;
  var highestLine = 7 * lineSpacing;

  while (highestLine < highest) {
    lineSpacing *= 10;
    highestLine = 7 * lineSpacing;
  }



  // grey background

  ctx.fillStyle = "rgb(200,200,200)";
  ctx.fillRect(0, 0, 600, 450);



  // draw and (if we have any data to scale from) label horizontal lines

  var lineNum = 0;
  ctx.fillStyle = "white";
  ctx.font = "16px sans-serif";
  for (y = 0; y <= 350; y += 50) {

    // line
    ctx.beginPath();
    ctx.moveTo(50, y + 50);
    ctx.lineTo(550, y + 50);
    ctx.stroke();

    // label (the 6 is an offset to centre the text vertically on the line)

    if (barVals.length > 0) {
      ctx.fillText(lineSpacing * lineNum, 10, 400 - y + 6);
      lineNum++;

    }
  }



  // draw boxes (widths based on how many we have)

  var barWidth = 500 / barVals.length;
  var halfBarWidth = barWidth / 2;
  for (b = 0; b < barVals.length; b++) {

    // calculate size of box and draw it

    var x = 60 + b * barWidth;
    var hgt = (barVals[b] / highestLine) * 350; // as fraction of highest line

    if (b % 2 == 0)
      ctx.fillStyle = "red";

    else

      ctx.fillStyle = "blue";

    ctx.fillRect(x, 400 - hgt, barWidth, hgt);

    // calculate position of text and draw it

    ctx.fillStyle = "white";
    var metrics = ctx.measureText(barVals[b]);
    var halfTextWidth = metrics.width / 2;
    x = 60 + halfBarWidth + (b * barWidth) - halfTextWidth;
    ctx.fillText(barVals[b], x, 420 - hgt);

  }

}



function addBar() {

  var textBoxObj = document.getElementById("barVal");
  barVals.push(parseInt(textBoxObj.value)); // add new value to end of array. As an integer not a string!!
  draw(); // redraw

  textBoxObj.value = 0;

}

function removeBar() {

  var textBoxObj = document.getElementById("removeBarVal");
  barVals.splice(parseInt(textBoxObj.value), 1); // choose which object to remove from the array by specifying the index
  draw(); // redraw
    
}
    
function editBar() {
    
    var textBoxObj = document.getElementById("editBarVal");
    barVals.replace(parseInt(textBoxObj.value), 1);
    draw(); // redraw
}
    

</script>
</head>


                                 
<body onload="draw()";                           
<center>
<canvas id="canvas" width="600" height="450"></canvas>
<form>
<br>
<input type=button value='Add Bar' onclick='addBar();'> <input id='barVal' value=0>
<input type="color" id="favcolor" name="favcolor" value="#ff0000"><input type=button value="Submit" onclick="addBar();">  
<input type=button value='Remove Bar' onclick='removeBar();'> <input id='removeBarVal' value=0>
<input type=button value='Edit Bar' onclick='editBar();'> <input id='editBarVal' value=0>

  </form>
</center>

【问题讨论】:

    标签: html canvas graphics html5-canvas


    【解决方案1】:

    它可能不是您正在寻找的东西,但如果您想自己做,您可以使用范围输入

    您需要做的就是添加一个 eventListener (onchange) 并获取颜色输入字段的值以将其存储在变量中(或将其传递给函数),然后使用此变量设置 ctx.fillStyle

    favcolor.onchange=(e)=>{
      mycolor=e.target.value;
    }
    

    var barVals = [];
    let mycolor = "rgb(200,200,200)";
    
    function draw() {
      var ctx = canvas.getContext("2d");
    
      // calculate highest bar value (used to scale the rest)
      var highest = 0;
      for (var b = 0; b < barVals.length; b++) {
        if (barVals[b] > highest)
          highest = barVals[b];
      }
    
      // we have 8 horizontal lines so calculate an appropriate scale
    
      var lineSpacing = 1;
      var highestLine = 7 * lineSpacing;
    
      while (highestLine < highest) {
        lineSpacing *= 10;
        highestLine = 7 * lineSpacing;
      }
    
    
    
      // grey background
    
      ctx.fillStyle = "rgb(200,200,200)";
      ctx.fillRect(0, 0, 600, 450);
    
    
    
      // draw and (if we have any data to scale from) label horizontal lines
    
      var lineNum = 0;
      ctx.fillStyle = "white";
      ctx.font = "16px sans-serif";
      for (y = 0; y <= 350; y += 50) {
    
        // line
        ctx.beginPath();
        ctx.moveTo(50, y + 50);
        ctx.lineTo(550, y + 50);
        ctx.stroke();
    
        // label (the 6 is an offset to centre the text vertically on the line)
    
        if (barVals.length > 0) {
          ctx.fillText(lineSpacing * lineNum, 10, 400 - y + 6);
          lineNum++;
    
        }
      }
    
    
    
      // draw boxes (widths based on how many we have)
    
      var barWidth = 500 / barVals.length;
      var halfBarWidth = barWidth / 2;
      for (b = 0; b < barVals.length; b++) {
    
        // calculate size of box and draw it
    
        var x = 60 + b * barWidth;
        var hgt = (barVals[b] / highestLine) * 350; // as fraction of highest line
    
          ctx.fillStyle = mycolor
    
        ctx.fillRect(x, 400 - hgt, barWidth, hgt);
    
        // calculate position of text and draw it
    
        ctx.fillStyle = "white";
        var metrics = ctx.measureText(barVals[b]);
        var halfTextWidth = metrics.width / 2;
        x = 60 + halfBarWidth + (b * barWidth) - halfTextWidth;
        ctx.fillText(barVals[b], x, 420 - hgt);
    
      }
    
    }
    
    
    
    function addBar() {
    
      var textBoxObj = document.getElementById("barVal");
      barVals.push(parseInt(textBoxObj.value)); // add new value to end of array. As an integer not a string!!
      draw(); // redraw
    
      textBoxObj.value = 0;
    
    }
    
    function removeBar() {
    
      var textBoxObj = document.getElementById("removeBarVal");
      barVals.splice(parseInt(textBoxObj.value), 1); // choose which object to remove from the array by specifying the index
      draw(); // redraw
        
    }
        
    function editBar() {
        
        var textBoxObj = document.getElementById("editBarVal");
        barVals.replace(parseInt(textBoxObj.value), 1);
        draw(); // redraw
    }
    
    favcolor.onchange=(e)=>{
      mycolor=e.target.value;
    }
    <body onload="draw()";                           
    <center>
    <canvas id="canvas" width="600" height="450"></canvas>
    <form>
    <br>
    <input type=button value='Add Bar' onclick='addBar();'> <input id='barVal' value=0>
    <input type="color" id="favcolor" name="favcolor" value="#ff0000"><input type=button value="Submit" onclick="addBar();">  
    <input type=button value='Remove Bar' onclick='removeBar();'> <input id='removeBarVal' value=0>
    <input type=button value='Edit Bar' onclick='editBar();'> <input id='editBarVal' value=0>
    
      </form>
    </center>

    在其他情况下,您可以使用 javascript 扩展来帮助您选择 hsl 或 rgb 颜色

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-06
      • 1970-01-01
      • 2017-02-08
      • 2016-08-27
      相关资源
      最近更新 更多