amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

Issues with using percentage values with transforms in responsive a4a ads

Open lexandera opened this issue 9 years ago • 4 comments

We have a use case for building responsive ads - clients have asked about a possibility of building a single ad which adapts to several placement sizes, or always fills a responsive amp-ad element, so we are currently exploring what can be done in terms of responsive design support for a4a ads inside our tool.

Producing a responsive ad usually involves using positioning and sizing in %, or positioning relative to origins other than the top left corner. Things get complicated once you try animating such objects - there are a couple of problems arising from the combination of % based values, limiting all animations to use transforms only, and not having the ability to run any javascript.

The short version is that CSS transforms behave very differently from basic CSS properties used for object positioning and sizing. This is not a problem when everything has a fixed size in pixels which is known in advance, but becomes a problem in responsive environments where actual sizes are calculated at layout time. Longer description follows below.

Sizing and positioning differences between CSS and transforms

CSS positioning and sizing properties (top, left, width, height, ...) calculate % based values as a percentage of the parent element's size. But when using transforms, % based values are calculated relative to the object's own size. This means that animating an object to left: 25% is not the same as animating it to transform: translateX(25%). While JS could be used for calculating the appropriate value to use with the translate transform for animating to the same visual position, this is not an option within AMP.

Since elements in AMP pages do not resize after the initial layout, recalculating all % based animation values to pixels ahead of time seems like a possibility, effectively generating a fixed size ad from the responsive source on the fly at render time. Could a client-side transformation step be introduced to perform conversion of all transform values (specified using a certain syntax) in order to allow sizing and positioning to refer to sizes of arbitrary elements?

Example: transform: translateX(toPx(25% of someElement.width))

Since there's usually a visible change (or a flash) in how an object is rendered when it has a transform applied, we use transforms for initial object positioning as well, not just for animations. Therefore the transformation step would need to be applied to all CSS, not just to animations.

Additionally, it would be useful if it was possible to refer to any element's size, not just to the parent element's, because we're using a number of wrapper elements to work around some other limitations. In addition to making the solution more versatile, it would probably also make it easier to implement as there would be no need to check the element hierarchy and limit allowed references in expressions in CSS based on DOM node structure.

lexandera avatar Apr 26 '17 21:04 lexandera

/cc @ampproject/a4a

keithwrightbos avatar Apr 27 '17 14:04 keithwrightbos

@lexandera Thanks a lot for feedback. This is certainly the problem I've dealt with in a number of projects very often. My typical solution (which is not always portable) would be to calculate couple --var in CSS and use them throughout keyframes and other positioning using calc().

However, I do like your proposal and wanted to see if we can work some good approach for animations. I've included it for this week's design review (#8900). I'll prepare some details.

dvoytenko avatar May 02 '17 02:05 dvoytenko

See https://docs.google.com/presentation/d/1z-a6Hxy3XIjRCAb4ExuFTe3AsLUsRRBezGyU3cVvwC0/edit# for tomorrow.

dvoytenko avatar May 02 '17 16:05 dvoytenko

Now supported via width/height extensions in amp-animation: https://github.com/ampproject/amphtml/blob/master/extensions/amp-animation/amp-animation.md#css-width-and-height-extensions

dvoytenko avatar May 30 '17 23:05 dvoytenko