xxxxxxxxxx
224
// base text code refereced from @pepepepebrick
// https://www.instagram.com/p/C1etC0FPo9I/
// Variable to store user input words
let inputWords = [];
// Flag to toggle color mode
let colored = true;
// Flag to track if keyboard controls are active
let pressed = false;
// Array to store synonyms fetched from API
let wordList = [];
// Object to store word synonyms
let wordDict = {};
// Declare an object to store synonym positions
let synonymPositions = {};
let music;
let typing;
let noSyn;
function preload() {
music = loadSound("music.mp4");
typing = loadSound("typing.mp3");
noSyn = loadSound("click.mp3");
}
function setup() {
createCanvas(windowWidth, windowHeight);
background(0);
textFont("Verdana");
input = createInput();
input.position(20, 20);
music.play();
}
function draw() {
background(20, 30);
translate(width / 2, height / 2);
// Process user input
inputWords = input.value().split(" ");
//let textSizeVar;
if (!pressed) {
textSizeVar = map(mouseY, 0, height, 10, 40);
mappedMouseX = map(mouseX, width / 2, 0, 0, 300);
}
for (i = 0; i < 400; i++) {
let ang = i / 4.6 + frameCount / 180;
let r = i + noise(i) * 2 * mappedMouseX;
if (colored) {
// Calculate a percentage for the color gradient
let percent = map(i, 0, 400, 0, 50);
// Interpolate between two colors using lerpColor
if (percent < 0.5) {
col = lerpColor(
color("#FDF48A"),
color("#FF5631"),
map(percent, 0, 0.5, 0, 1)
);
} else {
col = lerpColor(
color("#FF5631"),
color("#634FFF"),
map(percent, 0.5, 1, 0, 1)
);
}
} else {
col = 255;
}
fill(col);
textSize(textSizeVar);
// Draw text at calculated position
let word = inputWords[i];
if (word) {
for (let j = 0; j < word.length; j++) {
let charIndex = j % word.length;
text(word.charAt(charIndex), cos(ang) * r, sin(ang) * r);
ang += 0.1; // Increment angle for each character to avoid overlapping
}
}
// Display synonyms
let synonyms = wordDict[word];
if (synonyms) {
for (let synonym of synonyms) {
r += 7; // Increase radius for synonyms to separate them
for (let j = 0; j < synonym.length; j++) {
let charIndex = j % synonym.length;
let x = cos(ang) * r;
let y = sin(ang) * r;
text(synonym.charAt(charIndex), x, y);
ang += 0.1; // Increment angle for each character to avoid overlapping
// Store the position of the synonym
synonymPositions[synonym] = createVector(x, y);
}
}
}
}
}
function keyPressed() {
// Toggle color mode with the up arrow
if (keyCode === UP_ARROW) {
colored = !colored;
pressed = !pressed;
} else if (keyCode === RETURN) {
// Fetch synonyms when enter key is pressed
startup();
}
}
// Function to update synonym positions
function updateSynonymPositions() {
for (let word in wordDict) {
let synonyms = wordDict[word];
if (synonyms) {
for (let synonym of synonyms) {
// Calculate position relative to the center of the canvas
let posX = random(width); // Adjust position calculation as needed
let posY = random(height); // Adjust position calculation as needed
// Store the position in synonymPositions
synonymPositions[synonym] = { x: posX, y: posY };
}
}
}
}
function mousePressed() {
// Check if mouse click matches any synonym position
// console.log("clicked");
for (let synonym in synonymPositions) {
let pos = synonymPositions[synonym];
let distance = dist(mouseX, mouseY, pos.x + width / 2, pos.y + height / 2);
if (distance < 50) {
// Adjust the threshold as needed
console.log("Clicked on synonym:", synonym, "pos:", pos.x, pos.y);
// Push the clicked synonym to inputWords
inputWords.push(synonym);
// Append the clicked synonym to the existing value of the input box
let currentValue = input.value();
let newValue = currentValue + " " + synonym;
input.value(newValue);
// Fetch synonyms for the clicked word
getSynonyms(synonym)
.then((result) => {
// Update wordDict and redraw
wordDict[synonym] = result;
updateSynonymPositions(); // Update synonym positions
redraw();
})
.catch((err) => console.log(err));
break; // Exit loop after clicking on a synonym
}
}
}
// Fetch synonyms of a given word from API
async function getSynonyms(word) {
if (wordList.indexOf(word) == -1) {
wordList.push(word);
// let synonyms = [];
try {
let synonyms = [];
await fetch("https://api.api-ninjas.com/v1/thesaurus?word=" + word, {
method: "GET",
headers: { "X-Api-Key": "GhrROZHVzgzvaB/7pBQN2A==dhEHiZJ01zkonkso" },
})
.then((response) => response.json())
.then((data) => {
// Printing synonyms to the console
console.log("Synonyms for '" + word + "':");
data.synonyms.forEach((synonym) => {
//console.log(synonym);
synonyms.push(synonym);
});
})
.catch((error) => {
console.error("Error:", error);
});
console.log(synonyms);
if (synonyms.length > 40) {
synonyms.splice(40, synonyms.length - 40);
}
wordDict[word] = synonyms;
if (synonyms.length > 0) {
// Only play the sound effect if new synonyms are fetched
typing.play();
} else {
// else play this
noSyn.play();
}
return synonyms;
} catch (error) {
console.error("Error fetching synonyms:", error);
return [];
}
} else {
return [];
}
}
// Function to handle fetching synonyms when user inputs a new word
function startup() {
console.log("startup");
let startWord = inputWords[inputWords.length - 1];
getSynonyms(startWord)
.then((result) => (next_list = result))
.catch((err) => console.log(err));
inputWords.push(new Word(startWord, 0, 0, random(-10, 10)));
prev_list = [startWord];
}