HTMLキャンバスputImageData()メソッド

❮HTMLキャンバスリファレンス

以下のコードは、getImageData()を使用してキャンバス上の指定された長方形のピクセルデータをコピーし、次にputImageData()を使用して画像データをキャンバスに戻します。

var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.fillStyle = "red";
ctx.fillRect(10, 10, 50, 50);

function copy() {
  var imgData = ctx.getImageData(10, 10, 50, 50);
  ctx.putImageData(imgData, 10, 70);
}

ブラウザのサポート

表の数字は、このメソッドを完全にサポートする最初のブラウザバージョンを示しています。

Method
putImageData() Yes 9.0 Yes Yes Yes

定義と使用法

putImageData()メソッドは、(指定されたImageDataオブジェクトからの)画像データをキャンバスに戻します。

ヒント:キャンバス上の指定された長方形のピクセルデータをコピーするgetImageData()メソッドについてお読みください。

ヒント:新しい空白のImageDataオブジェクトを作成するcreateImageData()メソッドについてお読みください。


JavaScript構文

JavaScript構文: context .putImageData(imgData、x、y、dirtyX、dirtyY、dirtyWidth、dirtyHeight);

パラメータ値

Parameter Description
imgData Specifies the ImageData object to put back onto the canvas
x The x-coordinate, in pixels, of the upper-left corner of the ImageData object
y The y-coordinate, in pixels, of the upper-left corner of the ImageData object
dirtyX Optional. The horizontal (x) value, in pixels, where to place the image on the canvas
dirtyY Optional. The vertical (y) value, in pixels, where to place the image on the canvas
dirtyWidth Optional. The width to use to draw the image on the canvas
dirtyHeight Optional. The height to use to draw the image on the canvas

❮HTMLキャンバスリファレンス