draggableRectangle

版本 1.0.4 (3.9 KB) 作者: Hyeokjin Jho
Draws rectangle which can be dragged or resized by mouse pointer
46.0 次下载
更新时间 2020/12/16

查看许可证

Draws rectangle which can be dragged or resized by mouse pointer

D = draggableRectangle (parent_axes,xrange,yrange,...);
input
parent_axes : axes object : axes for drawing rectangle
xrange : 1 x 2 numeric : [xmin xmax]
yrange : 1 x 2 numeric : [ymin ymax]

name-value pairs
xdirlock : logical scalar : lock x direction drag
ydirlock : logical scalar : lock y direction drag
buttonDownFcn : function handle : callback executed upon mouse click
buttonDragFcn : function handle : callback executed upon mouse drag movement
buttonUpFcn : function handle : callback executed upon mouse release

additional name-value pair argument for 'patch' can be followed after these arguments (see:https://www.mathworks.com/help/matlab/ref/matlab.graphics.primitive.patch-properties.html)

properties
xrange : 1 x 2 numeric : current x range of rectangle
yrange : 1 x 2 numeric : current y range of rectangle
xdirlock : logical scalar : if true, locks x dir dragging
ydirlock : logical scalar : if true, locks y dir dragging
bezel : numeric positive scalar : determines width of edge dragging region
isdragging : logical scalar (read only) : true if object under dragging
buttonDownFcn(hObject,eventdata) : function handle : function executed when mouse is clicked on this object
buttonDragFcn(hObject,eventdata) : function handle : function executed when mouse is moved during drag
buttonUpFcn(hObject,eventdata) : function handle : function executed when drag is finished

Additional properties for 'patch' are available.
Standard get & set functions are available.

methods
D.drag_start([movingx,movingy]);

example
% draw axis
axis([0 40 0 60]);
% draw rectangle
D = draggableRectangle([10 30],[20 40],'facecolor','r');
% force start dragging
D.drag_start([-1,0]) % move left edge

% wait until dragging is done
while D.isdragging
pause(0.05)
end

% Here, we change rectangle's properties (same properties as 'patch')
D.FaceColor = 'y';
set(D,'EdgeColor','none');

% force start dragging
D.drag_start([1,1]) % move top right vertex

% wait until dragging is done
while D.isdragging
pause(0.05)
end

% Here, we define user callback
% force start dragging
D.drag_start([0,0]) % move position
% set mouse release callback
D.buttonUpFcn = @(h,~) fprintf('moved position : [%.2f %.2f %.2f %.2f]\n',h.xrange,h.yrange);

引用格式

Hyeokjin Jho (2024). draggableRectangle (https://www.mathworks.com/matlabcentral/fileexchange/84370-draggablerectangle), MATLAB Central File Exchange. 检索时间: .

MATLAB 版本兼容性
创建方式 R2020b
与 R2019b 及更高版本兼容
平台兼容性
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
版本 已发布 发行说明
1.0.4

Added properties to control patch appearance

1.0.3

changed wrong description

1.0.2

bugfix : suppress error on edge clicking

1.0.1

Changed patch to one piece
set some methods to private

1.0.0