Skip to content

Commit 28e56e7

Browse files
authored
✨ [amp-animation] Adds a new attr that allows to override pan scaling factor. (#37965)
* Adds a new attr that allows amp-animation to override pan scaling factor. * Update pan static scale attr variable name. * Update the animation scaling factor test to cover all pan animations. * Add the new attr into validation rule. * Update attr regex and the nullish operator syntax. * Add example in animateion-presets.html with pan-static-scale attr. * Update the pan-static-scale validation rules and tests. * Update pan-static-scale doc. * Update attr name to pan-scaling-factor, and minor updates on test variables.
1 parent a61e479 commit 28e56e7

File tree

7 files changed

+116
-9
lines changed

7 files changed

+116
-9
lines changed

examples/amp-story/animations-presets.html

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,23 @@ <h1>pan-up</h1>
234234
<h1>pan-down</h1>
235235
</amp-story-grid-layer>
236236
</amp-story-page>
237+
238+
<!-- ## Pan-Right with pan-scaling-factor attr -->
239+
<amp-story-page id="pan-right-with-static-scale">
240+
<amp-story-grid-layer template="vertical">
241+
<h1>pan-right</h1>
242+
<p>with pan-scaling-factor attr</p>
243+
<div class="img-container" style="width: 120%">
244+
<amp-img
245+
animate-in="pan-right" animate-in-timing-function="ease-out"
246+
pan-scaling-factor=".9"
247+
translate-x="35px" animate-in-duration="3.5s"
248+
id="img-pan-right" layout="responsive" src="https://picsum.photos/720/320?image=1026"
249+
width="720" height="320">
250+
</amp-img>
251+
</div>
252+
</amp-story-grid-layer>
253+
</amp-story-page>
237254
</amp-story>
238255
</body>
239256

extensions/amp-story/1.0/animation-presets.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ const SCALE_START_ATTRIBUTE_NAME = 'scale-start';
2323
/** @const {string} */
2424
const SCALE_END_ATTRIBUTE_NAME = 'scale-end';
2525
/** @const {string} */
26+
const PAN_SCALING_FACTOR_ATTRIBUTE_NAME = 'pan-scaling-factor';
27+
/** @const {string} */
2628
const TRANSLATE_X_ATTRIBUTE_NAME = 'translate-x';
2729
/** @const {string} */
2830
const TRANSLATE_Y_ATTRIBUTE_NAME = 'translate-y';
@@ -33,6 +35,7 @@ const DEFAULT_CURVE = '0.4, 0.4, 0.0, 1';
3335
export const PRESET_OPTION_ATTRIBUTES = [
3436
SCALE_START_ATTRIBUTE_NAME,
3537
SCALE_END_ATTRIBUTE_NAME,
38+
PAN_SCALING_FACTOR_ATTRIBUTE_NAME,
3639
TRANSLATE_X_ATTRIBUTE_NAME,
3740
TRANSLATE_Y_ATTRIBUTE_NAME,
3841
];
@@ -285,7 +288,9 @@ export const presets = {
285288
easing: 'linear',
286289
keyframes(dimensions, options) {
287290
const translateX = options[TRANSLATE_X_ATTRIBUTE_NAME];
288-
const scalingFactor = calculateTargetScalingFactor(dimensions);
291+
const scalingFactor =
292+
options[PAN_SCALING_FACTOR_ATTRIBUTE_NAME] ??
293+
calculateTargetScalingFactor(dimensions);
289294
dimensions.targetWidth *= scalingFactor;
290295
dimensions.targetHeight *= scalingFactor;
291296

@@ -306,8 +311,9 @@ export const presets = {
306311
easing: 'linear',
307312
keyframes(dimensions, options) {
308313
const translateX = options[TRANSLATE_X_ATTRIBUTE_NAME];
309-
310-
const scalingFactor = calculateTargetScalingFactor(dimensions);
314+
const scalingFactor =
315+
options[PAN_SCALING_FACTOR_ATTRIBUTE_NAME] ??
316+
calculateTargetScalingFactor(dimensions);
311317
dimensions.targetWidth *= scalingFactor;
312318
dimensions.targetHeight *= scalingFactor;
313319

@@ -328,7 +334,9 @@ export const presets = {
328334
easing: 'linear',
329335
keyframes(dimensions, options) {
330336
const translateY = options[TRANSLATE_Y_ATTRIBUTE_NAME];
331-
const scalingFactor = calculateTargetScalingFactor(dimensions);
337+
const scalingFactor =
338+
options[PAN_SCALING_FACTOR_ATTRIBUTE_NAME] ??
339+
calculateTargetScalingFactor(dimensions);
332340
dimensions.targetWidth *= scalingFactor;
333341
dimensions.targetHeight *= scalingFactor;
334342

@@ -349,7 +357,9 @@ export const presets = {
349357
easing: 'linear',
350358
keyframes(dimensions, options) {
351359
const translateY = options[TRANSLATE_Y_ATTRIBUTE_NAME];
352-
const scalingFactor = calculateTargetScalingFactor(dimensions);
360+
const scalingFactor =
361+
options[PAN_SCALING_FACTOR_ATTRIBUTE_NAME] ??
362+
calculateTargetScalingFactor(dimensions);
353363
dimensions.targetWidth *= scalingFactor;
354364
dimensions.targetHeight *= scalingFactor;
355365

extensions/amp-story/1.0/test/test-full-bleed-animations.js

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,12 @@ describes.realWin(
224224
const dimensions = setDimensions(380, 580, 360, 580);
225225
expect(targetFitsWithinPage(dimensions)).to.be.true;
226226

227-
const factorThatWillMakeTargetFitPage = 380 / 360;
227+
const factorThatWillMakeTargetFitPage =
228+
dimensions.pageWidth / dimensions.targetWidth;
228229
const factor = factorThatWillMakeTargetFitPage * 1.25;
229230
expect(calculateTargetScalingFactor(dimensions)).to.equal(factor);
230231

231-
const calculatedKeyframes = presets['pan-up'];
232-
calculatedKeyframes.keyframes = calculatedKeyframes.keyframes(
232+
const calculatedKeyframes = presets['pan-up'].keyframes(
233233
dimensions,
234234
/* options */ {}
235235
);
@@ -248,7 +248,46 @@ describes.realWin(
248248
},
249249
];
250250

251-
expect(calculatedKeyframes.keyframes).to.deep.equal(expectedKeyframes);
251+
expect(calculatedKeyframes).to.deep.equal(expectedKeyframes);
252+
});
253+
254+
['pan-up', 'pan-down', 'pan-right', 'pan-left'].forEach((panAnimation) => {
255+
it(`Should scale the target for ${panAnimation}.`, () => {
256+
const dimensions = setDimensions(380, 580, 360, 580);
257+
expect(targetFitsWithinPage(dimensions)).to.be.true;
258+
259+
const factorThatWillMakeTargetFitPage =
260+
dimensions.pageWidth / dimensions.targetWidth;
261+
const factor = factorThatWillMakeTargetFitPage * 1.25;
262+
expect(calculateTargetScalingFactor(dimensions)).to.equal(factor);
263+
264+
const calculatedKeyframes = presets[panAnimation].keyframes(
265+
dimensions,
266+
/* options */ {}
267+
);
268+
269+
const expectedScaleFactorStr = `scale(${factor})`;
270+
calculatedKeyframes.forEach((keyframe) => {
271+
expect(keyframe.transform).to.contain(expectedScaleFactorStr);
272+
});
273+
});
274+
275+
it(`Should not scale the target if scaling factor is set for ${panAnimation}.`, () => {
276+
const scalingFactor = 2;
277+
278+
const dimensions = setDimensions(380, 580, 360, 580);
279+
expect(targetFitsWithinPage(dimensions)).to.be.true;
280+
281+
const calculatedKeyframes = presets[panAnimation].keyframes(
282+
dimensions,
283+
/* options */ {'pan-scaling-factor': scalingFactor}
284+
);
285+
286+
const expectedScaleFactorStr = `scale(${scalingFactor})`;
287+
calculatedKeyframes.forEach((keyframe) => {
288+
expect(keyframe.transform).to.contain(expectedScaleFactorStr);
289+
});
290+
});
252291
});
253292
}
254293
);

extensions/amp-story/1.0/test/validator-amp-story-animations.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ <h1>ken-burns-effect</h1>
3737
src="https://picsum.photos/1024/768?image=1077"
3838
animate-in="pan-left"
3939
animate-in-duration="30s"
40+
pan-scaling-factor=".9"
4041
layout="fixed"
4142
width="1024"
4243
height="768">

extensions/amp-story/1.0/test/validator-amp-story-animations.out

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ PASS
3838
| src="https://picsum.photos/1024/768?image=1077"
3939
| animate-in="pan-left"
4040
| animate-in-duration="30s"
41+
| pan-scaling-factor=".9"
4142
| layout="fixed"
4243
| width="1024"
4344
| height="768">

extensions/amp-story/amp-story.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,27 @@ _Example_: An image panning 50px down over 15 seconds.
729729
</amp-img>
730730
```
731731

732+
#### pan-scaling-factor [optional, only works with `pan-left`, `pan-right`, `pan-up`, & `pan-down` animations]
733+
734+
The target scales automatically in a pan-left/pan-right/pan-up/pan-down animation to ensure it does not go out of the target boundary when panning.
735+
736+
Use this attribute to override the default scaling factor calculation, and specify a static scaling factor. The value must be greater than 0, and decimals are allowed.
737+
738+
_Example_: An image scales 1.3x when panning.
739+
740+
```html
741+
<amp-img
742+
animate-in="pan-left"
743+
pan-scaling-factor="1.3"
744+
layout="fixed"
745+
src="https://picsum.photos/720/320?image=1026"
746+
width="720"
747+
height="320"
748+
alt="..."
749+
>
750+
</amp-img>
751+
```
752+
732753
### Sequencing animations
733754

734755
To chain animations in sequence, use the `animate-in-after` attribute. All elements in a given chain must be present in the same `<amp-story-page>`. Elements without the `animate-in-after` attribute do not belong to a sequence chain, and will start independently on page entrance.

extensions/amp-story/validator-amp-story.protoascii

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,12 @@ tags: {
267267
name: "translate-y"
268268
value_regex_casei: "[0-9]+px"
269269
}
270+
attrs: {
271+
name: "pan-scaling-factor"
272+
# Allow float number such as "12.3", ".1", or "5."
273+
# Note that string with only decimal point such as "." is not allowed.
274+
value_regex_casei: "^([0-9]*[.]?[0-9]+)|([0-9]+[.]?[0-9]*)$"
275+
}
270276
attrs: {
271277
name: "justify-content"
272278
value: "center"
@@ -362,6 +368,12 @@ tags: {
362368
name: "translate-y"
363369
value_regex_casei: "[0-9]+px"
364370
}
371+
attrs: {
372+
name: "pan-scaling-factor"
373+
# Allow float number such as "12.3", ".1", or "5."
374+
# Note that string with only decimal point such as "." is not allowed.
375+
value_regex_casei: "^([0-9]*[.]?[0-9]+)|([0-9]+[.]?[0-9]*)$"
376+
}
365377
reference_points: {
366378
tag_spec_name: "AMP-STORY-GRID-LAYER animate-in"
367379
}
@@ -566,6 +578,12 @@ tags: {
566578
name: "translate-y"
567579
value_regex_casei: "[0-9]+px"
568580
}
581+
attrs: {
582+
name: "pan-scaling-factor"
583+
# Allow float number such as "12.3", ".1", or "5."
584+
# Note that string with only decimal point such as "." is not allowed.
585+
value_regex_casei: "^([0-9]*[.]?[0-9]+)|([0-9]+[.]?[0-9]*)$"
586+
}
569587
reference_points: {
570588
tag_spec_name: "AMP-STORY-CTA-LAYER animate-in"
571589
}

0 commit comments

Comments
 (0)