Hone logo
Hone
Problems

Angular Animation Keyframes: Building a Dynamic Pulse Effect

This challenge focuses on implementing a common UI animation pattern in Angular: a pulsing effect. You'll learn how to leverage Angular's animation module and keyframes to create a smooth, repeating visual feedback for elements on the screen, enhancing user experience.

Problem Description

Your task is to create an Angular component that displays a circular "dot" which repeatedly pulses in size and opacity. This pulsing effect should be implemented using Angular's native animation capabilities, specifically with keyframes.

Key Requirements:

  1. Component Structure: Create a standalone Angular component.
  2. Visual Element: The component should render a simple, visually distinct element (e.g., a div) that will be the target of the animation.
  3. Keyframe Animation: Implement a keyframe animation that modifies:
    • transform: scale(): The dot should grow and shrink.
    • opacity: The dot should fade in and out.
  4. Repetition: The animation should repeat indefinitely.
  5. Triggering: The animation should start automatically when the component is initialized.

Expected Behavior:

When the component is rendered, the circular dot will smoothly scale from its original size to a larger size and then back to its original size, while simultaneously fading from a visible opacity to a more transparent state and back. This cycle will repeat continuously.

Edge Cases to Consider:

  • Component Removal: Ensure the animation cleans up properly when the component is destroyed to avoid memory leaks.
  • Browser Compatibility: While Angular's animation module generally handles this well, be mindful of standard CSS properties used in the keyframes.

Examples

Example 1:

Input: No explicit input required for the component. The animation is self-contained.

Output:
A circular `div` element displayed on the screen that exhibits a continuous pulsing effect:
- Scales up from 100% to 120% of its original size and back.
- Fades from 100% opacity to 50% opacity and back.
- The animation runs indefinitely.

Explanation:
The component will render a `div`. This `div` will have an Angular animation defined using keyframes. The keyframes will specify changes to the `transform: scale()` and `opacity` CSS properties at different points in the animation cycle (e.g., at 0%, 50%, and 100% of the animation duration).

Example 2:

Input: None.

Output:
The same pulsing effect as Example 1, but with a slightly different timing. The animation duration is halved, making the pulse appear faster.

Explanation:
This demonstrates that the speed of the animation can be controlled by adjusting the `duration` property within the animation definition.

Constraints

  • Animation Duration: The base duration for one full pulse cycle should be 1 second (1000ms).
  • Scaling Factor: The scaling should range from 1 (100%) to 1.2 (120%).
  • Opacity Range: The opacity should range from 1 (100%) to 0.5 (50%).
  • Repetitions: The animation must repeat infinitely.
  • Dependencies: You should use Angular's built-in animation module (@angular/animations).

Notes

  • You will need to import and use the trigger, state, style, animate, and keyframes functions from @angular/animations.
  • Consider using CSS variables or input properties if you wanted to make the scaling factor or duration configurable later, though this is not a requirement for this challenge.
  • The animation should be applied directly to the element using the @myAnimationName syntax in your component's template.
  • Think about the different steps (keyframes) needed to achieve a smooth "grow, shrink, fade in, fade out" effect.
Loading editor...
typescript