context globalAlpha

This commit is contained in:
Ryan Oldenburg 2021-05-31 21:05:27 -05:00
parent 1022fdcbe2
commit 3658ff40c3
3 changed files with 64 additions and 4 deletions

View file

@ -10,6 +10,7 @@ type
Context* = ref object
image*: Image
fillStyle*, strokeStyle*: Paint
globalAlpha*: float32
lineWidth*: float32
miterLimit*: float32
lineCap*: LineCap
@ -25,6 +26,7 @@ type
ContextState = object
fillStyle, strokeStyle: Paint
globalAlpha: float32
lineWidth: float32
miterLimit: float32
lineCap: LineCap
@ -44,6 +46,7 @@ proc newContext*(image: Image): Context =
result = Context()
result.image = image
result.mat = mat3()
result.globalAlpha = 1
result.lineWidth = 1
result.miterLimit = 10
result.fillStyle = Paint(kind: pkSolid, color: rgbx(0, 0, 0, 255))
@ -56,6 +59,7 @@ proc newContext*(width, height: int): Context {.inline.} =
proc state(ctx: Context): ContextState =
result.fillStyle = ctx.fillStyle
result.strokeStyle = ctx.strokeStyle
result.globalAlpha = ctx.globalAlpha
result.lineWidth = ctx.lineWidth
result.miterLimit = ctx.miterLimit
result.lineCap = ctx.lineCap
@ -94,6 +98,7 @@ proc restore*(ctx: Context) =
let state = ctx.stateStack.pop()
ctx.fillStyle = state.fillStyle
ctx.strokeStyle = state.strokeStyle
ctx.globalAlpha = state.globalAlpha
ctx.lineWidth = state.lineWidth
ctx.miterLimit = state.miterLimit
ctx.lineCap = state.lineCap
@ -113,9 +118,13 @@ proc restore*(ctx: Context) =
else: # Otherwise draw to the root image
ctx.image.draw(poppedLayer)
proc fill(
ctx: Context, image: Image, path: Path, windingRule: WindingRule
) {.inline.} =
proc fill(ctx: Context, image: Image, path: Path, windingRule: WindingRule) =
var image = image
if ctx.globalAlpha != 1:
ctx.saveLayer()
image = ctx.layer
image.fillPath(
path,
ctx.fillStyle,
@ -123,7 +132,17 @@ proc fill(
windingRule
)
proc stroke(ctx: Context, image: Image, path: Path) {.inline.} =
if ctx.globalAlpha != 1:
ctx.layer.applyOpacity(ctx.globalAlpha)
ctx.restore()
proc stroke(ctx: Context, image: Image, path: Path) =
var image = image
if ctx.globalAlpha != 1:
ctx.saveLayer()
image = ctx.layer
image.strokePath(
path,
ctx.strokeStyle,
@ -135,6 +154,10 @@ proc stroke(ctx: Context, image: Image, path: Path) {.inline.} =
ctx.lineDash
)
if ctx.globalAlpha != 1:
ctx.layer.applyOpacity(ctx.globalAlpha)
ctx.restore()
proc fillText(ctx: Context, image: Image, text: string, at: Vec2) {.inline.} =
if ctx.font.typeface == nil:
raise newException(PixieError, "No font has been set on this Context")
@ -145,6 +168,12 @@ proc fillText(ctx: Context, image: Image, text: string, at: Vec2) {.inline.} =
ctx.font.paint = ctx.fillStyle
var image = image
if ctx.globalAlpha != 1:
ctx.saveLayer()
image = ctx.layer
image.fillText(
ctx.font,
text,
@ -152,6 +181,10 @@ proc fillText(ctx: Context, image: Image, text: string, at: Vec2) {.inline.} =
hAlign = ctx.textAlign
)
if ctx.globalAlpha != 1:
ctx.layer.applyOpacity(ctx.globalAlpha)
ctx.restore()
proc strokeText(ctx: Context, image: Image, text: string, at: Vec2) {.inline.} =
if ctx.font.typeface == nil:
raise newException(PixieError, "No font has been set on this Context")
@ -162,6 +195,12 @@ proc strokeText(ctx: Context, image: Image, text: string, at: Vec2) {.inline.} =
ctx.font.paint = ctx.strokeStyle
var image = image
if ctx.globalAlpha != 1:
ctx.saveLayer()
image = ctx.layer
image.strokeText(
ctx.font,
text,
@ -174,6 +213,10 @@ proc strokeText(ctx: Context, image: Image, text: string, at: Vec2) {.inline.} =
dashes = ctx.lineDash
)
if ctx.globalAlpha != 1:
ctx.layer.applyOpacity(ctx.globalAlpha)
ctx.restore()
proc beginPath*(ctx: Context) {.inline.} =
## Starts a new path by emptying the list of sub-paths.
ctx.path = Path()

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View file

@ -531,3 +531,20 @@ block:
ctx.fillRect(10, 10, 100, 100)
image.writeFile("tests/images/context/blendmode_1.png")
block:
let
image = newImage(300, 150)
ctx = newContext(image)
image.fill(rgba(255, 255, 255, 255))
ctx.globalAlpha = 0.5
ctx.fillStyle = "blue"
ctx.fillRect(10, 10, 100, 100)
ctx.fillStyle = "red"
ctx.fillRect(50, 50, 100, 100)
image.writeFile("tests/images/context/globalAlpha_1.png")