Using Vanta.js with Sugarcube 2.34.1 and Twin 2

Please specify version and format if asking for help, or apply optional tags above:
Twine Version: Twine 2.3.13
Story Format: SugarCube 2.34.1

I was interested in using Vanta.js’s fog feature as a background that changes color based on the passage. I was wondering if anyone has any tips or tricks when importing a Javascript library. Would it make more sense to implement at a Passage Level or should I use the Story Level Javascript editor? I’m running into issues where the library is having trouble running on page load. Thank you for your time.

Sam

There are a number of methods you can use to make those JavaScript libraries work with a Twine/TWEE based project, the following are two of them.

1: You can copy the contents of the referenced three.r119.min.js and vanta.fog.min.js files into your project’s Story JavaScript area and then add the following JavaScript code to the same area, making sure to change the "#your-element-selector" element reference to be one of the identifier of one of the HTML elements that make up the SugarCube User Interface.

VANTA.FOG({
  el: "#story",
  mouseControls: true,
  touchControls: true,
  gyroControls: false,
  minHeight: 200.00,
  minWidth: 200.00,
  highlightColor: 0x2fff,
  midtoneColor: 0xa4ff,
  lowlightColor: 0x3c00ff,
  baseColor: 0xebf9ff,
  blurFactor: 0.52,
  speed: 3.00,
  zoom: 0.10
})

2: You could download a copy of the referenced three.r119.min.js and vanta.fog.min.js files to the same folder that you are saving your generated Story HTML file in, the HTML file you are using the Twine 2.x application’s Publish to File option to generate.
And then add the following to your project’s Story JavaScript area, which uses the importScript() function to load and use the contents of those two JavaScript files.

/* Import all scripts sequentially, while saving the returned Promise so it may be used later. */
var scripts = importScripts([
	"three.r119.min.js",
	"vanta.fog.min.js"
]);

/* Use the returned Promise to ensure that the scripts have been fully loaded before executing dependent code to initiate the required effect. */
scripts
	.then(function () {
		VANTA.FOG({
		el: "#story",
		mouseControls: true,
		touchControls: true,
		gyroControls: false,
		minHeight: 200.00,
		minWidth: 200.00,
		highlightColor: 0x2fff,
		midtoneColor: 0xa4ff,
		lowlightColor: 0x3c00ff,
		baseColor: 0xebf9ff,
		blurFactor: 0.52,
		speed: 3.00,
		zoom: 0.10
		})
	})
	.catch(function (err) {
		// There was an error loading the script, log it to the console.
		console.log(err);
	});

I didn’t see a link to the Vanta.js library’s documentation, so I’m unsure how you would dynamically change the effect based on which Passage is being shown.

Thank you so much Greyelf for responding. :~) I was mainly thinking of changing the highlightColor, midtoneColor, and lowlightColor values for each Passage. That’s why I wasn’t sure if I would need to create a completely new Vanta.fog and drop it at the bottom of each Passage. I figured that might be the easiest way to have different color varients of the same effect across different Passages.

You might want to check out the “Loading External Scripts” section of my Twine/SugarCube sample code collection. It shows how to make it so that the script works both when run from the published HTML file and when launched from Twine.

Hope that helps! :slight_smile: