[英]how do i toggle visibility between 2 canvas using html / css / js /?
dear stackoverflowers 亲爱的stackoverflowers
I have 2 canvases: 我有2幅画布:
I need to switch which canvas is visible with a single button click 我需要通过单击一下按钮切换可见的画布
</div>
<canvas id="myCanvas" width="800" height="600" style="border:1px solid #000000;" hidden="outputcanvas">
</canvas>
</div>
<!----------------------------------------------------------------------------------------------------------------------------------------------------->
<!-- input canvas -->
<div>
<canvas width="800" height="600" hidden="inputcanvas" ></canvas>
</div>
is it possible ? 可能吗 ? if so , how ? 如果可以,怎么办?
at this moment i trying to solve it with JS ^^ 此刻,我试图用JS ^^解决它
thanks in advance 提前致谢
Here's one way to use a button to toggle visibility of 2 canvases so that only 1 canvas is visible: 这是使用按钮切换2个画布的可见性的一种方法,以便仅显示1个画布:
use CSS to stack the 2 canvases on top of each other inside a wrapper div using positioning. 使用CSS使用定位将两个画布彼此堆叠在包装div中。
Toggle the style.visibility
of the 2 canvases in response to your button click. 响应您的按钮单击,切换2个画布的style.visibility
。
Here's an example: 这是一个例子:
var canvas1=document.getElementById('canvas1'); canvas1.getContext('2d').fillText('This is canvas1',20,20); var canvas2=document.getElementById('canvas2'); canvas2.getContext('2d').fillText('This is canvas2',20,20); swapCanvases(); document.getElementById("test").onclick=function(){ swapCanvases(); }; function swapCanvases(){ if(canvas1.style.visibility=='visible'){ canvas1.style.visibility='hidden'; canvas2.style.visibility='visible'; }else{ canvas1.style.visibility='visible'; canvas2.style.visibility='hidden'; } }
body{ background-color: ivory;} #wrapper{position:relative;} #canvas1{position:absolute; border:1px solid red;} #canvas2{position:absolute; border:1px solid blue;}
<button id="test">Swap Canvas Visibilities</button> <div id=wrapper> <canvas id="canvas1" width=300 height=300></canvas> <canvas id="canvas2" width=300 height=300></canvas> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.