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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
/*global Slider: false, rAF: false, quadLimit: false */
// -------------------------- demo -------------------------- //
var canvas, ctx;
var canvasW, canvasH;
var slider;
var cells = [];
var leftBound, rightBound;
var cellWidths = [ 0.2, 0.2, 0.4, 0.2, 0.3 ];
document.addEventListener( 'DOMContentLoaded', init, false );
function init() {
canvas = document.querySelector('canvas');
ctx = canvas.getContext('2d');
// set size
canvasW = canvas.width = window.innerWidth - 20;
canvasH = canvas.height = 300;
// create cells
var cellX = 0;
var gutter = 40;
for ( var i=0, len = cellWidths.length; i < len; i++ ) {
var cellWidth = cellWidths[i];
var cell = {
width: canvasW * cellWidth,
height: canvasH - 40,
x: cellX,
index: i
};
cellX += cell.width + gutter;
cells.push( cell );
}
leftBound = 100;
rightBound = canvasW - 100;
canvas.addEventListener( 'mousedown', onMousedown, false );
slider = new Slider( leftBound, 0 );
slider.width = cellX - gutter;
animate();
}
// -------------------------- animate -------------------------- //
function animate() {
if ( !isDragging ) {
applyLeftBoundForce();
applyRightBoundForce();
}
slider.update();
render();
rAF( animate );
}
function applyLeftBoundForce() {
var sliderX = slider.x;
var distance = leftBound - sliderX;
var force = distance * 0.05;
force = Math.min( force, 0 );
// prevent slider from bounds too far back
// don't apply force if slider is returning from outside bounds
if ( force && slider.velocity < 0 ) {
// resting position
var restPosition = slider.getRestingPosition();
if ( restPosition.x < leftBound ) {
force = ( leftBound - slider.x ) / restPosition.frictionSum - slider.velocity;
}
}
slider.applyForce( force );
}
function applyRightBoundForce() {
var sliderX = slider.x + slider.width;
var distance = rightBound - sliderX;
var force = distance * 0.05;
force = Math.max( force, 0 );
// prevent slider from bounds too far back
// don't apply force if slider is returning from outside bounds
if ( force && slider.velocity > 0 ) {
// resting position
var restPosition = slider.getRestingPosition();
if ( restPosition.x + slider.width > rightBound ) {
force = ( rightBound - sliderX ) / restPosition.frictionSum - slider.velocity;
}
}
slider.applyForce( force );
}
// -------------------------- render -------------------------- //
function render() {
ctx.clearRect( 0, 0, canvasW, canvasH );
// bounds
ctx.lineWidth = 1;
ctx.strokeStyle = 'hsla(300, 100%, 40%, 1)';
line( leftBound, 0, leftBound, canvasH );
line( rightBound, 0, rightBound, canvasH );
// cells
ctx.save();
ctx.translate( slider.x, 0 );
for ( var i=0, len = cells.length; i < len; i++ ) {
var cell = cells[i];
var hue = i * 70;
ctx.fillStyle = 'hsla(' + hue + ', 100%, 40%, 0.5)';
ctx.fillRect( cell.x, 20, cell.width, cell.height );
// target line
ctx.strokeStyle = 'yellow';
line( cell.target, 40, cell.target, cell.height );
}
ctx.restore();
}
function line( x1, y1, x2, y2 ) {
ctx.beginPath();
ctx.moveTo( x1, y1 );
ctx.lineTo( x2, y2 );
ctx.stroke();
ctx.closePath();
}
function circle( x, y, radius ) {
ctx.beginPath();
ctx.arc( x, y, radius, 0, Math.PI * 2 );
ctx.fill();
ctx.closePath();
}
// -------------------------- mouse -------------------------- //
var isDragging = false;
var dragStartX;
function onMousedown( event ) {
event.preventDefault();
isDragging = true;
window.addEventListener( 'mousemove', onMousemove, false );
window.addEventListener( 'mouseup', onMouseup, false );
dragStartX = event.pageX;
slider.dragStartX = slider.x;
slider.velocity = 0;
}
var previousX;
var previousTime;
var currentTime;
function onMousemove( event ) {
// previous
previousX = slider.x;
previousTime = currentTime;
// current
var moveX = event.pageX - dragStartX;
var dragX = slider.dragStartX + moveX;
// limit dragging beyond left bound
dragX = quadLimit( dragX, leftBound, canvasW / 2, canvasW );
// limit dragging beyond right bound
var rightDragBound = rightBound - slider.width;
dragX = quadLimit( dragX, rightDragBound, rightDragBound - canvasW / 2, rightDragBound - canvasW );
slider.x = dragX;
currentTime = new Date();
}
function onMouseup( event ) {
dragEnd();
window.removeEventListener( 'mousemove', onMousemove, false );
window.removeEventListener( 'mousemove', onMouseup, false );
}
function dragEnd() {
if ( previousX ) {
// set slider velocity
slider.velocity = ( slider.x - previousX ) / ( currentTime - previousTime );
slider.velocity *= 1000 / 60;
// reset previousX
previousX = null;
}
isDragging = false;
}
|