热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何使用HTML5画布找出点击圈子的哪个部分?-Howtofindoutwhichpartofthecirclewasclicked,usingHTML5canvas?

IamtryingtocreateSimongameusingHTML5canvasandvanillaJavaScript.Iamconfusedaboutthe

I am trying to create Simon game using HTML5 canvas and vanilla Javascript. I am confused about the coordinate system in the arc() method. I have divided the circle into 4 quadrants and would like to alert the number of the quadrant clicked. But, I am not sure how to find out which part of the circle was clicked. https://jsfiddle.net/xawpLdys/1/

我正在尝试使用HTML5 canvas和vanilla Javascript创建Simon游戏。我对arc()方法中的坐标系感到困惑。我已将圆圈划分为4个象限,并想提醒点击的象限数量。但是,我不知道如何找出圈子的哪个部分被点击。 https://jsfiddle.net/xawpLdys/1/

var canvas = document.getElementById('myCanvas');
var cOntext= canvas.getContext('2d');
var x = canvas.width / 2;
var y = canvas.height / 2;

var pads = [];

var angle = 2 * Math.PI / 4;
var color = ["green","red","blue","yellow"];
var Pads = function(x, y, radius, start, end) {
   this.x = x;
   this.y = y;
   this.radius = radius;
   this.start = start;
   this.end = end;

};
function drawSimon(radius) {
for (var i = 0; i <4; i++) {
    context.beginPath();
    context.moveTo(x, y);
    context.arc(x, y, radius, i*angle, (i+1)*angle, false);
    context.lineWidth = radius;
    context.fillStyle = color[i];
    context.fill();
    context.lineWidth = 2;
    context.strokeStyle = '#444';
    context.stroke();
    var pad = new Pads(x, y, radius, i*angle, (i+1)*angle);
    pads.push(pad);
}

}

 drawSimon(150);

  $('#myCanvas').click(function (e) {

  /*for (var i = 0; i 

2 个解决方案

#1


2  

Try This

This example just translates the clicked e.pageX and e.pageY to normal quadrant system. And after some condition, you can determine which part has been clicked.

此示例仅将单击的e.pageX和e.pageY转换为正常象限系统。在某些条件之后,您可以确定单击了哪个部分。

    $('#myCanvas').click(function (e) {
    var nx,ny;
    nx=-(x- e.pageX);
    ny=y- e.pageY;
        if (nx>0 && ny>0){
      alert('Yellow');
      }else if (nx<0 && ny>0){
      alert('Blue');
      }else if (nx>0 && ny<0){
      alert('Green');
      }else if (nx<0 && ny<0){
      alert('Red');
      }

    });

Here is the fiddle https://jsfiddle.net/xawpLdys/3/

这是小提琴https://jsfiddle.net/xawpLdys/3/

UPDATE

John S was right, (It counts clicks that are outside the circle). To prevent the clicks outside the circle from considering, we need to just find the distance from the center of the circle and the clicked point. Then compare the distance with the circle's radius to see it is inside radius.

John S是对的,(它会计算圈外的点击次数)。为了防止圆圈外的咔嗒声被考虑,我们需要找到距离圆心和点击点的距离。然后将距离与圆的半径进行比较,看它是否在半径范围内。

The updated code :

更新的代码:

    $('#myCanvas').click(function (e) {
    var nx,ny;
    nx=-(x- e.pageX);
    ny=y- e.pageY;
     var dx = Math.abs(Math.abs(x)-Math.abs(e.pageX));
     var dy = Math.abs(Math.abs(y)-Math.abs(e.pageY));
     var distance_clicked = Math.sqrt((dx*dx)+(dy*dy));
     if(distance_clicked <= radius){
        if (nx>0 && ny>0){
          alert('Yellow');
          }else if (nx<0 && ny>0){
          alert('Blue');
          }else if (nx>0 && ny<0){
          alert('Green');
          }else if (nx<0 && ny<0){
          alert('Red');
          }
    }
    });

Here is the updated fiddle https://jsfiddle.net/xawpLdys/8/

这是更新的小提琴https://jsfiddle.net/xawpLdys/8/

It still have the limitations of dividing the circle more than 4 slices.

它仍然具有将圆分割超过4个切片的限制。

#2


0  

The accepted answer seems a bit limited. It counts clicks that are outside the circle. That could be fixed fairly easily, but it would still be limited to four sections.

接受的答案似乎有点受限。它会计算圈子外的点击次数。这可以很容易地修复,但它仍然只限于四个部分。

To determine if a point is in a sector:

要确定某个点是否在某个扇区中:

  1. First check if it is within the circle. The Pythagorean theorem comes into play here. Square the x and y values, if their sum is less than or equal to the radius squared, the point is in the circle.
  2. 首先检查它是否在圆圈内。毕达哥拉斯定理在这里发挥作用。将x和y值平方,如果它们的总和小于或等于半径的平方,则该点在圆中。

  3. If the point is in the circle, then check if its angle is between the start and end angles for the sector. You can get the point's angle using the arc tangent function from trigonometry.
  4. 如果该点位于圆圈中,则检查其角度是否在扇区的起始角度和结束角度之间。您可以使用三角函数的反正切函数获得点的角度。


Try this jsfiddle.

试试这个jsfiddle。

Here are the types that help make this work:

以下是有助于实现此功能的类型:

var Circle = function(center, radius) {
    this.center = center;
    this.radius = radius;

    this._radiusSquared = Math.pow(this.radius, 2);
}

$.extend(Circle.prototype, {
    containsPoint: function(point) {
        var relPoint = this.pointToRelPoint(point);
        return Math.pow(relPoint.x, 2) + Math.pow(relPoint.y, 2)
                <= this._radiusSquared;
    },

    getAngleForPoint: function(point) {
        var relPoint = this.pointToRelPoint(point);
        return Math.atan2(-relPoint.y, -relPoint.x) + Math.PI;
    },

    pointToRelPoint: function(point) {
        return { x: point.x - this.center.x, y: point.y - this.center.y }
    }
});

var CircleSector = function(startAngle, endAngle) {
    this.startAngle = startAngle;
    this.endAngle = endAngle;
};

$.extend(CircleSector.prototype, {
    containsAngle: function(angle) {
        return (angle >= this.startAngle) && (angle 

推荐阅读
author-avatar
点提土八撇又254
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有