I have a shape, in HTML5 canvas, drawn with JavaScript:

我在HTML5画布中有一个用JavaScript绘制的形状:

ctx.beginPath();
ctx.moveTo(25,0);
ctx.quadraticCurveTo(50,50,40,100);
ctx.lineTo(33,100);
ctx.quadraticCurveTo(50,50,20,0);
ctx.fill();
ctx.closePath();

If a click my canvas, how can I detect that I clicked this shape?

如果单击我的画布,我怎么能检测到我点击了这个形状?

canvas.addEventListener("click",function(e){
     alert(isItPartOfTheShape(e.clientX,e.clientY));
})

function isItPartOfTheShape(x,y){
     /* Here comes the code which detects is it part of the shape */
     return isIt;
}

1 个解决方案

#1


You have two choices

你有两个选择

  1. implement the point-in-polygon code and bezier curve computation in Javascript
  2. 在Javascript中实现点多边形代码和贝塞尔曲线计算

  3. you can use context.isPointInPath that however requires you to rebuild the shape when you do the test (that function checks the specified point only against the current path, i.e. the path that would be filled calling fill)
  4. 你可以使用context.isPointInPath但是你需要在进行测试时重建形状(该函数只针对当前路径检查指定的点,即填充调用fill的路径)

更多相关文章

  1. 检查并删除重复的坐标x,y画布
  2. Pandas concat:ValueError:传递值的形状为blah,indices表示blah2
  3. X和y有不相容的形状
  4. 将2d数组数据视为定义形状的像素——是否可能创建内部和表面?
  5. Canvas(画布)类的使用
  6. Android自定义万能Canvas画布
  7. 自定义Button形状(圆形、椭圆)

随机推荐

  1. Android 4.4环境搭建——Android SDK下载
  2. Android内存分析总结
  3. [原]如何在Android用FFmpeg+SDL2.0解码声
  4. Android UI 用户界面开发基本概念概览
  5. 安卓表格布局android:collapseColumns,an
  6. Android测试教程(2):测试基础
  7. android gesture检测
  8. Phonegap软键盘遮挡输入框问题
  9. 漫谈Android安全框架
  10. Android开发者指南(11) ―― Optimizing