Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

⭐️ Fix and refactorize antialiasing defaults #430

Merged
merged 3 commits into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions docs/docs/paint/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ There is also a `<Paint />` component which can be assigned directly to a drawin

The following painting attributes can be assigned as properties:
* [color](properties.md#color)
* [blendMode](properties.md#blendMode)
* [blendMode](properties.md#blendmode)
* [style](properties.md#style)
* [strokeWidth](properties.md#strokeWidth)
* [strokeJoin](properties.md#strokeJoin)
* [strokeCap](properties.md#strokeCap)
* [strokeMiter](properties.md#strokeMiter)
* [opacity](properties.md#opacity)
* [strokeWidth](properties.md#strokewidth)
* [strokeJoin](properties.md#strokejoin)
* [strokeCap](properties.md#strokecap)
* [strokeMiter](properties.md#strokemiter)
* [opacity](properties.md#opacity)
* [antiAlias](properties.md#antialias)

The following painting attributes can be assigned as children:
* [Shaders](/docs/shaders/overview)
Expand Down
22 changes: 13 additions & 9 deletions docs/docs/paint/properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The following children can also be assigned to a Paint:
* [Mask Filters](/docs/mask-filters)
* [Path Effects](/docs/path-effects)

## Color
## color

Sets the alpha and RGB used when stroking and filling.
The color is a string or a number.
Expand All @@ -28,7 +28,7 @@ import {Paint} from "@shopify/react-native-skia";
</>
```

## Opacity
## opacity

Replaces alpha, leaving RGBA unchanged. 0 means fully transparent, 1.0 means opaque.
When setting opacity in a Group component, the alpha component of all descending colors will inherit that value.
Expand All @@ -55,31 +55,35 @@ export const OpacityDemo = () => {

![Opacity](./assets/opacity.png)

## Blend Mode
## blendMode

Sets the blend mode that is, the mode used to combine source color with destination color.
The following values are available: `clear`, `src`, `dst`, `srcOver`, `dstOver`, `srcIn`, `dstIn`, `srcOut`, `dstOut`,
`srcATop`, `dstATop`, `xor`, `plus`, `modulate`, `screen`, `overlay`, `darken`, `lighten`, `colorDodge`, `colorBurn`, `hardLight`,
`softLight`, `difference`, `exclusion`, `multiply`, `hue`, `saturation`, `color`, `luminosity`.

## Style
## style

The paint style can be `fill` (default) or `stroke`.

## Stroke Width
## strokeWidth

Thickness of the pen used to outline the shape.

## Stroke Join
## strokeJoin

Sets the geometry drawn at the corners of strokes.
Values can be `bevel`, `miter`, or `round`.

## Stroke Cap
## strokeCap

Returns the geometry drawn at the beginning and end of strokes.
Values can be `butt`, `round`, or `square`.

## Stroke Miter
## strokeMiter

Limit at which a sharp corner is drawn beveled.
Limit at which a sharp corner is drawn beveled.

## antiAlias

Requests, but does not require, that edge pixels draw opaque or with partial transparency.
4 changes: 2 additions & 2 deletions package/src/renderer/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { debug as hostDebug, skHostConfig } from "./HostConfig";
import { vec } from "./processors";
import { Container } from "./nodes";
import { DependencyManager } from "./DependencyManager";
import { SkiaPaint } from "./processors/Paint";

const CanvasContext = React.createContext<SkiaReadonlyValue<{
width: number;
Expand Down Expand Up @@ -112,8 +113,7 @@ export const Canvas = forwardRef<SkiaView, CanvasProps>(
) {
canvasCtx.current = { width, height };
}
const paint = Skia.Paint();
paint.setAntiAlias(true);
const paint = SkiaPaint();
const ctx = {
width,
height,
Expand Down
5 changes: 2 additions & 3 deletions package/src/renderer/components/Paint.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import type { ReactNode } from "react";
import React, { useRef, useMemo, forwardRef, useImperativeHandle } from "react";

import type { SkPaint } from "../../skia";
import { Skia } from "../../skia";
import type { CustomPaintProps, AnimatedProps } from "../processors";
import { processPaint } from "../processors";
import { SkiaPaint, processPaint } from "../processors";
import { createDeclaration } from "../nodes";

export const usePaintRef = () => useRef<SkPaint>(null);
Expand All @@ -15,7 +14,7 @@ export interface PaintProps extends Omit<CustomPaintProps, "paint"> {

export const Paint = forwardRef<SkPaint, AnimatedProps<PaintProps>>(
(props, ref) => {
const paint = useMemo(() => Skia.Paint(), []);
const paint = useMemo(() => SkiaPaint(), []);
useImperativeHandle(ref, () => paint, [paint]);
const onDeclare = useMemo(
() =>
Expand Down
11 changes: 11 additions & 0 deletions package/src/renderer/processors/Paint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ import type { SkPaint, Color, SkImageFilter } from "../../skia";
import type { DeclarationResult } from "../nodes";
export type SkEnum<T> = Uncapitalize<keyof T extends string ? keyof T : never>;

export const SkiaPaint = () => {
const paint = Skia.Paint();
paint.setAntiAlias(true);
return paint;
};

export interface ChildrenProps {
children?: ReactNode | ReactNode[];
}
Expand All @@ -32,6 +38,7 @@ export interface CustomPaintProps extends ChildrenProps {
strokeCap?: SkEnum<typeof StrokeCap>;
strokeMiter?: number;
opacity?: number;
antiAlias?: boolean;
}

export const enumKey = <K extends string>(k: K) =>
Expand All @@ -50,6 +57,7 @@ export const processPaint = (
strokeCap,
strokeMiter,
opacity,
antiAlias,
}: CustomPaintProps,
children: DeclarationResult[]
) => {
Expand Down Expand Up @@ -85,6 +93,9 @@ export const processPaint = (
if (opacity !== undefined) {
paint.setAlphaf(opacity);
}
if (antiAlias !== undefined) {
paint.setAntiAlias(antiAlias);
}
children.forEach((child) => {
if (isShader(child)) {
paint.setShader(child);
Expand Down
5 changes: 2 additions & 3 deletions package/src/skia/Paint/usePaint.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { DependencyList } from "react";
import { useMemo } from "react";

import { Skia } from "../Skia";
import { SkiaPaint } from "../../renderer/processors/Paint";

import type { SkPaint } from "./Paint";

Expand All @@ -13,8 +13,7 @@ export const usePaint = (
deps?: DependencyList
) =>
useMemo(() => {
const p = Skia.Paint();
p.setAntiAlias(true);
const p = SkiaPaint();
if (initializer) {
initializer(p);
}
Expand Down