dwm icon indicating copy to clipboard operation
dwm copied to clipboard

Scale canvas to window size example

Open jakeklassen opened this issue 1 year ago • 3 comments

I'm not sure this is the proper place to ask, but does anyone have an example of how to best scale a Skia Canvas to match the window size?

For example, say I am making a game with a canvas size 128x128 and a window sized at 512x512 (so 4x), is there an easy way to auto scale (stretch) the canvas to the window size? I don't want to actually create a 512x512 canvas and the draw all the native 128x128 sprite at a larger resolution.

I'm basically after something like the transform: scale(4) you'd get with CSS.

jakeklassen avatar May 25 '23 16:05 jakeklassen

Skia-canvas has the same API surface as html-canvas. https://www.w3schools.com/tags/canvas_scale.asp

nhrones avatar Jul 10 '23 01:07 nhrones

so I guess we might need dwm version of window.devicePixelRatio to achieve that?

hashrock avatar Nov 18 '23 07:11 hashrock

@nhrones yeah that makes sense but I'm thinking more along the lines of:

canvas.style.transform = `scale(4)`;

Using the context API to scale a 128x128 canvas won't make the canvas fill the 512x512 window. Unless I am misunderstanding something. I'm thinking something along the lines of a window stretch mode that could be fill, etc. Not sure how to achieve that with this lib.

Hopefully that helps clear up my original question.

jakeklassen avatar Jan 25 '24 14:01 jakeklassen