OLD | NEW |
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 | 9 |
10 namespace blink { | 10 namespace blink { |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 // - 0 means not intersecting. | 43 // - 0 means not intersecting. |
44 // - 1 means intersecting. | 44 // - 1 means intersecting. |
45 // No other threshold crossings are possible. | 45 // No other threshold crossings are possible. |
46 // - Otherwise: | 46 // - Otherwise: |
47 // - If root and target do not intersect, the threshold index is 0. | 47 // - If root and target do not intersect, the threshold index is 0. |
48 // - If root and target intersect but the intersection has zero-area | 48 // - If root and target intersect but the intersection has zero-area |
49 // (i.e., they have a coincident edge or corner), we consider the | 49 // (i.e., they have a coincident edge or corner), we consider the |
50 // intersection to have "crossed" a zero threshold, but not crossed | 50 // intersection to have "crossed" a zero threshold, but not crossed |
51 // any non-zero threshold. | 51 // any non-zero threshold. |
52 unsigned newThresholdIndex; | 52 unsigned newThresholdIndex; |
53 float newVisibleRatio = 0; | 53 float newVisibleRatio; |
54 if (geometry.targetRect().isEmpty()) { | 54 if (geometry.doesIntersect()) { |
55 newThresholdIndex = geometry.doesIntersect() ? 1 : 0; | 55 if (geometry.targetRect().isEmpty()) { |
56 } else if (!geometry.doesIntersect()) { | 56 newVisibleRatio = 1; |
| 57 } else { |
| 58 float intersectionArea = |
| 59 geometry.intersectionRect().size().width().toFloat() * |
| 60 geometry.intersectionRect().size().height().toFloat(); |
| 61 float targetArea = geometry.targetRect().size().width().toFloat() * |
| 62 geometry.targetRect().size().height().toFloat(); |
| 63 newVisibleRatio = intersectionArea / targetArea; |
| 64 } |
| 65 newThresholdIndex = observer().firstThresholdGreaterThan(newVisibleRatio); |
| 66 } else { |
| 67 newVisibleRatio = 0; |
57 newThresholdIndex = 0; | 68 newThresholdIndex = 0; |
58 } else { | |
59 float intersectionArea = | |
60 geometry.intersectionRect().size().width().toFloat() * | |
61 geometry.intersectionRect().size().height().toFloat(); | |
62 float targetArea = geometry.targetRect().size().width().toFloat() * | |
63 geometry.targetRect().size().height().toFloat(); | |
64 newVisibleRatio = intersectionArea / targetArea; | |
65 newThresholdIndex = observer().firstThresholdGreaterThan(newVisibleRatio); | |
66 } | 69 } |
67 if (m_lastThresholdIndex != newThresholdIndex) { | 70 if (m_lastThresholdIndex != newThresholdIndex) { |
68 IntRect snappedRootBounds = geometry.rootIntRect(); | 71 IntRect snappedRootBounds = geometry.rootIntRect(); |
69 IntRect* rootBoundsPointer = | 72 IntRect* rootBoundsPointer = |
70 m_shouldReportRootBounds ? &snappedRootBounds : nullptr; | 73 m_shouldReportRootBounds ? &snappedRootBounds : nullptr; |
71 IntersectionObserverEntry* newEntry = new IntersectionObserverEntry( | 74 IntersectionObserverEntry* newEntry = new IntersectionObserverEntry( |
72 timestamp, newVisibleRatio, geometry.targetIntRect(), rootBoundsPointer, | 75 timestamp, newVisibleRatio, geometry.targetIntRect(), rootBoundsPointer, |
73 geometry.intersectionIntRect(), target()); | 76 geometry.intersectionIntRect(), target()); |
74 observer().enqueueIntersectionObserverEntry(*newEntry); | 77 observer().enqueueIntersectionObserverEntry(*newEntry); |
75 setLastThresholdIndex(newThresholdIndex); | 78 setLastThresholdIndex(newThresholdIndex); |
(...skipping 11 matching lines...) Expand all Loading... |
87 target()->ensureIntersectionObserverData().removeObservation(observer()); | 90 target()->ensureIntersectionObserverData().removeObservation(observer()); |
88 m_observer.clear(); | 91 m_observer.clear(); |
89 } | 92 } |
90 | 93 |
91 DEFINE_TRACE(IntersectionObservation) { | 94 DEFINE_TRACE(IntersectionObservation) { |
92 visitor->trace(m_observer); | 95 visitor->trace(m_observer); |
93 visitor->trace(m_target); | 96 visitor->trace(m_target); |
94 } | 97 } |
95 | 98 |
96 } // namespace blink | 99 } // namespace blink |
OLD | NEW |