跳到主要内容

绘画

在 Skia 中绘制任何内容时,您需要指定它的颜色、如何与背景混合,或者以何种样式绘制。我们称这些为绘画属性。在 React Native Skia 中,这些属性可以指定为属性,或者指定为绘图组件(例如 <Rect /><Circle />)或 <Group /> 的子组件。还有一个 <Paint /> 组件,可以通过其引用直接分配给绘图或组。

以下绘画属性可以作为属性进行分配

以下绘画属性可以作为子组件进行分配

填充和描边

在 Skia 中,画笔有一个样式属性来指示它是填充还是描边画笔。在绘制某些内容时,您可以传递 Paint 组件作为子组件来添加描边和填充。在下面的示例中,圆形有一个浅蓝色填充和两个描边画笔。

tsx
import {Canvas, Circle, Paint, vec} from "@shopify/react-native-skia";
 
const width = 256;
const height = 256;
 
export const PaintDemo = () => {
const strokeWidth = 10;
const c = vec(width / 2, height / 2);
const r = (width - strokeWidth) / 2;
return (
<Canvas style={{ width, height}}>
<Circle c={c} r={r} color="red">
<Paint color="lightblue" />
<Paint color="#adbce6" style="stroke" strokeWidth={strokeWidth} />
<Paint color="#ade6d8" style="stroke" strokeWidth={strokeWidth / 2} />
</Circle>
</Canvas>
);
};
tsx
import {Canvas, Circle, Paint, vec} from "@shopify/react-native-skia";
 
const width = 256;
const height = 256;
 
export const PaintDemo = () => {
const strokeWidth = 10;
const c = vec(width / 2, height / 2);
const r = (width - strokeWidth) / 2;
return (
<Canvas style={{ width, height}}>
<Circle c={c} r={r} color="red">
<Paint color="lightblue" />
<Paint color="#adbce6" style="stroke" strokeWidth={strokeWidth} />
<Paint color="#ade6d8" style="stroke" strokeWidth={strokeWidth / 2} />
</Circle>
</Canvas>
);
};
Paint Fill and strokes

继承

后代会继承画笔属性。在下面的示例中,第一个圆将被填充为浅蓝色,第二个圆将具有浅蓝色描边。

tsx
import {Canvas, Circle, Paint, Group} from "@shopify/react-native-skia";
 
const width = 256;
const height = 256;
 
export const PaintDemo = () => {
const r = width / 6;
return (
<Canvas style={{ width, height }}>
<Group color="lightblue">
<Circle cx={r} cy={r} r={r} />
<Group style="stroke" strokeWidth={10}>
<Circle cx={3 * r} cy={3 * r} r={r} />
</Group>
</Group>
</Canvas>
);
};
tsx
import {Canvas, Circle, Paint, Group} from "@shopify/react-native-skia";
 
const width = 256;
const height = 256;
 
export const PaintDemo = () => {
const r = width / 6;
return (
<Canvas style={{ width, height }}>
<Group color="lightblue">
<Circle cx={r} cy={r} r={r} />
<Group style="stroke" strokeWidth={10}>
<Circle cx={3 * r} cy={3 * r} r={r} />
</Group>
</Group>
</Canvas>
);
};
Paint Inheritance

像着色器或图像滤镜这样复杂的绘画属性可以作为子组件传递给组或绘图。

tsx
import {Canvas, Circle, Group, LinearGradient, vec} from "@shopify/react-native-skia";
 
const width = 256;
const height = 256;
 
export const PaintDemo = () => {
const r = width/2;
return (
<Canvas style={{ width, height }}>
<Circle cx={r} cy={r} r={r}>
<LinearGradient
start={vec(0, 0)}
end={vec(2 * r, 2 * r)}
colors={["#00ff87", "#60efff"]}
/>
</Circle>
<Group>
<LinearGradient
start={vec(2 * r, 2 * r)}
end={vec(4 * r, 4 * r)}
colors={["#0061ff", "#60efff"]}
/>
<Circle cx={3 * r} cy={3 * r} r={r} />
</Group>
</Canvas>
);
};
tsx
import {Canvas, Circle, Group, LinearGradient, vec} from "@shopify/react-native-skia";
 
const width = 256;
const height = 256;
 
export const PaintDemo = () => {
const r = width/2;
return (
<Canvas style={{ width, height }}>
<Circle cx={r} cy={r} r={r}>
<LinearGradient
start={vec(0, 0)}
end={vec(2 * r, 2 * r)}
colors={["#00ff87", "#60efff"]}
/>
</Circle>
<Group>
<LinearGradient
start={vec(2 * r, 2 * r)}
end={vec(4 * r, 4 * r)}
colors={["#0061ff", "#60efff"]}
/>
<Circle cx={3 * r} cy={3 * r} r={r} />
</Group>
</Canvas>
);
};

手动画笔分配

最后,我们可以将引用分配给 Paint 组件以供以后使用。

tsx
import {Canvas, Circle, Paint, Skia} from "@shopify/react-native-skia";
const width = 256;
const height = 256;
const r = width / 2;
const paint = Skia.Paint();
paint.setColor(Skia.Color("lightblue"));
 
export const PaintDemo = () => {
return (
<Canvas style={{ flex: 1 }}>
<Circle paint={paint} cx={r} cy={r} r={r} />
</Canvas>
);
};
tsx
import {Canvas, Circle, Paint, Skia} from "@shopify/react-native-skia";
const width = 256;
const height = 256;
const r = width / 2;
const paint = Skia.Paint();
paint.setColor(Skia.Color("lightblue"));
 
export const PaintDemo = () => {
return (
<Canvas style={{ flex: 1 }}>
<Circle paint={paint} cx={r} cy={r} r={r} />
</Canvas>
);
};