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

↕️ useCanvasSize() #380

Merged
merged 5 commits into from
Apr 8, 2022
Merged

↕️ useCanvasSize() #380

merged 5 commits into from
Apr 8, 2022

Conversation

wcandillon
Copy link
Contributor

@wcandillon wcandillon commented Apr 8, 2022

fixes #352

Exposes the canvas size as a Skia Value.
It also exposes the onLayout prop which could be useful.

Originally I wanted to expose the whole DrawingInfo via this hook but realized that it would update more than usually needed.

@wcandillon wcandillon requested a review from chrfalch April 8, 2022 12:18
@wcandillon wcandillon mentioned this pull request Apr 8, 2022
const canvasCtx = useValue<{ width: number; height: number }>({
width: -1,
height: -1,
});
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it will be -1 always on the first render but I'm not sure how we could deal with this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is the only way to go I think.

const root = useMemo(
() => skiaReconciler.createContainer(container, 0, false, null),
[container]
);
// Render effect
useEffect(() => {
render(
<CanvasContext.Provider value={canvasCtx.current}>
<CanvasContext.Provider value={canvasCtx}>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to be play well with hot reload, could this be improved?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does the reload problem materialise itself? It looks correct since the effect is dependent on the canvasCtx?

@@ -93,6 +72,7 @@ const defaultFontMgr = Skia.FontMgr.RefDefault();

export const Canvas = forwardRef<SkiaView, CanvasProps>(
({ children, style, debug, mode, onTouch, fontMgr }, forwardedRef) => {
const canvasCtx = useValue({ width: -1, height: -1 });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default value should be 0.

@wcandillon
Copy link
Contributor Author

I updated the default size and I can tell that the fast refresh works great (the issue was when updating the Canvas.tsx file)

@@ -93,6 +72,7 @@ const defaultFontMgr = Skia.FontMgr.RefDefault();

export const Canvas = forwardRef<SkiaView, CanvasProps>(
({ children, style, debug, mode, onTouch, fontMgr }, forwardedRef) => {
const canvasCtx = useValue({ width: -1, height: -1 });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this be a regular ref? The only reason for using a value would be that we want to be able to listen to changes on the value and this kind of does not make sense for the context?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes that's correct. I wanted to keep the types of the public API same but let me check if using a ref instead still keep the public types safe.

height !== canvasCtx.current.height
) {
canvasCtx.current = { width, height };
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm thinking it might be possible to include more of the drawing context - I saw your comment about wanting to do so. Is there a specific reason for not doing so?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea was that useDerived() that depends on the canvasCtx value should only be run if we modify the width/height. But the expression is always re-evaluated on redraw? I will double check.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checked and this does save some unnecessary JS evaluation. but of course let's keep it in mind (as well as offering a way to access the ref)

*/
onDraw?: RNSkiaDrawCallback;
};

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any specific reason for moving these types into the SkiaView file?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having the Props and the components together felt clearer which is which (NativeSkiaViewProps vs SkiaViewProps). I see also that NativeSkiaViewProps and requireComponent also live in separate file.

package/src/views/SkiaView.tsx Show resolved Hide resolved
@wcandillon wcandillon merged commit e2f07c6 into main Apr 8, 2022
@wcandillon wcandillon deleted the feat/useCanvas branch April 16, 2022 04:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

useCanvas() hook
2 participants