Skip to content

Canvas[width][height] props -- aka "manual size" #3552

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

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

abernier
Copy link
Member

@abernier abernier commented Jul 3, 2025

2 new width/height props:

<Canvas width={800} height={600}>

=> to produce a <canvas width="800" height="600" /> manually sized, independently from useMeasure

NB: if no width or height prop => useMeasure as fallback (backward compatible)

https://discord.com/channels/740090768164651008/1390380565425033216

Copy link

codesandbox-ci bot commented Jul 3, 2025

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit e64ba69:

Sandbox Source
example Configuration

@abernier abernier marked this pull request as draft July 3, 2025 17:37
@abernier abernier self-assigned this Jul 3, 2025
@abernier abernier changed the title Canvas[size] prop -- aka "manual size" Canvas[width][height] props -- aka "manual size" Jul 3, 2025
@abernier abernier force-pushed the canvas-manual-size branch from 66da4cb to 1922504 Compare July 3, 2025 17:45
* Initial plan

* Add comprehensive tests for Canvas width/height props

Co-authored-by: abernier <[email protected]>

---------

Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: abernier <[email protected]>
@abernier abernier marked this pull request as ready for review July 3, 2025 21:41
@abernier abernier marked this pull request as draft July 3, 2025 23:14
@abernier abernier force-pushed the canvas-manual-size branch 7 times, most recently from 4fddb54 to 50b19cd Compare July 3, 2025 23:59
@abernier abernier force-pushed the canvas-manual-size branch from 50b19cd to e64ba69 Compare July 4, 2025 05:27
@krispya
Copy link
Member

krispya commented Jul 10, 2025

I am interested in this change which would help align Canvas props to the element canvas, but I think the rules should work like this:

  • Adding an explicit width prop makes the canvas width controlled and the height calculated based on the aspect ratio.
  • Adding an explicit height prop makes the canvas width controlled and the width calculated based on the aspect ratio.
  • If a prop is controlled and a user tries to set it with setSize it will be ignored and they will be warned during dev time.
  • If no width or height prop is added to the canvas then default behavior is used where the size is based on the container's CSS size.

To make this work with setSize, I propose we add an auto option, like CSS, to make the behavior explicit. Under the hood, these are the values that the width and height props are setting.

// width and height are explicit and will not resize with the container
setSize(100, 100)
// Both are auto and resize with the container
setSize('auto', 'auto')
// width is explicit by height is auto based on the aspect ratio
setSize(100, 'auto')

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.

3 participants