cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console