Skip to content

Conversation

gabenovig
Copy link

@gabenovig gabenovig commented Sep 4, 2025

Description

Add webglContextAttributes prop for Canvas (web platform)

This PR adds support for configuring WebGL context attributes on the web platform, enabling features like preserveDrawingBuffer which is essential for canvas capture/screenshot functionality.

Motivation

Currently, it's not possible to capture/screenshot canvas content on web because the WebGL context is created without preserveDrawingBuffer: true. This PR allows developers to configure WebGL context attributes to enable this and other WebGL-specific features.

Changes

  • Added webglContextAttributes prop to CanvasProps and SkiaBaseViewProps
  • Use WebGLOptions directly for typing
  • Added prop to web-specific native component spec
  • Follows existing platform-specific patterns (.web.ts files)

Implementation Details

The implementation converts boolean values to numbers as required by CanvasKit's WebGLOptions interface:

  • all attributes use original typing from WebGLOptions

Usage

<Canvas 
  style={{ width: 256, height: 256 }}
  webglContextAttributes={{ 
    preserveDrawingBuffer: 1,
    antialias: 1,
    alpha: 1
  }}
>
  <Fill color="blue" />
  <Circle cx={128} cy={128} r={50} color="red" />
</Canvas>

Testing

Visual Example

You can view a quick demo of this feature (not included in the PR) at this link

Platform Impact

  • Web: ✅ Fully supported - enables WebGL context configuration
  • iOS/Android: No impact - prop is ignored on native platforms
  • Breaking changes: None - new optional prop with no effect on existing code

Checklist

  • TypeScript compilation passes
  • Linting passes
  • Follows existing codebase patterns
  • Web-only implementation (doesn't affect native)
  • Tested locally with canvas capture functionality

Other notes

If you'd like some extra context on why I ran into this problem and what I am trying to accomplish you can contact me gabe@novig.co, first time contributing to open source so please let me know if I am missing anything important in the contribution guidelines here. Great package that has made our work life and native apps render graphs fast and awesome! While porting our existing application to web we encountered this webGL edge case (react native for web)

Adds support for configuring WebGL context attributes on web platform,
particularly useful for enabling preserveDrawingBuffer for canvas capture
scenarios like screenshots or image exports.

- Add webglContextAttributes prop to CanvasProps and SkiaBaseViewProps
- Implement context attribute conversion (boolean to 0/1) in SkiaBaseWebView
- Pass attributes to CanvasKit.MakeWebGLCanvasSurface
- Web-only feature, ignored on native platforms

Example usage:
<Canvas
  webglContextAttributes={{
    preserveDrawingBuffer: true,
    alpha: true,
    antialias: true
  }}
>
  {/* Canvas content */}
</Canvas>
-update our context attribute types to be specific
-respect linting, no nested ternaries
@wcandillon wcandillon self-requested a review September 5, 2025 08:34
Copy link
Contributor

@wcandillon wcandillon left a comment

Choose a reason for hiding this comment

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

I like it a lot.
Let's use WebGLOptions type directly from canvaskit (and not even do the boolean conversion).

@wcandillon
Copy link
Contributor

and there is the CLA to be signed of course :)

-don't cast to bool
@gabenovig
Copy link
Author

@wcandillon adjusted to use WebGLOptions directly with no casting

-- going to check in with our legal team to see if they will require me to sign corporate CLA or if I can go individual

@wcandillon
Copy link
Contributor

@gabenovig I have an exciting refactoring at #3349 if you want I can add the change there myself (that would take care of the CLA issue). The downside is that you wouldn't get commit authorship on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants