-
Notifications
You must be signed in to change notification settings - Fork 0
/
panning_demo.m
59 lines (51 loc) · 2.02 KB
/
panning_demo.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
function panning_demo
% Load image:
demoImage = imread('peppers.png');
[nRows, nColumns, ~] = size(demoImage);
xLimits = [0.5 nColumns+0.5];
yLimits = [0.5 nRows+0.5];
% Create figure and graphics objects:
hFigure = figure('Name', 'Panning Demo', 'NumberTitle', 'off');
hAxes = axes(hFigure, 'Color', 'k', ...
'DataAspectRatio', [1 1 1], ...
'NextPlot', 'add', ...
'Tag', 'AXES_1', ...
'XColor', 'none', ...
'XLim', xLimits, ...
'YColor', 'none', ...
'YDir', 'reverse', ...
'YLim', yLimits);
image(hAxes, demoImage);
% Create MouseManager and intialize:
mmObject = MouseManager(hFigure);
mmObject.add_item(hAxes, 'normal', @pan_image, ...
'scroll', @zoom_image, ...
'click', 'open', @reset_image);
mmObject.enable(true);
display(mmObject);
% Nested functions:
function pan_image(hObject, eventData)
persistent panOrigin panLimits panScale
switch eventData.operation
case 'click'
panOrigin = eventData.figurePoint;
panLimits = [hObject.XLim hObject.YLim];
axesPosition = eventData.figureRegion;
panScale = max([diff(panLimits(1:2))/axesPosition(3) ...
diff(panLimits(3:4))/axesPosition(4)]);
case 'drag'
offset = panScale.*(eventData.figurePoint-panOrigin);
hObject.XLim = panLimits(1:2) - offset(1);
hObject.YLim = panLimits(3:4) + offset(2);
end
end
function zoom_image(hObject, eventData)
fraction = (1-1.25^eventData.scrollEventData.VerticalScrollCount)/2;
hObject.XLim = hObject.XLim + [1 -1].*fraction.*diff(hObject.XLim);
hObject.YLim = hObject.YLim + [1 -1].*fraction.*diff(hObject.YLim);
end
function reset_image(hObject, ~)
hObject.XLim = xLimits;
hObject.YLim = yLimits;
end
end