【问题标题】:How do I change a color of a Html element in a canvas?如何更改画布中 Html 元素的颜色?
【发布时间】:2021-03-12 12:39:30
【问题描述】:

我之前在此发表过一篇文章,但它不在画布中,但是当您尝试将其应用到画布时,它给出了一个错误,类似于 Cant change the color of undefined 所以我认为这是一个我定义它的方式错误,所以我将它添加到一个类中,然后运行它来改变类中所有元素的颜色,但是当我尝试它时,画布就消失了..但是直截了当地有一个简单的方法改变画布中 HTML 元素的颜色还是我疯了

代码

var myGamePiece = el.classList.add("class1");
var myObstacles = [];
var myScore;

function startGame() {
  myGamePiece = new component(30, 30, "red", 10, 120);
  myGamePiece.gravity = 0.05;
  myScore = new component("30px", "Consolas", "black", 280, 40, "text");
  myGameArea.start();
}

var myGameArea = {
  canvas: document.createElement("canvas"),
  start: function() {
    this.canvas.width = 480;
    this.canvas.height = 270;
    this.context = this.canvas.getContext("2d");
    document.body.insertBefore(this.canvas, document.body.childNodes[0]);
    this.frameNo = 0;
    this.interval = setInterval(updateGameArea, 20);
  },
  clear: function() {
    this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  }
}

function component(width, height, color, x, y, type) {
  this.type = type;
  this.score = 0;
  this.width = width;
  this.height = height;
  this.speedX = 0;
  this.speedY = 0;
  this.x = x;
  this.y = y;
  this.gravity = 0;
  this.gravitySpeed = 0;
  this.update = function() {
    ctx = myGameArea.context;
    if (this.type == "text") {
      ctx.font = this.width + " " + this.height;
      ctx.fillStyle = color;
      ctx.fillText(this.text, this.x, this.y);
    } else {
      ctx.fillStyle = color;
      ctx.fillRect(this.x, this.y, this.width, this.height);
    }
  }
  this.newPos = function() {
    this.gravitySpeed += this.gravity;
    this.x += this.speedX;
    this.y += this.speedY + this.gravitySpeed;
    this.hitBottom();
  }
  this.hitBottom = function() {
    var rockbottom = myGameArea.canvas.height - this.height;
    if (this.y > rockbottom) {
      this.y = rockbottom;
      this.gravitySpeed = 0;
    }
  }
  this.crashWith = function(otherobj) {
    var myleft = this.x;
    var myright = this.x + (this.width);
    var mytop = this.y;
    var mybottom = this.y + (this.height);
    var otherleft = otherobj.x;
    var otherright = otherobj.x + (otherobj.width);
    var othertop = otherobj.y;
    var otherbottom = otherobj.y + (otherobj.height);
    var crash = true;
    if ((mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)) {
      crash = false;
    }
    return crash;
  }
}

function updateGameArea() {
  var x, height, gap, minHeight, maxHeight, minGap, maxGap;
  for (i = 0; i < myObstacles.length; i += 1) {
    if (myGamePiece.crashWith(myObstacles[i])) {
      return;
    }
  }
  myGameArea.clear();
  myGameArea.frameNo += 1;
  if (myGameArea.frameNo == 1 || everyinterval(150)) {
    x = myGameArea.canvas.width;
    minHeight = 20;
    maxHeight = 200;
    height = Math.floor(Math.random() * (maxHeight - minHeight + 1) + minHeight);
    minGap = 50;
    maxGap = 200;
    gap = Math.floor(Math.random() * (maxGap - minGap + 1) + minGap);
    myObstacles.push(new component(10, height, "green", x, 0));
    myObstacles.push(new component(10, x - height - gap, "green", x, height + gap));
  }
  for (i = 0; i < myObstacles.length; i += 1) {
    myObstacles[i].x += -1;
    myObstacles[i].update();
  }
  myScore.text = "SCORE: " + myGameArea.frameNo;
  myScore.update();
  myGamePiece.newPos();
  myGamePiece.update();
}

function everyinterval(n) {
  if ((myGameArea.frameNo / n) % 1 == 0) {
    return true;
  }
  return false;
}

function accelerate(n) {
  myGamePiece.gravity = n;
}

function Test() {
  document.getElementsByClassName("class1").style.background = "yellow";
}
canvas {
  border: 1px solid #d3d3d3;
  background-color: #f1f1f1;
}

.class1 {}
<body onload="startGame()">
  <button onmousedown="accelerate(-0.2)" onmouseup="accelerate(0.05)">ACCELERATE</button>
  <p> Credit To W3Schools.com </p>
  <button onclick="test">Click To Change Player's Color</button>
</body>

【问题讨论】:

  • 我找不到您用来更改 html 元素颜色的任何代码。
  • 等等,让我更新它,输入错误的代码
  • 修复了找到正确的代码
  • el 没有定义。你的代码有很多错误。
  • 如果要改变小方块的颜色,可以新建一个myGamePiece。

标签: javascript html css button canvas


【解决方案1】:

代码开头:var myGamePiece = el.classList.add("class1");el 没有定义,但无论如何 myGamePiece 需要是 startGame 中创建的组件,它不是普通的 HTML 元素。

设置颜色的是该组件的创建。目前颜色被硬编码为“红色”。要更改颜色,您需要设置一个变量,我们称之为 playerColor,并在组件创建调用中使用它而不是“红色”。

您正在使用函数 test 来设置颜色,所以让我们更改测试函数来执行此操作,但您需要在 startGame 之前设置该颜色,否则画布将具有不正确的颜色(或没有设置颜色)。

var playersColor = "red"; //the default value if the player doesn't change it
var myGamePiece;
var myObstacles = [];
var myScore;

function startGame() {
  myGamePiece = new component(30, 30, playersColor, 10, 120);//use playersColor
  myGamePiece.gravity = 0.05;
  myScore = new component("30px", "Consolas", "black", 280, 40, "text");
  myGameArea.start();
}

var myGameArea = {
  canvas: document.createElement("canvas"),
  start: function() {
    this.canvas.width = 480;
    this.canvas.height = 270;
    this.context = this.canvas.getContext("2d");
    document.body.insertBefore(this.canvas, document.body.childNodes[0]);
    this.frameNo = 0;
    this.interval = setInterval(updateGameArea, 20);
  },
  clear: function() {
    this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
  }
}

function component(width, height, color, x, y, type) {
  this.type = type;
  this.score = 0;
  this.width = width;
  this.height = height;
  this.speedX = 0;
  this.speedY = 0;
  this.x = x;
  this.y = y;
  this.gravity = 0;
  this.gravitySpeed = 0;
  this.update = function() {
    ctx = myGameArea.context;
    if (this.type == "text") {
      ctx.font = this.width + " " + this.height;
      ctx.fillStyle = color;
      ctx.fillText(this.text, this.x, this.y);
    } else {
      ctx.fillStyle = color;
      ctx.fillRect(this.x, this.y, this.width, this.height);
    }
  }
  this.newPos = function() {
    this.gravitySpeed += this.gravity;
    this.x += this.speedX;
    this.y += this.speedY + this.gravitySpeed;
    this.hitBottom();
  }
  this.hitBottom = function() {
    var rockbottom = myGameArea.canvas.height - this.height;
    if (this.y > rockbottom) {
      this.y = rockbottom;
      this.gravitySpeed = 0;
    }
  }
  this.crashWith = function(otherobj) {
    var myleft = this.x;
    var myright = this.x + (this.width);
    var mytop = this.y;
    var mybottom = this.y + (this.height);
    var otherleft = otherobj.x;
    var otherright = otherobj.x + (otherobj.width);
    var othertop = otherobj.y;
    var otherbottom = otherobj.y + (otherobj.height);
    var crash = true;
    if ((mybottom < othertop) || (mytop > otherbottom) || (myright < otherleft) || (myleft > otherright)) {
      crash = false;
    }
    return crash;
  }
}

function updateGameArea() {
  var x, height, gap, minHeight, maxHeight, minGap, maxGap;
  for (i = 0; i < myObstacles.length; i += 1) {
    if (myGamePiece.crashWith(myObstacles[i])) {
      return;
    }
  }
  myGameArea.clear();
  myGameArea.frameNo += 1;
  if (myGameArea.frameNo == 1 || everyinterval(150)) {
    x = myGameArea.canvas.width;
    minHeight = 20;
    maxHeight = 200;
    height = Math.floor(Math.random() * (maxHeight - minHeight + 1) + minHeight);
    minGap = 50;
    maxGap = 200;
    gap = Math.floor(Math.random() * (maxGap - minGap + 1) + minGap);
    myObstacles.push(new component(10, height, "green", x, 0));
    myObstacles.push(new component(10, x - height - gap, "green", x, height + gap));
  }
  for (i = 0; i < myObstacles.length; i += 1) {
    myObstacles[i].x += -1;
    myObstacles[i].update();
  }
  myScore.text = "SCORE: " + myGameArea.frameNo;
  myScore.update();
  myGamePiece.newPos();
  myGamePiece.update();
}

function everyinterval(n) {
  if ((myGameArea.frameNo / n) % 1 == 0) {
    return true;
  }
  return false;
}

function accelerate(n) {
  myGamePiece.gravity = n;
}

function test() {
  playersColor = "yellow";
}

<body>
  <button onmousedown="accelerate(-0.2)" onmouseup="accelerate(0.05)">ACCELERATE</button>
  <p> Credit To W3Schools.com </p>
  <button onclick="test()">Click To Change Player's Color</button>
  <button onclick="startGame();">Click to start the game</button>
</body>

.................................................. ....................

虽然以下错误并不是导致播放器颜色不变的根本原因,但它们包含一些编码错误,可能有助于将来指出。

<button onclick="test">Click To Change Player's Color</button>
<script>
    function Test() {
         document.getElementsByClassName("class1").style.background = "yellow";
</script>

onclick="test" 什么都不做 - 您可能会混淆结构 element.onclick = test - 设置在被点击的元素上运行的函数 - 与 onclick="test();" 的语法需要在按钮元素属性中。

另一个错误是该函数被称为测试,而不是测试。 Javascript 区分大小写。此外,您需要选择一个具有 class1 的元素,而不是检索此类元素的集合,因此您可以设置其样式(如果这有用,是吗?)。

【讨论】:

  • 我现在不在我的电脑前,所以这应该改变元素的颜色,或者我必须在加载画布之前改变元素的颜色
  • 您必须在将元素绘制到画布上之前更改填充颜色,因为画布不了解元素,它根本不了解结构。
  • 哦,所以我不能启动游戏 onload 并运行 document.getElementbyID("Mygamepeice").style.background = "Blue";然后让玩家按下你说的开始按钮?
  • 是的,让玩家在有机会选择颜色后点击按钮开始游戏是正确的做法。我已经更新了我的答案以描述如何在开始时设置颜色。您可以通过添加颜色属性来修饰它并让它们在游戏期间改变颜色(例如您已经具有重力),但除非我误解了,否则我认为这不是您要问的吗?
  • 是的,这就是我想要做的谢谢你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-12-15
  • 1970-01-01
  • 2023-03-16
  • 2018-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多