burger menu icon
WillMaster

WillMaster > LibraryMarketing With Software

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Random Colored Circle

Just for fun, I made a circle filled with a random color. It runs with JavaScript.

This illustration is a circle filled with one of the five Christmas colors — red, green, gold, silver, and white. The color changes every 5 seconds.

(The demonstration was put in a div with a background color so the white-colored circle would be visible.)

The color selection is random, except it won't randomly show the same color selection twice in a row.

Wanna play?

First:

Construct the circle. It's HTML, a square div with rounded corners and a background color:

<div id="my-circle" style="
   background-color:transparent;
   width:100px; 
   height:100px; 
   border-radius:50% 50%;">
</div>

The div needs to have an id value. You may change my-circle to a different id value.

The div must be square to form a circle; otherwise, you end up with an oval. Change the two instances of 100px to the dimensions you prefer.

Second:

Place this JavaScript somewhere below the div with the circle. Customization notes follow.

<script type="text/javascript">
var IDofCircle = "my-circle";
var Colors = new Array( "red", "green", "gold", "#bab9c1", "white" );
var Frequency = 1; // number of seconds wait before new color; use 0 (zero) to avoid animation.
var LastSelection = -1;
var NumberOfColors = Colors.length;
function ChangeTheCircleColors()
{
   var Selection = Math.floor(Math.random() * NumberOfColors);
   if( NumberOfColors > 1 )
   {
      while(Selection==LastSelection) { Selection = Math.floor(Math.random() * NumberOfColors); }
   }
   LastSelection = Selection;
   document.getElementById(IDofCircle).style.backgroundColor = Colors[Selection];
}
ChangeTheCircleColors();
if(Frequency>0) { setInterval(ChangeTheCircleColors,parseInt(Frequency*1000)); }
</script>

The my-circle value in the JavaScript must be identical to the id value of the div with the cirle.

Replace "red", "green", "gold", "#bab9c1", "white" with your own list of colors. There is no built-in limit to how many colors you may specify. Colors may be specified as color name, hex, rgb, or hsl. For example, the color gold can be specified as "#fed631", "gold", "rgb(254,214,49)", or "hsl(48,99%,59.42%)".

The frequency value 1 represents 1 second between color changes. The value 2 would represent 2 seconds. 5 represents 5 seconds between color changes. So long as the number is more than 0 seconds, the color is supposed to change every specified number of seconds. To opt out of the color-change animation, specify the number 0.

You are ready to go. Load the page and watch the color changes.

(This content first appeared in Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2025 Will Bontrager Software LLC