KeyframeUtils.h (4090B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_KeyframeUtils_h 8 #define mozilla_KeyframeUtils_h 9 10 #include "NonCustomCSSPropertyId.h" 11 #include "js/RootingAPI.h" // For JS::Handle 12 #include "mozilla/KeyframeEffectParams.h" // For CompositeOperation 13 #include "nsTArrayForwardDeclare.h" // For nsTArray 14 15 struct JSContext; 16 class JSObject; 17 18 namespace mozilla { 19 struct AnimationProperty; 20 class ComputedStyle; 21 struct CSSPropertyId; 22 23 enum class PseudoStyleType : uint8_t; 24 class ErrorResult; 25 struct Keyframe; 26 struct PropertyStyleAnimationValuePair; 27 28 namespace dom { 29 class Document; 30 class Element; 31 } // namespace dom 32 } // namespace mozilla 33 34 namespace mozilla { 35 36 // Represents the set of property-value pairs on a Keyframe converted to 37 // computed values. 38 using ComputedKeyframeValues = nsTArray<PropertyStyleAnimationValuePair>; 39 40 /** 41 * Utility methods for processing keyframes. 42 */ 43 class KeyframeUtils { 44 public: 45 /** 46 * Converts a JS value representing a property-indexed keyframe or a sequence 47 * of keyframes to an array of Keyframe objects. 48 * 49 * @param aCx The JSContext that corresponds to |aFrames|. 50 * @param aDocument The document to use when parsing CSS properties. 51 * @param aFrames The JS value, provided as an optional IDL |object?| value, 52 * that is the keyframe list specification. 53 * @param aContext Information about who is trying to get keyframes from the 54 * object, for use in error reporting. This must be be a non-null 55 * pointer representing a null-terminated ASCII string. 56 * @param aRv (out) Out-param to hold any error returned by this function. 57 * Must be initially empty. 58 * @return The set of processed keyframes. If an error occurs, aRv will be 59 * filled-in with the appropriate error code and an empty array will be 60 * returned. 61 */ 62 static nsTArray<Keyframe> GetKeyframesFromObject( 63 JSContext* aCx, dom::Document* aDocument, JS::Handle<JSObject*> aFrames, 64 const char* aContext, ErrorResult& aRv); 65 66 /** 67 * Calculate the computed offset of keyframes by evenly distributing keyframes 68 * with a missing offset. 69 * 70 * @see 71 * https://drafts.csswg.org/web-animations/#calculating-computed-keyframes 72 * 73 * @param aKeyframes The set of keyframes to adjust. 74 */ 75 static void DistributeKeyframes(nsTArray<Keyframe>& aKeyframes); 76 77 /** 78 * Converts an array of Keyframe objects into an array of AnimationProperty 79 * objects. This involves creating an array of computed values for each 80 * longhand property and determining the offset and timing function to use 81 * for each value. 82 * 83 * @param aKeyframes The input keyframes. 84 * @param aElement The context element. 85 * @param aStyle The computed style values. 86 * @param aEffectComposite The composite operation specified on the effect. 87 * For any keyframes in |aKeyframes| that do not specify a composite 88 * operation, this value will be used. 89 * @return The set of animation properties. If an error occurs, the returned 90 * array will be empty. 91 */ 92 static nsTArray<AnimationProperty> GetAnimationPropertiesFromKeyframes( 93 const nsTArray<Keyframe>& aKeyframes, dom::Element* aElement, 94 const PseudoStyleRequest& aPseudoRequest, const ComputedStyle* aStyle, 95 dom::CompositeOperation aEffectComposite); 96 97 /** 98 * Check if the property or, for shorthands, one or more of 99 * its subproperties, is animatable. 100 * 101 * @param aProperty The property to check. 102 * @param aBackend The style backend, Servo or Gecko, that should determine 103 * if the property is animatable or not. 104 * @return true if |aProperty| is animatable. 105 */ 106 static bool IsAnimatableProperty(const CSSPropertyId& aProperty); 107 }; 108 109 } // namespace mozilla 110 111 #endif // mozilla_KeyframeUtils_h