DEV Community

Cover image for Adding a boomerang effect to our Spyrograph animations
Chris Greening
Chris Greening

Posted on • Originally published at chris-greening.github.io

Adding a boomerang effect to our Spyrograph animations

DISCLAIMER: This blog post was written by a human with the help of AI

Introduction

Spirographs are captivating geometric designs created by rolling a circle inside or outside another circle. These mesmerizing patterns have fascinated artists, mathematicians, and enthusiasts alike for centuries. With the power of Python and the spyrograph library, we can create complex spirograph animations that visualize the evolution of these intricate patterns

In this blog post, we will explore the boomerang effect, a newly introduced feature in the spyrograph library, which allows us to create mesmerizing animations that play forward and then in reverse. We'll provide examples and explanations on how to harness this feature to create captivating spirograph animations

Table of contents

GitHub logo chris-greening / spyrograph

Python library for analyzing, exploring, and visualizing epitrochoids and hypotrochoids in just a few lines of code

spyrograph: elegant mathematics and geometries

Animation of three geometric, symmetrical shapes being drawn next to one another left to right. The shape on the left is red, the middle green, and the right is blue.

What is it?

spyrograph is a lightweight Python package that provides an expressive and flexible set of tools for drawing beautiful mathematically driven art. With just a few lines of easy-to-read code you can start analyzing, visualizing, and exploring elegant mathematics

Downloads Issues License Version Documentation Status

"Buy Me A Coffee"

Official website

Official docs

Table of Contents


πŸ”‘ Key features

  • Expressive and consistent syntax
  • Robust underlying mathematics
  • Beginner and expert friendly
  • numpy is the only required third-party installation
  • Clear visualizations and animations
  • Flexible to a wide range of usecases
  • Lightweight, just plug and play

Sample hypotrochoid drawing showing a circle rolling around the interior of another circle drawing a geometric shape


πŸ’» Installation

pip

Install the latest stable release from PyPI using

$ pip3 install spyrograph
Enter fullscreen mode Exit fullscreen mode

or clone the development version from GitHub with

$ git clone https://github.com/chris-greening/spyrograph.git
Enter fullscreen mode Exit fullscreen mode

🌱 Quickstart

spyrograph is designed to be expressive and easy-to-use - simply import spyrograph and jump right into drawing elegant, complex shapes…





The boomerang effect

The boomerang effect is achieved by playing the animation sequence in reverse after it reaches the end, creating a back-and-forth effect. This feature is particularly useful when you want to visualize the evolution of a spirograph pattern with changing parameters and then observe how it returns to its initial state

An animation of a white tracing of a star that is rotating back and forth on a black background

To enable the boomerang effect, we need to set boomerang=True when calling the animate method of the spyrograph library. In the following sections, we will demonstrate how to use this feature with various examples.


Example 1: Basic boomerang animation

In this example, we will create a simple spirograph animation with the boomerang effect. We will use the Hypotrochoid class from the spyrograph library to generate our animation

from spyrograph import Hypotrochoid
import numpy as np

# Animate a hypotrochoid by incrementally
# changing the rolling radius r
Hypotrochoid.animate(
    R=200,
    r=np.arange(113.75, 114.25, .01),
    d=133,
    thetas=np.arange(0,100, .05),
    frame_pause=.01,
    boomerang=True,
)
Enter fullscreen mode Exit fullscreen mode

An animation of a tracing of a star that is rotating back and forth

This example generates a Hypotrochoid animation with varying r values while keeping R and d the same. After reaching the end of the sequence, the animation plays in reverse, creating a boomerang effect.


Example 2: Combining boomerang with looping

The boomerang effect can also be combined with looping, allowing the animation to play continuously. To achieve this, set both the boomerang and repeat arguments to True in the animate method

from spyrograph import Hypotrochoid
import numpy as np

# Repeating animation of a hypotrochoid by
# incrementally changing the rolling radius r
Hypotrochoid.animate(
    R=200,
    r=np.arange(113.75, 114.25, .01),
    d=133,
    thetas=np.arange(0,100, .05),
    boomerang=True,
    repeat=True
)
Enter fullscreen mode Exit fullscreen mode

With these settings, the animation will loop indefinitely, playing forwards and then in reverse, creating an entrancing visual experience


Example 3: boomerang effect with custom configurations

The boomerang effect can be combined with other custom configurations, such as changing the:

  • background color
  • line color
  • or line width.

In this example, we will create an animation with a black background, a neon green tracing line, and a thicker line width

from spyrograph import Hypotrochoid
import numpy as np

# Neon green hypotrochoid boomerang
# animation on black background
Hypotrochoid.animate(
    R=200,
    r=np.arange(113.75, 114.25, .01),
    d=133,
    thetas=np.arange(0,100, .05),
    frame_pause=.01,
    repeat=True,
    boomerang=True,
    screen_color="black",
    color="#39FF14",
    width=3
)
Enter fullscreen mode Exit fullscreen mode

An animation of a neon green tracing of a star that is rotating back and forth on a black background

This animation will feature a striking contrast between the neon green spirograph patterns and the black background. The boomerang effect adds another layer of visual interest to this already captivating animation

Conclusion

The boomerang effect is a powerful addition to the spyrograph library, allowing us to create captivating spirograph animations that play forwards and in reverse. By combining this feature with various parameter configurations and customization options, we can create a wide array of mesmerizing spirograph animations

With a basic understanding of the boomerang effect and the examples provided in this blog post, we are well-equipped to explore the endless possibilities of spirograph animations. Whether you are an artist, mathematician, or a spirograph enthusiast, the boomerang effect is sure to add a unique touch to your spirograph creations

Top comments (0)