OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "config.h" |
| 6 #include "core/dom/IntersectionObservation.h" |
| 7 |
| 8 #include "core/dom/IntersectionObserver.h" |
| 9 #include "core/layout/LayoutObject.h" |
| 10 |
| 11 namespace blink { |
| 12 |
| 13 IntersectionObservation::IntersectionObservation(IntersectionObserver& observer,
Element* target) |
| 14 : m_observer(observer) |
| 15 , m_target(target->createWeakPtr()) |
| 16 , m_active(true) |
| 17 , m_lastVisibleRatio(0) |
| 18 { |
| 19 target->addIntersectionObservation(*this); |
| 20 } |
| 21 |
| 22 void IntersectionObservation::computeIntersectionObservations(int timestamp) |
| 23 { |
| 24 // TODO: rootMargin is not yet supported; need to clarify how it interacts w
ith |
| 25 // ancestor clipping (https://github.com/slightlyoff/IntersectionObserver/is
sues/70) |
| 26 Element* targetElement = target(); |
| 27 if (!targetElement || !isActive()) |
| 28 return; |
| 29 Element* rootElement = observer()->root(); |
| 30 LayoutObject* targetLayoutObject = targetElement->layoutObject(); |
| 31 // TODO: Support SVG |
| 32 if (!targetLayoutObject || (!targetLayoutObject->isBox() && !targetLayoutObj
ect->isInline())) |
| 33 return; |
| 34 |
| 35 LayoutRect targetRect, rootRect, intersectionRect; |
| 36 if (!targetElement->computeIntersection(rootElement, targetRect, rootRect, i
ntersectionRect)) |
| 37 return; |
| 38 |
| 39 float intersectionArea = intersectionRect.size().width().toFloat() * interse
ctionRect.size().height().toFloat(); |
| 40 float targetArea = targetRect.size().width().toFloat() * targetRect.size().h
eight().toFloat(); |
| 41 if (!targetArea) |
| 42 return; |
| 43 float newVisibleRatio = intersectionArea / targetArea; |
| 44 size_t newThresholdIndex = observer()->firstThresholdGreaterThan(newVisibleR
atio); |
| 45 size_t oldThresholdIndex = observer()->firstThresholdGreaterThan(lastVisible
Ratio()); |
| 46 setLastVisibleRatio(newVisibleRatio); |
| 47 if (oldThresholdIndex != newThresholdIndex) |
| 48 observer()->enqueueIntersectionObserverEntry(new IntersectionObserverEnt
ry(timestamp / 1000., pixelSnappedIntRect(targetRect), pixelSnappedIntRect(rootR
ect), pixelSnappedIntRect(intersectionRect), targetElement)); |
| 49 } |
| 50 |
| 51 void IntersectionObservation::disconnect() |
| 52 { |
| 53 observer()->disconnect(*this); |
| 54 if (m_target) |
| 55 m_target->removeIntersectionObservation(*this); |
| 56 } |
| 57 |
| 58 DEFINE_TRACE(IntersectionObservation) |
| 59 { |
| 60 visitor->trace(m_observer); |
| 61 visitor->trace(m_target); |
| 62 } |
| 63 |
| 64 } // namespace blink { |
OLD | NEW |