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

Unified Diff: third_party/WebKit/Source/core/dom/IntersectionObservation.cpp

Issue 1449623002: IntersectionObserver: second cut. (Closed) Base URL: https://chromium.googlesource.com/chromium/src@master
Patch Set: Refactor clipping code Created 5 years 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 side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/core/dom/IntersectionObservation.cpp
diff --git a/third_party/WebKit/Source/core/dom/IntersectionObservation.cpp b/third_party/WebKit/Source/core/dom/IntersectionObservation.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..51d0cab7d5b3c30de217100bdfbe3b485801e176
--- /dev/null
+++ b/third_party/WebKit/Source/core/dom/IntersectionObservation.cpp
@@ -0,0 +1,64 @@
+// Copyright 2015 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "config.h"
+#include "core/dom/IntersectionObservation.h"
+
+#include "core/dom/IntersectionObserver.h"
+#include "core/layout/LayoutObject.h"
+
+namespace blink {
+
+IntersectionObservation::IntersectionObservation(IntersectionObserver& observer, Element* target)
+ : m_observer(observer)
+ , m_target(target->createWeakPtr())
+ , m_active(true)
+ , m_lastVisibleRatio(0)
+{
+ target->addIntersectionObservation(*this);
+}
+
+void IntersectionObservation::computeIntersectionObservations(int timestamp)
+{
+ // TODO: rootMargin is not yet supported; need to clarify how it interacts with
+ // ancestor clipping (https://github.com/slightlyoff/IntersectionObserver/issues/70)
+ Element* targetElement = target();
+ if (!targetElement || !isActive())
+ return;
+ Element* rootElement = observer()->root();
+ LayoutObject* targetLayoutObject = targetElement->layoutObject();
+ // TODO: Support SVG
+ if (!targetLayoutObject || (!targetLayoutObject->isBox() && !targetLayoutObject->isInline()))
+ return;
+
+ LayoutRect targetRect, rootRect, intersectionRect;
+ if (!targetElement->computeIntersection(rootElement, targetRect, rootRect, intersectionRect))
+ return;
+
+ float intersectionArea = intersectionRect.size().width().toFloat() * intersectionRect.size().height().toFloat();
+ float targetArea = targetRect.size().width().toFloat() * targetRect.size().height().toFloat();
+ if (!targetArea)
+ return;
+ float newVisibleRatio = intersectionArea / targetArea;
+ size_t newThresholdIndex = observer()->firstThresholdGreaterThan(newVisibleRatio);
+ size_t oldThresholdIndex = observer()->firstThresholdGreaterThan(lastVisibleRatio());
+ setLastVisibleRatio(newVisibleRatio);
+ if (oldThresholdIndex != newThresholdIndex)
+ observer()->enqueueIntersectionObserverEntry(new IntersectionObserverEntry(timestamp / 1000., pixelSnappedIntRect(targetRect), pixelSnappedIntRect(rootRect), pixelSnappedIntRect(intersectionRect), targetElement));
+}
+
+void IntersectionObservation::disconnect()
+{
+ observer()->disconnect(*this);
+ if (m_target)
+ m_target->removeIntersectionObservation(*this);
+}
+
+DEFINE_TRACE(IntersectionObservation)
+{
+ visitor->trace(m_observer);
+ visitor->trace(m_target);
+}
+
+} // namespace blink {

Powered by Google App Engine
This is Rietveld 408576698