Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource


Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.


                  <h1>D3 "Hand Drawn" Circles</h1>
  <svg class="inset" width='100' height='100' viewBox="-0.5 -0.5 2 2">
    <path class='pencil' d="M0,1 C0.552,1 1,0.552 1,0"/>
    <circle cx="0" cy="1" r="0.04"/>
    <circle cx="0.552" cy="1" r="0.04"/>
    <circle cx="1" cy=".552" r="0.04"/>
    <circle cx="1" cy="0" r="0.04"/>  
  <p>I started from this <a href="">nice approximation of a perfect circle</a> based on 
    cubic B&eacute;zier segments, where they show that a good approximation 
    to a quarter circle of unit radius is a cubic B&eacute;zier curve with control points
    <em>P<sub>0</sub></em> = (0, 1), <em>P<sub>1</sub></em> = (<em>c</em>, 1), 
    <em>P<sub>2</sub></em> = (1, <em>c</em>), <em>P<sub>3</sub></em> = (1, 0) where c = 0.551915024494.  
    For example we can use this SVG, with the control points shown in red: <code>&lt;path d="M0,1 C0.552,1 1,0.552 1,0"/></code>
  <svg class="inset" width='100' height='100' viewBox="-1.5 -1.5 3 3">
    <path class='pencil' d="M0,1 C0.552,1 1,0.552 1,0 S0.552,-1 0,-1 S-1,-0.552 -1,0 S-0.552,1 0,1"/>
    <circle cx="0" cy="1" r="0.06"/>
    <circle cx="1" cy="0" r="0.06"/>
    <circle cx="0" cy="-1" r="0.06"/>
    <circle cx="-1" cy="0" r="0.06"/>
    To draw a full circle, we just chain four of these together, using a path like this:
    <code>&lt;path d="M0,1 C0.552,1 1,0.552 1,0 S0.552,-1 0,-1 S-1,-0.552 -1,0 S-0.552,1 0,1"/></code>
    To make an approximate circle, it's more convenient to work in polar coordinates, 
    where we represent points using a radius and angle, (<em>r, &theta;</em>), 
    relative to the origin. 
    So the four control points for each arc, in polar coordinates, become
    <em>P<sub>0</sub></em> = (1, 0), <em>P<sub>1</sub></em> = (<em>d, &beta;</em>), 
    <em>P<sub>2</sub></em> = (<em>d, &pi;</em>/2 - <em>&beta;</em>), 
    <em>P<sub>3</sub></em> = (1, <em>&pi;</em>/2),
    where <em>&beta;</em> = tan<sup>-1</sup>(<em>c</em>) and 
    <em>d</em> = &radic; (<em>c</em><sup>2</sup> + 1).
    Now we just need to add a bit of randomness as we generate the path.
    I decided to do this in four ways:
      <li>Add a random delta to the current radius (starting from <em>r</em> = 1) 
      when we generate the start of each quarter 
      (along with its corresponding control points).
      If we bias the change to be slightly positive (or negative), the circle ends up
      looking more like a spiral.
      <li>Start the path at a random angle.</li>
      <li>Add a random delta to the angle we rotate for each quarter, instead of
      exactly <em>&pi;</em>/2.  If we bias the change to be slightly positive
      (or negative) then the circle tends to overshoot (undershoot) the starting point.
      <li>Squash the circle using a non-uniform scaling and random rotation.
      If we bias the scaling to be more or less than one we can make the circles tend to
      fit inside a fixed outline for example. 
    If we plan to draw lots of circles, we might want to choose the random variations 
    from a limited range so they all look like the same "handwriting".  Try experimenting:
    <p>Radius variation (0: none, &lt;0: shrink, &gt;0: grow)
      <span class='inputs'>
        <label for="drmin">min</label><input name='drmin' id='drmin' value='-0.1'>
        <label for="drmax">max</label><input name='drmax' id='drmax' value='0'>
    <p>Starting angle (0-360):
      <span class='inputs'>
        <label for="amin">min</label><input name='amin' id='amin' value='200'>
        <label for="amax">max</label><input name='amax' id='amax' value='240'>
    <p>Rotation variation (0: none, &lt;0: undershoot, &gt;0: overshoot)
      <span class='inputs'>
        <label for="damin">min</label><input name='damin' id='damin' value='0'>
        <label for="damax">max</label><input name='damax' id='damax' value='0.2'>
    <p>Squash factor (1: none, &lt;1: shrink, &gt;1: grow)
      <span class='inputs'>
        <label for="smin">min</label><input name='smin' id='smin' value='0.6'>
        <label for="smax">max</label><input name='smax' id='smax' value='0.8'>
    <p>Squash orientation (0-360)
      <span class='inputs'>
        <label for="samin">min</label><input name='samin' id='samin' value='30'>
        <label for="samax">max</label><input name='samax' id='samax' value='50'>
    <note>Using very high values results in some neat abstrations.</note>
    <div id="gallery"></div>


                @import url(,700|Open+Sans:600,400,300,700);
@import url(;
body {
  font-size: 1em;
  font-family: "Open Sans Condensed";
  margin: 0 20vmin auto;
body, svg {
    background: GhostWhite;
h1 {
    font-family: Paytone One;
    font-size: 3em;
    color: LightSlateGray;
    padding: 0 0.25em 0;
note {
  font-weight: bold;
  color: Tomato;
input {
    width: 6em;
span.inputs {
    float: right;
svg.inset {
    float: left;
    margin-right: 20px;
#perturb0 {
    float: right;
    margin-left: 20px;
div.svg-group {
    text-align: center;
    margin: auto;
.svg-group svg {
    margin: 10px;
circle {
  stroke: none;
  fill: Tomato;
path.pencil {
  stroke-width: 2px;
  stroke: LightSlateGray;
  stroke-linecap: round;
  fill: none;
  vector-effect: non-scaling-stroke;
circle.reference {
  stroke: LightGray;
  stroke-width: 1px;
  vector-effect: non-scaling-stroke;
  fill: none;
input {
  padding: .5em;


                function circlePath(δr_min,δr_max, θ0_min, θ0_max, δθ_min,δθ_max) {
    var c = 0.551915024494,
        β = Math.atan(c),
        d = Math.sqrt(c*c+1*1),
        r = 1,
        θ = (θ0_min + Math.random()*(θ0_max - θ0_min))*Math.PI/180,
        path = 'M';
    path += [r * Math.sin(θ), r * Math.cos(θ)];
    path += ' C' + [d * r * Math.sin(θ + β), d * r * Math.cos(θ + β)];
    for (var i=0; i<4; i++) {
        θ += Math.PI/2 * (1 + δθ_min + Math.random()*(δθ_max - δθ_min));
        r *= (1 + δr_min + Math.random()*(δr_max - δr_min));
        path += ' ' + (i?'S':'') + [d * r * Math.sin(θ - β), d * r * Math.cos(θ - β)];
        path += ' ' + [r * Math.sin(θ), r * Math.cos(θ)];
    return path;
function circleXform(λ_min, λ_max, θ_min, θ_max) {
    var θ = (θ_min + Math.random()*(θ_max - θ_min));
    return 'rotate(' + θ + ') '
        + 'scale(1, ' + (λ_min + Math.random()*(λ_max - λ_min)) + ')'
        + 'rotate(' + (-θ) + ')';

// D3'#gallery')
    .data(function(d) { return d3.range(5); })
    .attr('width', 100)
    .attr('height', 100)
    .attr('viewBox', "-1.5 -1.5 3 3");

function drawCircles() {
    $('#gallery svg').html('');
    var svg = d3.selectAll('#gallery svg');
        .classed('pencil', true)
        .attr('d', function() { return circlePath(
            +$('#drmin').val(), +$('#drmax').val(),
            +$('#amin').val(), +$('#amax').val(), 
            +$('#damin').val(), +$('#damax').val()); })
        .attr('transform', function() { return circleXform(
            +$('#smin').val(), +$('#smax').val(), 
            +$('#samin').val(), +$('#samax').val()); });
        .classed('pencil', true)
        .attr('d', circlePath(-0.1,0, 0,0, 0,0));'#perturb2')
        .classed('pencil', true)
        .attr('d', circlePath(-0.1,0, 0,360, 0,0));'#perturb3')
        .classed('pencil', true)
        .attr('d', circlePath(-0.1,0, 0,360, 0,0.2));
       d3.selectAll('#perturb4, #perturb0')
        .classed('pencil', true)
        .attr('d', circlePath(-0.1,0, 0,360, 0,0.2))
        .attr('transform', circleXform(0.6, 0.8, 0, 360));