简体   繁体   English

有没有办法检测一个点在旋转和平移后是否在 Path2D 对象中?

[英]Is there a way to detect if a point is in a Path2D object after it has been rotated and translated?

I am trying to find a point in a Path2D object in an HTML5 canvas using JavaScript.我正在尝试使用 JavaScript 在 HTML5 画布中的 Path2D 对象中找到一个点。

var path = new Path2D;
path.rect(0,0,100,100);
// declare the path

ctx.beginPath(); 
ctx.save()
ctx.translate(500,500);
ctx.rotate(Math.PI/2);
ctx.fill(path);
ctx.restore()
// draw the path after translation and rotation

ctx.isPointInPath(path,505,500) // return false because path is defined at 0, 0 not 500,500 where path is drawn
// Is there a way know if the points are in the path where it is drawn.
// I do not want to re - define the path, but if there is a way to move it that would work.

Please understand that this code is simplified, and the path I am using is not a generic shape, so this needs to work for any given 2d path.请理解此代码已简化,并且我使用的路径不是通用形状,因此这需要适用于任何给定的 2d 路径。 This is not a question about syntax so please excuse the undefined variables.这不是关于语法的问题,所以请原谅未定义的变量。

isPointInPath() takes its x and y coordinates as untransformed , ie as absolute coordinates on the canvas, however it does apply the transformations on the given path . isPointInPath()将其xy坐标视为未转换,即作为画布上的绝对坐标,但它确实在给定的path上应用了转换。

So you need to call it with the applied transformation (ie, get rid of this ctx.restore() ).所以你需要用应用的转换来调用它(即摆脱这个ctx.restore() )。

However your numbers don't add up:但是你的数字不加起来:

 const canvas = document.getElementById('canvas') const ctx = canvas.getContext('2d'); const path = new Path2D; path.rect(0,0,100,100); // declare the path ctx.translate(500,500); ctx.rotate(Math.PI / 2) ctx.fillStyle = "green"; ctx.fill(path); // using fixed values const x = 505; const y = 500; console.log(ctx.isPointInPath(path,x,y)); // reset to identity matrix ctx.setTransform(1,0,0,1,0,0); ctx.fillStyle = "red"; ctx.fillRect(x-2,y-2,4,4); document.scrollingElement.scrollBy(600, 650);
 <canvas id="canvas" width="600" height="650"></canvas>

But if you use correct coordinates, it will work as expected:但是如果您使用正确的坐标,它将按预期工作:

 const canvas = document.getElementById('canvas') const ctx = canvas.getContext('2d'); const path = new Path2D; path.rect(0,0,100,100); // declare the path ctx.translate(500,500); ctx.rotate(Math.PI / 2) ctx.fillStyle = "green"; ctx.fill(path); // using fixed values const x = 495; const y = 505; console.log(ctx.isPointInPath(path,x,y)); // reset to identity matrix ctx.setTransform(1,0,0,1,0,0); ctx.fillStyle = "red"; ctx.fillRect(x-2,y-2,4,4); document.scrollingElement.scrollBy(600, 650);
 <canvas id="canvas" width="600" height="650"></canvas>

And if one day you really need to transform a Path2D object, you can refer to this post which uses the second parameter matrix of Path2D.add(path, matrix) to return a transformed copy of the Path2D object.如果有一天你真的需要转换一个 Path2D 对象,你可以参考这篇文章,它使用Path2D.add(path, matrix)的第二个参数matrix来返回 Path2D 对象的转换副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM