将另一个函数添加到 onclick

Adding in another function to an onclick

您好,我有一个已简化的 onclick 按钮方案。我有两个 canvasses(一个正方形和一个圆圈)和一个 onclick。单击onclick时...

1)方块canvas'饱和度应该在3秒后增加5%

2) 3秒后打开模态对话框,里面有一个onclick

当按下 "modal dialog" onclick 时,它会关闭并清除并在旧圆之上重新绘制一个新圆 canvas。

理想情况下,当黄色方块的饱和度为 25%(或按下按钮 5 次)时,研究场景应该完成并将我发送到 404。但是方块 canvas不会增加它的饱和度,我不知道它是否会将我发送到 404,因为它不会增加到 25%。

var squaretime = document.getElementById("timeCanvas");
  var sqt= squaretime.getContext("2d");
  sqt.globalAlpha = 0.1;
  sqt.beginPath();
  sqt.rect(0, 0, 80, 80);
  sqt.fillStyle = "yellow";
  //cir.globalAlpha = 0.4;
  sqt.fill();
  sqt.stroke();

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.beginPath();
ctx.arc(300, 300, 250, 0, 2 * Math.PI);
ctx.stroke();

ctx.beginPath();
ctx.moveTo(125,125);
ctx.lineTo(475,475);
ctx.stroke();
ctx.beginPath();

ctx.moveTo(125,475);
ctx.lineTo(475, 125);
ctx.stroke();


var modal = document.querySelector(".modal");
var trigger = document.querySelector(".trigger");
var closeButton = document.querySelector(".close-button");


function toggleModal() {
    setTimeout(function() {
        modal.classList.toggle("show-modal");
    }, 3000)
}

function closeModalNow() {
  modal.classList.toggle("show-modal");
  document.querySelector('input[name="conf1d3nce"]').value = null;

var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.clearRect(0, 0, 600, 600);

ctx.beginPath();
ctx.moveTo(125,125);
ctx.lineTo(475,475);
ctx.stroke();
ctx.beginPath();

ctx.moveTo(125,475);
ctx.lineTo(475, 125);
ctx.stroke();
}

trigger.addEventListener("click", toggleModal);
closeButton.addEventListener("click", closeModalNow);

//increases the yellow square by 5%
   submit = function() {
     reset;
     
reset = function() {
    check_time()
    var y = document.getElementById("timeCanvas")
    var context = y.getContext('2d');
    context.clearRect(0, 0, y.width, y.height);

    //var squaretime = document.getElementById("timeCanvas");
    var sqt= y.getContext("2d");
    console.log(sqt.globalAlpha)
    check_time()
    sqt.globalAlpha = sqt.globalAlpha + 0.05;
    setTimeout(function() {
    sqt.beginPath();
    sqt.rect(0, 0, 80, 80);
    sqt.fillStyle = 'yellow';
    sqt.fill();
    sqt.stroke();
}, 3000)
}}
    
//finished the task at 25% saturation of yellow square
check_time = function() {
 var y = document.getElementById("timeCanvas")
  var sqt= y.getContext("2d");
 if (sqt.globalAlpha >= 0.95) {
 console.log(sqt.globalAlpha);
   window.location.href = "INTERCEPT";
 }
}
.modal {
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.01);
    opacity: 0;
    visibility: hidden;
    transform: scale(1.1);
    transition: visibility 0s linear 0.25s, opacity 0.25s 0s, transform 0.25s;
}
.modal-content {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: white;
    padding: 1rem 1.5rem;
    width: 24rem;
    border-radius: 0.5rem;
}
.close-button {
    float: right;
    width: 1.5rem;
    line-height: 1.5rem;
    text-align: center;
    cursor: pointer;
    border-radius: 0.25rem;
    background-color: lightgray;
}
.close-button:hover {
    background-color: darkgray;
}
.show-modal {
    opacity: 1;
    visibility: visible;
    transform: scale(1.0);
    transition: visibility 0s linear 0s, opacity 0.25s 0s, transform 0.25s;
}
<div class="styling">
<div class="general_text_2">
<p>Time left </p>
<canvas id="timeCanvas" width="80" height="80" style="border:1px solid #d3d3d3;">
</canvas>

<button class="trigger">Click here </button>
<div class="modal">
    <div class="modal-content">
      
        <h1>
          <label><b>Please state your confidence with this decision (0-100%)</b></label>
          <p>
         <input class="bottomaftertrialquestions" type="number" placeholder="Type here" name="conf1d3nce"min="0" max="100" required>
          </p>
          <p></h1>
            <button type="button" class="close-button">SAVE</button>
    </div>
</div>


// CIRCLE GRID (MAP) 

<canvas id="myCanvas" width="600" height="600" style="border 12px solid black">
</canvas> 

但是 none 这在我的代码中有效。谁能帮忙。如有任何问题,请随时提出。

您必须将代码缩减为最小示例...

我看到几个问题:

  • 没有调用提交
  • 重复 document.getElementById("timeCanvas");
  • alpha 的增加超出了增加它的实际函数

请参阅下面来自您的代码的最小工作示例

var squaretime = document.getElementById("timeCanvas");
var sqt = squaretime.getContext("2d");
sqt.globalAlpha = 0.1;
sqt.beginPath();
sqt.rect(0, 0, 80, 80);
sqt.fillStyle = "yellow";
//cir.globalAlpha = 0.4;
sqt.fill();
sqt.stroke();

document.querySelector(".trigger").onclick = submit;

//increases the yellow square by 5%
function submit() {
  setTimeout(function() {
    sqt.clearRect(0, 0, squaretime.width, squaretime.height);
    sqt.beginPath();
    sqt.globalAlpha = sqt.globalAlpha + 0.05;
    sqt.rect(0, 0, 80, 80);
    sqt.fillStyle = 'yellow';
    sqt.fill();
    sqt.beginPath();
    sqt.fillStyle = 'black';
    sqt.fillText(sqt.globalAlpha, 10, 50);
  }, 100)

}
<canvas id="timeCanvas" width="80" height="80"></canvas>

<button class="trigger">Click here </button>