Skip to content

Commit ac67307

Browse files
authored
Merge pull request matplotlib#30560 from lucasgruwez/bugfix/consistent-rubber-bands
Consistent zoom boxes
2 parents 28780cb + e4ef103 commit ac67307

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Consistent zoom boxes
2+
---------------------
3+
4+
Zooming now has a consistent dashed box style across all backends.

lib/matplotlib/backends/_backend_tk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ def draw_rubberband(self, event, x0, y0, x1, y1):
769769
y1 = height - y1
770770
self.canvas._rubberband_rect_black = (
771771
self.canvas._tkcanvas.create_rectangle(
772-
x0, y0, x1, y1))
772+
x0, y0, x1, y1, outline='black'))
773773
self.canvas._rubberband_rect_white = (
774774
self.canvas._tkcanvas.create_rectangle(
775775
x0, y0, x1, y1, outline='white', dash=(3, 3)))

lib/matplotlib/backends/web_backend/js/mpl.js

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,6 @@ mpl.figure.prototype._init_canvas = function () {
325325
canvas_div.appendChild(rubberband_canvas);
326326

327327
this.rubberband_context = rubberband_canvas.getContext('2d');
328-
this.rubberband_context.strokeStyle = '#000000';
329328

330329
this._resize_canvas = function (width, height, forward) {
331330
if (forward) {
@@ -469,19 +468,38 @@ mpl.figure.prototype.handle_rubberband = function (fig, msg) {
469468
y0 = Math.floor(y0) + 0.5;
470469
x1 = Math.floor(x1) + 0.5;
471470
y1 = Math.floor(y1) + 0.5;
472-
var min_x = Math.min(x0, x1);
473-
var min_y = Math.min(y0, y1);
474-
var width = Math.abs(x1 - x0);
475-
var height = Math.abs(y1 - y0);
476471

477-
fig.rubberband_context.clearRect(
472+
var ctx = fig.rubberband_context;
473+
ctx.clearRect(
478474
0,
479475
0,
480476
fig.canvas.width / fig.ratio,
481477
fig.canvas.height / fig.ratio
482478
);
483479

484-
fig.rubberband_context.strokeRect(min_x, min_y, width, height);
480+
var drawRubberband = function () {
481+
// Draw the lines from x0, y0 towards x1, y1 so that the
482+
// dashes don't "jump" when moving the zoom box.
483+
ctx.beginPath();
484+
ctx.moveTo(x0, y0);
485+
ctx.lineTo(x0, y1);
486+
ctx.moveTo(x0, y0);
487+
ctx.lineTo(x1, y0);
488+
ctx.moveTo(x0, y1);
489+
ctx.lineTo(x1, y1);
490+
ctx.moveTo(x1, y0);
491+
ctx.lineTo(x1, y1);
492+
ctx.stroke();
493+
};
494+
495+
fig.rubberband_context.lineWidth = 1;
496+
fig.rubberband_context.setLineDash([3]);
497+
fig.rubberband_context.lineDashOffset = 0;
498+
fig.rubberband_context.strokeStyle = '#000000';
499+
drawRubberband();
500+
fig.rubberband_context.strokeStyle = '#ffffff';
501+
fig.rubberband_context.lineDashOffset = 3;
502+
drawRubberband();
485503
};
486504

487505
mpl.figure.prototype.handle_figure_label = function (fig, msg) {

0 commit comments

Comments
 (0)