我是新來的Javascript和帆布,有很多我用過的資源,現在我感到困惑。我正在使用Canvas進行着裝定製。 這是我的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>
所以對我的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); }
}
他們都工作正常,它顯示的形狀,如果我選擇其中的一個按鈕。我擔心的是,我該如何一次只顯示1形狀。
我不確定切換是否正確,請告訴我什麼是正確的做法。非常感謝你提前,
對不起,在名稱中的錯誤,我做了他們同樣的名稱=「形狀」,它應該是價值,這就是爲什麼他們不同。 – Poofbrayy