Merge pull request #236 from treeform/drawImage

Add drawImage to context.
This commit is contained in:
treeform 2021-06-26 21:58:38 -07:00 committed by GitHub
commit 4597aec6a5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 99 additions and 1 deletions

View file

@ -589,3 +589,47 @@ proc strokePolygon*(ctx: Context, pos: Vec2, size: float32, sides: int) =
var path: Path
path.polygon(pos, size, sides)
ctx.stroke(path)
proc drawImage*(ctx: Context, image: Image, dx, dy, dWidth, dHeight: float32) =
## Draws a source image onto the destination image.
var
imageMat = ctx.mat * translate(vec2(dx, dy)) * scale(vec2(
dWidth / image.width.float32,
dHeight / image.height.float32
))
savedStyle = ctx.fillStyle
ctx.fillStyle = Paint(kind: pkImage, image: image, imageMat: imageMat)
var path: Path
path.rect(rect(dx, dy, dWidth, dHeight))
ctx.fill(path)
ctx.fillStyle = savedStyle
proc drawImage*(ctx: Context, image: Image, dx, dy: float32) =
## 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 a source image onto the destination image.
ctx.drawImage(image, pos.x, pos.y)
proc drawImage*(ctx: Context, image: Image, rect: Rect) =
## Draws a source image onto the destination image.
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 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
)

View file

@ -1,4 +1,4 @@
import opengl, pixie, pixie/context
import opengl, pixie, pixie/contexts
export pixie

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 807 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

BIN
tests/images/pip1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

BIN
tests/images/rhino.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 196 KiB

View file

@ -548,3 +548,57 @@ block:
ctx.fillRect(50, 50, 100, 100)
image.writeFile("tests/images/context/globalAlpha_1.png")
block:
let
image = newImage(100, 100)
ctx = newContext(image)
testImage = readImage("tests/images/pip1.png")
ctx.drawImage(testImage, 0, 0)
ctx.drawImage(testImage, 30, 30)
image.writeFile("tests/images/context/draw_image.png")
block:
let
image = newImage(100, 100)
ctx = newContext(image)
testImage = readImage("tests/images/pip1.png")
ctx.translate(30, 30)
ctx.drawImage(testImage, -30, -30)
ctx.drawImage(testImage, 0, 0)
image.writeFile("tests/images/context/draw_image_translated.png")
block:
let
image = newImage(100, 100)
ctx = newContext(image)
testImage = readImage("tests/images/pip1.png")
ctx.scale(2, 2)
ctx.drawImage(testImage, 0, 0)
ctx.scale(0.25, 0.25)
ctx.drawImage(testImage, 0, 0)
image.writeFile("tests/images/context/draw_image_scaled.png")
block:
let
image = newImage(100, 100)
ctx = newContext(image)
testImage = readImage("tests/images/pip1.png")
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")
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")