Pen Settings

HTML

CSS

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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

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.

HTML

              
                <div id="root"></div>
              
            
!

CSS

              
                
              
            
!

JS

              
                const circleStyle = {
  background:
    "linear-gradient(90deg, rgba(2,0,36,1) 0%, rgba(9,9,121,1) 35%, rgba(0,212,255,1) 100%)",
  width: 200,
  height: 200,
  borderRadius: "50%"
};

function Wheel({ fullSpins = 5, stopAt = 45 }) {
  const circleRef = React.useRef(null);
  const [spinning, setSpinning] = React.useState(false);

  const loopIteration = React.useRef(0);
  const stopRotation = React.useMemo(() => 360 + stopAt, [stopAt]);

  const handleStartSpinning = () => {
    setSpinning(true);
  };

  const stopAnim = gsap.to(circleRef.current, {
    rotation: `+=${stopRotation}`,
    // Calculate the new duration ...
    // ... to make the transition between spinning and ...
    // ... stopping as smooth as possible.
    duration: stopRotation / 360,
    paused: true
  });

  const loopAnim = gsap.to(circleRef.current, {
    rotation: "+=360",
    ease: "none",
    duration: 0.5,
    onComplete: () => {
      // The props won't update in here...
      if (loopIteration.current >= fullSpins && typeof stopAt === "number") {
        stopAnim.play();
      } else {
        loopIteration.current++;
        loopAnim.play(0);
      }
    },
    paused: true
  });

  const startAnim = gsap.to(circleRef.current, {
    rotation: "+=360",
    ease: "power1.in",
    duration: 1,
    onComplete: () => loopAnim.play(),
    paused: true
  });

  React.useEffect(() => {
    if (spinning) {
      startAnim.play();
    }
  }, [spinning, startAnim]);

  React.useEffect(() => {
    console.log("prop", { stopAt });
  }, [stopAt]);

  return (
    <div data-id="wheel" style={{ margin: 24, border: "1px solid black" }}>
      <button type="text" data-id="wheel-button" onClick={handleStartSpinning}>
        Start Spinning
      </button>
      <div data-id="wheel-circle" ref={circleRef} style={circleStyle} />
    </div>
  );
}

function App() {
  const [stopAt, setStopAt] = React.useState(null);

  const handleGetStopPos = () => {
    setStopAt(48);
  };

  return (
    <div>
      <button type="text" onClick={handleGetStopPos}>
        Set Stop Position after "Start Spinning"
      </button>
      <Wheel stopAt={stopAt} />
    </div>
  );
}

ReactDOM.render(<App />, document.getElementById("root"));

              
            
!
999px

Console