Pen Settings

HTML

CSS

CSS Base

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

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.

Vue

              
                <!-- https://vuejs.org/api/sfc-script-setup.html#reactivity -->
<script setup>
import { ref, reactive, onMounted, nextTick } from "vue";
const frameCenterRefs = reactive([]); // Element in center of each frame
const currentFrameIdx = ref(0);
let animation;
onMounted(() => {
  animation = new Animation("#svg", { frameCenterRefs }, currentFrameIdx);
});

class Animation {
  constructor(selector, config, currentFrameIdx, setIsReady) {
    console.log("init factory", config);
    this.selector = selector;
    this.config = config;
    this.currentFrameIdx = currentFrameIdx;
    nextTick(() => {
      // wait for SVG render to complete
      this.createTimelines();
      this.initiallyPositionSvg();
    });
  }
  gotoFrame(label) {
    this.timeline.tweenFromTo(this.timeline.time(), label);
  }
  viewPortWidth() {
    return document.documentElement.clientWidth;
  }
  initiallyPositionSvg() {
    const calcX = () => {
      const positionToCenter = this.viewPortWidth() / 2;
      const rawOffset = positionToCenter;
      const frameADim = this.config.frameCenterRefs[0].getBoundingClientRect();
      const frameACenter = frameADim.width / 2 + frameADim.left;
      const x = rawOffset - frameACenter;
      return `${x}px`;
    };

    gsap.set(this.selector, {
      scale: 0.5,
      x: calcX,
      transformOrigin: () => {
        const transformX = this.center(
          this.config.frameCenterRefs[0].getBoundingClientRect()
        );
        const transformOrigin = `${transformX}px center`;
        this.mostRecentTransformOrigin = transformOrigin;
        return transformOrigin;
      }
    });
  }

  center(rect) {
    return rect.left + rect.width / 2;
  }

  createTimelines() {
    const svgEl = document.querySelector(this.selector);
    const centers = this.config.frameCenterRefs.map((r) => {
      return this.center(r.getBoundingClientRect());
    });
    const offsets = centers.map((center) => {
      const svgRect = svgEl.getBoundingClientRect();
      return this.viewPortWidth() / 2 - center;
    });

    const setTOrigin = gsap.quickSetter(this.selector, "transformOrigin");
    const main = gsap.timeline({ paused: true });
    this.timeline = gsap
      .timeline({ paused: true })
      .to(this.selector, {
        duration: 0.1,
        scale: 1,
        transformOrigin: `${centers[0]}px`
      })
      .addLabel("zoomOutF0")
      .to(
        this.selector,
        {
          duration: 0.3,
          x: offsets[1],
          onComplete: () => {
            this.currentFrameIdx.value = this.currentFrameIdx.value + 1;
          }
        },
        "+=0.25"
      )
      .to(
        this.selector,
        {
          scale: 1.4,
          duration: 0.5,
          transformOrigin: `${centers[1]}px`
          // onUpdate: (...args) => {
          //   // const currentOrigin = gsap.getProperty(this.selector, 'transform-origin')
          //   // console.log({ currentOrigin, progress: f1Zoom.progress() })
          //   console.log(f1ZoomIn.progress(), f1ZoomIn.progress() * centers[1])
          //   setTOrigin(`${f1ZoomIn.progress() * centers[1]}px`)
          // }
        },
        "+=0.25"
      )
      .addLabel("f1")
      .to(this.selector, {
        scale: 1,
        transformOrigin: `${centers[1]}px`
      })
      .to(this.selector, {
        duration: 0.3,
        scale: 1,
        x: offsets[2],
        onComplete: () => {
          this.currentFrameIdx.value = this.currentFrameIdx.value + 1;
        }
      })
      .addLabel("f2");
  }
}
</script>

<template>
  <div>
    <div class="stage">
      <svg
        version="1.1"
        xmlns="http://www.w3.org/2000/svg"
        xmlns:xlink="http://www.w3.org/1999/xlink"
        x="0px"
        y="0px"
        viewBox="0 0 6877.84 1600"
        style="enable-background: new 0 0 6877.84 1600; will-change: transform"
        xml:space="preserve"
        id="svg"
      >
        <g>
          <path
            :ref="(ref) => (frameCenterRefs[1] = ref)"
            class="frame"
            d="M2487.77,1223.94v-718.2c0-8.86,7.21-16.07,16.07-16.07h466.45c8.86,0,16.07,7.21,16.07,16.07v718.2
    c0,8.86-7.21,16.07-16.07,16.07h-466.45C2494.98,1240,2487.77,1232.79,2487.77,1223.94z"
          />
          <path
            :ref="(ref) => (frameCenterRefs[0] = ref)"
            class="frame"
            d="M1433.33,600.03l-28.22,532.38l-246.14-22.14l-0.06-2.23c-0.12-4.37-11.55-437.23-16.59-490.16
    c-1.5-15.75-0.63-37.87,12.02-53.83c7.73-9.74,16.16-12.53,16.52-12.64l0.32-0.1l0.33-0.01c1.38-0.06,138.69-6.4,203.1-7.66
    c21.76-0.42,37.9,5.24,47.96,16.84C1436.46,576.48,1433.66,597.89,1433.33,600.03z M1418.77,563.72
    c-9.05-10.4-23.86-15.46-44.06-15.08c-62.71,1.23-194.68,7.28-202.6,7.64c-3.22,1.19-29.44,12.42-24.81,61.13
    c4.87,51.13,15.65,454.47,16.55,488.28l236.54,21.28l27.96-527.46l0.02-0.13C1428.41,599.16,1431.56,578.42,1418.77,563.72z"
          />
          <g>
            <g>
              <path
                :ref="(ref) => (frameCenterRefs[2] = ref)"
                class="frame"
                d="M5464.16,646.06c1.51-1.65,3.42-2.5,5.71-2.58c2.29-0.08,4.28,0.66,5.96,2.21c1.67,1.53,2.55,3.44,2.64,5.73
        c0.1,2.29-0.62,4.27-2.16,5.95c-1.55,1.68-3.46,2.56-5.74,2.62c-2.29,0.07-4.29-0.69-5.99-2.25c-1.51-1.39-2.38-3.16-2.62-5.31
        C5461.7,650.05,5462.43,647.93,5464.16,646.06z M5465.92,647.72c-1.19,1.29-1.73,2.75-1.63,4.39c0.1,1.63,0.71,2.96,1.84,4
        c1.23,1.13,2.65,1.66,4.28,1.58c1.63-0.08,3.02-0.74,4.15-1.97c1.15-1.25,1.69-2.68,1.63-4.28c-0.05-1.61-0.68-2.98-1.9-4.1
        c-1.22-1.12-2.63-1.64-4.25-1.56C5468.42,645.83,5467.05,646.48,5465.92,647.72z"
              />
            </g>
          </g>
        </g>
      </svg>
    </div>
    <div class="controls">
      Frame index: {{ currentFrameIdx }}
      <button class="next-btn" @click="animation.gotoFrame('f1')">
        Frame 1
      </button>
      <button class="next-btn" @click="animation.gotoFrame('f2')">
        Frame 2
      </button>
    </div>
  </div>
</template>
<style scoped>
.stage {
  overflow: auto;
  position: relative;
  width: 100%;
  height: 100%;
  min-height: 100%;
}

#svg {
  position: fixed;
  min-width: 300vw;
  height: 100%;
  transition: all 0.3s;
}

.frame {
  fill: #000;
  stroke: #000;
}

.controls {
  position: absolute;
  display: flex;
  top: 0;
  left: 0;
}
button {
  margin-left: 0.35rem;
}
</style>

              
            
!
999px

Console