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

Side by Side 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 unified diff | Download patch
OLDNEW
(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 {
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698