Skip to content

Commit 63d5708

Browse files
author
Sepand Parhami
committed
Add some more function/file comments.
1 parent d828e3b commit 63d5708

File tree

8 files changed

+96
-42
lines changed

8 files changed

+96
-42
lines changed

src/bezier-curve-utils.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,23 @@ export interface Curve {
2424
/**
2525
* A string representation of the curve that can be used as an
2626
* `animation-timing-function`.
27+
* @param curve The curve to conver.
28+
* @return A string in the form of 'cubic-bezier(x1, y1, x2, y2)'.
2729
*/
2830
export function curveToString(curve: Curve): string {
2931
return `cubic-bezier(${curve.x1}, ${curve.y1}, ${curve.x2}, ${curve.y2})`;
3032
}
3133

3234
/**
33-
* Gets the x/y value for the given control points for a given value of t.
35+
* Gets the x/y value for the given control points for a given value of t. The
36+
* first control point is always zero and the fourth is always one.
37+
* @param c1 The second control point.
38+
* @param c2 The third control point.
39+
* @param t
40+
* @return The value at t.
3441
*/
35-
export function getBezierCurveValue(c1: number, c2: number, t: number): number {
42+
export function getCubicBezierCurveValue(
43+
c1: number, c2: number, t: number): number {
3644
const t_2 = t * t;
3745
const t_3 = t_2 * t;
3846
// Formula for 4 point bezier curve with c0 = 0 and c3 = 1.

src/img-dimensions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
* @see https://developer.mozilla.org/en-US/docs/Web/CSS/object-fit
3131
*/
3232

33-
interface Size {
33+
export interface Size {
3434
width: number,
3535
height: number,
3636
}

src/imitation-img.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,31 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {getRenderedDimensions} from './img-dimensions.js';
17+
import {Size, getRenderedDimensions} from './img-dimensions.js';
1818

1919
/**
2020
* Creates a replacement for a given img, which should render the same as the
2121
* source img, but implemented with a cropping container and and img using
2222
* `object-fit: fill`. This can be used to implement a transition of the image.
2323
* The crop can be transitioned by scaling up the container while scaling down
2424
* the image by the inverse amount.
25-
* @param srcImg
26-
* @param srcImgRect
25+
* @param srcImg The source img.
26+
* @param srcImgRect The rect for `srcImg`. Can be provided if already
27+
* measured.
28+
* @param imageDimensions The dimensions for the rendered image. Can be
29+
* provided if already measured.
2730
* @return The replacement container along with structural information.
2831
*/
2932
export function createImitationImg(
3033
srcImg: HTMLImageElement,
3134
srcImgRect: ClientRect = srcImg.getBoundingClientRect(),
35+
imageDimensions: Size = getRenderedDimensions(srcImg, srcImgRect),
3236
): {
3337
translateElement: HTMLElement,
3438
scaleElement: HTMLElement,
3539
counterScaleElement: HTMLElement,
3640
img: HTMLImageElement,
37-
imgWidth: number,
38-
imgHeight: number,
3941
} {
40-
const {
41-
width: imgWidth,
42-
height: imgHeight
43-
} = getRenderedDimensions(srcImg, srcImgRect);
44-
4542
const translateElement = document.createElement('div');
4643
const scaleElement = document.createElement('div');
4744
const counterScaleElement = document.createElement('div');
@@ -65,16 +62,14 @@ export function createImitationImg(
6562

6663
Object.assign(img.style, {
6764
display: 'block',
68-
width: `${imgWidth}px`,
69-
height: `${imgHeight}px`,
65+
width: `${imageDimensions.width}px`,
66+
height: `${imageDimensions.height}px`,
7067
});
7168

7269
return {
7370
translateElement,
7471
scaleElement,
7572
counterScaleElement,
7673
img,
77-
imgWidth,
78-
imgHeight,
7974
};
8075
}

src/testing/animation-test-controller.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,20 @@
1414
* limitations under the License.
1515
*/
1616

17+
/**
18+
* @fileoverview Helpers for controlling animations within a test. This
19+
* supports pausing all CSS-based animations on the page and then controlling
20+
* them by manually moving the time offset within the animation.
21+
*
22+
* During the test setup, you should call `setup()` and during the test tear
23+
* down, you should call `tearDown()`. During a test case, you can call
24+
* `offset(time)` to move all animations to a specific time. Note that this
25+
* does not move them forward by `time` but rather to `time`.
26+
*
27+
* Note: for animationend events to fire, you will need to wait for the browser
28+
* to render (e.g. using requestAnimationFrame + setTimeout).
29+
*/
30+
1731
/**
1832
* Sets all animations in a DOM subtree to a certain time into the animation.
1933
* This is done by adding CSS rules to target the Elements and their pseudo

src/transform-img/crop-animation.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {Curve, getBezierCurveValue} from '../bezier-curve-utils.js';
17+
import {Curve, getCubicBezierCurveValue} from '../bezier-curve-utils.js';
1818

1919
interface Scale {
2020
x: number,
@@ -36,13 +36,25 @@ const numSamples = 20;
3636

3737
/**
3838
* Interpolates a value x% between a and b.
39+
* @param a The start point.
40+
* @param b The end point.
41+
* @param x A percentage of the way between `a` to `b`.
3942
*/
4043
function interpolate(a: number, b: number, x: number): number {
4144
return a + x * (b - a);
4245
}
4346

4447
/**
4548
* Generates a CSS stylesheet for animating between two images.
49+
* @param options
50+
* @param options.startScale The starting scale for the animation.
51+
* @param options.endScale The ending scale for the animation.
52+
* @param options.curve The timing curve for how the crop should expand or
53+
* contract.
54+
* @param options.scaleKeyframesName The names for the scaling keyframes.
55+
* @param options.counterScaleKeyframesName The names for the counter-scaling
56+
* keyframes.
57+
* @return CSS style text to perform the aniamtion.
4658
*/
4759
function generateCropKeyframes({
4860
startScale,
@@ -71,9 +83,9 @@ function generateCropKeyframes({
7183
for (let i = 0; i <= numSamples; i++) {
7284
const t = i * (1 / numSamples);
7385
// The progress through the animation at this point.
74-
const px = getBezierCurveValue(curve.x1, curve.x2, t);
86+
const px = getCubicBezierCurveValue(curve.x1, curve.x2, t);
7587
// The output percentage at this point.
76-
const py = getBezierCurveValue(curve.y1, curve.y2, t);
88+
const py = getCubicBezierCurveValue(curve.y1, curve.y2, t);
7789
const keyframePercentage = px * 100;
7890
const scaleX = interpolate(startScale.x, endScale.x, py);
7991
const scaleY = interpolate(startScale.y, endScale.y, py);
@@ -106,6 +118,21 @@ function generateCropKeyframes({
106118
* content. This function sets up the animation by setting the appropriate
107119
* style properties on the desired Elements. The returned style text needs
108120
* to be inserted for the animation to run.
121+
* @param options
122+
* @param options.scaleElement The element to apply the scaling to. This should
123+
* have `overflow: hidden`,
124+
* @param options.counterScaleElement The element to counteract the scaling.
125+
* This should be a child of `scaleElement`.
126+
* @param options.largerRect The larger of the start/end cropping rects.
127+
* @param options.smallerRect The smaller of the start/end cropping rects.
128+
* @param options.curve The timing curve for how the crop should expand or
129+
* contract.
130+
* @param options.style The styles to apply to both the `scaleElement` and
131+
* `counterScaleElement`.
132+
* @param options.keyframesPrefix A prefix to use for the generated
133+
* keyframes to ensure they do not clash with existing keyframes.
134+
* @param options.toLarger Whether or not `largerRect` is the rect we are
135+
* animating to.
109136
* @return CSS style text to perform the aniamtion.
110137
*/
111138
export function prepareCropAnimation({

src/transform-img/scale-animation.ts

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,36 @@
1515
*/
1616

1717
import {Curve, curveToString} from '../bezier-curve-utils.js';
18-
18+
import {Size} from '../img-dimensions';
1919

2020
/**
2121
* Prepares a scale animation. This function sets up the animation by setting
2222
* the appropriate style properties on the desired Element. The returned style
2323
* text needs to be inserted for the animation to run.
24+
* @param options
25+
* @param options.element The element to apply the scaling to.
26+
* @param options.largerImgDimensions The larger of the start/end image dimensions,
27+
* @param options.smallerImgDimensions The smaller of the start/end cropping rects.
28+
* @param options.curve The timing curve for the scaling.
29+
* @param options.style The styles to apply to `element`.
30+
* @param options.keyframesPrefix A prefix to use for the generated
31+
* keyframes to ensure they do not clash with existing keyframes.
32+
* @param options.toLarger Whether or not `largerImgDimensions` are the
33+
* dimensions are we are animating to.
2434
* @return CSS style text to perform the aniamtion.
2535
*/
2636
export function prepareScaleAnimation({
2737
element,
28-
largerImgWidth,
29-
largerImgHeight,
30-
smallerImgWidth,
31-
smallerImgHeight,
38+
largerDimensions,
39+
smallerDimensions,
3240
curve,
3341
styles,
3442
keyframesPrefix,
3543
toLarger,
3644
} : {
3745
element: HTMLElement,
38-
largerImgWidth: number,
39-
largerImgHeight: number,
40-
smallerImgWidth: number,
41-
smallerImgHeight: number,
46+
largerDimensions: Size,
47+
smallerDimensions: Size,
4248
curve: Curve,
4349
styles: Object,
4450
keyframesPrefix: string,
@@ -47,8 +53,8 @@ export function prepareScaleAnimation({
4753
const keyframesName = `${keyframesPrefix}-scale`;
4854

4955
const scaleImgDown = {
50-
x: smallerImgWidth / largerImgWidth,
51-
y: smallerImgHeight / largerImgHeight,
56+
x: smallerDimensions.width / largerDimensions.width,
57+
y: smallerDimensions.height / largerDimensions.height,
5258
};
5359
const neutralScale = {x: 1, y: 1};
5460
const startImgScale = toLarger ? scaleImgDown : neutralScale;

src/transform-img/transform-img.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,14 @@ export function prepareImageAnimation({
109109
const keyframesPrefix = getKeyframesPrefix(keyframesNamespace);
110110
const toLarger = largerImg == targetImg;
111111

112+
const smallerImageDimensions = getRenderedDimensions(smallerImg, smallerRect);
113+
const largerImageDimensions = getRenderedDimensions(largerImg, largerRect);
112114
const {
113115
translateElement,
114116
scaleElement,
115117
counterScaleElement,
116118
img,
117-
imgWidth: largerImgWidth,
118-
imgHeight: largerImgHeight,
119-
} = createImitationImg(largerImg, largerRect);
120-
const {
121-
width: smallerImgWidth,
122-
height: smallerImgHeight,
123-
} = getRenderedDimensions(smallerImg, smallerRect);
119+
} = createImitationImg(largerImg, largerRect, largerImageDimensions);
124120

125121
const cropStyleText = prepareCropAnimation({
126122
scaleElement,
@@ -143,10 +139,8 @@ export function prepareImageAnimation({
143139
});
144140
const scaleStyleText = prepareScaleAnimation({
145141
element: img,
146-
largerImgWidth,
147-
largerImgHeight,
148-
smallerImgWidth,
149-
smallerImgHeight,
142+
largerDimensions: largerImageDimensions,
143+
smallerDimensions: smallerImageDimensions,
150144
curve,
151145
styles,
152146
keyframesPrefix,

src/transform-img/translate-animation.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ import {Curve, curveToString} from '../bezier-curve-utils.js';
2222
* This function sets up the animation by setting the appropriate style
2323
* properties on the desired Element. The returned style text needs to be
2424
* inserted for the animation to run.
25+
* @param options
26+
* @param options.element The element to apply the scaling to.
27+
* @param options.largerRect The larger of the start/end scaling rects.
28+
* @param options.smallerRect The smaller of the start/end scaling rects.
29+
* @param options.curve The timing curve for the scaling.
30+
* @param options.style The styles to apply to `element`.
31+
* @param options.keyframesPrefix A prefix to use for the generated
32+
* keyframes to ensure they do not clash with existing keyframes.
33+
* @param options.toLarger Whether or not `largerRect` is the rect we are
34+
* animating to.
2535
* @return CSS style text to perform the aniamtion.
2636
*/
2737
export function prepareTranslateAnimation({

0 commit comments

Comments
 (0)