This repository has been archived by the owner on Feb 13, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
splitpic.js
167 lines (159 loc) · 5.13 KB
/
splitpic.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
(function (window) {
function SplitPic(element) {
var el = $(element);
var leftPane = $('.splitpic-left-image', el);
var rightPane = $('.splitpic-right-image', el);
var bar = $('.splitpic-bar', el);
var infoHidden = false;
// split the image at the cursor
function updateSplit(x, isRelative) {
var relativeX;
if (!isRelative) {
var elOffset = el.offset();
relativeX = x - elOffset.left;
} else {
relativeX = x;
}
leftPane.css(
'clip', 'rect(0px, ' + relativeX + 'px, auto, 0px)'
);
bar.css('left', relativeX - bar.width() / 2 + 'px');
};
// how much of the left image should we show at start? 50% if not specified
var startPercentage = parseInt(el.attr('data-start-percent'));
if (isNaN(startPercentage)) {
startPercentage = 50;
}
startPercentage /= 100;
updateSplit(el.width() * startPercentage, true);
var isMoving = false;
var lastX = 0,
lastY = 0;
el.on('touchmove touchstart', function (event) {
if (!infoHidden) {
$('.splitpic-info', el).fadeOut(200);
infoHidden = true;
}
var touches;
if (event.touches) {
touches = event.touches;
} else if (event.originalEvent && event.originalEvent.touches) {
touches = event.originalEvent.touches;
}
if (touches) {
var touch = touches[0];
var dx = 0,
dy = 0;
if (isMoving) {
dx = touch.pageX - lastX;
dy = touch.pageY - lastY;
} else {
isMoving = true;
}
// if we move a bit and it's laterally
if (Math.abs(dx) > Math.abs(dy)) {
event.preventDefault();
updateSplit(touches[0].pageX);
}
lastX = touch.pageX;
lastY = touch.pageY;
}
});
el.on('touchend', function (event) {
isMoving = false;
});
el.on('mouseenter mousemove mouseleave', function (event) {
if (!infoHidden) {
$('.splitpic-info', el).fadeOut(200);
infoHidden = true;
}
updateSplit(event.pageX);
});
};
window.SplitPic = SplitPic;
function SplitPicVertical(element) {
var el = $(element);
var overPane = $('.splitpic-left-image', el);
var underPane = $('.splitpic-right-image', el);
var bar = $('.splitpic-bar', el);
var infoHidden = false;
// split the image at the cursor
function updateSplit(y, isRelative) {
var relativeY;
if (!isRelative) {
var elOffset = el.offset();
relativeY = y - elOffset.top;
} else {
relativeY = y;
}
overPane.css(
'clip', 'rect(' + relativeY + 'px, auto, auto, 0px)'
);
bar.css('top', relativeY - bar.height() / 2 + 'px');
};
// this is necessary since we're using original crops and therefore the
// image height won't be known until it's loaded.
function setInitialPosition() {
// how much of the left image should we show at start? 50% if not specified
var startPercentage = parseInt(el.attr('data-start-percent'));
if (isNaN(startPercentage)) {
startPercentage = 50;
}
startPercentage /= 100;
updateSplit(el.height() * startPercentage, true);
}
var img = $('.splitpic-left-image img', element);
if (img[0].complete) {
setInitialPosition();
} else {
img.load(setInitialPosition.bind(this));
}
var isMoving = false;
var lastX = 0,
lastY = 0;
el.on('touchmove touchstart', function (event) {
if (!infoHidden) {
$('.splitpic-info', el).fadeOut(200);
infoHidden = true;
}
var touches;
if (event.touches) {
touches = event.touches;
} else if (event.originalEvent && event.originalEvent.touches) {
touches = event.originalEvent.touches;
}
if (touches) {
var touch = touches[0];
var dx = 0,
dy = 0;
if (isMoving) {
dx = touch.pageX - lastX;
dy = touch.pageY - lastY;
} else {
dy = 0.001; // HACK: so it's not equal to dx and immediately updates
isMoving = true;
}
// if we move a bit and it's vertically and not too fast
// TODO: something better
if (Math.abs(dy) > Math.abs(dx) && Math.abs(dy) < 10) {
event.preventDefault();
updateSplit(touch.pageY);
}
lastX = touch.pageX;
lastY = touch.pageY;
}
});
el.on('touchend', function (event) {
isMoving = false;
});
el.on('mouseenter mousemove mouseleave', function (event) {
if (!infoHidden) {
$('.splitpic-info', el).fadeOut(200);
infoHidden = true;
}
updateSplit(event.pageY);
});
};
window.SplitPic = SplitPic;
window.SplitPicVertical = SplitPicVertical;
}(window));