on 4cd62a3 help sign in
unity8/Components/carousel.js
85 lines 3.2 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
17.pragma library
19// get the element which is selected accordingly to x, the hero of the view
20function getContinuousIndex(x, tileWidth, gapToMiddlePhase, gapToEndPhase, kGapEnd, kMiddleIndex, kXBeginningEnd) {
21 if (x < gapToMiddlePhase) {
22 // beginning
23 return x * kXBeginningEnd
24 } else if (x > gapToEndPhase) {
25 // end
26 return x * kXBeginningEnd + kGapEnd
27 }
29 // middle
30 return x / tileWidth + kMiddleIndex
33// obtain x position relative to an index, essentially an inverse of getContinuousIndex()
34function getXFromContinuousIndex(index, viewWidth, contentWidth, tileWidth, gapToMiddlePhase, gapToEndPhase) {
35 var middleX = (index + 0.5) * tileWidth - viewWidth / 2
37 if (middleX < gapToMiddlePhase) {
38 // inverse of 'middleIndex - kGap' of getContinuousIndex()
39 return index /
40 ((1 / tileWidth) +
41 (viewWidth / (2 * tileWidth * gapToMiddlePhase)) -
42 1 / (2 * gapToMiddlePhase))
43 } else if (middleX > gapToEndPhase) {
44 // inverse of 'middleIndex + kGap' of getContinuousIndex()
45 return (index +
46 1 -
47 viewWidth / tileWidth +
48 (contentWidth * viewWidth - viewWidth * viewWidth) / (2 * tileWidth * gapToMiddlePhase) +
49 (viewWidth - contentWidth) / (2 * gapToMiddlePhase)) /
50 (1 / tileWidth +
51 viewWidth / (2 * tileWidth * gapToMiddlePhase) -
52 1 / (2 * gapToMiddlePhase))
53 }
55 // inverse of 'middleIndex' of getContinuousIndex()
56 return middleX
59// get translation of the whole view, adds gaps on sides
60function getViewTranslation(x, tileWidth, gapToMiddlePhase, gapToEndPhase, translationXViewFactor) {
61 if (x < gapToMiddlePhase) {
62 // beginning
63 return (gapToMiddlePhase - x) * translationXViewFactor
64 } else if (x > gapToEndPhase) {
65 // end
66 return (gapToEndPhase - x) * translationXViewFactor
67 }
69 // middle
70 return 0
73// item scale
74function getItemScale(distance, continuousIndex, end, scaleFactor) {
75 var distanceAbs = Math.abs(distance)
76 var distanceToBounds = Math.min(continuousIndex, end - continuousIndex)
77 var k = Math.max(200 + 100 * (-distanceToBounds / (3 * scaleFactor)), 50)
78 return Math.max(0.01, 1 - Math.pow(distanceAbs, 2.5) / (k * scaleFactor))
81// item translation
82function getItemTranslation(distance, scale, maxScale, translationFactor) {
83 var sign = distance > 0 ? 1 : -1
84 return sign * (maxScale - scale) * translationFactor