Skip to content

Canvas size behaviour regression when transformed #3836

@donaldharvey

Description

@donaldharvey

Description

There appears to have been a regression in the behaviour of canvas onSize at some point between 2.2.12 and 2.6.2, specifically when the canvas is itself transformed. The width and height supplied by onSize appear to use the size on-screen after the transform has been applied, rather than its "natural" layout size as rendered. This causes anything that is drawn using the canvas size as input to be drawn at the wrong size when there is a canvas transform applied.

React Native Skia Version

2.6.2

React Native Version

0.81.5

Using New Architecture

  • Enabled

Steps to Reproduce

Apply a scale transformation to the canvas (either directly or via a parent view), then render a <Rect> with the width and height from the canvas size.

The expected behaviour in the code below is that the red <Rect> fills the canvas and the view behind it (so that no blue appears). However, it renders at half size.

  const size = useSharedValue({ width: 0, height: 0 });
  const width = useDerivedValue(() => size.value.width);
  const height = useDerivedValue(() => size.value.height);

  return (
    <View style={{transform: [{ scale: 0.5, backgroundColor: 'blue' }]}}>
      <Canvas style={{ flex: 1 }} onSize={size}>
        <Rect
          x={0}
          y={0}
          width={width}
          height={height}
          color="red"
          style="fill"
        ></Rect>
      </Canvas>
    </View>  
  );

Snack, Code Example, Screenshot, or Link to Repository

The example above can be checked out from https://github.com/donaldharvey/react-native-skia-canvas-size-regression.

Behaviour in 2.6.2:

Image

Behaviour in 2.2.12:

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions