Fix DPI increasing in size on each reload

This commit is contained in:
Michael Droettboom 2018-06-04 15:34:07 -04:00
parent 65628719c8
commit d7ea2c9608
1 changed files with 28 additions and 17 deletions

View File

@ -93,10 +93,10 @@ class FigureCanvasWasm(backend_agg.FigureCanvasAgg):
canvas = document.createElement('canvas') canvas = document.createElement('canvas')
context = canvas.getContext('2d') context = canvas.getContext('2d')
self._ratio = self.get_dpi_ratio(context) self._ratio = self.get_dpi_ratio(context)
if self._ratio != 1:
self.figure.dpi *= self._ratio
width, height = self.get_width_height() width, height = self.get_width_height()
width *= self._ratio
height *= self._ratio
div = self.create_root_element() div = self.create_root_element()
div.setAttribute('style', 'width: {}px'.format(width / self._ratio)) div.setAttribute('style', 'width: {}px'.format(width / self._ratio))
div.id = self._id div.id = self._id
@ -166,16 +166,22 @@ class FigureCanvasWasm(backend_agg.FigureCanvasAgg):
def draw(self): def draw(self):
# Render the figure using Agg # Render the figure using Agg
super().draw() orig_dpi = self.figure.dpi
# Copy the image buffer to the canvas if self._ratio != 1:
width, height = self.get_width_height() self.figure.dpi *= self._ratio
canvas = self.get_element('canvas') try:
image_data = ImageData.new( super().draw()
self.buffer_rgba(), # Copy the image buffer to the canvas
width, height) width, height = self.get_width_height()
ctx = canvas.getContext("2d") canvas = self.get_element('canvas')
ctx.putImageData(image_data, 0, 0) image_data = ImageData.new(
self._idle_scheduled = False self.buffer_rgba(),
width, height);
ctx = canvas.getContext("2d");
ctx.putImageData(image_data, 0, 0);
finally:
self.figure.dpi = orig_dpi
self._idle_scheduled = False
def draw_idle(self): def draw_idle(self):
if not self._idle_scheduled: if not self._idle_scheduled:
@ -189,8 +195,8 @@ class FigureCanvasWasm(backend_agg.FigureCanvasAgg):
def _convert_mouse_event(self, event): def _convert_mouse_event(self, event):
width, height = self.get_width_height() width, height = self.get_width_height()
x = (event.offsetX * self._ratio) x = event.offsetX
y = ((height / self._ratio) - event.offsetY) * self._ratio y = height - event.offsetY
button = event.button + 1 button = event.button + 1
# Disable the right-click context menu in some browsers # Disable the right-click context menu in some browsers
if button == 3: if button == 3:
@ -380,14 +386,19 @@ class FigureCanvasWasm(backend_agg.FigureCanvasAgg):
if y1 < y0: if y1 < y0:
y0, y1 = y1, y0 y0, y1 = y1, y0
context = rubberband.getContext('2d') context = rubberband.getContext('2d')
context.clearRect(0, 0, width, height) context.clearRect(
context.strokeRect(x0, y0, x1 - x0, y1 - y0) 0, 0, width * self._ratio, height * self._ratio)
context.strokeRect(
x0 * self._ratio,
y0 * self._ratio,
(x1 - x0) * self._ratio,
(y1 - y0) * self._ratio)
def remove_rubberband(self): def remove_rubberband(self):
rubberband = self.get_element('rubberband') rubberband = self.get_element('rubberband')
width, height = self.get_width_height() width, height = self.get_width_height()
context = rubberband.getContext('2d') context = rubberband.getContext('2d')
context.clearRect(0, 0, width, height) context.clearRect(0, 0, width * self._ratio, height * self._ratio)
def new_timer(self, *args, **kwargs): def new_timer(self, *args, **kwargs):
return TimerWasm(*args, **kwargs) return TimerWasm(*args, **kwargs)