User0123456789
User0123456789

Reputation: 758

Change background colour of div inside canvas Javascript

I've got this program where I'm creating a paint software. When i press the ctrl key while moving the mouse, it turns on the eraser. I've got a div element that's a box that's tracking the mouse so the user knows where the mouse is in the canvas.

The problem I'm having is that I can't change the colour of the div element to white when it goes to eraser mode. Here's my code:

<!doctype html>
<html lang="en">
    <head> 
    <title>Coursework 1 Template v1.0</title>

    <style>
            #box {
          pointer-events: none;
          background-color: #000000;
          width: 5px;
          height: 5px;
        }
        </style>

    <script>
        var oCanvas, oCanvasContext; //declare the global variables used to hold and control the canvas element
        var sFillColour; //create a global variable used to hold the active/selected colour
        var sCanvasColour; //create a global variable used to hold the canvas colour
        var iMouseX, iMouseY; //declare the global variables used to hold the mouse's coordinates
        var iBrushWidth, iBrushHeight; //declare the global variables used to hold the selected brush sizes
            function fnTrackMouse(e) {
            //this function is called "everytime" the user's mouse is moved when over the associated element (in this case the canvas)
            //we have also added the ability for it to accept a parameter (called e, actually we can call it anything but as event is a reserved work "e" makes some sense
            var canvasRect = oCanvas.getBoundingClientRect(); //use this function to dynamically get the size of the canvas and its position
            iMouseX=(e.clientX - canvasRect.left - 3); //modify the original position of the mouse by accounting for the position on the canvas; on the x
            iMouseY=(e.clientY - canvasRect.top - 3); //modify the original position of the mouse by accounting for the position on the canvas; on the y

            var bx = document.getElementById("box");
            bx.style.left = (iMouseX + 1)+"px";
            bx.style.top = (iMouseY + 1)+"px";
            fnDebugMessage("Working!"+iMouseX)

            if (e.ctrlKey) { //this checks if the user has pressed the control key to use the eraser funtion
                fnSetFillColour(sCanvasColour); //calls the fnSetFillColour function with the background colour of the canvas in the parameter
                bx.style.background-color = #ffffff
            }       


            if (e.buttons==1) { //this checks to see if the user is pressing the left mouse button (1 = the left mouse button)
                //the user has pressed the left button - so lets start painting
                fnPaint(iMouseX, iMouseY, iBrushWidth, iBrushHeight); //call the fnPaint function and pass it the coordinates and size to paint
            }

        </script>

</head>

<!-- 
    this "onload" event fires when the HTML <body> has loaded. In essence, we are telling the browser that once the page 
    has completely loaded all the content to execute a script. 
    in this case the function being called is "fnInitialise" and we are passing it two parameters: 
        the first (work out how this sets the width) = 100 
        the second (work out how this sets the height) = 100 
-->


<body onload="fnInitialise(100, 100);">

    <!-- 
        this div block (HTML page divider) is used to hold the entire interactive painting HTML elements 
        the benefit of putting multiple elements in a single container is that if you set the location of the 
        container each of the elements held by the container will move relative to it; move one, move all 
    -->
    <div id="cw1MainContainer">


        <!-- this div block is only used to hold the HTML canvas element -->
        <div id="cw1CanvasContainer">
            <canvas id="cw1Canvas" onmousemove="fnTrackMouse(event);" onkeypress="fnBrushSize(event);"></canvas>
            <div id="box" style="position:absolute;" />
            <!-- 
                by specifing the onmouseover event the canvas will call the "fnTrackMouse" function EVERY time the 
                mouse moves 1 pixel over the canvas.
                by passing the JavaScript "event" we are effectively also passing details about the event, 
                e.g. where the mouse was, what buttons were pressed etc. 
            -->
        </div>
        </div>      

</body>

It's throwing a Invalid left-hand side in assignment error. How do I change the background colour of the div element? Please let me know if you want to see the rest of my code

Any help would be appreciated. Thanks

Upvotes: 0

Views: 471

Answers (1)

Yusuf Azan
Yusuf Azan

Reputation: 236

try to change it from this

bx.style.background-color = #ffffff

to this

box.style.backgroundColor = "#333333";

so no need to use (-) in the word backgroundColor..

I wish it helps

Upvotes: 2

Related Questions