Animated Box in Vue with TypeScript
This challenge focuses on implementing a simple animation in a Vue.js application using TypeScript. You'll create a component that displays a box and animates its position (both horizontally and vertically) over time, demonstrating basic Vue reactivity and animation principles. This is a fundamental skill for creating dynamic and engaging user interfaces.
Problem Description
You are tasked with creating a Vue component called AnimatedBox.vue that displays a square box. The box should initially be positioned at the top-left corner of its container. Upon mounting, the box should smoothly animate its position to the bottom-right corner of the container over a period of 5 seconds. The animation should be continuous, looping back to the top-left corner after reaching the bottom-right corner and repeating. The box should have a background color of lightblue and a width and height of 100px.
Key Requirements:
- Component Structure: Create a functional component named
AnimatedBox. - Reactivity: Use Vue's reactivity system to manage the box's position (x and y coordinates).
- Animation: Implement the animation using
setIntervalorsetTimeoutto update the position values at regular intervals. Consider usingrequestAnimationFramefor smoother animations. - Looping: The animation should loop continuously.
- Styling: The box should be styled with a lightblue background, a width of 100px, and a height of 100px.
- TypeScript: The component must be written in TypeScript.
Expected Behavior:
- When the component mounts, the box should start at coordinates (0, 0).
- The box should smoothly move to coordinates (containerWidth - 100, containerHeight - 100) over 5 seconds.
- After reaching the bottom-right corner, the box should smoothly return to the top-left corner over another 5 seconds.
- This movement should repeat indefinitely.
- The box should always be visible and within the bounds of its container.
Edge Cases to Consider:
- Container Size: The component should adapt to different container sizes. Assume the container's width and height are available as props.
- Animation Speed: The animation speed should be consistent regardless of browser performance.
- Component Unmounting: The animation should stop when the component is unmounted to prevent memory leaks.
Examples
Example 1:
Input: containerWidth = 500, containerHeight = 300
Output: A lightblue square (100x100) animating from (0,0) to (400,200) and back, continuously.
Explanation: The box moves across the container, respecting the container's dimensions.
Example 2:
Input: containerWidth = 200, containerHeight = 150
Output: A lightblue square (100x100) animating from (0,0) to (100,50) and back, continuously.
Explanation: The box moves within a smaller container.
Constraints
- Animation Duration: The animation from top-left to bottom-right should take 5 seconds.
- Container Dimensions: The container width and height will be provided as props to the component.
- Box Size: The box should always be 100px x 100px.
- Performance: The animation should be reasonably smooth and not cause noticeable performance issues on typical devices. Avoid excessively complex calculations within the animation loop.
- TypeScript: Strict type checking is expected.
Notes
- Consider using
requestAnimationFramefor smoother animations compared tosetIntervalorsetTimeout. - You'll need to manage the state of the animation (e.g., current position, animation direction) within the component.
- Remember to clean up the animation interval/timeout when the component is unmounted to prevent memory leaks.
- The container's width and height are assumed to be available as props. You don't need to handle getting these values from the DOM directly.
- Focus on the core animation logic; you don't need to implement complex UI elements or interactions beyond the box's movement.