iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
✅

Building an Animated Checkbox with React Native Skia

に公開

Hi, I'm nano72mkn.
In this article, I tried making an "animated checkbox" using React Native Skia!
Basically, this is just how I managed to get it to work, so there might be some mistakes!

What is Skia?

Skia is an open-source 2D graphics library developed by Google. It seems to be used in things like Google Chrome, ChromeOS, Android, and Flutter.

https://skia.org/

By the way, looking at the Flutter documentation, it seems they will be transitioning to Skia's successor, Impeller, in the future.
https://docs.flutter.dev/perf/impeller

Main Topic: Creating the Checkbox

Here is the finished product↓
Checkbox finished product
I'm going to create this checkbox.

Creating a Square

Specifying the Rectangle

const x = 0;
const y = 0;
const width = 100;
const height = 100;

const rectangle = rect(x, y, width, height);

Generating SkPath and adding rectangle

Generate an SkPath with Skia.Path.Make and add the rectangle as an argument to the SkPath method.

const rectPath = Skia.Path.Make();
rectPath.addRect(rectangle);

Let's try drawing the SkPath

The created SkPath is drawn using the Path component.

<Path path={rectPath} color="#ffffff" />

When displaying it on the screen, you need to wrap it with a Canvas component.
You also need to specify width and height in the style to secure the drawing area.

<Canvas style={{ width, height }}>
  <Path path={rectPath} color="#ffffff" />
</Canvas>

The rendered square

Rendered square

Full Code
import { Skia, rect, Path, Canvas } from '@shopify/react-native-skia';

const x = 0;
const y = 0;
const width = 100;
const height = 100;
const rectangle = rect(x, y, width, height);

const rectPath = Skia.Path.Make();
rectPath.addRect(rectangle);

export default function Index() {
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Canvas style={{ width, height }}>
        <Path path={rectPath} color="#ffffff" />
      </Canvas>
    </View>
  );
}

Making the square rounded

As you can see from the previous screenshot, the corners are very sharp, so let's round them off.

Specifying corner radius for the created rectangle

Pass the previously created rectangle into rrect to specify the corner radius.

+const rx = 10;
+const ry = 10;

const rectangle = rect(x, y, width, height);
+const roundedRect = rrect(rectangle, rx, ry);

Slight correction to SkPath

Change addRect to addRRect and pass roundedRect.

const rectPath = Skia.Path.Make();
-rectPath.addRect(rectangle);
+rectPath.addRRect(roundedRect);

The rendered rounded rectangle

Rendered rounded rectangle

Full Code
import { Skia, rect, Path, Canvas, rrect } from '@shopify/react-native-skia';

const x = 0;
const y = 0;
const width = 100;
const height = 100;
const rx = 10;
const ry = 10;

const rectangle = rect(x, y, width, height);
const roundedRect = rrect(rectangle, rx, ry);

const rectPath = Skia.Path.Make();
rectPath.addRRect(roundedRect);

export default function Index() {
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Canvas style={{ width, height }}>
        <Path path={rectPath} color="#ffffff" />
      </Canvas>
    </View>
  );
}

Creating the border of the square

Set style to stroke.
You can specify the thickness of the border with strokeWidth.

const strokeWidth = 5;

...
<Path path={rectPath} color="#3ea8ff" style="stroke" strokeWidth={strokeWidth} />

If you draw it as is, it will look awkward as shown in the screenshot.
Border preview

This is because the Canvas size and the drawing position of rectPath do not account for the stroke width, causing half of the line to be cut off and making it look messy.

Adjusting by considering the stroke width

Adjusting the position of the square

Shift x and y by half of the protruding stroke width.

+const strokeWidth = 5;
-const x = 0;
+const x = 0 + strokeWidth / 2;
-const y = 0;
+const y = 0 + strokeWidth / 2;
Adjusting the Canvas size

Since half of the stroke width is protruding on all sides (top, bottom, left, right), add them up and increase the size by the total stroke width.

+const canvasWidth = width + strokeWidth * 2;
+const canvasHeight = height + strokeWidth * 2;
...
-      <Canvas style={{ width, height }}>
+      <Canvas style={{ width: canvasWidth, height: canvasHeight }}>

Let's try drawing it

After adjusting the position, it is displayed beautifully!
Drawing after adjustment

Full Code
import { Skia, rect, Path, Canvas, rrect } from '@shopify/react-native-skia';

+const strokeWidth = 5;
-const x = 0;
+const x = 0 + strokeWidth / 2;
-const y = 0;
+const y = 0 + strokeWidth / 2;
const width = 100;
const height = 100;
...
+const canvasWidth = width + strokeWidth * 2;
+const canvasHeight = height + strokeWidth * 2;

...
export default function Index() {
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
-      <Canvas style={{ width, height }}>
+      <Canvas style={{ width: canvasWidth, height: canvasHeight }}>
        <Path path={rectPath} color="#ffffff" />
        <Path path={rectPath} color="#3ea8ff" style="stroke" strokeWidth={strokeWidth} />
      </Canvas>
    </View>
  );
}

Creating the checkmark

Now for the most fun part—creating the checkmark.

Creating SkPath

This time, we will draw using lineTo in SkPath.
Just like the previous rectangle, we use Skia.Path.Make.

const checkPath = Skia.Path.Make();

Drawing the lines

First, we decide the position of the "first stroke" (or "Kihitsu/Entry" in calligraphy).
Place the brush about 1/4 from the left and halfway from the top.

checkPath.moveTo(25, 50);

Then, following your intuition, move the brush to the turning point of the checkmark.

checkPath.lineTo(40, 70);

And decisively finish the stroke.

checkPath.lineTo(85, 35);

Drawing the finished path on the Canvas looks like this:

<Path path={checkPath} color="#3ea8ff" style="stroke" strokeWidth={10} />

It looks like this:
Checkmark preview

Sorry, I can't explain it very well... lol
There might be some complex calculations or formulas for moveTo and lineTo.
But drawing based on intuition is fun too!

Animating it

When adding animations, we use react-native-reanimated.
https://docs.swmansion.com/react-native-reanimated/

Making it tappable

I changed the View to a Pressable and made it so that the isChecked state can be toggled using onPress.

export default function Index() {
  const [isChecked, setIsChecked] = useState(false);
  return (
-    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
+    <Pressable style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }} onPress={() => setIsChecked(v => !v)}>
     ...
-    </View>
+    </Pressable>
  );
}

Making the border appear when checked

Import useSharedValue from reanimated.

const progress = useSharedValue(0);

Make it so that progress becomes 1 when isChecked is true.

  useEffect(() => {
    if (!isChecked) {
      progress.value = withTiming(0);
      return;
    }
    progress.value = withTiming(1);
  }, [isChecked]);

Specify progress for the end prop of the Path component.

<Path path={rectPath} color="#3ea8ff" style="stroke" strokeWidth={strokeWidth} end={progress} strokeCap="round" />

By specifying round for strokeCap, the ends of the path will be rounded.

Border animation preview

Border animation

Full Code
export default function Index() {
  const [isChecked, setIsChecked] = useState(false);
+  const progress = useSharedValue(0);

+  useEffect(() => {
+    if (!isChecked) {
+      progress.value = withTiming(0);
+      return;
+    }
+    progress.value = withTiming(1);
+  }, [isChecked]);

  return (
    <Pressable style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }} onPress={() => setIsChecked(v => !v)}>
      <Canvas style={{ width: canvasWidth, height: canvasHeight }}>
        <Path path={rectPath} color="#ffffff" />
-        <Path path={rectPath} color="#3ea8ff" style="stroke" strokeWidth={strokeWidth} />
+        <Path path={rectPath} color="#3ea8ff" style="stroke" strokeWidth={strokeWidth} end={progress} strokeCap="round" />
        <Path path={checkPath} color="#3ea8ff" style="stroke" strokeWidth={10} />
      </Canvas>
    </Pressable>
  );
}

Animating the checkmark as well

Just like the border, add end and strokeCap props.
I don't want the turning point to be sharp, so I'll specify round for strokeJoin, just like strokeCap.

<Path path={checkPath} color="#3ea8ff" style="stroke" strokeWidth={10} end={progress} strokeCap="round" strokeJoin="round" />

Completion

Once you set up the animation for the checkmark, it's complete!
Animation set for the checkmark

In my case, I made it so the background color also changes at the same time as the checkmark!

Conclusion

From the moment I decided to make a checkbox with React Native Skia until I finished it, it took me about two hours... lol

There are many React Native Skia videos on YouTube, and most of them are very high quality. I spent time gathering bits and pieces that seemed useful—like how to draw lines or create rounded corners—and before I knew it, time had flown by.

I hope this reaches someone who, like me, just wants to try it out!

GitHubで編集を提案

Discussion