变换画布不会影响可点击的区域
Transforming a canvas doesnt affect the clickable areas
我试图通过转换它来改变我的canvas
。我用刻度(-1,-1)把它倒过来。但是,画布内的可点击区域不受此转换的影响。对于这个问题,谷歌没有答案。我觉得这很奇怪。有人能解释一下原因吗?并可能给出一个解决这个问题的方法。
transform:scale(-1,-1);
。可点击区域搞砸了,即使代码清楚地从画布上选择像素(被点击)。
如果它只是缩放,那么只需将鼠标转换为相同的。
mouse.x = ?
mouse.y = ?
var scalex = -1;
var scaley = -1;
mouse.tx = mouse.x * scalex;
mouse.ty = mouse.y * scaley;
但是我猜你也移动了你正在画的图像,这样你就可以看到它了。
你可能做了类似
这样的事var drawImage(image,-200,-200); // move image back onto the canvas
您必须对鼠标做同样的操作(缩放和翻译)。当你做反向变换时你做的是反向变换。所以当你缩放然后平移时,你通过平移然后缩放来反转它。
var box = { // a hit region in normal screen coordinates.
x : 20,
y : 20,
w : 60,
h : 60,
}
// the mouse
mouse.x = ?
mouse.y = ?
// the scale on translations so you can see the box
var scalex = -1;
var scaley = -1;
var originx = -200;
var originy = -200;
// transform the mouse screen coords into transformed canvas space
mouse.tx = mouse.x + orginx; // translate first
mouse.ty = mouse.y + orginy;
mouse.tx *= scalex; // then scale
mouse.ty *= scaley;
// draw the transformed image/box/whatever
ctx.scale(scalex,scaley);
ctx.translate(originx,originy);
ctx.strokeRect(box.x,box.y,box.w,box.h); // draw the back to front box
// is the mouse over the hit region and hence the box
if(mouse.tx > box.x && mouse.tx < box.x + box.w && mouse.ty > box.y && mouse.ty < box.y + box.h){
ctx.fillStyle = "red"
ctx.fillRect(box.x,box.y,box.w,box.h);
}
这是一个简单的版本,只有缩放和平移,将适用于所有的缩放和平移,只要你不旋转它。如果你旋转,你需要做完整的反转。你可以在这个问题中找到如何做到这一点HTML画布,鼠标位置后缩放和翻译
相关文章:
- 提交表单时,Javascript(JQuery)不会在文本区域中填充内容
- CKeditor不会在源代码模式下更新文本区域
- tinyMCE不会添加特定文本区域的Control
- TineMCE 不会启动 ajax 加载的文本区域
- 文本区域字段在设置时不会更改
- JavaScript 不会隐藏 asp.net 中的文本区域
- 如果有样式标记,jQuery的.html函数不会正确地将内容写入文本区域
- 当我单击生成时,文本区域不会更新
- 文本区域的高度在值增加时增加,但在值减小时不会降低
- 在 Chrome 中输入内容时,HTML 文本区域不会更新
- 文本区域使用 jQuery 展开,当我单击提交按钮时不会提交表单
- IE 不会清除文本区域值
- JavaScript:当文本位于文本区域内时,所选文本不会被替换
- getElementById 不会获取文本区域内容
- 导航栏的链接不会转到区域 ID
- JavaScript 不会在内容区域 wordpress 之外工作
- 按回车键时,文本区域不会停止创建新行
- AngularJS-动态创建<text区域>使用ngSanitize不会't显示(其他元素显示)
- 用ng重复或ng模型填充一个文本区域,这样它就不会用迭代的数据一个接一个地创建新的文本区域
- 表单被替换为文本区域,不会给出不同的输出