キャンバスに 2D コンテキストを作成しました。そのコンテキストでは...関数を使用して...いくつかの「円オブジェクト」を作成できます。今、私が望むのは、コンテキスト全体の画像データではなく、単一の円オブジェクトの ImageData を取得することです。
以下のコードでは、私の願いがコメントアウトされていることがわかります。
var c = document.getElementById('canvas');
var ctx = c.getContext('2d');
var circle = function (X,Y) {
var that = this;
that.X = X;
that.Y = Y;
that.clicked = function(e) {
//
//
//!!!!!!!!!!!!!!
// Code below works fine, on context level
imgData = ctx.getImageData(e.pageX, e.pageY, 1, 1);
//
// Code below is at the level of the circle, that's what I want, but isn't working
imgData = that.getImageData(e.pageX, e.pageY, 1, 1);
//!!!!!!!!!!!!!!
//
//
alert(imgData.data[3]);
}
that.draw = function () {
ctx.save();
ctx.translate(that.X, that.Y);
ctx.fillStyle = '#33cc33';
ctx.beginPath();
ctx.arc(0, 0, 50, 0, 2 * Math.PI);
ctx.fill();
ctx.stroke();
ctx.restore();
}
}
var circles = new Array();
circles.push(new circle(50,50));
document.addEventListener('click',function() {
circles.forEach(function(circ,index){
circ.clicked();
});
})
では、特定のオブジェクトの画像データを取得するにはどうすればよいでしょうか?
編集: 最初に円を描画する必要があることを理解しています。コードの後半でそれを行いますが、コンテキストに背景四角形がある場合、円の横をクリックすると、その imageData が取得されますアルファ rgba の 0 値を返したい場合は、背景の四角形。