drawImageToRect 方法
- CanvasImageSource source,
- Rectangle<
num> destRect, { - Rectangle<
num> ? sourceRect,
将一个 CanvasImageSource 的图像绘制到画布的指定区域。
图像将被绘制到由 destRect
定义的画布区域。 sourceRect
定义了源图像中被绘制的那部分区域。如果没有提供 sourceRect
,则将从 source
绘制整个矩形图像到当前上下文。
如果图像比画布允许的还要大,图像将被裁剪以适应可用空间。
CanvasElement canvas = new CanvasElement(width: 600, height: 600);
CanvasRenderingContext2D ctx = canvas.context2D;
ImageElement img = document.query('img');
img.width = 100;
img.height = 100;
// Scale the image to 20x20.
ctx.drawImageToRect(img, new Rectangle(50, 50, 20, 20));
VideoElement video = document.query('video');
video.width = 100;
video.height = 100;
// Take the middle 20x20 pixels from the video and stretch them.
ctx.drawImageToRect(video, new Rectangle(50, 50, 100, 100),
sourceRect: new Rectangle(40, 40, 20, 20));
// Draw the top 100x20 pixels from the otherCanvas.
CanvasElement otherCanvas = document.query('canvas');
ctx.drawImageToRect(otherCanvas, new Rectangle(0, 0, 100, 20),
sourceRect: new Rectangle(0, 0, 100, 20));
另请参阅
- CanvasImageSource 了解从
source
获取的数据的更多信息。 - 来自 WHATWG 的 drawImage。
实现
void drawImageToRect(CanvasImageSource source, Rectangle destRect,
{Rectangle? sourceRect}) {
if (sourceRect == null) {
drawImageScaled(
source, destRect.left, destRect.top, destRect.width, destRect.height);
} else {
drawImageScaledFromSource(
source,
sourceRect.left,
sourceRect.top,
sourceRect.width,
sourceRect.height,
destRect.left,
destRect.top,
destRect.width,
destRect.height);
}
}