Skip to content

Commit

Permalink
Fix opacity bug (#975)
Browse files Browse the repository at this point in the history
  • Loading branch information
wcandillon authored Oct 6, 2022
1 parent 9b36c98 commit de98dbd
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 4 additions & 3 deletions package/src/dom/nodes/RenderNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,10 @@ export abstract class JsiRenderNode<P extends GroupProps>
const { invertClip, layer, matrix, transform } = this.props;
const { canvas } = parentCtx;

const opacity = this.props.opacity
? parentCtx.opacity * this.props.opacity
: parentCtx.opacity;
const opacity =
this.props.opacity !== undefined
? parentCtx.opacity * this.props.opacity
: parentCtx.opacity;

if (
this.paintCache === null ||
Expand Down
53 changes: 53 additions & 0 deletions package/src/renderer/__tests__/Opacity.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React from "react";

import { processResult } from "../../__tests__/setup";
import { Fill, Group, RoundedRect } from "../components";

import { drawOnNode, width, importSkia } from "./setup";

describe("Opacity", () => {
it("Should multiply the opacity to 0", () => {
const { rect, rrect } = importSkia();
const r = width * 0.5;
const surface = drawOnNode(
<Group>
<Fill color="lightblue" />
<Group opacity={0}>
<RoundedRect
rect={rrect(rect(0, 0, r, r), r, r)}
color="rgba(0, 0, 0, 0.2)"
/>
<RoundedRect
rect={rrect(rect(0, 0, r / 2, r / 2), r, r)}
color="white"
/>
</Group>
</Group>
);
processResult(surface, "snapshots/drawings/opacity-multiplication.png");
});
it("Should multiply the opacity to 0.25", () => {
const { rect, rrect } = importSkia();
const r = width * 0.5;
const surface = drawOnNode(
<Group opacity={0.5}>
<Fill color="lightblue" />
<Group opacity={0.5}>
<RoundedRect
rect={rrect(rect(0, 0, r, r), r, r)}
color="rgba(0, 0, 0, 0.2)"
/>
<RoundedRect
rect={rrect(rect(0, 0, r / 2, r / 2), r, r)}
color="white"
/>
</Group>
</Group>
);
processResult(
surface,
"snapshots/drawings/opacity-multiplication2.png",
true
);
});
});

0 comments on commit de98dbd

Please sign in to comment.