1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/core/animation/timeline_range.h"
#include "third_party/blink/renderer/core/animation/timeline_offset.h"
#include "third_party/blink/renderer/core/animation/timing_calculations.h"
#include "third_party/blink/renderer/platform/geometry/layout_unit.h"
#include "third_party/blink/renderer/platform/geometry/length_functions.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
namespace blink {
bool TimelineRange::IsEmpty() const {
return TimingCalculations::LessThanOrEqualToWithinEpsilon(
offsets_.end - offsets_.start, 0.0);
}
double TimelineRange::ToFractionalOffset(
const TimelineOffset& timeline_offset) const {
if (IsEmpty()) {
// This is either a monotonic timeline or an inactive ScrollTimeline.
return 0.0;
}
double full_range_size = offsets_.end - offsets_.start;
ScrollOffsets range(0, 0);
if (view_offsets_ == ViewOffsets()) {
// This is a non-view ScrollTimeline, or it can also be a ViewTimeline
// that happens have subject with size=0.
range = {offsets_.start, offsets_.end};
} else {
range = ConvertNamedRange(timeline_offset.name);
}
DCHECK_GT(full_range_size, 0);
double offset =
range.start + MinimumValueForLength(timeline_offset.offset,
LayoutUnit(range.end - range.start));
return (offset - offsets_.start) / full_range_size;
}
TimelineRange::ScrollOffsets TimelineRange::ConvertNamedRange(
NamedRange named_range) const {
// https://drafts.csswg.org/scroll-animations-1/#view-timelines-ranges
double align_subject_start_view_end = offsets_.start;
double align_subject_end_view_start = offsets_.end;
double align_subject_start_view_start =
align_subject_end_view_start - view_offsets_.exit_crossing_distance;
double align_subject_end_view_end =
align_subject_start_view_end + view_offsets_.entry_crossing_distance;
// TODO(crbug.com/1448294): This needs to account for when the subject (or an
// ancestor) is position: sticky and stuck to the viewport during entry/exit
// or before entry/cover. Currently, we only handle stickiness during the
// "contain" range (see ViewTimeline::CalculateOffsets).
switch (named_range) {
case TimelineOffset::NamedRange::kNone:
case TimelineOffset::NamedRange::kCover:
// Represents the full range of the view progress timeline:
// 0% progress represents the position at which the start border edge of
// the element’s principal box coincides with the end edge of its view
// progress visibility range.
// 100% progress represents the position at which the end border edge of
// the element’s principal box coincides with the start edge of its view
// progress visibility range.
return {align_subject_start_view_end, align_subject_end_view_start};
case TimelineOffset::NamedRange::kContain:
// Represents the range during which the principal box is either fully
// contained by, or fully covers, its view progress visibility range
// within the scrollport.
// 0% progress represents the earlier position at which:
// 1. the start border edge of the element’s principal box coincides
// with the start edge of its view progress visibility range.
// 2. the end border edge of the element’s principal box coincides with
// the end edge of its view progress visibility range.
// 100% progress represents the later position at which:
// 1. the start border edge of the element’s principal box coincides
// with the start edge of its view progress visibility range.
// 2. the end border edge of the element’s principal box coincides with
// the end edge of its view progress visibility range.
return {
std::min(align_subject_start_view_start, align_subject_end_view_end),
std::max(align_subject_start_view_start, align_subject_end_view_end)};
case TimelineOffset::NamedRange::kEntry:
// Represents the range during which the principal box is entering the
// view progress visibility range.
// 0% is equivalent to 0% of the cover range.
// 100% is equivalent to 0% of the contain range.
return {
align_subject_start_view_end,
std::min(align_subject_start_view_start, align_subject_end_view_end)};
case TimelineOffset::NamedRange::kEntryCrossing:
// Represents the range during which the principal box is crossing the
// entry edge of the viewport.
// 0% is equivalent to 0% of the cover range.
return {align_subject_start_view_end, align_subject_end_view_end};
case TimelineOffset::NamedRange::kExit:
// Represents the range during which the principal box is exiting the view
// progress visibility range.
// 0% is equivalent to 100% of the contain range.
// 100% is equivalent to 100% of the cover range.
return {
std::max(align_subject_start_view_start, align_subject_end_view_end),
align_subject_end_view_start};
case TimelineOffset::NamedRange::kExitCrossing:
// Represents the range during which the principal box is exiting the view
// progress visibility range.
// 100% is equivalent to 100% of the cover range.
return {align_subject_start_view_start, align_subject_end_view_start};
case TimelineOffset::NamedRange::kScroll:
DCHECK(RuntimeEnabledFeatures::ScrollTimelineNamedRangeScrollEnabled());
// Represents the full scroll range of the viewport.
return scroll_limits_;
}
}
} // namespace blink
|