Skip to content

support for d3.js transitions #456

Closed

Description

@jlegewie

Hi,

it would be great if reveal.js provides better support for d3.js (and possibly vega) visualizations. The idea would be that users can embed d3.js graphs in slides (which is already possible). But the important addition is that pressing the button for next slide runs a certain transition instead of changing to the next slide. So there should be some standard way in which the user defines the container for a figure such as <div id="fig-name" class="fig-container">, and a standard way in which each step of the transitions are saved such an array of transition functions. reveal.js then checks each figure for transitions and instead of changing to the next slide, runs this transitions. To enable back and forth, there could be a second array of transitions that reverses the previous one. So which figure could be a javascript object a function for create (run at the beginning to create the vis), an array t with functions for each step of the transitions, and an array t_inv for the inverse functions that revers the transitions.

Does that make any sense? I think it would be a beautiful way to add dynamic visualizations to presentations and d3.js (and possibly vega) are just the right tools for that!

Activity

  1. gmp commented on Jul 18, 2013

    @gmp

    This is actually a really cool idea and I think would be a fantastic feature that is likely to be used.

    I wouldn't mind working on something along these lines.

  2. hakimel commented on Jul 18, 2013

    @hakimel
    Owner

    From what I understand this would be very similar to how fragments work currently. Fragments are steps within a slide and are all stepped through before leaving the slide, they also work when navigating backwards. You could do something like the following:

    Reveal.addEventListener( 'fragmentshown', updateVisualization );
    Reveal.addEventListener( 'fragmenthidden', updateVisualization );
    
    function updateVisualization( event ) {
      var step = Reveal.getIndices().f;
    
      switch( step ) {
        case 0:
          // move to vis state 0
          break;
        case 1:
          // move to vis state 1
          break;
        case 2:
          // move to vis state 2
          break;
      }
    }

    That doesn't include a condition for which slide you're on but you get the idea.

    I'd like to do this through fragments rather than adding new functionality that would then need to behave well together with fragments. I'm open to discussing other ways of exposing this if you have any ideas though.

  3. ptamarit commented on Dec 16, 2013

    @ptamarit

    Hi,

    I confirm that you can have D3 transitions triggered on Reveal's previous/next fragments events.
    You can see it in action here: http://ptamarit.com/slides-data-viz-web-d3/en/#/example-bind-result

    What I have in the slides is a placeholder and some empty fragments like this: <span class="fragment"></span>.

    Then I have the following config: https://github.com/ptamarit/slides-data-viz-web-d3/blob/master/src/js/reveal-to-d3-config.js
    It has a map where the keys are the slide IDs, containing another map with an init function (typically creating an SVG element and appending it to the placeholder), and then one function per fragment index.

    The following code is taking care of listening to the events and calling the corresponding functions: https://github.com/ptamarit/slides-data-viz-web-d3/blob/master/src/js/reveal-to-d3.js

    I didn't need inverse functions because the functions I'm calling can be called in any order (I'm just binding different data sets), but there might be cases where it's needed.

    One improvements I could add is to programmatically add the right amount of fragments in the slides given the number of functions in the config.

    And of course, the code is not generic enough to turn it into a Reveal plugin, but if anyone feels like working on it, that would be great.

  4. bnjmnt4n commented on Mar 8, 2016

    @bnjmnt4n
    Collaborator

    Closing this for now as there’s a working demo of this. Feel free to implement this as a plugin.

  5. flying-sheep commented on Apr 25, 2016

    @flying-sheep
    Contributor

    nice. in modern:

    import Reveal from 'reveal.js'
    
    export default function registerSlideListeners(config) {
        Reveal.addEventListener('slidechanged',   e => handleEvent(config, true))
        Reveal.addEventListener('fragmentshown',  e => handleEvent(config))
        Reveal.addEventListener('fragmenthidden', e => handleEvent(config))
    }
    
    function handleEvent(config, isSlideEvent=false) {
        const currentSlideId = Reveal.getCurrentSlide().id
        const currentFragment = Reveal.getIndices().f
    
        if (!currentSlideId || !(currentSlideId in config))
            return
    
        const functions = config[currentSlideId]
    
        if (isSlideEvent && 'init' in functions) {
            functions.init()
            delete functions.init
        }
    
        if (currentFragment in functions) {
            functions[currentFragment]()
        }
    }
  6. gcalmettes commented on Mar 11, 2017

    @gcalmettes

    very useful, thanks @ptamarit.

  7. jlegewie commented on Aug 17, 2017

    @jlegewie
    Author

    Has anyone worked on this more? A Reveal plugin for @ptamarit solution would be great.

  8. jlegewie commented on Sep 13, 2017

    @jlegewie
    Author

    I put together a reveal.js plugin that integrates d3.js with transitions seamlessly into reveal.js presentations. The plugin is here.

    Here is a demo.

  9. flying-sheep commented on Sep 14, 2017

    @flying-sheep
    Contributor

    it’s broken. “event” is not defined jlegewie/reveal.js-d3js-plugin#1

  10. jlegewie commented on Sep 14, 2017

    @jlegewie
    Author

    Should be fixed...

  11. ptamarit commented on Sep 14, 2017

    @ptamarit

    @jlegewie: you should add a link to your project in the Plugins page.

  12. gcalmettes commented on Nov 29, 2017

    @gcalmettes

    I have been inspired by @jlegewie's work and I made a D3 plugin that fully supports the Reveal fragments. It also plays back the last transition when navigating back from another slide to render the last state of the visualization, and it is compatible with multi visualizations per slides. The plugin itself doesn't have any D3 dependencies and could be used for any other javascript visualization library. See the repo and demo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions