Add-ons / Crossfade

Macros to make crossfading between two images easier.

Author Cyrus Firheir
Website https://github.com/cyrusfirheir/cycy-wrote-custom-macros/tree/master/crossfade
Story format SugarCube 2
Last checked Sun Aug 29 2021
License Unlicense
Download crossfade.zip

Index

Overview

Macros to make crossfading between two images easier.

Installation

If using the Twine desktop/web app, copy contents of crossfade.js to Story JavaScript, and contents of crossfade.css to Story Stylesheet.

If using a compiler like Tweego, drop crossfade.js and crossfade.css to your source folder.

crossfade.twee-config.yaml can also be added to the workspace if using the Twee 3 Language Tools VSCode extension, for macro definitions.

Macros

<<crossfadecontainer>>

<<crossfadecontainer>>

Creates a container to use for crossfading between images.

Usage:

<<crossfadecontainer id [initialImagePath]>>

READ: Documentation

<<crossfade>>

<<crossfade>>

Crossfades between new image and image already in a pre-defined <<crossfadecontainer>>.

Usage:

<<crossfade id imagePath [fadeDuration]>>

READ: Documentation

JavaScript API

setup.crossfade.container(id, [initialImagePath])

setup.crossfade.container(id, [initialImagePath])

Generates the contaner, similar to the <<crossfadecontainer>> macro, optionally with an image already in it.

Arguments:

Returns:

setup.crossfade.fade(element, imagePath, [fadeDuration])

setup.crossfade.fade(element, imagePath, [fadeDuration])

Crossfades between image already in the passed element and new image, similar to <<crossfade>>.

Arguments:

setup.crossfade.selector(id)

setup.crossfade.selector(id)

Creates a selector string from a crossfade container ID.

Arguments:

Returns:

Example usage

<<crossfadecontainer "testID">>

<<button "Fade in">>
    <<crossfade "testID" "path/to/image.png">>
<</button>>
<<crossfadecontainer "testID" "path/to/image_1.png">>

<<button "Switch image">>
    <<crossfade "testID" "path/to/image_2.png">>
<</button>>
<<crossfadecontainer "testID" "path/to/image.png">>

<<button "Fade out">>
    <<crossfade "testID" "">>
<</button>>

JavaScript example

var id = "testID";

$(".some-element-selector")
    .append(setup.crossfade.container(id, "path/to/image.png"));

$(".some-interactive-element")
    .ariaClick(function () {
        var element = setup.crossfade.selector(id);
        setup.crossfade.fade(element, "path/to/anotherImage.png", 800);
    });

Live demo

Demo Twee code:

:: Start
<<button "Fade in">><<crossfade "testID" "https://loremflickr.com/120/120">><</button>>
<<crossfadecontainer "testID">>\