Macros to make crossfading between two images easier.
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.
<<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>>
Creates a container to use for crossfading between images.
-
id
: (string) The unique ID to be used to refer to this contianer later. -
initialImagePath
: (optional|string) Path to the image which should already be in the container. If unspecified, no image is present by default, and<<crossfade>>
-ing fades in the new image.
Example:
<<crossfadecontainer "testID" "path/to/fancyImage.png">>
<!-- Blank container -->
<<crossfadecontainer "anotherID">>
Crossfades between new image and image already in a pre-defined <<crossfadecontainer>>
.
SEE: DOM Macros Warning
-
id
: (string) ID of pre-defined<<crossfadecontainer>>
. -
imagePath
: (string) Path to the new image to crossfade to. Use empty string (""
) — or any falsy value — to fade out the image already in the container. -
fadeDuration
: (optional|cssTime) The duration of the crossfade. Has to be a valid CSS time value (250ms
,3.1415s
, etc.) If unspecified, defaults to400ms
.
Example:
<<crossfadecontainer "testID" "path/to/fancyImage.png">>
<<button "Switch">>
<<crossfade "testID" "path/to/anotherFancyImage.png">>
<</button>>
<<button "Slowww fade out">>
<<crossfade "testID" "" 4s>>
<</button>>
Generates the contaner, similar to the <<crossfadecontainer>>
macro, optionally with an image
already in it.
-
id
: (string) The unique ID to be used to refer to this contianer later. -
initialImagePath
: (optional|string) Path to the image which should already be in the container. If unspecified, no image is present by default, and<<crossfade>>
-ing fades in the new image.
Returns:
HTMLElement
: The container element.
Crossfades between image already in the passed element
and new image, similar to <<crossfade>>
.
-
element
: (string|HTMLElement) Selector string (seesetup.crossfade.selector
below) or HTMLElement to do the crossfade in. -
imagePath
: (string) Path to the new image to crossfade to. Use empty string (""
) — or any falsy value — to fade out the image already in the container. -
fadeDuration
: (optional|number) The duration of the crossfade in milliseconds (250
,3141
, etc.) Defaults to400
.
Creates a selector string from a crossfade container ID.
id
: (string) Container ID to make selector string out of.
Returns:
string
: The selector string, e.g.testID
becomes.macro-crossfade[data-macro-crossfade-id="testID"]
.
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);
});