cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <div class="coordinate-pane">
  <div class="coordinate-pane__inner">
    <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 218 218">
      <line x1="20" y1="144" x2="70" y2="20" stroke="#9E9E9E"/>
      <line x1="70" y1="20" x2="198" y2="72" stroke="#9E9E9E"/>
      <line x1="20" y1="144" x2="70" y2="20" stroke="#9E9E9E"/>
      <g style="transform:translate(20px, 144px)">
        <circle cx="0" cy="0" r="10" fill="#8A81AE"/>
        <text x="0" y="0" font-size="8" stroke-width="1" text-anchor="middle" dy="2.5em">A</text>
      </g>
      <g style="transform:translate(70px, 20px)">
        <circle cx="0" cy="0" r="10" fill="#8A81AE"/>
        <text x="0" y="0" font-size="8" stroke-width="1" text-anchor="middle" dy="2.5em">B</text>
      </g>
      <g style="transform:translate(198px, 72px)">
        <circle cx="0" cy="0" r="10" fill="#8A81AE"/>
        <text x="0" y="0" font-size="8" stroke-width="1" text-anchor="middle" dy="2.5em">C</text>
      </g>
      <g style="transform:translate(20px, 144px)">
        <circle cx="0" cy="0" r="4" fill="#000"/>
        <text x="0" y="0" font-size="6" stroke-width="1" text-anchor="middle" dy="2em">E</text>
      </g>
      <g style="transform:translate(70px, 20px)">
        <circle cx="0" cy="0" r="4" fill="#000"/>
        <text x="0" y="0" font-size="6" stroke-width="1" text-anchor="middle" dy="2em">F</text>
      </g>
      <g style="transform:translate(20px, 144px)">
        <circle cx="0" cy="0" r="4" fill="#000"/>
        <text x="0" y="0" font-size="6" stroke-width="1" text-anchor="middle" dy="2em">P</text>
      </g>
    </svg>
  </div>
</div>

<button class="interpolate-btn">Interpolating</button>
            
          
!
            
              html,
body {
  padding: 0;
  margin: 0;
}

.coordinate-pane {
  width: 218px;
  height: 218px;
  margin-top: 10px;
  margin-left: 10px;
  border: 1px solid #ebebeb;

  &__inner {
    position: relative;
    width: 100%;
    height: 100%;
  } 
}

button {
  margin-left: 10px;
}

            
          
!
            
              function getPosition(element) {
  const {transform} = element.style;
  const [, x, y] = /translate\(([\d|\.]+)px,\s?([\d|\.]+)px\)/g.exec(transform);
  
  return {
    x: parseFloat(x), 
    y: parseFloat(y)
  };
}

function setPosition(element, x, y) {
  element.style.transform = `translate(${x}px, ${y}px)`;
}

function blender(A, B, t) {
    if (t === 0) {
        return A;
    }

    if (t === 1) {
        return B;
    }

    return ((1 - t) * A) + (t * B); // or A + t * (B - A)
}

function blend(x1, x2, y1, y2, t) {
  const x = blender(x1, x2, t);
  const y = blender(y1, y2, t);

  return {x, y};
}

function quadBezier(A, B, C, t) {
  if (t === 0) {
    return A;
  }
  
  if (t === 1) {
    return C;
  }
  
  const s = 1 - t;
  
  console.log(s);
  
  // (s²)A + 2(st)B + (t²)C
  return Math.pow(s, 2) * A + 2 * (s * t) * B + Math.pow(t, 2) * C;
}

function raf(callback, duration) {
  let startTime = 0;
  
  function step(timestamp) {
      if (!startTime) {
        startTime = timestamp;
      }

      const pastTime = timestamp - startTime;
      let progress = pastTime / duration;

      if (progress > 1) {
        callback(1);
        return;
      }
    
      callback(progress);

      requestAnimationFrame(step);
    }
    
    requestAnimationFrame(step);
}

const svg = document.getElementsByTagName('svg')[0];
const controlPointA = document.getElementsByTagName('g')[0];
const controlPointB = document.getElementsByTagName('g')[1];
const controlPointC = document.getElementsByTagName('g')[2];
const midPointE = document.getElementsByTagName('g')[3];
const midPointF = document.getElementsByTagName('g')[4];
const midPointP = document.getElementsByTagName('g')[5];
const lineEtoF = document.getElementsByTagName('line')[2];
const interpolateBtn = document.getElementsByClassName('interpolate-btn')[0];
const posA = getPosition(controlPointA);
const posB = getPosition(controlPointB);
const posC = getPosition(controlPointC);

interpolateBtn.addEventListener('click', function() {
  // Start the interpolation.
  raf(function(t) {
    const posE = blend(posA.x, posB.x, posA.y, posB.y, t);
    const posF = blend(posB.x, posC.x, posB.y, posC.y, t);
    const posP = blend(posE.x, posF.x, posE.y, posF.y, t);
    
    // 아래와 같이 quadBezier()를 이용해 그릴 수도 있다.
    // const x = quadBezier(posA.x, posB.x, posC.x, t);
    // const y = quadBezier(posA.y, posB.y, posC.y, t);
    
    // 블랜딩한 각 좌표 값을 엘리먼트에 설정한다.
    setPosition(midPointE, posE.x, posE.y);
    setPosition(midPointF, posF.x, posF.y);
    setPosition(midPointP, posP.x, posP.y);
    
    // 점 E와 F 상의 선분을 렌더링한다.
    lineEtoF.setAttribute('x1', posE.x);
    lineEtoF.setAttribute('y1', posE.y);
    lineEtoF.setAttribute('x2', posF.x);
    lineEtoF.setAttribute('y2', posF.y);
    
    // 포인트가 되는 각 지점에 새로운 점을 그린다.
    const g = document.createElementNS('http://www.w3.org/2000/svg', 'g');
    const circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
    
    g.style.transform = `translate(${posP.x}px, ${posP.y}px)`;
    circle.setAttribute('cx', 0);
    circle.setAttribute('cy', 0)
    circle.setAttribute('r', .5);
    circle.setAttribute('fill', '#000');
    
    g.appendChild(circle);
    svg.appendChild(g);
    
    // 사파리에서 동작 안함.
    // svg.insertAdjacentHTML('beforeend', `
    //   <g style="transform:translate(${posP.x}px, ${posP.y}px)">
    //     <circle cx="0" cy="0" r=".5" fill="#000"/>
    //   </g>
    // `);
  }, 1000);
});
            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console