Imitation of Beijing health treasure animation
Beijing health treasure has an animation, a circle of small balls, rolling clockwise along the square, and the color is gradient, like the picture below
The following key information was extracted:
- The ball rolls continuously clockwise along the rectangle, and the distance between the balls is uniform
- When the ball is at the right angle of the rectangle, it will roll along the right angle, and the transition is smooth
- The color of the ball has some gradients and changes with the position of the ball. The upper left corner is always dark, and the lower right is always light.
border implementation
I wanted to imitate this animation. The first thing I thought of was to use a border. I wanted to use a dotted border to achieve it. I found a similar one.
Main logic:
- Set the border to be dotted
- make 4 borders
- Use animation to move the 4 borders in different directions to simulate continuous scrolling
After careful observation, I found a more serious problem. There is a problem with the transition of the 4 right angles, and there is no way to fix it. Take the upper right corner as an example, the two scrolling borders cannot be perfectly integrated. The ball from left to right and from Top to bottom balls will meet, not the same as the desired left->bottom roll
background scroll
Later, I found another scroll, this time using the background for continuous scrolling, as shown below:
- background defines the direction and color of the 4 edges
- background-repeat defines scrolling only along the edges
- background-size defines the length and width of the sides
- background-position defines the starting point of the edge
- Finally, animate the 4 backgrounds and make them scroll
This gap is large, even the small ball can't be completed, give up
scroll SVG
This is a cow, and it looks almost like this:
- First prepare a svg image that will scroll by yourself
- Find a way to splice the sides of the small svg to the big rectangle
Since the svg scrolls by itself, the scrolling of the corners is very natural and smooth, but later I found that it could not be done:
- How to make a rolling ball svg
- If the same small SVG is made into a big one, the gradient color of the small ball can't be fixed.
SVG animation
Interesting plan:
- Put a rectangle inside the svg
- The rectangle's border is set to a dashed line (stroke-dasharray)
- The gap of the dotted line is set as a semi-circle (round)
- Use css to control the offset to make the border scroll
Current issues are:
- Make a gradient ball
- The right angle needs to be optimized, and the small balls will overlap in the upper left corner.
perfect rolling ball
Next, I removed the rounded corners (rx), and there was a problem. In the upper left corner, the small ball would merge and flicker. After many experiments, I found that my side length and stroke-dasharray need to be well matched to perfectly fit the small ball. The siege is exactly one circle, so that there will be no gathering and flickering at the junction of the head and tail (upper left corner), as shown below:
The color of the ball has also found a way to use linearGradient:
Finally, add CSS animation to achieve continuous scrolling:
I am not familiar with SVG, there is a better way, welcome to discuss
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。