Sign in to follow this  
coelacanth

fillStyle affects on a few objects

Recommended Posts

Hello, guys

could you explain it to me, why does the fillStyle in the drawPlayer function affects the grid too?

var canvas = document.getElementById("MyCanvas");
var ctx = canvas.getContext("2d");


function drawGrid() {
  
  for (var i = 0; i <= canvas.width; i = i + 32){
      ctx.beginPath();
      ctx.rect(0+i, 0, 2, canvas.height);
      ctx.fillStyle = "#F620";
      ctx.fill();
      ctx.closePath();
  }

for (var j = 0; j <= canvas.height; j = j + 32){
  
      ctx.beginPath();
      ctx.rect(0, 0+j,canvas.width, 2);
      ctx.fillStyle = "#F620";
      ctx.fill();
      ctx.closePath();
  }
    
}

function drawPlayer(){
  
    ctx.beginPath();
    ctx.rect(2, 2, 30, 30);
    ctx.fillStyle = "#F40";
    ctx.fill();
    ctx.closePath();
}


function draw() {
  
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    drawGrid();
    drawPlayer();
   

}

setInterval(draw, 10);

 

Share this post


Link to post
Share on other sites
1 hour ago, Fatalist said:

I think it can't parse this color, so it ignores it.

#F40 = #FF4400, but a 4-letter color can not be parsed, AFAIK. 

Thank you! I even didn't know that I also can use "red", "black", or change my color to something like "rgba(0, 0, 255, 0.5)". I'm very new to js. 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.