Of course! Here is a self-contained, animated SVG that shows a hexagon rotating clockwise with a ball inside that realistically tumbles and falls due to gravity.
You can copy and paste this code directly into an HTML file and open it in your browser to see the live animation.
Live Demo & Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rotating Hexagon with Ball</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #f0f0f0;
margin: 0;
}
svg {
width: 80%;
max-width: 400px;
overflow: visible; /* Helps see elements if they go slightly out of bounds */
}
</style>
</head>
<body>
<!-- Start of SVG Animation -->
<svg viewBox="-120 -120 240 240" xmlns="http://www.w3.org/2000/svg">
<!--
This is the hexagon container.
It has a simple rotation animation that turns it 360 degrees clockwise over 12 seconds.
-->
<g id="hexagon-group">
<polygon points="100,0 50,-86.6 -50,-86.6 -100,0 -50,86.6 50,86.6"
fill="none" stroke="slategray" stroke-width="5" stroke-linejoin="round">
<animateTransform
attributeName="transform"
attributeType="XML"
type="rotate"
from="0 0 0"
to="360 0 0"
dur="12s"
repeatCount="indefinite"
/>
</polygon>
</g>
<!--
This is the ball.
Its movement is the most complex part. We use <animateMotion> to define its path.
- The 'path' attribute defines the route the ball's center will take. It's a series of arcs
that simulate rolling along one face and then quickly tumbling to the next.
- 'keyPoints' and 'keyTimes' are used to control the speed. The ball spends most of its
time rolling slowly on a face and then moves very quickly during the tumble.
Notice the small time gaps (e.g., from 0.15 to 0.166) which create the fast "fall".
-->
<circle id="ball" r="15" fill="crimson">
<animateMotion
dur="12s"
repeatCount="indefinite"
path="M 0,71
A 71 71 0 0 1 -61.5,35.5
A 71 71 0 0 1 -61.5,-35.5
A 71 71 0 0 1 0,-71
A 71 71 0 0 1 61.5,-35.5
A 71 71 0 0 1 61.5,35.5
A 71 71 0 0 1 0,71 Z"
keyPoints="0; 0.15; 0.166; 0.316; 0.333; 0.483; 0.5; 0.65; 0.666; 0.816; 0.833; 0.983; 1"
keyTimes="0; 0.15; 0.166; 0.316; 0.333; 0.483; 0.5; 0.65; 0.666; 0.816; 0.833; 0.983; 1"
calcMode="linear"
/>
</circle>
</svg>
<!-- End of SVG Animation -->
</body>
</html>
How it Works
-
SVG ViewBox: The <svg viewBox="-120 -120 240 240"> creates a coordinate system with the origin (0,0) at the very center. This makes rotation calculations much simpler.
-
The Hexagon:
- A
<polygon> element is used to draw the hexagon.
- The
<animateTransform> tag inside it is the key to its movement.
type="rotate" specifies the kind of animation.
from="0 0 0" and to="360 0 0" define a full 360-degree rotation around the center point (0,0).
dur="12s" sets the duration for one full rotation to 12 seconds.
repeatCount="indefinite" makes the animation loop forever.
-
The Ball (Physics Simulation):
- This is a classic case of "faking" physics to look realistic. A true physics engine is not used; instead, the ball's path is pre-calculated.
- The
<animateMotion> tag moves the ball along a specific SVG <path>.
path: The path is a series of six arcs (A) that form a smaller, rounded hexagon inside the main one. This is the track the center of the ball follows.
keyTimes and keyPoints: This is the secret to the "gravity" effect. These attributes control the animation's pacing.
keyTimes is a list of timestamps (from 0.0 to 1.0) marking progress through the animation.
keyPoints maps these timestamps to specific points along the defined path.
- We make the time between "resting on a flat surface" and "hitting the next surface" very short (e.g., the jump from a time of
0.15 to 0.166). This tiny duration forces the ball to travel a large distance on its path very quickly, creating the illusion of it falling or tumbling. The longer durations in between represent the ball rolling slowly along a face as it's carried upwards by the rotation.