diff --git a/src/pixie/contexts.nim b/src/pixie/contexts.nim index bb9e764..884f4df 100644 --- a/src/pixie/contexts.nim +++ b/src/pixie/contexts.nim @@ -590,25 +590,35 @@ proc strokePolygon*(ctx: Context, pos: Vec2, size: float32, sides: int) = path.polygon(pos, size, sides) ctx.stroke(path) -# proc drawImage*(ctx: Context, image: Image, dx, dy, dWidth, dHeight: float32) = -# ## Draws an image onto the canvas. -# var path: Path -# var imageMat = ctx.mat * translate(vec2(dx, dy)) * scale(vec2( -# image.width.float32 / dWidth, -# image.height.float32 / dHeight -# )) -# ctx.fillStyle = Paint(kind: pkImage, image: image, imageMat: imageMat) -# path.rect(rect(dx, dy, image.width.float32, image.height.float32)) -# ctx.fill(path) +proc drawImage*(ctx: Context, image: Image, dx, dy, dWidth, dHeight: float32) = + ## Draws an image onto the canvas. + var path: Path + var imageMat = ctx.mat * translate(vec2(dx, dy)) * scale(vec2( + dWidth / image.width.float32, + dHeight / image.height.float32 + )) + ctx.fillStyle = Paint(kind: pkImage, image: image, imageMat: imageMat) + path.rect(rect(dx, dy, dWidth, dHeight)) + ctx.fill(path) proc drawImage*(ctx: Context, image: Image, dx, dy: float32) = ## Draws an image onto the canvas. - var path: Path - var imageMat = ctx.mat * translate(vec2(dx, dy)) - ctx.fillStyle = Paint(kind: pkImage, image: image, imageMat: imageMat) - path.rect(rect(dx, dy, image.width.float32, image.height.float32)) - ctx.fill(path) + 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. ctx.drawImage(image, pos.x, pos.y) + +proc drawImage*(ctx: Context, image: Image, rect: Rect) = + ## Draws an image onto the canvas. + ctx.drawImage(image, rect.x, rect.y, rect.w, rect.h) + +proc drawImage*( + ctx: Context, + image: Image, + sx, sy, sWidth, sHeight, + dx, dy, dWidth, dHeight: float32 +) = + ## Draws an image onto the canvas. + var image = image.subImage(sx.int, sy.int, sWidth.int, sHeight.int) + ctx.drawImage(image, dx, dx, image.width.float32, image.height.float32) diff --git a/tests/images/context/draw_image_rhino.png b/tests/images/context/draw_image_rhino.png new file mode 100644 index 0000000..2fcd692 Binary files /dev/null and b/tests/images/context/draw_image_rhino.png differ diff --git a/tests/images/context/draw_image_self_scaled.png b/tests/images/context/draw_image_self_scaled.png new file mode 100644 index 0000000..f209d8d Binary files /dev/null and b/tests/images/context/draw_image_self_scaled.png differ diff --git a/tests/images/rhino.png b/tests/images/rhino.png new file mode 100644 index 0000000..41cbea8 Binary files /dev/null and b/tests/images/rhino.png differ diff --git a/tests/test_contexts.nim b/tests/test_contexts.nim index 00936c7..ee99c0d 100644 --- a/tests/test_contexts.nim +++ b/tests/test_contexts.nim @@ -584,5 +584,13 @@ block: image = newImage(100, 100) ctx = newContext(image) var testImage = readImage("tests/images/pip1.png") - ctx.drawImage(testImage, 0, 0, 10, 10) + ctx.drawImage(testImage, 30, 30, 20, 20) image.writeFile("tests/images/context/draw_image_self_scaled.png") + +block: + let + image = newImage(300, 227) + ctx = newContext(image) + 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")