/    Sign up×
Community /Pin to ProfileBookmark

How to replace hex bg colors to use images instead in JavaScript?

Hi there,

I have the following code lines in my JavaScript code:

[CODE]var colors = [“#F08”, “#0B8”, “#04F”, “#FD0”, “#808”, “#F20”];[/CODE]

This sets the bg colors of my divs. This var is only used here:

[CODE]function activateItems(callback) {
var itemsActivated = false;
for (var i=0;i<cfg.rows;++i) for (var j=0; j<cfg.cols; ++j) {
var item = itemAt(i,j);
if(!item) continue;
if (item.deleted>0) {
item.style.background=colors[Math.floor(Math.random()*colors.length)];
itemsActivated = true;
}
}
if (itemsActivated)
animateAppear(callback ? callback : findPatterns);
}[/CODE]

and here:

[CODE]function animateDecor() {
if(state == ‘init’ || state == ‘game’) return;
for (var i=0;i<cfg.rows;++i) for (var j=0;j<cfg.cols;++j) {
var item = document.getElementById(“decor_”+i+”_”+j)
if(!item) continue;
item.style.background=colors[Math.floor(Math.random()*colors.length)];
}
animation = setTimeout(animateDecor, 120);
}[/CODE]

This all works great, only I don’t want to use hex (#) colors as background, I want to use images. These are located for example in: images/blue.png, images/red.png, etc. But how to pull this off? Tried several stuff including backgroundImage:url and so, but these where unsuccessful. I am just starting to learn this so some corrected code examples with comments will really help.

I think the solution is very simple, but I just don’t see it (yet)…

Many thanks

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @Nulll spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...