Draw with source cutout.

This commit is contained in:
treeform 2021-06-25 22:07:02 -07:00
parent 8736b74a3e
commit 1ee2532087
5 changed files with 34 additions and 16 deletions

View file

@ -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)

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 807 B

BIN
tests/images/rhino.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 196 KiB

View file

@ -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")