キャンバス要素を再スケーリングできるため、さらにいくつかの課題がありました。したがって、最初に図形を描画するとき、私の場合は円弧で、名前と一緒に配列に保存して描画します。
if (this.coInit == false)
{
let co = new TempCO ();
co.name= sensor.Name;
co.path = new Path2D();
co.path.arc(c.X, c.Y, this.radius, 0, 2 * Math.PI);
this.coWithPath.push(co);
}
let coWP = this.coWithPath.find(c=>c.name == sensor.Name);
this.ctx.fillStyle = color;
this.ctx.fill(coWP.path);
次に、マウスイベントでアイテムをループし、クリックイベントがパス内にあるかどうかを確認します。ただし、サイズ変更されたキャンバスに応じてマウス座標を再スケーリングする必要もあります。
getCursorPosition(event) {
const rect = this.ctx.canvas.getBoundingClientRect();
const x = ((event.clientX - rect.left ) / rect.width) * this.canvasWidth;
const y = ((event.clientY - rect.top) / rect.height) * this.canvasHeight;
this.coWithPath.forEach(c=>{
if (this.ctx.isPointInPath(c.path, x, y))
{
console.log("arc is hit", c);
//Switch light
}
});
}
そこで、キャンバスの現在のサイズを取得し、ポイントを元のサイズに再スケーリングします。今それは動作します!
TempCOは次のようになります。
export class TempCO
{
path : Path2D;
name : string;
}