【发布时间】:2021-06-22 19:59:37
【问题描述】:
我有一个网格覆盖,其中每个单独框的所有角都保存在一个数组数组中(即让 box = [[x0,y0,x1,y1,x2,y2,x3,y3],[.. .]];) 我正在模拟施加在整个网格上的力并将其分解为每个方格(就像自制的 FEA)。当鼠标悬停在该范围上时,我想尝试让每个正方形显示的力量。这可能吗?我知道如何在 HTML 中使用 div,但我不确定如何从数组中提取范围并让程序知道鼠标在该范围上。现在我用鼠标点击显示它们:
///Shows force at clicked coordinates
function getCursorPosition(canvas, event){
let rect = canvas.getBoundingClientRect()
let x = event.clientX - rect.left
let y = event.clientY - rect.top
for(let i = 0; i < plateForces.length; i++){
if( plateForces[i][0] < x && x <= plateForces[i][4] && plateForces[i][1] < y && y <= plateForces[i][5] ) {
// Point is in bounding box
context.fillStyle = "black";
context.font = "bold 20px Ariel";
context.fillText( JSON.stringify(plateForces[i][8].toFixed(1)), x, y);
}
}
console.log(x,y)
}
canvas.addEventListener('mousedown', function(e) { getCursorPosition(canvas, e) });
感谢任何帮助或建议。
【问题讨论】:
标签: javascript html mouseevent onmouseover