(function(storyContent) {
var story = new inkjs.Story(storyContent);
var storyContainer = document.querySelectorAll('#story')[0];
function showAfter(delay, el) {
setTimeout(function() { el.classList.add("show") }, delay);
}
function scrollToBottom() {
var progress = 0.0;
var start = document.body.scrollTop;
var dist = document.body.scrollHeight - window.innerHeight - start;
if( dist < 0 ) return;
var duration = 300 + 300*dist/100;
var startTime = null;
function step(time) {
if( startTime == null ) startTime = time;
var t = (time-startTime) / duration;
var lerp = 3*t*t - 2*t*t*t;
window.scrollTo(0, start + lerp*dist);
if( t < 1 ) requestAnimationFrame(step);
}
requestAnimationFrame(step);
}
function continueStory() {
var paragraphIndex = 0;
var delay = 0.0;
// Generate story text - loop through available content
while(story.canContinue) {
// Get ink to generate the next paragraph
var paragraphText = story.Continue();
// Create paragraph element
var paragraphElement = document.createElement('p');
paragraphElement.innerHTML = paragraphText;
storyContainer.appendChild(paragraphElement);
// Fade in paragraph after a short delay
showAfter(delay, paragraphElement);
delay += 200.0;
}
// Create HTML choices from ink choices
story.currentChoices.forEach(function(choice) {
// Create paragraph with anchor element
var choiceParagraphElement = document.createElement('p');
choiceParagraphElement.classList.add("choice");
choiceParagraphElement.innerHTML = `${choice.text}`
storyContainer.appendChild(choiceParagraphElement);
// Fade choice in after a short delay
showAfter(delay, choiceParagraphElement);
delay += 200.0;
// Click on choice
var choiceAnchorEl = choiceParagraphElement.querySelectorAll("a")[0];
choiceAnchorEl.addEventListener("click", function(event) {
// Don't follow link
event.preventDefault();
// Remove all existing choices
var existingChoices = storyContainer.querySelectorAll('p.choice');
for(var i=0; i