Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: third_party/WebKit/Source/core/dom/IntersectionObservation.cpp

Issue 1879323003: Add IntersectionObserverEntry.intersectionRatio attribute. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@2704
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "core/dom/IntersectionObservation.h" 5 #include "core/dom/IntersectionObservation.h"
6 6
7 #include "core/dom/ElementRareData.h" 7 #include "core/dom/ElementRareData.h"
8 #include "core/dom/IntersectionObserver.h" 8 #include "core/dom/IntersectionObserver.h"
9 #include "core/frame/FrameView.h" 9 #include "core/frame/FrameView.h"
10 #include "core/layout/LayoutBox.h" 10 #include "core/layout/LayoutBox.h"
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
191 // only two states are recognized: 191 // only two states are recognized:
192 // - 0 means not intersecting. 192 // - 0 means not intersecting.
193 // - 1 means intersecting. 193 // - 1 means intersecting.
194 // No other threshold crossings are possible. 194 // No other threshold crossings are possible.
195 // - Otherwise: 195 // - Otherwise:
196 // - If root and target do not intersect, the threshold index is 0. 196 // - If root and target do not intersect, the threshold index is 0.
197 // - If root and target intersect but the intersection has zero-area (i. e., they 197 // - If root and target intersect but the intersection has zero-area (i. e., they
198 // have a coincident edge or corner), we consider the intersection to have 198 // have a coincident edge or corner), we consider the intersection to have
199 // "crossed" a zero threshold, but not crossed any non-zero threshold. 199 // "crossed" a zero threshold, but not crossed any non-zero threshold.
200 unsigned newThresholdIndex; 200 unsigned newThresholdIndex;
201 float newVisibleRatio = 0;
201 if (geometry.targetRect.isEmpty()) { 202 if (geometry.targetRect.isEmpty()) {
202 newThresholdIndex = geometry.doesIntersect ? 1 : 0; 203 newThresholdIndex = geometry.doesIntersect ? 1 : 0;
203 } else if (!geometry.doesIntersect) { 204 } else if (!geometry.doesIntersect) {
204 newThresholdIndex = 0; 205 newThresholdIndex = 0;
205 } else { 206 } else {
206 float intersectionArea = geometry.intersectionRect.size().width().toFloa t() * geometry.intersectionRect.size().height().toFloat(); 207 float intersectionArea = geometry.intersectionRect.size().width().toFloa t() * geometry.intersectionRect.size().height().toFloat();
207 float targetArea = geometry.targetRect.size().width().toFloat() * geomet ry.targetRect.size().height().toFloat(); 208 float targetArea = geometry.targetRect.size().width().toFloat() * geomet ry.targetRect.size().height().toFloat();
208 float newVisibleRatio = intersectionArea / targetArea; 209 newVisibleRatio = intersectionArea / targetArea;
209 newThresholdIndex = observer().firstThresholdGreaterThan(newVisibleRatio ); 210 newThresholdIndex = observer().firstThresholdGreaterThan(newVisibleRatio );
210 } 211 }
211 if (m_lastThresholdIndex != newThresholdIndex) { 212 if (m_lastThresholdIndex != newThresholdIndex) {
212 IntRect snappedRootBounds = pixelSnappedIntRect(geometry.rootRect); 213 IntRect snappedRootBounds = pixelSnappedIntRect(geometry.rootRect);
213 IntRect* rootBoundsPointer = m_shouldReportRootBounds ? &snappedRootBoun ds : nullptr; 214 IntRect* rootBoundsPointer = m_shouldReportRootBounds ? &snappedRootBoun ds : nullptr;
214 IntersectionObserverEntry* newEntry = new IntersectionObserverEntry( 215 IntersectionObserverEntry* newEntry = new IntersectionObserverEntry(
215 timestamp, 216 timestamp,
217 newVisibleRatio,
216 pixelSnappedIntRect(geometry.targetRect), 218 pixelSnappedIntRect(geometry.targetRect),
217 rootBoundsPointer, 219 rootBoundsPointer,
218 pixelSnappedIntRect(geometry.intersectionRect), 220 pixelSnappedIntRect(geometry.intersectionRect),
219 target()); 221 target());
220 observer().enqueueIntersectionObserverEntry(*newEntry); 222 observer().enqueueIntersectionObserverEntry(*newEntry);
221 } 223 }
222 setLastThresholdIndex(newThresholdIndex); 224 setLastThresholdIndex(newThresholdIndex);
223 } 225 }
224 226
225 void IntersectionObservation::disconnect() 227 void IntersectionObservation::disconnect()
(...skipping 10 matching lines...) Expand all
236 m_observer.clear(); 238 m_observer.clear();
237 } 239 }
238 240
239 DEFINE_TRACE(IntersectionObservation) 241 DEFINE_TRACE(IntersectionObservation)
240 { 242 {
241 visitor->trace(m_observer); 243 visitor->trace(m_observer);
242 visitor->trace(m_target); 244 visitor->trace(m_target);
243 } 245 }
244 246
245 } // namespace blink 247 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698