diff --git a/src/pixie/contexts.nim b/src/pixie/contexts.nim index 884f4df..c7f7d5a 100644 --- a/src/pixie/contexts.nim +++ b/src/pixie/contexts.nim @@ -591,7 +591,7 @@ proc strokePolygon*(ctx: Context, pos: Vec2, size: float32, sides: int) = ctx.stroke(path) proc drawImage*(ctx: Context, image: Image, dx, dy, dWidth, dHeight: float32) = - ## Draws an image onto the canvas. + ## Draws a source image onto the destination image. var path: Path var imageMat = ctx.mat * translate(vec2(dx, dy)) * scale(vec2( dWidth / image.width.float32, @@ -602,15 +602,15 @@ proc drawImage*(ctx: Context, image: Image, dx, dy, dWidth, dHeight: float32) = ctx.fill(path) proc drawImage*(ctx: Context, image: Image, dx, dy: float32) = - ## Draws an image onto the canvas. + ## Draws a source image onto the destination image. ctx.drawImage(image, dx, dx, image.width.float32, image.height.float32) proc drawImage*(ctx: Context, image: Image, pos: Vec2) = - ## Draws an image onto the canvas. + ## Draws a source image onto the destination image. ctx.drawImage(image, pos.x, pos.y) proc drawImage*(ctx: Context, image: Image, rect: Rect) = - ## Draws an image onto the canvas. + ## Draws a source image onto the destination image. ctx.drawImage(image, rect.x, rect.y, rect.w, rect.h) proc drawImage*( @@ -619,6 +619,14 @@ proc drawImage*( sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight: float32 ) = - ## Draws an image onto the canvas. + ## Draws a source image onto the destination image. var image = image.subImage(sx.int, sy.int, sWidth.int, sHeight.int) ctx.drawImage(image, dx, dx, image.width.float32, image.height.float32) + +proc drawImage*(ctx: Context, image: Image, src, dest: Rect) = + ## Draws a source image onto the destination image. + ctx.drawImage( + image, + src.x, src.y, src.w, src.h, + dest.x, dest.y, dest.w, dest.h + ) diff --git a/tests/images/context/draw_image_rhino2.png b/tests/images/context/draw_image_rhino2.png new file mode 100644 index 0000000..2fcd692 Binary files /dev/null and b/tests/images/context/draw_image_rhino2.png differ diff --git a/tests/test_contexts.nim b/tests/test_contexts.nim index ee99c0d..bb27f31 100644 --- a/tests/test_contexts.nim +++ b/tests/test_contexts.nim @@ -594,3 +594,11 @@ block: rhino = readImage("tests/images/rhino.png") ctx.drawImage(rhino, 33, 71, 104, 124, 21, 20, 87, 104); image.writeFile("tests/images/context/draw_image_rhino.png") + +block: + let + image = newImage(300, 227) + ctx = newContext(image) + rhino = readImage("tests/images/rhino.png") + ctx.drawImage(rhino, rect(33, 71, 104, 124), rect(21, 20, 87, 104)); + image.writeFile("tests/images/context/draw_image_rhino2.png")