forked from ampproject/amphtml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimation-presets-utils.js
More file actions
144 lines (135 loc) · 4.83 KB
/
animation-presets-utils.js
File metadata and controls
144 lines (135 loc) · 4.83 KB
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
/**
* Copyright 2018 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Common animations utility functions used in the animation
* presets.
*/
import {StoryAnimationDimsDef, WebKeyframesDef} from './animation-types';
import {rotate, scale, translate} from '../../../src/style';
/**
* Translates the element on the 2d plane according to the given points.
* @param {number} startX Starting point in the abscissa.
* @param {number} startY Starting point in the ordinate.
* @param {number} endX Ending point in the abscissa.
* @param {number} endY Ending point in the ordinate.
* @return {WebKeyframesDef} Keyframes that make up the animation.
*/
export function translate2d(startX, startY, endX, endY) {
return [
{transform: translate(startX, startY)},
{transform: translate(endX, endY)},
];
}
/**
* Translates and rotates the element on the 2d plane.
* @param {number} startX Starting point in the abscissa.
* @param {number} startY Starting point in the ordinate.
* @param {number} endX Ending point in the abscissa.
* @param {number} endY Ending point in the ordinate.
* @param {number} direction -1 for left, 1 for right
* @return {WebKeyframesDef} Keyframes that make up the animation.
*/
export function rotateAndTranslate(startX, startY, endX, endY, direction) {
return [
{transform: translate(startX, startY) + ' ' + rotate(direction * 120)},
{transform: translate(endX, endY) + ' ' + rotate(0)},
];
}
/**
* Gradually shows and grows the element while translating it on the 2d plane.
* @param {number} startX Starting point in the abscissa.
* @param {number} startY Starting point in the ordinate.
* @param {number} endX Ending point in the abscissa.
* @param {number} endY Ending point in the ordinate.
* @return {WebKeyframesDef} Keyframes that make up the animation.
*/
export function whooshIn(startX, startY, endX, endY) {
return [
{
opacity: 0,
transform: translate(startX, startY) + ' ' + scale(0.15),
},
{
opacity: 1,
transform: translate(endX, endY) + ' ' + scale(1),
},
];
}
/**
* Checks if either of the target's dimensions are smaller than or equal to
* those of the page.
* @param {StoryAnimationDimsDef} dimensions Dimensions of page and target.
* @return {boolean}
* @visibleForTesting
*/
export function targetFitsWithinPage(dimensions) {
return (
dimensions.targetWidth <= dimensions.pageWidth ||
dimensions.targetHeight <= dimensions.pageHeight
);
}
/**
* Calculate target scaling factor so that it is at least 25% larger than the
* page.
* @param {StoryAnimationDimsDef} dimensions Dimensions of page and target.
* @return {number}
*/
export function calculateTargetScalingFactor(dimensions) {
if (targetFitsWithinPage(dimensions)) {
const scalingFactor = 1.25;
const widthFactor =
dimensions.pageWidth > dimensions.targetWidth
? dimensions.pageWidth / dimensions.targetWidth
: 1;
const heightFactor =
dimensions.pageHeight > dimensions.targetHeight
? dimensions.pageHeight / dimensions.targetHeight
: 1;
return Math.max(widthFactor, heightFactor) * scalingFactor;
}
return 1;
}
/**
* Scale the image in every frame by a certain factor.
* @param {WebKeyframesDef} keyframes Keyframes that will be used for the animation.
* @param {number} scalingFactor Scaling factor at which target will be scaled.
* @return {WebKeyframesDef}
*/
function enlargeKeyFrames(keyframes, scalingFactor) {
/** @type {!Array} */ (keyframes).forEach((frame) => {
frame['transform'] += ' ' + scale(scalingFactor);
frame['transform-origin'] = 'left top';
});
return keyframes;
}
/**
* Translates the element and scales it if necessary.
* @param {number} startX Starting point in the abscissa.
* @param {number} startY Starting point in the ordinate.
* @param {number} endX Ending point in the abscissa.
* @param {number} endY Ending point in the ordinate.
* @param {number} scalingFactor Factor by which target will be scaled.
* @return {WebKeyframesDef} Keyframes that make up the animation.
*/
export function scaleAndTranslate(startX, startY, endX, endY, scalingFactor) {
if (scalingFactor === 1) {
return translate2d(startX, startY, endX, endY);
}
return enlargeKeyFrames(
translate2d(startX, startY, endX, endY),
scalingFactor
);
}