简体   繁体   English

使用单选按钮显示/隐藏画布

[英]Show/Hide Canvas using radio button

I'm new to Javascript and Canvas, there's a lot of resources I've used and now I got confused. 我是Java和Canvas的新手,我已经使用了很多资源,但现在我很困惑。 I'm doing a dress customization using Canvas. 我正在使用Canvas定制礼服。 This is my HTML: 这是我的HTML:

 //these are my button samples
<input type="radio" id="shape1" name="round" onchange="display()" /> 
<input type="radio" id="shape2" name="box" onchange="display()" />

// this is where I'll display the canvas
<canvas id="displaycanvas" height="450px" width="820px" style="position:absolute;"> </canvas>

So for my Javascript I have this: 所以对于我的Javascript我有这个:

  function display() { 
    if (document.getElementById('shape1').checked) {
      var canvasC = document.getElementById('displaycanvas'),
          context = canvasC.getContext('2d');
           context.beginPath();
           context.arc(95,50,40,0,2*Math.PI);
           context.stroke();     }

    if (document.getElementById('shape2').checked) {
      var canvasR = document.getElementById('displaycanvas'),
          context = canvasR.getContext('2d');
            context.beginPath();
            context.rect(50, 27, 350, 400);    }
}

They are all working fine, it displays the shapes if I choose one of these buttons. 它们都工作正常,如果我选择这些按钮之一,它将显示形状。 My concern is how do I display only 1 shape at a time. 我关心的是如何一次只显示1个形状

I'm not sure if toggle was the right thing, please tell me what is the right thing to do. 我不确定切换是否正确,请告诉我什么是正确的事情。 Thank you very much in advance, 预先非常感谢您,

You need to give the radios the same name to toggle them and you need to clear the canvas too. 您需要给收音机起相同的名称来切换它们,并且还需要清除画布。

I have used the same name for the canvas now 我现在为画布使用了相同的名称

 window.onload = function() { document.querySelector("#shape1").onclick = document.querySelector("#shape2").onclick = function() { var canvas = document.querySelector('#displaycanvas'); context = canvas.getContext('2d'); context.clearRect(0,0,canvas.width,canvas.height); context.beginPath(); if (this.id == "shape1") { context.arc(95, 50, 40, 0, 2 * Math.PI); } else { context.rect(50, 27, 350, 400); } context.stroke(); } } 
 <input type="radio" id="shape1" name="shape" value="round" /> <input type="radio" id="shape2" name="shape" value="box" /> <canvas id="displaycanvas" height="450px" width="820px" style="position:absolute;"></canvas> 

If you want TWO canvases, you can do 如果要两个画布,可以

window.onload = function() {
  document.querySelector("#shape1").onclick = function() {
    document.querySelector('#displaycanvas1').style.display="";
    document.querySelector('#displaycanvas2').style.display="none";
  }
  document.querySelector("#shape2").onclick = function() {
    document.querySelector('#displaycanvas2').style.display="";
    document.querySelector('#displaycanvas1').style.display="none";
  }
}

still giving the radios the same name 仍然给收音机起同样的名字

If the radio buttons have the same name attribute, they will alternate between the available ones. 如果单选按钮具有相同的name属性,则它们将在可用按钮之间交替。 If they have different name attributes, they will be grouped separately. 如果它们具有不同的name属性,则将它们分别分组。

 function display() { var s1 = document.querySelector('#shape1').checked, s2 = document.querySelector('#shape2').checked; alert("shape1: " + s1 + ", shape2: " + s2) } function display2() { var s3 = document.querySelector('#shape3').checked, s4 = document.querySelector('#shape4').checked; alert("shape3: " + s3 + ", shape4: " + s4) } 
 1-2 <input type="radio" name="shape" value="shape1" id="shape1" onclick="display()" /> <input type="radio" name="shape" value="shape2" id="shape2" onclick="display()" /> <br /><br /> 3-4 <input type="radio" name="round" value="shape3" id="shape3" onclick="display2()" /> <input type="radio" name="square" value="shape4" id="shape4" onclick="display2()" /> 

You can use the below code to achieve your requirement: 您可以使用以下代码来满足您的要求:

 function display() { var canvasC = document.getElementById('displaycanvas'); context = canvasC.getContext('2d'); context.clearRect(0, 0, canvasC.width, canvasC.height); if (document.getElementById('shape1').checked) { context.beginPath(); context.arc(95,50,40,0,2*Math.PI); context.stroke(); } if (document.getElementById('shape2').checked) { context.beginPath(); context.rect(50, 27, 350, 400); context.stroke(); } } 
 <input type="radio" id="shape1" name="round[]" onchange="display()" /> <input type="radio" id="shape2" name="round[]" onchange="display()" /> <canvas id="displaycanvas" height="450px" width="820px" style="position:absolute;"> </canvas> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM