下面是我页面的简化版本。在下面的演示中,单击一个圆圈,应打印出在div中单击的圆圈的名称。我有2个问题。 1)当我放大页面时(Chrome似乎在加载页面时自动在页面上执行此操作,因此,当使用Chrome浏览器时,我目前必须缩小以使可点击区域起作用),按像素点击检测不更长的作品。我通过检查距圆心的距离(以像素为单位)来检测是否在三个圆之一内发生了单击。您可以尝试放大并查看单击圆圈是否产生预期的结果。
2)有时不显示圆圈右边的文本,我必须手动刷新一次页面(第一次加载后),以查看文本出现在圆圈旁边。
定义点击的代码位于脚本部分的底部。 建议您整页查看下面的页面。
var total_vap_count = 12;
var total_sta_count = 3;
//ac
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.arc(175,50,30,0,2*Math.PI);
ctx.fillStyle = "#004379";
ctx.font = "14px Lato";
if ("1" < 2){
ctx.fillText("1" + " AC", 211, 50);
} else {
ctx.fillText("2" + " ACs", 211, 50);
}
ctx.fill();
//sta
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.arc(175,300,30,0,2*Math.PI);
ctx.fillStyle = "#004379";
ctx.font = "14px Lato";
if (total_sta_count < 2){
ctx.fillText("" + total_sta_count + " device", 211, 300);
} else {
ctx.fillText("" + total_sta_count + " devices", 211, 300);
}
ctx.fillText("connected", 211, 315);
ctx.fill();
//ssid
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.arc(50,175,30,0,2*Math.PI);
ctx.fillStyle = "#004379";
ctx.font = "14px Lato";
if (total_vap_count < 2){
ctx.fillText("" + total_vap_count + " SSID", 86, 175);
} else {
ctx.fillText("" + total_vap_count + " SSIDs", 86, 175);
}
ctx.fill();
// ssid status
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.arc(50 + Math.sin(Math.PI/4) * 30,175-Math.cos(Math.PI/4) * 30,8,0,2*Math.PI);
if (total_vap_count){
ctx.fillStyle = "#4caf50";
} else {
ctx.fillStyle = "#ff0000";
}
ctx.fill();
// sta status
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.arc(175 + Math.sin(Math.PI/4) * 30,300 - Math.cos(Math.PI/4) * 30,8,0,2*Math.PI);
if (total_sta_count){
ctx.fillStyle = "#4caf50";
} else {
ctx.fillStyle = "#ff0000";
}
ctx.fill();
// ac status
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.arc(175 + Math.sin(Math.PI/4) * 30,50 - Math.cos(Math.PI/4) * 30,8,0,2*Math.PI);
if (parseInt("1") > 0){
ctx.fillStyle = "#4caf50";
} else {
ctx.fillStyle = "#ff0000";
}
//ctx.fillStyle = "#e9ebee";
ctx.fill();
// Lines.
if (parseInt("1") > 0){
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.moveTo(50 + Math.sin(Math.PI/4) * 30+10,175-Math.cos(Math.PI/4) * 30-10);
ctx.lineTo(175 - Math.sin(Math.PI/4) * 30-5,50 + Math.cos(Math.PI/4) * 30+5);
ctx.lineWidth = 3;
ctx.strokeStyle = "#004379"
ctx.stroke();
}
if (total_vap_count && total_sta_count){
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.moveTo(50 + Math.sin(Math.PI/4) * 30+5,175+Math.cos(Math.PI/4) * 30+ 5);
ctx.lineTo(175 - Math.sin(Math.PI/4) * 30-5,300 - Math.cos(Math.PI/4) * 30-5);
ctx.lineWidth = 3;
ctx.strokeStyle = "#004379"
ctx.stroke();
}
$(document).ready(function() {
var canvas=document.getElementById("myCanvas");
var ctx=canvas.getContext("2d");
var paint1 = setTimeout(()=>{
ctx.font="16px FontAwesome";
ctx.fillStyle = "white";
ctx.fillText('\uf0ac',170, 55);
},2000);
var canvas=document.getElementById("myCanvas");
var ctx=canvas.getContext("2d");
var paint2 = setTimeout(()=>{
ctx.font="16px FontAwesome";
ctx.fillStyle = "white";
ctx.fillText('\uf10b',170, 305);
},2000);
var canvas=document.getElementById("myCanvas");
var ctx=canvas.getContext("2d");
var paint3 = setTimeout(()=>{
ctx.font="16px FontAwesome";
ctx.fillStyle = "white";
ctx.fillText('\uf012',45, 180);
},2000);
$('#myCanvas').click(function(e){
var x = e.clientX;
var y = e.clientY;
console.log("inside click");
console.log(x);
console.log(y);
//x start: 5, y start: 213 for canvas
if (Math.pow(x-55, 2) + Math.pow(y-175-213, 2) < Math.pow(30, 2)){
//$j("#overview_page").hide();
//openNav();
$("#demo").empty().append("ssid clicked");
}
if (Math.pow(x-175, 2) + Math.pow(y-300-213, 2) < Math.pow(30, 2)){
//$j("#overview_page").hide();
//openNav_sta();
$("#demo").empty().append("sta clicked");
}
if (Math.pow(x-175, 2) + Math.pow(y-50-213, 2) < Math.pow(30, 2)){
//$j("#overview_page").hide();
//openNav_ac();
$("#demo").empty().append("ac clicked");
}
});
})
#left_menu {
list-style-type: none;
margin: 0;
padding: 0;
width: 330px;
background-color: #e9ebee;
position: fixed;
height: 100%;
overflow: auto;
border: 1px solid #e9ebee;
border-radius: 6px;
font-family: 'Lato', Helvetica, sans-serif;
font-size: 18px;
}
li a {
display: block;
color: #000;
padding: 8px 18px;
text-decoration: none;
}
li a.active {
background-color: #008b10;
color: white;
}
li a:hover:not(.active) {
background-color: #555;
color: white;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet"/>
<ul id="left_menu">
<li><div><a id="tab_wireless" href="#"><i class="fas fa-chart-line"></i> System Status</a></div></li>
<li><div><a id="tab_system" href="#"><i class="fa fa-laptop" aria-hidden="true"></i> Network Configuration</a></div></li>
<li><div><a id="radio_list" href="#"><i class="fas fa-broadcast-tower"></i> Radio Information</a></div></li>
<!--<li><a id="search" href="#">Search</a></li>-->
<li><canvas id="myCanvas" width="310" height="400" style="position: relative; top: 110px; left: 5px;">
Your browser does not support the canvas element.
</canvas></li>
</ul>
<div id="demo" style="margin-left:330px;padding:1px 16px;border:1px solid #e9ebee; border-radius:6px;">hello world</div>
答案 0 :(得分:1)
我只是在这里使用offsetX
和offsetY
作为快速解决方案,但是如果要在生产中使用,您需要一种更安全的方法来获得鼠标相对于画布的点击位置。当您单击画布的左上角时,您需要将鼠标位置设置为[0,0]。
var total_vap_count = 12;
var total_sta_count = 3;
//ac
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.arc(175,50,30,0,2*Math.PI);
ctx.fillStyle = "#004379";
ctx.font = "14px Lato";
if ("1" < 2){
ctx.fillText("1" + " AC", 211, 50);
} else {
ctx.fillText("2" + " ACs", 211, 50);
}
ctx.fill();
//sta
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.arc(175,300,30,0,2*Math.PI);
ctx.fillStyle = "#004379";
ctx.font = "14px Lato";
if (total_sta_count < 2){
ctx.fillText("" + total_sta_count + " device", 211, 300);
} else {
ctx.fillText("" + total_sta_count + " devices", 211, 300);
}
ctx.fillText("connected", 211, 315);
ctx.fill();
//ssid
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.arc(50,175,30,0,2*Math.PI);
ctx.fillStyle = "#004379";
ctx.font = "14px Lato";
if (total_vap_count < 2){
ctx.fillText("" + total_vap_count + " SSID", 86, 175);
} else {
ctx.fillText("" + total_vap_count + " SSIDs", 86, 175);
}
ctx.fill();
// ssid status
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.arc(50 + Math.sin(Math.PI/4) * 30,175-Math.cos(Math.PI/4) * 30,8,0,2*Math.PI);
if (total_vap_count){
ctx.fillStyle = "#4caf50";
} else {
ctx.fillStyle = "#ff0000";
}
ctx.fill();
// sta status
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.arc(175 + Math.sin(Math.PI/4) * 30,300 - Math.cos(Math.PI/4) * 30,8,0,2*Math.PI);
if (total_sta_count){
ctx.fillStyle = "#4caf50";
} else {
ctx.fillStyle = "#ff0000";
}
ctx.fill();
// ac status
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.arc(175 + Math.sin(Math.PI/4) * 30,50 - Math.cos(Math.PI/4) * 30,8,0,2*Math.PI);
if (parseInt("1") > 0){
ctx.fillStyle = "#4caf50";
} else {
ctx.fillStyle = "#ff0000";
}
//ctx.fillStyle = "#e9ebee";
ctx.fill();
// Lines.
if (parseInt("1") > 0){
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.moveTo(50 + Math.sin(Math.PI/4) * 30+10,175-Math.cos(Math.PI/4) * 30-10);
ctx.lineTo(175 - Math.sin(Math.PI/4) * 30-5,50 + Math.cos(Math.PI/4) * 30+5);
ctx.lineWidth = 3;
ctx.strokeStyle = "#004379"
ctx.stroke();
}
if (total_vap_count && total_sta_count){
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
ctx.beginPath();
ctx.moveTo(50 + Math.sin(Math.PI/4) * 30+5,175+Math.cos(Math.PI/4) * 30+ 5);
ctx.lineTo(175 - Math.sin(Math.PI/4) * 30-5,300 - Math.cos(Math.PI/4) * 30-5);
ctx.lineWidth = 3;
ctx.strokeStyle = "#004379"
ctx.stroke();
}
$(document).ready(function() {
var canvas=document.getElementById("myCanvas");
var ctx=canvas.getContext("2d");
var paint1 = setTimeout(()=>{
ctx.font="16px FontAwesome";
ctx.fillStyle = "white";
ctx.fillText('\uf0ac',170, 55);
},2000);
var canvas=document.getElementById("myCanvas");
var ctx=canvas.getContext("2d");
var paint2 = setTimeout(()=>{
ctx.font="16px FontAwesome";
ctx.fillStyle = "white";
ctx.fillText('\uf10b',170, 305);
},2000);
var canvas=document.getElementById("myCanvas");
var ctx=canvas.getContext("2d");
var paint3 = setTimeout(()=>{
ctx.font="16px FontAwesome";
ctx.fillStyle = "white";
ctx.fillText('\uf012',45, 180);
},2000);
$('#myCanvas').click(function(e){
var x = e.offsetX;
var y = e.offsetY;
//x start: 5, y start: 213 for canvas
if (pointInCircle(x, y, 50, 175, 30)){
//$j("#overview_page").hide();
//openNav();
$("#demo").empty().append("ssid clicked");
}
if (pointInCircle(x, y, 175, 300, 30)){
//$j("#overview_page").hide();
//openNav_sta();
$("#demo").empty().append("sta clicked");
}
if (pointInCircle(x, y, 175, 50, 30)){
//$j("#overview_page").hide();
//openNav_ac();
$("#demo").empty().append("ac clicked");
}
});
})
function pointInCircle(x, y, cx, cy, radius) {
var distancesquared = (x - cx) * (x - cx) + (y - cy) * (y - cy);
return distancesquared <= radius * radius;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet"/>
<canvas id="myCanvas" width="310" height="400" style="position: relative; top: 110px; left: 5px;">
Your browser does not support the canvas element.
</canvas>
<div id="demo"></div>