I am working on a project and had the idea to create a button that would generate a number using Math.floor and Math.random to select an image in an array, and then apply it as a background to my canvas.
I have been fiddling around with changing just the background color before getting images in the code, and I was curious if there is a way to change the background using a JavaScript function. So far the only way I have been able to change the canvas background is by editing the background-color in css, then putting the styles in the head and foregoing the css altogether.
Is this possible?
styleproperty of the element - this has the same effect as modifying the CSS file. eg:myElement.style = "background-color: blue; font-size: 30px;"var myBackgroundColor = '#dd0300'; myElement.style.backgroundColor = myBackgroundColor;. Wrapping this in a function could befunction changeBG(element, hexcode){ element.style.backgroundColor = hexcode;}and then use it likechangeBG (document.querySelector('canvas'),'#dd0300')fillRectin your canvas code which will then fill in the canvas, or use CSS and use the code @somethinghere has provided