为了账号安全,请及时绑定邮箱和手机立即绑定

如何让btn.on(“click”,show);能够执行 点击图形能够实现隐藏显示

如何让btn.on(“click”,show);能够执行 点击图形能够实现隐藏显示

慕粉2308161447 2017-04-05 11:05:09
<html> <head>   <title>   </title> <style> </style> </head> <body>   <canvas id='canvas'width="1100"  height="630">     <div>     </div>   </canvas> <script> var c=document.getElementById("canvas"); var ctx=c.getContext("2d"); ctx.save(); ctx.beginPath(); ctx.border=" 1px solid #aaa"; ctx.shadowBlur=5; ctx.fillStyle="#F0FFF0"; ctx.shadowColor="green"; ctx.fillRect(20,20,1000,600);//填充的矩形\\ ctx.fillStyle="green"; var img=new Image();//背景图 img.src="2.jpg"; img.onload=function(){   ctx.drawImage(img,70,70,900,500); } window.onload=function(){    btn=new Button();    ctx.save();    ctx.beginPath();    ctx.fillRect(150,570,60,40);    ctx.font="25px Arial";    ctx.fillStyle="#000000 "//字体颜色    ctx.fillText("显示",155,600);//要填充的字及位置    ctx.fillStyle="yellow";    ctx.closePath();    ctx.restore();    btn1=new Button();    ctx.save();    ctx.beginPath();    ctx.fillRect(750,570,60,40);    ctx.font="25px Arial";    ctx.fillStyle="#000000 "//字体颜色    ctx.fillText("隐藏",755,600);//要填充的字及位置    ctx.closePath();    ctx.restore();    c.onclick=function(e){     e=e||event;     var x=e.offsetX;     var y=e.offsetY;     if(x>=750&&x<=810&&y>=570&&y<=600){         alert("nihao");         btn1.on("click",hide);                }     else if(x>=150&&x<=210&&y>=570&&y<=600){         alert("你好");         btn.on("click",show);              }     } } function Button(){   Button.prototype.on=function(events,fn){         btn.addEventListener(events,fn);   }       } //隐藏 var hide=function(){   ctx.clearRect(70,70,900,500);   ctx.save();   ctx.beginPath();   ctx.fillStyle="#F0FFF0";   ctx.fillRect(70,70,900,500);   ctx.closePath();   ctx.restore(); } //显示 var show=function(){    ctx.drawImage(img,70,70,900,500); } </script> </body> </html>
查看完整描述

1 回答

  • 1 回答
  • 1 关注
  • 1978 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信