# JavaScript Spirograph

Remember using Spirograph as a child being amazed by the patterns that seemed to magically appear as the pen moved? Let's look at geometry behind the magic by drawing a Spirograph pattern on a canvas element using JavaScript.

## Drawing a Circle

If you remember high school trigonometry, you know the parametric equation to draw a circle. If not, here is it:

x = cx + radius × cos(θ) y = cy + radius × sin(θ)

Plugging in θ ("theta") values from 0 up to 2π will give you the x and y coordinates for all the points along the circumference of the circle. The values cx and cy represent the center point of our circle. If you’re like me, it’s much easier to understand this with a visual demonstration.

## Multiple Circles

Let’s add a second circle. We’ll center this circle on the circumference of our first circle. After calculating a point on the first circle, we’ll use this as the center point for the next circle.

The result? A larger circle—not very exciting.

## Give It Some Teeth

In the physical world, Spirograph circles are gears connected by little teeth. The gears have differing number of teeth, which causes them to spin at different rates. This is where the magic comes from. In our JavaScript world, we can simulate this by adjusting theta for the second circle by a ratio.

x = cx + radius1 × cos(θ) + radius2 × cos(θ × ratio) y = cy + radius1 × sin(θ) + radius2 × cos(θ × ratio)

## Source Code

``````function drawSpirograph(context, cx, cy, radius1, radius2, ratio) {
var x, y, theta;

// Move to starting point (theta = 0)
context.beginPath();

// Draw segments from theta = 0 to theta = 2PI
for (theta = 0; theta <= Math.PI * 2; theta += 0.01) {
x = cx + radius1 * Math.cos(theta) + radius2 * Math.cos(theta * ratio);
y = cy + radius1 * Math.sin(theta) + radius2 * Math.sin(theta * ratio);
context.lineTo(x, y);
}

// Apply stroke
context.stroke();
}

// Get drawing context
var canvas = document.getElementById('myCanvas');
var context = canvas.getContext('2d');

// Draw spirograph
drawSpirograph(context, canvas.width / 2, canvas.height / 2, 100, 20, 10);``````

# Keywords

• B-Splines 1
• Bezier 1
• Canvas 1 2
• Curves 1
• Drupal 1
• Gulp 1
• JavaScript 1 2
• PHP 1
• Xdebug 1
{"ft_lang_label":"__label__en","ft_lang_prob":0.74475497,"math_prob":0.9863394,"size":2185,"snap":"2019-26-2019-30","text_gpt3_token_len":572,"char_repetition_ratio":0.15451628,"word_repetition_ratio":0.08831169,"special_character_ratio":0.28375286,"punctuation_ratio":0.14651163,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.998212,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2019-07-15T21:54:14Z\",\"WARC-Record-ID\":\"<urn:uuid:678faf39-5c66-437e-9a5a-189f4c2a1be1>\",\"Content-Length\":\"22977\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:692a369f-4189-4872-9f63-4f27a8202d49>\",\"WARC-Concurrent-To\":\"<urn:uuid:6aed4a74-e8ac-4000-b60d-d6f81ba4a866>\",\"WARC-IP-Address\":\"104.130.69.124\",\"WARC-Target-URI\":\"https://www.maissan.net/\",\"WARC-Payload-Digest\":\"sha1:UFPFPYN6EWLRWPE34WNTFJXIAQGSFDYQ\",\"WARC-Block-Digest\":\"sha1:DOHTHWIRRNOKP2XX4Z5MC7Z2COYXIAOB\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2019/CC-MAIN-2019-30/CC-MAIN-2019-30_segments_1563195524254.28_warc_CC-MAIN-20190715215144-20190716001144-00181.warc.gz\"}"}