gpt4 book ai didi

javascript - 为什么我的一些按钮在 html5 Canvas 上不能正常工作?

转载 作者:太空宇宙 更新时间:2023-11-04 06:47:52 25 4
gpt4 key购买 nike

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

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>

最佳答案

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

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

关于javascript - 为什么我的一些按钮在 html5 Canvas 上不能正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53136376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com