Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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.

HTML

              
                    <div class="container">
<svg id="svg" viewbox="0 0 120 120">
  <g id="face" transform='translate(60 60)'>
    <circle
      id="facecircle"
      cx="0"
      cy="0"
      r="50"
      stroke="#000000"
      stroke-width="2"
      fill="#FAD257" />
    <circle
      cx="-20"
      cy="-10"
      r="5"
      fill="#000000" />
    <circle
      cx="20"
      cy="-10"
      r="5"
      fill="#000000" />

    <g id="smile" transform="translate(0, 25)">
      <path
        id="smilepath"
        fill="none"
        stroke="#000000"
        stroke-width="3"
        stroke-linecap="round"
        d="M-20,-10 C-20,10 20,10 20,-10" />
    </g>
  </g>
</svg>
      <div class="slidecontainer">
        <input type="range" min="1" max="100" value="100" class="slider" id="myRange">
      </div>
      <div>
        <p><a href="https://github.com/dwmkerr/svg-smile">github.com/dwmkerr/svg-smile</a></p>
      </div>
    </div>

              
            
!

CSS

              
                      html, body {
        margin          : 0;
        padding         : 0;
        width           : 100%;
        height          : 100%;
        font-family: 'Karla', Arial, Helvetica, sans-serif;
      }

      a:link, a:hover, a:visited, a:active {
        color: #0879bf;
        text-decoration: none;
      }
      a:hover {
        text-decoration: underline;
      } 
      .container {
        height          : 100%;
        display         : flex;
        flex-direction  : column;
        justify-content : center;
        align-items     : center;
      }

      #svg {
        flex            : 1;
      }

              
            
!

JS

              
                function interpolateColor(color1, color2, scale) {
  const r1 = parseInt(color1.substr(1, 2), 16);
  const g1 = parseInt(color1.substr(3, 2), 16);
  const b1 = parseInt(color1.substr(5, 2), 16);
  const r2 = parseInt(color2.substr(1, 2), 16);
  const g2 = parseInt(color2.substr(3, 2), 16);
  const b2 = parseInt(color2.substr(5, 2), 16);

  const r = Math.round(r1 + scale * (r2 - r1));
  const g = Math.round(g1 + scale * (g2 - g1));
  const b = Math.round(b1 + scale * (b2 - b1));

  const hex = val => val.toString(16).padStart(2, '0');

  return `#${hex(r)}${hex(g)}${hex(b)}`;
};

//  Returns the bezier points for a smile. Scale is 0-1.
function smilePoints(scale) {
  const factor = scale * 2 - 1; // i.e sad = -1, happy = +1
  const p1 = { x: -20, y: -10 * factor };
  const c1 = { x: -20, y: 10 * factor };
  const p2 = { x: 20, y: -10 * factor };
  const c2 = { x: 20, y: 10 * factor };
  return [p1, c1, c2, p2];
}

function writeSmilePoints(points) {
  const p = p => `${p.x},${p.y}`; // write a point as a coord
  return `M${p(points[0])} C${p(points[1])} ${p(points[2])} ${p(points[3])}`;
}

function scaleSmile(scale) {
  //  Get the geometry of the new smile.
  const points = writeSmilePoints(smilePoints(scale));
  const svg = document.getElementById('svg');

  //  Create the geometry animation, which will look summat like this:
  //  <animate attributeName="d" attributeType="XML"
  //    to="M-20,10 C-20,-10 20,-10 20,10" dur="3s" repeatCount="indefinite"
  //    fill="freeze" />
  const smilePath = document.getElementById('smilepath');
  const animate = document.createElementNS(svg.namespaceURI, 'animate');
  animate.setAttribute('attributeName','d');
  animate.setAttribute('attributeType','XML');
  animate.setAttribute('to',points);
  animate.setAttribute('dur','0.3s');
  animate.setAttribute('repeatCount','1');
  animate.setAttribute('fill','freeze');
  smilePath.appendChild(animate);
  animate.beginElement();

  //  Animate the face color.
  const faceCircle = document.getElementById('facecircle');
  const a = document.createElementNS(svg.namespaceURI, 'animate');
  a.setAttribute('attributeName', 'fill');
  a.setAttribute('attributeType', 'CSS');
  a.setAttribute('dur', '0.3s');
  a.setAttribute('to', interpolateColor('#FF0000', '#FAD257', scale));
  a.setAttribute('fill', 'freeze');
  faceCircle.appendChild(a);
  a.beginElement();
}

function onChange() {
  //  Get the slider value, scale it from percentage to 0-1.
  const val = this.value;
  const scale = parseInt(val, 10) / 100;
  scaleSmile(scale);
}

const slider = document.getElementById('myRange');
slider.oninput = onChange;

              
            
!
999px

Console