Skip to content

Commit 8868e1e

Browse files
authored
Added support for set_pixel_ratio to offscreen canvas (#122)
1 parent 737d9fe commit 8868e1e

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

rendercanvas/base.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,13 @@ def is_closed(self):
566566
# implement the methods they can, but these features are likely not critical.
567567

568568
def set_logical_size(self, width: float, height: float) -> None:
569-
"""Set the window size (in logical pixels)."""
569+
"""Set the window size (in logical pixels).
570+
571+
This changes the physical size of the canvas, such that the new logical
572+
size matches the given width and height. Since the physical size is
573+
integer (i.e. rounded), the re-calculated logical size may differ slightly
574+
from the given width and height (depending on the pixel ratio).
575+
"""
570576
width, height = float(width), float(height)
571577
if width < 0 or height < 0:
572578
raise ValueError("Canvas width and height must not be negative")

rendercanvas/offscreen.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def _rc_present_bitmap(self, *, data, format, **kwargs):
7272

7373
def _rc_set_logical_size(self, width, height):
7474
logical_size = float(width), float(height)
75+
7576
physical_size = (
7677
int(logical_size[0] * self._pixel_ratio),
7778
int(logical_size[1] * self._pixel_ratio),
@@ -94,6 +95,24 @@ def _rc_set_cursor(self, cursor):
9495

9596
# %% Extra API
9697

98+
def set_physical_size(self, width: int, height: int):
99+
"""Set the size of the backbuffer (in physical pixels).
100+
101+
The logical size is re-calculated using the current pixel ratio.
102+
"""
103+
physical_size = int(width), int(height)
104+
self._set_size_info(physical_size, self._pixel_ratio)
105+
106+
def set_pixel_ratio(self, pixel_ratio: float):
107+
"""Set the pixel ratio, changing the logical size of the canvas.
108+
109+
The physical size remains the same. If you want to retain a certain
110+
logical size, first set the pixel ratio and then the logical size.
111+
"""
112+
self._pixel_ratio = float(pixel_ratio)
113+
physical_size = self.get_physical_size()
114+
self._set_size_info(physical_size, self._pixel_ratio)
115+
97116
def draw(self):
98117
"""Perform a draw and get the resulting image.
99118

0 commit comments

Comments
 (0)