【发布时间】:2015-04-15 16:09:37
【问题描述】:
编辑:我最好重写这个问题,我可以看到措辞不好。非常值得一票否决...道歉。
当用户在画布元素内单击时,我试图获取 x,y 坐标。
我相信我有一个正确的解决方案。我已经构建了一个单元测试,我的解决方案通过了。
我要问的是是否可以构建一个我的解决方案失败的单元测试。也许通过 CSS 缩放 canvas 元素、封闭在 iframe 中等。这个问题是关于加强单元测试。
由于我是 Web 开发的新手,我猜有些尴尬的场景是我目前无法预料的。
这是我的单元测试:
function getNumericStyleProperty(style, prop){
return parseInt(style.getPropertyValue(prop),10) ;
}
function element_position(e) {
var x = 0, y = 0;
var inner = true ;
do {
x += e.offsetLeft;
y += e.offsetTop;
var style = getComputedStyle(e,null) ;
var borderTop = getNumericStyleProperty(style,"border-top-width") ;
var borderLeft = getNumericStyleProperty(style,"border-left-width") ;
y += borderTop ;
x += borderLeft ;
if (inner){
var paddingTop = getNumericStyleProperty(style,"padding-top") ;
var paddingLeft = getNumericStyleProperty(style,"padding-left") ;
y += paddingTop ;
x += paddingLeft ;
}
inner = false ;
} while (e = e.offsetParent);
return { x: x, y: y };
}
var c = document.getElementById('c');
var t = c.getContext('2d');
t.font = '10px monospace';
c.addEventListener('click', function(e) {
t.fillStyle = "white"
t.fillRect(0, 0, c.width, c.height);
t.fillStyle = "black"
t.fillText('page: ' + e.pageX
+ ", " + e.pageY, 16, 16*1 );
var bcr = e.target.getBoundingClientRect();
t.fillText('BoundingClientRect: ' + bcr.left
+ ", " + bcr.top , 16, 16*2);
// Method 1
var style = getComputedStyle(e.target,null) ;
var borderTop = getNumericStyleProperty(style,"border-top-width") ;
var borderLeft = getNumericStyleProperty(style,"border-left-width") ;
var paddingTop = getNumericStyleProperty(style,"padding-top") ;
var paddingLeft = getNumericStyleProperty(style,"padding-left") ;
t.fillText('client : ' + e.clientX
+ ", " + e.clientY, 16, 16*3);
t.fillText('bcr.x-client-border-padding: '
+ (e.clientX - bcr.left - borderLeft - paddingLeft)
+ ", " + (e.clientY - bcr.top - borderTop - paddingTop), 16, 16*4);
// Method 2
var p = element_position(e.target);
t.fillText('element_position: ' + p.x
+ ", " + p.y, 16, 16*5);
t.fillText('e.page - element_position(): '
+ (e.pageX - p.x)
+ ", " + (e.pageY - p.y), 16, 16*6);
}, false);
body {
margin: 1em;
background: #8888ff;
padding: 1em;
}
.myDiv {
position: absolute;
left: 20px;
top: 40px;
border: solid #88ff88 10px;
background: green;
margin: 1em;
padding: 0.5em;
}
canvas {
border: solid red 20px;
position: relative;
left: 20px;
top: 0px;
padding: 10px;
background: brown;
margin: 1em
}
<p style="background:white">body</p>
<div class="myDiv">
<p style="background:white">myDiv</p>
<canvas id="c" width="300" height="200" style="cursor:crosshair"> </canvas>
</div>
如何改进这个单元测试?我正在尝试构建最简单的“最尴尬的场景”。
谁能调整它以使其不再返回画布内的正确点击位置?
编辑:原始问题的简介移至此处:
有很多不好的解决方案。互联网似乎充满了 当我谷歌这个时复制粘贴编码。虽然我有解决方案 可以合作,我想更仔细地研究这个问题, 看看是否可以实现稳健的通用解决方案。
以下是我找到的各种解决方案:
getting mouse position relative to content area of an element - 这个问题有一个很好的答案(连同现场示例),它仍然表现出相同的偏移问题。How do I get the coordinates of a mouse click on a canvas element?
http://miloq.blogspot.in/2011/05/coordinates-mouse-click-canvas.html
Getting cursor position in a canvas without jQuery document.documentElement 这可能是 与 CSS 边距/边框/填充混淆,或者只是一个错误。的一部分 问题是这是一个常见的问题,每个人都尝试过 它,但没有唯一的解决方案。所以有很多混乱。 问题的另一部分是底层浏览器 API 保持 转移。
问题的另一部分是似乎没有人在测试 证明劣质方法失败的设置,这就是 我试图在这里解决。
【问题讨论】:
-
WADR,既然您可以控制编码,那么您的工作就是避免最尴尬的情况!
-
我有点困惑:“任何人都可以调整它,使其不再返回画布内的正确点击位置吗?” 你don '不想要画布中的正确位置?
-
请说明您希望点击坐标相对于什么:客户端、文档、屏幕或画布
-
抱歉,我的沟通不畅。我已经重写了问题!
-
这类问题可能更适合姐妹网站 CodeReview:codereview.stackexchange.com
标签: html5-canvas mouseclick-event