• Jump To … +
    ./demo/modules/canvas-minimap.js ./demo/modules/canvas-scene-editor.js ./demo/modules/dom-entity-editor.js ./demo/modules/entity-copy-paste.js ./demo/modules/entity-manipulation-gui.js ./demo/modules/entity-navigation.js ./demo/modules/entity-ring-builder.js ./demo/modules/london-crime-graphic.js ./demo/modules/london-crime-lines.js ./demo/modules/london-crime-stacked-bars.js ./demo/modules/lottie-loader.js ./demo/modules/simple-chart-frame-tests.js ./demo/modules/simple-chart-frame.js ./demo/modules/simple-graph-lines.js ./demo/modules/simple-graph-stacked-bars.js ./demo/modules/wikipedia-views-spiral-chart.js ./demo/snippets/animated-highlight-gradient-text-snippet.js ./demo/snippets/animated-hover-gradient-snippet.js ./demo/snippets/animated-word-gradient-snippet.js ./demo/snippets/before-after-slider-infographic.js ./demo/snippets/bubbles-text-snippet.js ./demo/snippets/green-box-snippet.js ./demo/snippets/jazzy-button-snippet.js ./demo/snippets/page-performance-snippet-test.js ./demo/snippets/page-performance-snippet.js ./demo/snippets/pan-image-snippet.js ./demo/snippets/placeholder-effect-snippet.js ./demo/snippets/ripple-effect-snippet.js ./demo/snippets/risograph-text-gradient-snippet.js ./demo/snippets/spotlight-text-snippet-test.js ./demo/snippets/spotlight-text-snippet.js ./demo/snippets/swirling-stripes-text-snippet.js ./demo/snippets/text-snippet-helper.js ./demo/snippets/word-highlighter-snippet.js ./demo/snippets/worley-text-gradient-snippet.js
  • §

    Demo Snippets 003

    DOM element snippet - test canvas adaption to local element variations

    Related files:

    • DOM element snippets - main module

    ‘Ripple effect’ snippet

    Purpose: replicate the Material design ‘ripple effect’ when user clicks on an element decorated with this snippet. Function input:

    • the DOM element, or a handle to it, as the first argument.
    • an optional key:value Object as the second argument

    Function output:

    {
        element           // wrapper
        canvas            // wrapper
        animation         // object
        demolish          // function
    }
    
    Usage example:
    import ripples from './relative/or/absolute/path/to/this/file.js';
    
    let myElements = document.querySelectorAll('.some-class');
    
    myElements.forEach(el => ripples(el, {
      backgroundColor: 'red',
      rippleColor: 'pink',
    }));
    
  • §

    Import the Scrawl-canvas object

    • there’s various ways to do this. See Demo DOM-001 for more details
    import * as scrawl from '../../source/scrawl.js';
  • §

    Effects on the element:

    • The DOM element’s background color will be brought into the canvas, with the element’s backgroundColor set to transparent - any background image, gradient, etc will be hidden by the snippet effect
    export default function (el, args = {}) {
  • §

    The snippet will accept an optional key:value Object as the second argument

    • rippleColor - default: white
    • backgroundColor - default: false (will use the DOM element’s background-color style)
        let backgroundColor = args.backgroundColor || false,
            rippleColor = args.rippleColor || 'white';
  • §

    Apply the snippet to the DOM element

        let snippet = scrawl.makeSnippet({
            domElement: el,
        });
    
        if (snippet) {
  • §

    Set some convenience variables

            let canvas = snippet.canvas,
                wrapper = snippet.element,
                styles = wrapper.elementComputedStyles,
                name = wrapper.name;
  • §

    Transfer the DOM element’s current background-color style over to the canvas

    • This does not handle situations where the DOM element has a gradient assigned to it
    • if the function is invoked with the backgroundColor attribute set in the args Object, that color will replace the DOM element’s current background color
            if (!backgroundColor) backgroundColor = styles.backgroundColor;
  • §

    We don’t want a transparent background - default to beige!

            if ('rgba(0, 0, 0, 0)' === backgroundColor || 'transparent' === backgroundColor || '#00000000' === backgroundColor || '#0000' === backgroundColor) backgroundColor = 'beige';
    
            canvas.set({
                backgroundColor,
            })        
            wrapper.domElement.style.backgroundColor = 'transparent';
  • §

    We add an event listener to the DOM element

            let clickAction = (e) => {
    
                let {x, y, active} = canvas.here;
    
                if (active) {
  • §

    Implement the ripple effect using a run-once-and-die tween operating on a create-and-destroy Wheel entity

                    let r = scrawl.makeWheel({
                        name: `${name}-ripple`,
                        start: [x, y],
                        group: canvas.base.name,
                        handle: ['center', 'center'],
                        radius: '100%',
                        fillStyle: rippleColor,
                    });
    
                    scrawl.makeTween({
                        name: `${name}-tween`,
                        targets: r,
                        duration: 1000,
                        completeAction: () => r.kill(),
                        killOnComplete: true,
                        definitions: [
                            {
                                attribute: 'scale',
                                start: 0,
                                end: 1,
                            }, {
                                attribute: 'globalAlpha',
                                start: 1,
                                end: 0,
                            }
                        ],
                    }).run();
                }
            };
            scrawl.addNativeListener(['click', 'touchend'], clickAction, el);
        }
        return snippet;
    };
    
    console.log(scrawl.library);