Canvas判断区间点击生效
<!doctype html> <html> <head></head> <body> <canvas id="canvas" width="500" height="500" style = "border:1px solid red"></canvas> <script> var rect={x:100,y:100,w:40,h:20};//定义要画的矩形的位置属性 var canvas=document.getElementById('canvas'); var cxt=canvas.getContext('2d'); cxt.fillRect(rect.x,rect.y,rect.w,rect.h);//绘制矩形 canvas.onclick=function(e){//给canvas添加点击事件 e=e||event;//获取事件对象 //获取事件在canvas中发生的位置 var x=e.clientX-canvas.offsetLeft; var y=e.clientY-canvas.offsetTop; //如果事件位置在矩形区域中 if(x>=rect.x&&x<=rect.x+rect.w&&y>=rect.y&&y<=rect.y+rect.h){ window.open('链接地址');//打开指定链接 } } </script> </body> </html>
相关推荐
jinxiutong 2020-07-26
northwindx 2020-05-31
jinxiutong 2020-05-10
大地飞鸿 2020-11-12
星星有所不知 2020-10-12
MIKUScallion 2020-07-05
songfens 2020-07-05
songfens 2020-06-11
songfens 2020-06-08
northwindx 2020-05-31
northwindx 2020-05-27
northwindx 2020-05-25
MIKUScallion 2020-05-25
xdyangxiaoromg 2020-05-10
大地飞鸿 2020-05-06
northwindx 2020-04-25