Click here to Skip to main content
15,880,956 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have been trying to debug my mostly java script button object's mouseover function. What I am trying to create, is a function call (in this case show()) within the object that will listen for the mouseover event and change the object's button color to grey. The problem I get is that the btnColor (keep in mind btnColor is an array with 2 colors, only btnColor[0] matters in this case) never updates color when I call it on the show()function through the addEventListener. I made sure the the event listener is indeed listening and will call the console log I have in the event listener, but I cannot get a change to the rectangle's color (though it can be changed manually and works after another call of show()). I think the problem is that the btnColor is not updating the original show() call, but I am not sure how to remedy this. Help is greatly appreciated!

My Code below

What I have tried:

<html>
        <head>
            <title>Playground</title>
    
        </head>
    
        <body>
    
            </form>
        <canvas width="1500" height="1500" id = "JScanvas"></canvas>
        <script>
        
     let canvas = document.getElementById("JScanvas"),
          c = canvas.getContext("2d");
    
        // ignore, a few functions I might need for this to run
    
        function buildRect(fillColor, outlineColor, outlineSize, x, y, w, h) {
    
          if (fillColor && outlineColor) {
            c.beginPath();
            c.rect(x, y, w, h);
            c.fillStyle = fillColor;
            c.fill();
            c.lineWidth = outlineSize;
            c.strokeStyle = outlineColor;
            c.stroke();
          } else if (fillColor && !outlineColor) {
            c.beginPath();
            c.rect(x, y, w, h);
            c.fillStyle = fillColor;
            c.fill();
          } else if (!fillColor && outlineColor) {
            c.beginPath();
            c.rect(x, y, w, h);
            c.lineWidth = outlineSize;
            c.strokeStyle = outlineColor;
            c.stroke();
          }
        }
    
        function write(str, x, y, color, txtSize, font) {
          let size = txtSize.toString();
          c.font = size + "px" + " " + font;
          c.fillStyle = color;
          c.fillText(str, x, y);
    
        }
        // end of useless functions
    
        class button {
          // mouse is {canvs: canvas, mClicked: t/f, mPosition: mousePosition{x, y}}
          constructor(name, order, btnColor, x, y, w, h, txtColor, txtSize, m, f) {
            this.name = name;
            this.order = order;
            this.btnColor = btnColor;
            this.x = x;
            this.y = y;
            this.w = w;
            this.h = h;
            this.txtColor = txtColor;
            this.txtSize = txtSize;
            this.m = m;
            this.f = f;
          }
    
          show() {
            //***This is the problem area***
            // :(
            //change color when mouse over button...at least ideally... 
            this.m.canvs.addEventListener("mousemove", (event) => {
              console.log("hello there");
              /*if(this.x < this.m.mPosition.x && this.m.mPosition.x < this.x + this.w && this.y < this.m.mPosition.y && this.m.mPosition.y < this.y + this.h)
               */
              this.btnColor[0] = "grey";
            }); // end of problem area
            console.log(this.btnColor);
            if (!this.btnColor[0] && !this.btnColor[0]) {
    
              buildRect("transparent", false, 1, this.x, this.y, this.w, this.h);
            } else if (!this.btnColor[0]) {
    
              buildRect(false, this.btnColor[1], 1, this.x, this.y, this.w, this.h);
            } else if (!this.btnColor[1]) {
    
              buildRect(this.btnColor[0], false, 1, this.x, this.y, this.w, this.h);
            } else {
              buildRect(this.btnColor[0], this.btnColor[1], 1, this.x, this.y, this.w, this.h);
    
            }
            c.fillStyle = this.txtColor;
            let theString = String(this.txtSize) + "px Arial";
            c.font = theString;
            let width = Math.round(c.measureText(c.fillText(this.name, -1000, 0)).width);
    
            if (width > this.w) {
              let center = this.x + (this.w / 2);
              let newSize = this.w / width;
              c.font = String(this.txtSize * newSize);
              let newWidth = Math.round(newSize * width);
              c.textAlign = "center";
              c.textBaseline = "middle";
              c.fillText(this.name, this.x + (this.w / 2), this.y + (this.h / 2));
    
            } else {
              c.textAlign = "center";
              c.textBaseline = "middle";
              c.fillText(this.name, this.x + (this.w / 2), this.y + (this.h / 2));
            }
          }
    
          clickButton(mouseX, mouseY) {
            if (mouseX >= this.x && mouseX <= this.x + this.w && mouseY >= this.y && mouseY <= this.y + this.h) {
              return true;
            } else {
              return false;
            }
          }
    
          runf() {
            this.f();
          }
        }
    
        //getting mouse position 
    
        function getMousePos(canvas, evt) {
          const rect = canvas.getBoundingClientRect();
          return {
            x: evt.clientX - rect.left,
            y: evt.clientY - rect.top
          };
        }
    
        canvas.addEventListener("mousemove", (evt) => {
          let r = getMousePos(canvas, event);
          mousePosition.x = r.x, mousePosition.y = r.y;
        });
    
        let mouse = {
          canvs: canvas,
          mClicked: false,
          mPosition: mousePosition
        };
    
        //button object to call: ("red" is the color of the button I am trying to change to grey)
        let cookie = new button("cookie", 1, ["red", false], 50, 100, 150, 50, "black", 30, mouse, 2);
        cookie.show();
    
        </script>
        </body>
    </html>
Posted
Updated 22-Nov-21 20:04pm
v2
Comments
gggustafson 24-Oct-21 14:26pm    
Why is there a close form?

"mousePosition" is not defined

1 solution

make your button like this
<input type="button" id="btn" value="btn" onmouseover ="colorin()" onmouseout="color()"><br />
	<script>function colorin(){<br />
    	document.getElementById('btn').style.color = "grey";<br />
      <br />
    }<br />
    function color(){<br />
    	document.getElementById('btn').style.color = color[1];<br />
        // color[1] is your choise of color when mouse is out<br />
    }<br />
    </script>

i think its solved now
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900