Metoda HTML canvas drawImage()

❮ HTML Canvas Reference

Obrázek k použití:

Křik

Příklad

Nakreslete obrázek na plátno:

Váš prohlížeč nepodporuje HTML5 canvas tag.

JavaScript:

window.onload = function() {
  var c = document.getElementById("myCanvas");
  var ctx = c.getContext("2d");
  var img = document.getElementById("scream");
  ctx.drawImage(img, 10, 10);
};

Podpora prohlížeče

Čísla v tabulce určují první verzi prohlížeče, která tuto metodu plně podporuje.

Method
drawImage() Yes 9.0 Yes Yes Yes

Definice a použití

Metoda drawImage() nakreslí obrázek, plátno nebo video na plátno.

Metoda drawImage() může také nakreslit části obrazu a/nebo zvětšit/zmenšit velikost obrazu.

Poznámka: Metodu drawImage() nemůžete volat, dokud se obrázek nenačte. Chcete-li zajistit, aby byl obrázek načten, můžete zavolat drawImage() z window.onload() nebo z document.getElementById(" imageID ").onload.

Syntaxe JavaScriptu

Umístěte obrázek na plátno:

JavaScript syntax: context.drawImage(img,x,y);

Umístěte obrázek na plátno a určete šířku a výšku obrázku:

JavaScript syntax: context.drawImage(img,x,y,width,height);

Ořízněte obrázek a umístěte oříznutou část na plátno:

JavaScript syntax: context.drawImage(img,sx,sy,swidth,sheight,x,y,width,height);

Hodnoty parametrů

Parameter Description Play it
img Specifies the image, canvas, or video element to use  
sx Optional. The x coordinate where to start clipping
sy Optional. The y coordinate where to start clipping
swidth Optional. The width of the clipped image
sheight Optional. The height of the clipped image
x The x coordinate where to place the image on the canvas
y The y coordinate where to place the image on the canvas
width Optional. The width of the image to use (stretch or reduce the image)
height Optional. The height of the image to use (stretch or reduce the image)

Další příklady

Příklad

Umístěte obrázek na plátno a určete šířku a výšku obrázku:

Váš prohlížeč nepodporuje HTML5 canvas tag.

JavaScript:

window.onload = function() {
  var c = document.getElementById("myCanvas");
  var ctx = c.getContext("2d");
  var img = document.getElementById("scream");
  ctx.drawImage(img, 10, 10, 150, 180);
};

Příklad

Ořízněte obrázek a umístěte oříznutou část na plátno:

Váš prohlížeč nepodporuje HTML5 canvas tag.

JavaScript:

window.onload = function() {
  var c = document.getElementById("myCanvas");
  var ctx = c.getContext("2d");
  var img = document.getElementById("scream");
  ctx.drawImage(img, 90, 130, 50, 60, 10, 10, 50, 60);
};

Příklad

Video k použití (stisknutím tlačítka Přehrát spustíte ukázku):

Plátno:

Váš prohlížeč nepodporuje HTML5 canvas tag.

JavaScript (kód vykreslí aktuální snímek videa každých 20 milisekund):

var v = document.getElementById("video1");
var c = document.getElementById("myCanvas");
var ctx = c.getContext('2d');
var i;

v.addEventListener('play',function() {i=window.setInterval(function() {ctx.drawImage(v,5,5,260,125)},20);},false);
v.addEventListener('pause',function() {window.clearInterval(i);},false);
v.addEventListener('ended',function() {clearInterval(i);},false);

❮ HTML Canvas Reference