【问题标题】:Why some of my buttons not work correctly on html5 canvas?为什么我的某些按钮在 html5 画布上无法正常工作?
【发布时间】:2019-04-07 17:58:45
【问题描述】:

我正在尝试制作一些按钮来控制画布中的某些内容。我希望它们出现在画布的右侧,所以我使用 css 来设置和移动它们。我还制作了一个计时器,它在按下绿色按钮 START 时启动,在按下 FINISH 时停止。我的问题是当我移动到正确的位置时,第一个按钮不起作用或按下。你知道为什么会这样吗?这是我所说的代码的一部分。提前谢谢!!!

document.body.style.backgroundColor = "#FAFAD2";

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

window.requestAnimFrame = (function(callback) {
  return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback) {
    window.setTimeout(callback, 1000 / 60);
  };
})();

var startTime = Date.now();
var t;
var timer_is_on = 0;

function timedCount() {
  var elapsedTime = Date.now() - startTime;
  document.getElementById("timer").innerHTML = (elapsedTime / 1000).toFixed(3);
  t = setTimeout("timedCount()", 100);
}

function doTimer() {
  if (!timer_is_on) {
    timer_is_on = 1;
    timedCount();
  }
}

function stopCount() {
  clearTimeout(t);
  timer_is_on = 0;
}

$('#start').on('click', function() {
  doTimer();
});

$('#finish').on('click', function() {
  stopCount();
});
.timer {
  position: absolute;
  bottom: 55px;
  left: 600px;
}

.both,
.one,
.two,
.timer {
  display: block;
  padding: 30px;
  position: absolute;
  left: 600px;
}

.both button {
  background-color: #4CAF50;
  border: 1px solid black;
  margin-left: 50px;
  color: white;
  padding: 15px 10px;
  text-align: center;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  width: 100px;
  display: block;
  position: relative;
  bottom: 20px;
}

.one button {
  border: 1px solid black;
  color: white;
  padding: 15px 5px;
  text-align: center;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  width: 200px;
  display: block;
  position: relative;
  top: 100px;
}

.two button {
  border: 1px solid black;
  color: white;
  padding: 15px 5px;
  text-align: center;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  width: 200px;
  display: block;
  position: relative;
  top: 300px;
}

.timer button {
  background-color: #696969;
  border: 1px solid black;
  color: white;
  padding: 12px 8px;
  text-align: center;
  font-size: 15px;
  font-weight: bold;
  cursor: pointer;
  width: 80px;
}
<!DOCTYPE html>
<html>

<head>
  <title> Simulation </title>

</head>

<body>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
  <div class='both'>
    <button type='button' id='start'>START</button>
    <button type='button' id='stop'>STOP</button>
  </div>
  <div class='one'>
    <button type='button' id='start1' style='background-color:#B22222;'>START 1</button>
    <button type='button' id='stop1' style='background-color:#B22222;'>STOP 1</button>
    <button type='button' id='position1' style='background-color:#B22222;'>POSITION 1</button>
  </div>
  <div class='two'>
    <button type='button' id='start2' style='background-color:#FFD700;'>START 2</button>
    <button type='button' id='stop2' style='background-color:#FFD700;'>STOP 2</button>
    <button type='button' id='position2' style='background-color:#FFD700;'>POSITION 2</button>
  </div>
  <div class='timer'>
    <span id="timer"></span> sec
    <button type='button' id='finish'>FINISH</button>
  </div>
  <br>
  <canvas id="myCanvas" width="455" height="650" style="border:1px solid #d3d3d3;">
    Your browser does not support the HTML5 canvas tag.</canvas>



</body>

</html>

【问题讨论】:

标签: javascript jquery css html html5-canvas


【解决方案1】:

尝试将 z-index 添加到按钮包装器和画布的相对位置

.both {z-index:99;} canvas{position:relative;z-index:10;}

【讨论】:

  • 感谢您的回答!我设法将.both, .one, .two, .timer { display: block; padding: 30px; position: absolute; left: 600px; } 替换为.both, .one, .two, .timer { display: block; padding: 30px; float: right; }
  • 很高兴知道您使用其他解决方案修复了它
【解决方案2】:

您的代码无法正常工作,因为您尝试在字符串而非函数上设置超时:

function timedCount() {
  var elapsedTime = Date.now() - startTime;
  document.getElementById("timer").innerHTML = (elapsedTime / 1000).toFixed(3);
  t = setTimeout("timedCount()", 100);
}

改变这一行:

t = setTimeout(timedCount, 100);

它应该可以工作。

【讨论】:

  • 你是对的,但我的代码在此之前确实有效!我的问题是,当我移动右侧的按钮时,第一个按钮无法像我将它们放在画布上方时那样按下。
  • @dim109 那么问题出在您的 CSS 中 - 尝试添加或删除不同的规则和属性,看看会发生什么
  • 我认为问题可能出在我的 CSS 中,我尝试更改许多规则,但没有一个对我有用!但是,我真的很感谢您的时间和回答! :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-09
  • 2019-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多