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

Unexpected Canvas Resize Behavior #41

Closed
jackrugile opened this issue Jun 3, 2019 · 5 comments
Closed

Unexpected Canvas Resize Behavior #41

jackrugile opened this issue Jun 3, 2019 · 5 comments
Labels
bug Something isn't working

Comments

@jackrugile
Copy link

jackrugile commented Jun 3, 2019

When setting resize: true on a canvas based illustration, it doesn't behave as I'd expect. For demos 1 and 2 below, I'd expect them to both have the same behavior. The SVG demo resizes proportionally, but the canvas demo stays locked at 480px×240px.

Test Cases

1. Canvas Resize: https://codepen.io/desandro/pen/pmdqZv
2. SVG Resize: https://codepen.io/desandro/pen/wbPREZ
3. Fullscreen Canvas Resize: https://codepen.io/desandro/pen/dEJxaV

I realize that SVGs behave differently than canvases, since they retain their intrinsic proportions through their viewbox if explicit width/height is not defined, but that is not the case for canvas.

I'm not sure what the solution is here. I went down a few roads, but then realized that there might be a few different behaviors that people are trying to achieve. Perhaps it's one, or a combination of the following (don't mind the horrible names):

resize: false

Zdog does not attempt to resize the illustration. Currently Working

resize: 'fullscreen'

Zdog will resize the illustration to match the viewport width and height. Currently Working, demo 3 above

resize: 'fill-parent'

Zdog will resize the illustration to match the width and height of the element's parent.

resize: 'fill-parent-proportionally'

Zdog will resize the illustration to match the smallest dimension of the element's parent, while proportionally scaling the opposite axis. This means the initial aspect ratio will be retained, but will try to fill its container. Think background-size: contain; from CSS.

resize: 'respect-natural-dom-size-and-css'

Zdog will resize the illustration based on the element's natural manifestation in the DOM, including any CSS applied. This would require Zdog to strip any explicit widths and heights from the element, take a natural measurement of the element in the DOM, then reapply those settings explicitly after the fact the keep the rendering working properly.


There may be more options, and maybe I'm looking into this a little too closely, but I think resize solutions like this would cover most use cases. At a minimum, I'd expect resize: true to have some effect on the canvas, but that's not currently the case.

Any suggestions or guidance here?

P.S. - I'm loving Zdog, Dave, well done!

@desandro
Copy link
Member

desandro commented Jun 3, 2019

If you'd like to see more resize options, please add a 👍 and respond with which behavior from above you're looking for.


Hi Jack. Thanks for digging into this and for your careful consideration of the issue.

Currently resize: true works as resize: 'respect-natural-dom-size-and-css' (as you put it). Zdog takes the original width and height measurements, and then updates the rendering element & internal geometries to match that display ratio. If you inspect the canvas demo, you'll see the actual width and height attributes change. In the SVG demo, the viewBox attribute changess. With resize: true canvas and SVG element both with scale proportionally, but the outer CSS may differ between the two elements.

As for the other use cases, yeah, I can see people needing these. But I'd like to hear what they're trying to achieve before I start more features. Dealing with resizing was tricky.

@jackrugile
Copy link
Author

Thanks for your reply. Yeah, resizing is definitely a tricky thing. Expectations from developers could vary widely.

I think I may not be seeing the same thing as you described. In test case 1, the canvas does not change size for me when resizing my browser. It stays at 480px × 280px. I'm attaching a GIF of the behavior I see here just to clarify. Do you get the same behavior?

resize

With your description, I would expect to see the canvas element expand to the full width of the browser while retaining a 2:1 aspect ratio since it has the following CSS:

.zdog-canvas {
  display: block;
  width: 100%;
}

I wonder if this might be an issue where the browser needs an extra frame to recalculate the DOM's representation? Kind of like how if you remove a CSS class and then add the CSS class back to the same element within the same frame of execution, you wouldn't see any change.

@desandro desandro added the bug Something isn't working label Jun 3, 2019
@desandro
Copy link
Member

desandro commented Jun 3, 2019

Aaaaaah. There's a bug with high pixelRatio screens. Dang. Okay, I'll look into this. Thanks for reporting that.

@jackrugile
Copy link
Author

Oh ok, great to hear! I think fixing that should solve everything that was going through my head. There's no need to add extra complications since resize: true as you described is super flexible. All of the above scenarios I outlined could be covered with that.

Feel free to close this once fixed, thanks!

@desandro
Copy link
Member

desandro commented Jun 4, 2019

v1.0.2 has been released with fix for this bug Please update! Thanks again for reporting this issue. Closing as fixed.

@desandro desandro closed this as completed Jun 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Development

No branches or pull requests

2 participants