i would like a javascript function that finds all code elements on a web page, that are themselves the child of a pre element. for each code element, it finds all span elements within. it then changes each span element to have a random text color.
Assistant
Here's a possible implementation of the function you described in JavaScript:
javascript
function randomizeCodeColors() {
// Find all code elements that are children of a pre element
const codeElements = document.querySelectorAll("pre code");
// Loop through each code element
codeElements.forEach(code => {
// Find all span elements within the code element
const spanElements = code.querySelectorAll("span");
// Loop through each span element and set a random text color
spanElements.forEach(span => {
span.style.color = getRandomColor();
});
});
}
function getRandomColor() {
// Generate a random hexadecimal color code
return "#" + Math.floor(Math.random()*16777215).toString(16);
}
You can call this function whenever you want to randomize the colors of all span elements within code elements that are children of a pre element. Note that this function assumes that the code and span elements have already been added to the page's HTML.
User
nice, it worked like a charm. now lets make it more fun. i want each span have its color changed at random intervals. i also want it to have a css transition of 1 second for color changes. approximately 1/20th of the spans should be changed every second, so you'll have to start off by counting them. you can keep an array of all spans and assume it isn't changing
Assistant
Sure, here's an updated implementation of the function that changes the color of each span element randomly at regular intervals, with a CSS transition of 1 second. It also keeps an array of all the spans and updates approximately 1/20th of them every second:
javascript
let allSpans = [];
function randomizeCodeColors() {
// Find all code elements that are children of a pre element
const codeElements = document.querySelectorAll("pre code");
// Loop through each code element
codeElements.forEach(code => {
// Find all span elements within the code element
const spanElements = code.querySelectorAll("span");
allSpans.push(...spanElements);
});
setInterval(() => {
// Calculate the number of spans to change in this interval
const numToChange = Math.ceil(allSpans.length / 20);
// Choose random spans to change
const indicesToChange = new Set();
while (indicesToChange.size < numToChange) {
indicesToChange.add(Math.floor(Math.random() * allSpans.length));
}
// Update the chosen spans with random colors and CSS transitions
indicesToChange.forEach(i => {
const span = allSpans[i];
span.style.transition = "color 1s";
span.style.color = getRandomColor();
});
}, 50);
}
function getRandomColor() {
// Generate a random hexadecimal color code
return "#" + Math.floor(Math.random()*16777215).toString(16);
}
This implementation uses the setInterval() function to run a code block every 50 milliseconds (or approximately every 1/20th of a second). Within that code block, it calculates the number of spans to change in this interval, chooses random spans to change, and updates them with random colors and CSS transitions. Note that the allSpans array is assumed to be populated with all the span elements beforehand.
User
sweet. you rock. do something even cooler. make each span that is getting its color changed get larger temporarily, based around center, using a transform, each time its color is changed. have it use an easing function to make it smooth. have it also take one second to scale up, then an additional second to back down. Limit this (including color changes) it to the first thousand spans in the code element.
Assistant
Sure, here's an updated implementation that adds a scaling effect to each span that's getting its color changed, limited to the first 1000 spans in each code element. The scaling effect is based around the center of each span, and uses a CSS transform and an easing function to make it smooth:
javascript
let allSpans = [];
function randomizeCodeColors() {
// Find all code elements that are children of a pre element
const codeElements = document.querySelectorAll("pre code");
// Loop through each code element
codeElements.forEach(code => {
// Find all span elements within the code element, up to the first 1000
const spanElements = code.querySelectorAll("span");
const limitedSpanElements = Array.from(spanElements).slice(0, 1000);
allSpans.push(...limitedSpanElements);
});
setInterval(() => {
// Calculate the number of spans to change in this interval
const numToChange = Math.ceil(allSpans.length / 20);
// Choose random spans to change
const indicesToChange = new Set();
while (indicesToChange.size < numToChange) {
indicesToChange.add(Math.floor(Math.random() * allSpans.length));
}
// Update the chosen spans with random colors and scaling effects
indicesToChange.forEach(i => {
const span = allSpans[i];
span.style.transition = "color 1s, transform 2s";
span.style.color = getRandomColor();
span.style.transform = "scale(1.2)";
setTimeout(() => {
span.style.transform = "scale(1)";
}, 1000);
});
}, 50);
}
function getRandomColor() {
// Generate a random hexadecimal color code
return "#" + Math.floor(Math.random()*16777215).toString(16);
}
This implementation uses the slice() method to limit the number of spans to the first 1000, and adds a new setTimeout() function to handle the scaling effect. Within the indicesToChange.forEach() loop, it sets the transition property of each span to include both color and transform transitions. It also updates the transform property to scale the span up by a factor of 1.2, and sets a setTimeout() function to scale it back down after 1 second. The overall duration of the scaling effect is 2 seconds, with an easing function applied to make it smooth.
User
it doesn't make an apparent change of size. should it be display inline block maybe? use scale3d? I don't know, just thinking of what it might be
Assistant
You're correct, the scaling effect might not be apparent if the spans have a default display value of inline. In that case, you can try setting the display value to inline-block to allow the scaling to take effect. Here's an updated implementation that does this:
javascript
let allSpans = [];
function randomizeCodeColors() {
// Find all code elements that are children of a pre element
const codeElements = document.querySelectorAll("pre code");
// Loop through each code element
codeElements.forEach(code => {
// Find all span elements within the code element, up to the first 1000
const spanElements = code.querySelectorAll("span");
const limitedSpanElements = Array.from(spanElements).slice(0, 1000);
allSpans.push(...limitedSpanElements);
});
setInterval(() => {
// Calculate the number of spans to change in this interval
const numToChange = Math.ceil(allSpans.length / 20);
// Choose random spans to change
const indicesToChange = new Set();
while (indicesToChange.size < numToChange) {
indicesToChange.add(Math.floor(Math.random() * allSpans.length));
}
// Update the chosen spans with random colors and scaling effects
indicesToChange.forEach(i => {
const span = allSpans[i];
span.style.transition = "color 1s, transform 2s";
span.style.color = getRandomColor();
span.style.display = "inline-block";
span.style.transform = "scale3d(1.2, 1.2, 1.2)";
setTimeout(() => {
span.style.transform = "scale3d(1, 1, 1)";
}, 1000);
});
}, 50);
}
function getRandomColor() {
// Generate a random hexadecimal color code
return "#" + Math.floor(Math.random()*16777215).toString(16);
}
This implementation sets the display property of each span to inline-block, and updates the transform property to use scale3d() instead of scale(). The scale3d() function allows you to specify scaling values in all three dimensions (x, y, and z), which can help ensure that the scaling effect is visible even if the span's default z dimension is zero.
User
ok, now change it so it checks for code elements every 2 seconds. if any are found that already are having their spans colored, leave them alone. (use a data attribute on it?) if they are new, call the random color function. make sure that when a code element is removed, its elements are removed from the list of ones to color. this might mean making a 2d array. this is for a page where new content is often loaded by ajax calls
Assistant
Sure, here's an updated implementation that checks for new code elements every 2 seconds, skips any that are already being colored, removes any elements that are no longer present, and maintains a 2D array of code and span elements for coloring:
javascript
let codeSpans = [];
function randomizeCodeColors() {
// Check for new code elements every 2 seconds
setInterval(() => {
// Find all code elements that are children of a pre element and not already being colored
const codeElements = document.querySelectorAll("pre code:not([data-randomize-colors])");
// Loop through each new code element
codeElements.forEach(code => {
// Find all span elements within the code element
const spanElements = code.querySelectorAll("span");
const limitedSpanElements = Array.from(spanElements).slice(0, 1000);
codeSpans.push([code, ...limitedSpanElements]);
// Mark the code element as already being colored
code.dataset.randomizeColors = true;
// Update the chosen spans with random colors and scaling effects
limitedSpanElements.forEach(span => {
span.style.transition = "color 1s, transform 2s";
span.style.color = getRandomColor();
span.style.display = "inline-block";
span.style.transform = "scale3d(1.2, 1.2, 1.2)";
setTimeout(() => {
span.style.transform = "scale3d(1, 1, 1)";
}, 1000);
});
});
// Remove any code elements and spans that are no longer present
codeSpans = codeSpans.filter(([code, ...spans]) => {
if (!document.contains(code)) {
return false;
}
spans = spans.filter(span => document.contains(span));
codeSpans.push([code, ...spans]);
return true;
});
}, 2000);
}
function getRandomColor() {
// Generate a random hexadecimal color code
return "#" + Math.floor(Math.random()*16777215).toString(16);
}
This implementation sets a setInterval() function to check for new code elements every 2 seconds. Within that function, it finds all code elements that are not already being colored (as indicated by a data-randomize-colors attribute), and updates them with random colors and scaling effects. It also adds the code and span elements to the codeSpans array, and removes any elements that are no longer present in the DOM. The codeSpans array is maintained as a 2D array of code and span elements, with each row representing a single code element and its associated spans.
User
doesn't seem to work, anything you can see wrong?
Assistant
I apologize for the mistake. I see that there are a couple of issues with the implementation I provided. Here's an updated implementation that should fix the issues:
javascript
let codeSpans = [];
function randomizeCodeColors() {
// Check for new code elements every 2 seconds
setInterval(() => {
// Find all code elements that are children of a pre element and not already being colored
const codeElements = document.querySelectorAll("pre code:not([data-randomize-colors])");
// Loop through each new code element
codeElements.forEach(code => {
// Find all span elements within the code element
const spanElements = code.querySelectorAll("span");
const limitedSpanElements = Array.from(spanElements).slice(0, 1000);
codeSpans.push([code, ...limitedSpanElements]);
// Mark the code element and its spans as already being colored
code.dataset.randomizeColors = true;
limitedSpanElements.forEach(span => {
span.dataset.randomizeColors = true;
});
// Update the chosen spans with random colors and scaling effects
limitedSpanElements.forEach(span => {
span.style.transition = "color 1s, transform 2s";
span.style.color = getRandomColor();
span.style.display = "inline-block";
span.style.transform = "scale3d(1.2, 1.2, 1.2)";
setTimeout(() => {
span.style.transform = "scale3d(1, 1, 1)";
}, 1000);
});
});
// Remove any code elements and spans that are no longer present
codeSpans = codeSpans.filter(([code, ...spans]) => {
if (!document.contains(code)) {
return false;
}
spans = spans.filter(span => document.contains(span));
if (spans.length === 0) {
delete code.dataset.randomizeColors;
return false;
}
codeSpans.push([code, ...spans]);
return true;
});
}, 2000);
}
function getRandomColor() {
// Generate a random hexadecimal color code
return "#" + Math.floor(Math.random()*16777215).toString(16);
}
The issues were:
The data-randomize-colors attribute was not being set on the span elements, which was causing the selector to miss them.
The filter() method for removing elements was not properly handling the case where a code element had no spans remaining.
I hope this updated implementation works as expected.
User
hmmm still doesn't work. can you put console logs that fire when anything happens, such as it looks for code elements, finds them, removes them, etc. also please call the function at the end of the code so it autoruns. and make the scale 1.8,1.8,1 for the spans
Assistant
Certainly! Here's an updated implementation with console logs added to help with debugging:
javascript
let codeSpans = [];
function randomizeCodeColors() {
console.log("Looking for new code elements...");
// Check for new code elements every 2 seconds
setInterval(() => {
// Find all code elements that are children of a pre element and not already being colored
const codeElements = document.querySelectorAll("pre code:not([data-randomize-colors])");
// Loop through each new code element
codeElements.forEach(code => {
console.log(`Found new code element: ${code}`);
// Find all span elements within the code element
const spanElements = code.querySelectorAll("span");
const limitedSpanElements = Array.from(spanElements).slice(0, 1000);
codeSpans.push([code, ...limitedSpanElements]);
// Mark the code element and its spans as already being colored
code.dataset.randomizeColors = true;
limitedSpanElements.forEach(span => {
span.dataset.randomizeColors = true;
});
// Update the chosen spans with random colors and scaling effects
limitedSpanElements.forEach(span => {
span.style.transition = "color 1s, transform 2s";
span.style.color = getRandomColor();
span.style.display = "inline-block";
span.style.transform = "scale3d(1.8, 1.8, 1)";
setTimeout(() => {
span.style.transform = "scale3d(1, 1, 1)";
}, 1000);
});
});
// Remove any code elements and spans that are no longer present
codeSpans = codeSpans.filter(([code, ...spans]) => {
if (!document.contains(code)) {
console.log(`Removed code element: ${code}`);
return false;
}
spans = spans.filter(span => document.contains(span));
if (spans.length === 0) {
console.log(`Removed code element: ${code}`);
delete code.dataset.randomizeColors;
return false;
}
codeSpans.push([code, ...spans]);
return true;
});
}, 2000);
}
function getRandomColor() {
// Generate a random hexadecimal color code
return "#" + Math.floor(Math.random()*16777215).toString(16);
}
randomizeCodeColors(); // Call the function to autorun it
This implementation adds console logs to indicate when new code elements are found, existing ones are removed, etc. It also sets the transform property to scale the span up by a factor of 1.8 (as requested). Finally, it calls the randomizeCodeColors() function at the end to autorun it when the script loads.
User
i think it got confused. there should be different set intervals, one for the coloring, one for the checking for code elements. make sure that it still colors (and transforms) different spans at regular intervals, while checking for code elements every 2 seconds
Assistant
You're correct, I apologize for the confusion. Here's an updated implementation that uses separate setInterval() functions for checking for new code elements and randomizing colors, and maintains a separate list of spans to randomize for each code element:
javascript
let codeSpans = [];
function randomizeCodeColors() {
console.log("Looking for new code elements...");
// Check for new code elements every 2 seconds
setInterval(() => {
// Find all code elements that are children of a pre element and not already being colored
const codeElements = document.querySelectorAll("pre code:not([data-randomize-colors])");
// Loop through each new code element
codeElements.forEach(code => {
console.log(`Found new code element: ${code}`);
// Find all span elements within the code element
const spanElements = code.querySelectorAll("span");
const limitedSpanElements = Array.from(spanElements).slice(0, 1000);
codeSpans.push([code, ...limitedSpanElements]);
// Mark the code element and its spans as already being colored
code.dataset.randomizeColors = true;
limitedSpanElements.forEach(span => {
span.dataset.randomizeColors = true;
});
});
// Remove any code elements and spans that are no longer present
codeSpans = codeSpans.filter(([code, ...spans]) => {
if (!document.contains(code)) {
console.log(`Removed code element: ${code}`);
return false;
}
spans = spans.filter(span => document.contains(span));
if (spans.length === 0) {
console.log(`Removed code element: ${code}`);
delete code.dataset.randomizeColors;
return false;
}
codeSpans.push([code, ...spans]);
return true;
});
}, 2000);
// Randomize colors for spans every 50 milliseconds
setInterval(() => {
// Choose random spans to change
const indicesToChange = new Set();
while (indicesToChange.size < codeSpans.length / 20) {
indicesToChange.add(Math.floor(Math.random() * codeSpans.length));
}
// Update the chosen spans with random colors and scaling effects
indicesToChange.forEach(i => {
const [code, ...spans] = codeSpans[i];
const span = spans[Math.floor(Math.random() * spans.length)];
if (!span.dataset.randomizeColors) {
return;
}
span.style.transition = "color 1s, transform 2s";
span.style.color = getRandomColor();
span.style.display = "inline-block";
span.style.transform = "scale3d(1.8, 1.8, 1)";
setTimeout(() => {
span.style.transform = "scale3d(1, 1, 1)";
}, 1000);
});
}, 50);
}
function getRandomColor() {
// Generate a random hexadecimal color code
return "#" + Math.floor(Math.random()*16777215).toString(16);
}
randomizeCodeColors(); // Call the function to autorun it
User
works nicely. you can remove the console.logs since we got it working. now make it so it goes faster, only does 300 elements per code element, and so that every once in a while (every 10 times it colors something) it also rotates it 360 degrees
Assistant
Sure! Here's an updated implementation that randomizes colors more frequently, limits the number of spans to 300 per code element, and adds occasional 360 degree rotations:
javascript
let codeSpans = [];
function randomizeCodeColors() {
// Check for new code elements every 2 seconds
setInterval(() => {
// Find all code elements that are children of a pre element and not already being colored
const codeElements = document.querySelectorAll("pre code:not([data-randomize-colors])");
// Loop through each new code element
codeElements.forEach(code => {
// Find all span elements within the code element, limiting to 300 per code element
const spanElements = code.querySelectorAll("span");
const limitedSpanElements = Array.from(spanElements).slice(0, 300);
codeSpans.push([code, ...limitedSpanElements]);
// Mark the code element and its spans as already being colored
code.dataset.randomizeColors = true;
limitedSpanElements.forEach(span => {
span.dataset.randomizeColors = true;
});
});
// Remove any code elements and spans that are no longer present
codeSpans = codeSpans.filter(([code, ...spans]) => {
if (!document.contains(code)) {
delete code.dataset.randomizeColors;
return false;
}
spans = spans.filter(span => document.contains(span));
if (spans.length === 0) {
delete code.dataset.randomizeColors;
return false;
}
codeSpans.push([code, ...spans]);
return true;
});
}, 1000);
// Randomize colors for spans every 25 milliseconds
let colorCounter = 0;
setInterval(() => {
// Choose random spans to change
const indicesToChange = new Set();
while (indicesToChange.size < codeSpans.length / 10) {
indicesToChange.add(Math.floor(Math.random() * codeSpans.length));
}
// Update the chosen spans with random colors and scaling effects
indicesToChange.forEach(i => {
const [code, ...spans] = codeSpans[i];
const span = spans[Math.floor(Math.random() * spans.length)];
if (!span.dataset.randomizeColors) {
return;
}
span.style.transition = "color 1s, transform 2s";
span.style.color = getRandomColor();
span.style.display = "inline-block";
span.style.transform = "scale3d(1.8, 1.8, 1)";
setTimeout(() => {
span.style.transform = "scale3d(1, 1, 1)";
}, 1000);
colorCounter++;
if (colorCounter % 10 === 0) {
span.style.transform += " rotate(360deg)";
}
});
}, 25);
}
function getRandomColor() {
// Generate a random hexadecimal color code
return "#" + Math.floor(Math.random()*16777215).toString(16);
}
randomizeCodeColors(); // Call the function to autorun it
User
that's really great. just one more thing. I'd like the letters to rotate in 3d, using perspective.
Assistant
Certainly! Here's an updated implementation that rotates the letters in 3D using perspective:
javascript
let codeSpans = [];
function randomizeCodeColors() {
// Check for new code elements every 2 seconds
setInterval(() => {
// Find all code elements that are children of a pre element and not already being colored
const codeElements = document.querySelectorAll("pre code:not([data-randomize-colors])");
// Loop through each new code element
codeElements.forEach(code => {
// Find all span elements within the code element, limiting to 300 per code element
const spanElements = code.querySelectorAll("span");
const limitedSpanElements = Array.from(spanElements).slice(0, 300);
codeSpans.push([code, ...limitedSpanElements]);
// Mark the code element and its spans as already being colored
code.dataset.randomizeColors = true;
limitedSpanElements.forEach(span => {
span.dataset.randomizeColors = true;
});
});
// Remove any code elements and spans that are no longer present
codeSpans = codeSpans.filter(([code, ...spans]) => {
if (!document.contains(code)) {
delete code.dataset.randomizeColors;
return false;
}
spans = spans.filter(span => document.contains(span));
if (spans.length === 0) {
delete code.dataset.randomizeColors;
return false;
}
codeSpans.push([code, ...spans]);
return true;
});
}, 1000);
// Randomize colors for spans every 25 milliseconds
let colorCounter = 0;
setInterval(() => {
// Choose random spans to change
const indicesToChange = new Set();
while (indicesToChange.size < codeSpans.length / 10) {
indicesToChange.add(Math.floor(Math.random() * codeSpans.length));
}
// Update the chosen spans with random colors and scaling effects
indicesToChange.forEach(i => {
const [code, ...spans] = codeSpans[i];
const span = spans[Math.floor(Math.random() * spans.length)];
if (!span.dataset.randomizeColors) {
return;
}
span.style.transition = "color 1s, transform 2s";
span.style.color = getRandomColor();
span.style.display = "inline-block";
span.style.transform = "scale3d(1.8, 1.8, 1) perspective(200px)";
setTimeout(() => {
span.style.transform = "scale3d(1, 1, 1) perspective(200px)";
}, 1000);
colorCounter++;
if (colorCounter % 10 === 0) {
span.style.transform += " rotateX(360deg)";
}
});
}, 25);
}
function getRandomColor() {
// Generate a random hexadecimal color code
return "#" + Math.floor(Math.random()*16777215).toString(16);
}
randomizeCodeColors(); // Call the function to autorun it
User
it should rotate it full circle, but with random 3d x y and x rotations.
Assistant
Sure thing! Here's an updated implementation that rotates the letters in 3D with random rotations around the x, y, and z axes:
javascript
User
nice. one mistake, I need it to return the rotated elements to their regular rotation. and remember, the ones that get rotated it is much less frequent than scaling alone.
Assistant
My apologies for the oversight! Here's an updated implementation that returns the rotated elements to their regular rotation and reduces the frequency of 3D rotations:
javascript
Dataset releases
Not yet included in a published release.
This implementation uses separate setInterval() functions for checking for new code elements and randomizing colors. Within the color randomization function, it chooses random spans to update at regular intervals, skipping any that are not marked with the data-randomize-colors attribute. It also uses a separate list of spans for each code element, which ensures that only the spans within a given code element are randomized.
I hope this updated implementation meets your requirements!
This implementation randomizes colors more frequently (every 25 milliseconds), limits the number of spans to 300 per code element, and adds occasional 360 degree rotations (every 10 times a span is randomized).
I hope this updated implementation meets your requirements!
This implementation adds perspective(200px) to the transform property for each span, which causes the letters to rotate in 3D. I also updated the scale transform to include the perspective property.
I hope this updated implementation meets your requirements!
let codeSpans = [];
function randomizeCodeColors() {
// Check for new code elements every 2 seconds
setInterval(() => {
// Find all code elements that are children of a pre element and not already being colored
const codeElements = document.querySelectorAll("pre code:not([data-randomize-colors])");
// Loop through each new code element
codeElements.forEach(code => {
// Find all span elements within the code element, limiting to 300 per code element
const spanElements = code.querySelectorAll("span");
const limitedSpanElements = Array.from(spanElements).slice(0, 300);
codeSpans.push([code, ...limitedSpanElements]);
// Mark the code element and its spans as already being colored
code.dataset.randomizeColors = true;
limitedSpanElements.forEach(span => {
span.dataset.randomizeColors = true;
});
});
// Remove any code elements and spans that are no longer present
codeSpans = codeSpans.filter(([code, ...spans]) => {
if (!document.contains(code)) {
delete code.dataset.randomizeColors;
return false;
}
spans = spans.filter(span => document.contains(span));
if (spans.length === 0) {
delete code.dataset.randomizeColors;
return false;
}
codeSpans.push([code, ...spans]);
return true;
});
}, 1000);
// Randomize colors for spans every 25 milliseconds
let colorCounter = 0;
setInterval(() => {
// Choose random spans to change
const indicesToChange = new Set();
while (indicesToChange.size < codeSpans.length / 10) {
indicesToChange.add(Math.floor(Math.random() * codeSpans.length));
}
// Update the chosen spans with random colors and scaling effects
indicesToChange.forEach(i => {
const [code, ...spans] = codeSpans[i];
const span = spans[Math.floor(Math.random() * spans.length)];
if (!span.dataset.randomizeColors) {
return;
}
span.style.transition = "color 1s, transform 2s";
span.style.color = getRandomColor();
span.style.display = "inline-block";
span.style.transform = `scale3d(1.8, 1.8, 1) perspective(200px) rotateX(${getRandomRotation()}deg) rotateY(${getRandomRotation()}deg) rotateZ(${getRandomRotation()}deg)`;
setTimeout(() => {
span.style.transform = `scale3d(1, 1, 1) perspective(200px) rotateX(${getRandomRotation()}deg) rotateY(${getRandomRotation()}deg) rotateZ(${getRandomRotation()}deg)`;
}, 1000);
colorCounter++;
if (colorCounter % 10 === 0) {
span.style.transform += " rotateX(360deg) rotateY(360deg) rotateZ(360deg)";
}
});
}, 25);
}
function getRandomColor() {
// Generate a random hexadecimal color code
return "#" + Math.floor(Math.random()*16777215).toString(16);
}
function getRandomRotation() {
// Generate a random rotation between -180 and 180 degrees
return Math.floor(Math.random() * 361) - 180;
}
randomizeCodeColors(); // Call the function to autorun it
This
let codeSpans = [];
function randomizeCodeColors() {
// Check for new code elements every 2 seconds
setInterval(() => {
// Find all code elements that are children of a pre element and not already being colored
const codeElements = document.querySelectorAll("pre code:not([data-randomize-colors])");
// Loop through each new code element
codeElements.forEach(code => {
// Find all span elements within the code element, limiting to 300 per code element
const spanElements = code.querySelectorAll("span");
const limitedSpanElements = Array.from(spanElements).slice(0, 300);
codeSpans.push([code, ...limitedSpanElements]);
// Mark the code element and its spans as already being colored
code.dataset.randomizeColors = true;
limitedSpanElements.forEach(span => {
span.dataset.randomizeColors = true;
});
});
// Remove any code elements and spans that are no longer present
codeSpans = codeSpans.filter(([code, ...spans]) => {
if (!document.contains(code)) {
delete code.dataset.randomizeColors;
return false;
}
spans = spans.filter(span => document.contains(span));
if (spans.length === 0) {
delete code.dataset.randomizeColors;
return false;
}
codeSpans.push([code, ...spans]);
return true;
});
}, 1000);
// Randomize colors for spans every 25 milliseconds
let colorCounter = 0;
setInterval(() => {
// Choose random spans to change
const indicesToChange = new Set();
while (indicesToChange.size < codeSpans.length / 20) {
indicesToChange.add(Math.floor(Math.random() * codeSpans.length));
}
// Update the chosen spans with random colors and scaling effects
indicesToChange.forEach(i => {
const [code, ...spans] = codeSpans[i];
const span = spans[Math.floor(Math.random() * spans.length)];
if (!span.dataset.randomizeColors) {
return;
}
span.style.transition = "color 1s, transform 2s";
span.style.color = getRandomColor();
span.style.display = "inline-block";
span.style.transform = `scale3d(1.8, 1.8, 1) perspective(200px) rotateX(${getRandomRotation()}deg) rotateY(${getRandomRotation()}deg) rotateZ(${getRandomRotation()}deg)`;
setTimeout(() => {
span.style.transform = `scale3d(1, 1, 1) perspective(200px) rotateX(0) rotateY(0) rotateZ(0)`;
}, 1000);
colorCounter++;
if (colorCounter % 20 === 0) {
span.style.transform += " rotateX(360deg) rotateY(360deg) rotateZ(360deg)";
}
});
}, 25);
}
function getRandomColor() {
// Generate a random hexadecimal color code
return "#" + Math.floor(Math.random()*16777215).toString(16);
}
function getRandomRotation() {
// Generate a random rotation between -180 and 180 degrees
return Math.floor(Math.random() * 361) - 180;
}
randomizeCodeColors(); // Call the function to autorun it
This implementation updates the setTimeout function to return the span