image-not-fully-visible.html (2456B)
1 <!DOCTYPE HTML> 2 <meta charset=utf-8> 3 <meta name="viewport" content="width=device-width, minimum-scale=1"> 4 <title>Largest Contentful Paint: size when image overflows</title> 5 <body> 6 <style> 7 body { 8 /* Preventing a scrollbar from showing and removing any margins simplifies 9 the calculations below. */ 10 overflow: hidden; 11 margin: 0px; 12 } 13 </style> 14 <script src="/resources/testharness.js"></script> 15 <script src="/resources/testharnessreport.js"></script> 16 <script src="resources/largest-contentful-paint-helpers.js"></script> 17 <script> 18 setup({"hide_test_state": true}); 19 let beforeRender; 20 const viewportWidth = document.documentElement.clientWidth; 21 const viewportHeight = document.documentElement.clientHeight; 22 const imgWidth = 100; 23 const imgHeight = 50; 24 async_test(function (t) { 25 assert_implements(window.LargestContentfulPaint, "LargestContentfulPaint is not implemented"); 26 const beforeLoad = performance.now(); 27 new PerformanceObserver( 28 t.step_func_done(function(entryList) { 29 assert_equals(entryList.getEntries().length, 1); 30 const entry = entryList.getEntries()[0]; 31 const url = window.location.origin + '/images/lcp-100x50.png'; 32 // To compute the size, compute the percentage of the image visible and 33 // scale by its natural dimensions. In this test, the image is expanded to twice its size 34 // but place towards the bottom right corner of the viewport, so it is 35 // effectively clipped to 50% by 50% of its display size. Scaling by 36 // its natural width and height of 100px and 50px respectively, leads 37 // to a weighted size of 50 by 25. 38 const truncatedWidth = imgWidth / 2; 39 const truncatedHeight = imgHeight / 2; 40 const weightedSize = truncatedWidth * truncatedHeight; 41 checkImage(entry, url, 'image_id', weightedSize, beforeLoad); 42 }) 43 ).observe({type: 'largest-contentful-paint', buffered: true}); 44 // Add an image, setting width and height equal to viewport. 45 img = document.createElement('img'); 46 img.src = '/images/lcp-100x50.png'; 47 img.id = 'image_id'; 48 img.width = imgWidth * 2; 49 img.height = imgHeight * 2; 50 img.style.position = 'absolute'; 51 img.style.left = viewportWidth - imgWidth + 'px'; 52 img.style.top = viewportHeight - imgHeight + 'px'; 53 document.body.appendChild(img); 54 }, 'The intersectionRect of an img element overflowing is computed correctly'); 55 </script> 56 </body>