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

              
                <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Plotly.js Animation</title>
  <!-- Include the Plotly.js library -->
  <script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
</head>
<body>
  <!-- Create a container div for the plot -->
  <div id="plotContainer"></div></body>
</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                    // Line plot data
    const numPoints = 100;
    const diff = 2 * Math.PI / (numPoints - 1);
    const theta = Plotly.d3.range(0, 2 * Math.PI + diff, diff);
    const y1 = theta.map(t => Math.cos(t));
    const y2 = theta.map(t => Math.sin(t));
    
    const trigData = [
        { x: theta, y: y1, name: 'Cosine / x-coordinate', mode: 'lines', line: { color: 'blue' } },
        { x: theta, y: y2, name: 'Sine / y-coordinate', mode: 'lines', line: { color: 'red' } },
    ];

    const r = theta.map(t => 1);

    // Circle data
    const circData = [
        {
            x: y1,
            y: y2,
            mode: 'lines',
            name: 'Unit Circle',
            xaxis: 'x2',
            yaxis: 'y2',
            line: {color: 'green' },
        },

        {
            x: [0, 1],
            y: [0, 0],
            mode: 'lines',
            name: 'Radius',
            xaxis: 'x2',
            yaxis: 'y2',
            line: {color: 'black' },
        },
    ];

    // Create the layout with subplots
    const layout = {
        grid: { rows: 1, columns: 2, pattern: 'independent' },
        yaxis2: {scaleanchor: "x2"}

    };
    const data = [...trigData, ...circData];
    Plotly.newPlot('plotContainer', data, layout);


    // Add markers for animation
    const markerShared = {marker:{color: 'black', size: 10},  showlegend: false, mode: 'markers'};
    const markerData = [
        { x: [0], y: [Math.cos(0)],  ...markerShared},
        { x: [0], y: [Math.sin(0)],  ...markerShared},
        { x: [1], y: [0], xaxis: 'x2', yaxis: 'y2',  ...markerShared}
    ];

    Plotly.addTraces('plotContainer', markerData);

    // Create animation frames
    const frames = theta.map(
        (i, idx) => {
            let cosX = Math.cos(i);
            let sinX = Math.sin(i);
            return {
                name: idx,
                data: [
                    { x: [0, cosX], y: [0, sinX] },
                    { x: [i], y: [cosX] },
                    { x: [i], y: [sinX] },
                    { x: [cosX], y: [sinX] },
                ],
                traces: [3, 4, 5, 6],
            }
        }
    )

    Plotly.addFrames('plotContainer', frames);

    // Add buttons for control
    const updatemenus = [
        {
          buttons: [{
          method: 'animate',
          args: [null, {
            mode: 'immediate',
            fromcurrent: true,
            transition: {duration: 0},
            frame: {duration: 100, redraw: false}
          }],
          label: 'Play'
        }, {
          method: 'animate',
          args: [[null], {
            mode: 'immediate',
            transition: {duration: 0},
            frame: {duration: 0, redraw: false}
          }],
          label: 'Pause'
        },{
          method: 'animate',
          args: [[frames[0].name], {
            mode: 'immediate',
            transition: {duration: 0},
            frame: {duration: 0, redraw: false}
          }],
          label: 'Reset'
        }],
          type: 'buttons',
          pad: {t: -50, r: 100},
          x: 0.1,
          y: 1.1,
        },
      ]

    Plotly.update('plotContainer', {}, { updatemenus: updatemenus });

    // Add a slider for control
    const sliderSteps = frames.map((frame, idx) => {
        return {
            method: 'animate',
            label: (theta[idx] * 180 / Math.PI).toFixed() + '°',
            args: [[frame.name], { frame: { duration: 50, redraw: false }, transition: { duration: 0 } }],
        }
    })

    const slider = [
    {
        pad: { t: 30 },
        steps: sliderSteps,
    },
    ];

    Plotly.update('plotContainer', {}, { sliders: slider });
              
            
!
999px

Console