Skip to content

Commit a951081

Browse files
committed
Merge branch 'main' into next
2 parents c6aa01c + 66d44b1 commit a951081

File tree

5 files changed

+26
-69
lines changed

5 files changed

+26
-69
lines changed

Diff for: packages/skia/cpp/api/recorder/Drawings.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ class BoxCmd : public Command {
418418
SkPaint shadowPaint;
419419
shadowPaint.setAntiAlias(true);
420420
shadowPaint.setColor(shadow.color.value_or(SK_ColorBLACK));
421-
shadowPaint.setAlphaf(opacity);
421+
shadowPaint.setAlphaf(opacity * shadowPaint.getAlphaf());
422422
shadowPaint.setMaskFilter(SkMaskFilter::MakeBlur(
423423
SkBlurStyle::kNormal_SkBlurStyle, shadow.blur, true));
424424

@@ -442,7 +442,7 @@ class BoxCmd : public Command {
442442
SkPaint shadowPaint;
443443
shadowPaint.setAntiAlias(true);
444444
shadowPaint.setColor(shadow.color.value_or(SK_ColorBLACK));
445-
shadowPaint.setAlphaf(opacity);
445+
shadowPaint.setAlphaf(opacity * shadowPaint.getAlphaf());
446446
shadowPaint.setMaskFilter(SkMaskFilter::MakeBlur(
447447
SkBlurStyle::kNormal_SkBlurStyle, shadow.blur, true));
448448

Loading

Diff for: packages/skia/src/renderer/__tests__/e2e/Box.spec.tsx

+22
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,26 @@ describe("Box", () => {
8080
);
8181
checkImage(img, "snapshots/box/box-stroke.png");
8282
});
83+
84+
it("should draw a shadow with opacity", async () => {
85+
const { rect } = importSkia();
86+
const { width } = surface;
87+
const size = width / 2;
88+
const img = await surface.draw(
89+
<>
90+
<Fill color="white" />
91+
<Box box={rect(size / 2, size / 2, size, size)} color="red">
92+
<BoxShadow
93+
dx={0}
94+
dy={0}
95+
blur={5}
96+
spread={10}
97+
inner
98+
color={"rgba(0, 0, 255, 0.5)"}
99+
/>
100+
</Box>
101+
</>
102+
);
103+
checkImage(img, "snapshots/box/box-shadow-opacity.png");
104+
});
83105
});

Diff for: packages/skia/src/sksg/Recorder/commands/Box.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const drawBox = (ctx: DrawingContext, command: BoxCommand) => {
3030
const { color = "black", blur, spread = 0, dx = 0, dy = 0 } = shadow;
3131
const lPaint = Skia.Paint();
3232
lPaint.setColor(processColor(Skia, color));
33-
lPaint.setAlphaf(paint.getAlphaf() * opacity);
33+
lPaint.setAlphaf(lPaint.getAlphaf() * opacity);
3434
lPaint.setMaskFilter(
3535
Skia.MaskFilter.MakeBlur(BlurStyle.Normal, blur, true)
3636
);
@@ -48,7 +48,7 @@ export const drawBox = (ctx: DrawingContext, command: BoxCommand) => {
4848
canvas.clipRRect(box, ClipOp.Intersect, false);
4949
const lPaint = Skia.Paint();
5050
lPaint.setColor(Skia.Color(color));
51-
lPaint.setAlphaf(paint.getAlphaf() * opacity);
51+
lPaint.setAlphaf(lPaint.getAlphaf() * opacity);
5252

5353
lPaint.setMaskFilter(
5454
Skia.MaskFilter.MakeBlur(BlurStyle.Normal, blur, true)

Diff for: packages/skia/src/sksg/Recorder/commands/Drawing.ts

-65
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import {
2-
deflate,
32
enumKey,
43
fitRects,
5-
inflate,
6-
NodeType,
74
processCircle,
85
processColor,
96
processPath,
@@ -12,8 +9,6 @@ import {
129
} from "../../../dom/nodes";
1310
import type {
1411
AtlasProps,
15-
BoxProps,
16-
BoxShadowProps,
1712
CircleProps,
1813
DiffRectProps,
1914
DrawingNodeProps,
@@ -38,18 +33,13 @@ import { saturate } from "../../../renderer/processors";
3833
import type { SkPoint, SkRSXform } from "../../../skia/types";
3934
import {
4035
BlendMode,
41-
BlurStyle,
42-
ClipOp,
4336
FillType,
4437
FilterMode,
4538
isCubicSampling,
46-
isRRect,
4739
MipmapMode,
4840
PointMode,
4941
VertexMode,
5042
} from "../../../skia/types";
51-
import type { Node } from "../../Node";
52-
import { materialize } from "../../utils";
5343
import type { DrawingContext } from "../DrawingContext";
5444

5545
export const drawLine = (ctx: DrawingContext, props: LineProps) => {
@@ -64,61 +54,6 @@ export const drawOval = (ctx: DrawingContext, props: OvalProps) => {
6454
ctx.canvas.drawOval(rect, ctx.paint);
6555
};
6656

67-
export const drawBox = (
68-
ctx: DrawingContext,
69-
props: BoxProps,
70-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
71-
children: Node<any>[]
72-
) => {
73-
"worklet";
74-
const { paint, Skia, canvas } = ctx;
75-
const { box: defaultBox } = props;
76-
const opacity = paint.getAlphaf();
77-
const box = isRRect(defaultBox) ? defaultBox : Skia.RRectXY(defaultBox, 0, 0);
78-
const shadows = children
79-
.map((node) => {
80-
if (node.type === NodeType.BoxShadow) {
81-
return materialize(node.props);
82-
}
83-
return null;
84-
})
85-
.filter((n): n is BoxShadowProps => n !== null);
86-
shadows
87-
.filter((shadow) => !shadow.inner)
88-
.map((shadow) => {
89-
const { color = "black", blur, spread = 0, dx = 0, dy = 0 } = shadow;
90-
const lPaint = Skia.Paint();
91-
lPaint.setColor(processColor(Skia, color));
92-
lPaint.setAlphaf(paint.getAlphaf() * opacity);
93-
lPaint.setMaskFilter(
94-
Skia.MaskFilter.MakeBlur(BlurStyle.Normal, blur, true)
95-
);
96-
canvas.drawRRect(inflate(Skia, box, spread, spread, dx, dy), lPaint);
97-
});
98-
99-
canvas.drawRRect(box, paint);
100-
101-
shadows
102-
.filter((shadow) => shadow.inner)
103-
.map((shadow) => {
104-
const { color = "black", blur, spread = 0, dx = 0, dy = 0 } = shadow;
105-
const delta = Skia.Point(10 + Math.abs(dx), 10 + Math.abs(dy));
106-
canvas.save();
107-
canvas.clipRRect(box, ClipOp.Intersect, false);
108-
const lPaint = Skia.Paint();
109-
lPaint.setColor(processColor(Skia, color));
110-
lPaint.setAlphaf(paint.getAlphaf() * opacity);
111-
112-
lPaint.setMaskFilter(
113-
Skia.MaskFilter.MakeBlur(BlurStyle.Normal, blur, true)
114-
);
115-
const inner = deflate(Skia, box, spread, spread, dx, dy);
116-
const outer = inflate(Skia, box, delta.x, delta.y);
117-
canvas.drawDRRect(outer, inner, lPaint);
118-
canvas.restore();
119-
});
120-
};
121-
12257
export const drawImage = (ctx: DrawingContext, props: ImageProps) => {
12358
"worklet";
12459
const { image, sampling } = props;

0 commit comments

Comments
 (0)