amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

amp-ad : centering image in flying-carpet

Open coreybyrnes opened this issue 9 years ago • 29 comments

I'm trying to horizontally and vertically center a DFP ad in the flying-carpet component when the image in the ad is larger than the flying-carpet container. For my test case the ad size for my creative is 1600x1000 and I'm serving a 1600x1000 image. It looks like if I could somehow resize the tag upon load from 1600x1000 to 100vw and 100vh it would sit perfectly centered in both portrait and landscape modes. From what I understand requestResize is only available upon user interaction. Is there any other way to manipulate the dimensions of the tag at the time the ad loads so I can get this to work? Perhaps I have to do it through an 'amp-3p-iframe-src' js? Thanks!

To reproduce, simply modify the width and height of the tag to 100vw and 100vh in the browser developer tools at the following test url: http://rev.cbsi.com/corey/test/amp/amp_demo_flying-carpet_v2.html

coreybyrnes avatar Mar 13 '17 20:03 coreybyrnes

/cc @jasti

aghassemi avatar Mar 14 '17 22:03 aghassemi

@coreybyrnes requestResize is available even when the ad is away from the viewport, but obviously won't succeed all the time. @jridgewell This seems reasonable - any chance to center align and fit the creative regardless of actual size?

jasti avatar Mar 15 '17 13:03 jasti

@jasti But doesn't requestResize require user interaction in order to execute? In order for an ad image to be centered when served inside the flying-carpet container the resize would have to occur upon load.

coreybyrnes avatar Mar 15 '17 13:03 coreybyrnes

requestResize has several factors that determine whether it will succeed.

  1. Whether user interacted with ad (this always succeeds)
  2. Ad position in relation to viewport (we don't resize things in the viewport)
  3. Scroll speed (for ads that are above viewport)

We actually wanted to include a special exception for <amp-fx-flying-carpet> children (like your ad) to allow resize whenever. This is because the children are on a physically different layer than the scroll layer, so it can't affect the user scroll position at all. But, we never got around to coding this exception. 😳

For now, go ahead and do a requestResize. It may fail, and you can go through the overflow element process like the other ads to get the resize to succeed after the user interacts.

This'll be fixed implicitly by #3434 (scroll layers get resize exceptions, which means the flying carpet too). I'm actively working on it, with a ship date sometime in the next few months. Or we can codify the exception in the current system, but I'd rather not waist my time for now. /cc @jasti

jridgewell avatar Mar 15 '17 19:03 jridgewell

@jridgewell Unfortunately requestResize doesn't allow me to set 100vw and 100vh for the size (hopefully will get implemented in #8095). And I can't find a way to obtain the exact viewport dimensions so I can use a px value when trying to call requestResize. Just to see if the resize will be successful I will try to call requestResize with some crazy large number like 3000,3000. But without setting the to exactly the dimensions of the viewport, the ad asset will not be centered. So I guess I'll have to wait for #3434 to be in prod?

Also, can you explain this more please? Thanks!

It may fail, and you can go through the overflow element process like the other ads to get the resize to succeed after the user interacts.

coreybyrnes avatar Mar 15 '17 19:03 coreybyrnes

You can use window.innerWidth and window.innerHeight instead. Those return pixel numbers, which is what requestResize expects.

Also, can you explain this more please? Thanks!

#8085. 😬

Bascially, we allow you to specify an overflow element that is only shown when a resize request is declined. When they click it, the resize will pass the "interaction" rule and will resize.

<amp-ad type="...">
  <div overflow>Click to resize</div>
  <!-- whatever else -->
</amp-ad>

jridgewell avatar Mar 15 '17 19:03 jridgewell

Ha, but innerWidth and innerHeight don't help here, because the iframe is already 1000x1600 px. Maybe @dvoytenko knows the right DOM API?

jridgewell avatar Mar 15 '17 19:03 jridgewell

@jridgewell But from within a DFP ad the window.innerWidth and window.innerHeight will simply return the size of the iframe which is the size of the ad slot.

coreybyrnes avatar Mar 15 '17 19:03 coreybyrnes

Why aren't you specifying 100vh/100vw directly on the <amp-ad>? See https://jsbin.com/xamuxof/edit?html,output (which doesn't fill, for some reason).

<amp-fx-flying-carpet height="400px">
    <amp-ad width="100vw" height="100vh"
        layout="fixed" 
        type="doubleclick" 
        data-slot="/7336/corey/amp-flying-carpet"
        json='{"useSameDomainRenderingUntilDeprecated":1}'
    ></amp-ad>
</amp-fx-flying-carpet>

This solves the core problem, no resize necessary.

jridgewell avatar Mar 15 '17 20:03 jridgewell

@jridgewell I was under the impression that the width and height that you set on the determines the size of the creative that is served. Is that not correct? That might be why the ad isn't serving in your demo.

I also tried implementing a style attr that I hoped would override the width and height attributes, but the width and height properties I set in the style attr are overwritten.

coreybyrnes avatar Mar 15 '17 20:03 coreybyrnes

I was under the impression that the width and height that you set on the determines the size of the creative that is served.

Yup. But you control the creative, so you can have whatever returned for any size.

but the width and height properties I set in the style attr are overwritten.

Yup, we will set the width/height CSS to whatever is specified in the HTML.

jridgewell avatar Mar 15 '17 20:03 jridgewell

@jridgewell Thanks for helping me work through this. I truly appreciate it.

But there is no "100vw x 100vh" ad size in DFP so I can't get the creative size and the amp-ad sizes to match which means I can't get a creative to serve.

coreybyrnes avatar Mar 15 '17 20:03 coreybyrnes

/cc @jasti

jridgewell avatar Mar 15 '17 20:03 jridgewell

Speaking with @jasti, there's no way to specify 100vw and have an ad server know what width that means. Which means a lot of them fail to fill the request. I hate ad servers.

jridgewell avatar Mar 15 '17 22:03 jridgewell

I think the best option is to give the ad the full available width. The iframe's window will have the right width then (window.innerWidth). Then the Ad can serve the desired size and ad can request resize, which will be to the smaller size. Once updated to the smaller size, the ad can be centered in the flying carpet via flexbox CSS.

dvoytenko avatar Mar 15 '17 22:03 dvoytenko

@dvoytenko And since we need we'd need the image to be centered vertically in addition to horizontally we'd need the amp-ad tag to have the full height. Please see my demo url in the first comment to reproduce the issue.

coreybyrnes avatar Mar 15 '17 22:03 coreybyrnes

@dvoytenko Are you saying we should do centering logic in the AMP doc instead of inside the ad creative?

jridgewell avatar Mar 16 '17 00:03 jridgewell

@jridgewell I don't think creative itself can do that very well. I'm wondering if we can find a way for flying carpet component to do the right thing naturally.

dvoytenko avatar Mar 16 '17 22:03 dvoytenko

@dvoytenko That would be great as well. I'm on the fence if it's best to allow this to be set at the creative level or as an attribute of <amp-ad> or <amp-fx-flying-carpet>. I would assume that most ads running in this slot would want to be vertically and horizontally centered relative to the viewport, but perhaps not so maybe having the option to set its position at the creative level is best. But in order to do that I believe the <amp-ad> tag has to be set to 100vw and 100vh. Then the <amp-fx-flying-carpet> container can be set to the actual height of the see-through "window". If you're worried about the height of the <amp-fx-flying-carpet> container being set to 100vh then that could become a restriction of the component - it won't serve if it's set to 100vh for example. Just trying to help think this all through.

coreybyrnes avatar Mar 17 '17 18:03 coreybyrnes

@coreybyrnes The concern I have specifically here is that Ad normally has very limited info how it's hosted, in what container. Thus, 100vw/100vh would typically be overflowing in many cases, e.g. if flying carpet has a padding or another container is sized differently. The way I see it, the container has to somehow communicate to the Ad how much possible space it can get. Currently we do this by giving the Ad the maximum width. In this case, the Ad's width could hypothetically be reduced and the Ad's height could be reduced or increased. This, of course, is not always true. E.g. in case of flying carpet, the maximum height cannot realistically be more than 100vh.

So, going back to the use cases, what I meant was:

<amp-fx-flying-carpet>
  <amp-ad width=300 height="200" ...></amp-ad>
</amp-fx-flying-carpet>

This should simply center the 300x200 ad in the viewport. The iframe's window would have innerWidth=300 and innerHeight=200. The embed can then measure and allow height to increase/decrease. If height changes, the Ad should be re-centered.

If Ad is intrinsically responsive (which is rare), it's fine to use this markup:

<amp-fx-flying-carpet>
  <amp-ad layout="fill" ...></amp-ad>
</amp-fx-flying-carpet>

In this case, the iframe's window size will be viewportWidth x viewportHeight. In this case, the Ad would either be responsive enough to fill the space or center its main content. Or it can ask for smaller width/height, in which case flying carpet will do the centering.

If we want more control, we can think about how we can pass maxWidth/maxHeight to the Ad.

dvoytenko avatar Mar 17 '17 19:03 dvoytenko