0

I am trying to draw then erase rectangles on a gray canvas. I draw in red, then erase by drawing in gray. This works perfectly for fillRect() but not for strokeRect(). In the latter case, it acts like the opacity/alpha is not 1.0. This problem with strokeRect() is not fixed by explicitly use of css "opacity: 1.0;", or ctx.globalAlpha = 1.

Im including a stripped down version of the problem. Clicking on the drawFillRect buttons draw and "erase" a saturated opaque red filled rectangle. Clicking on the drawStrokeRect buttons draw semi-transparent rectangle outlines. Multiple clicks on the Red one make the outline redder and redder; multiple clicks on the Gray one make slowly fade the red outline to gray. I want it to behave like the filled case -- bright, opaque red outline, then cleared gray "erase".

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html> <head>
    <title></title>
    <style>
      canvas {background: Gray; opacity: 1.0;}
    </style>
  </head>

  <body>
    <canvas id="c" width="500" height="400"></canvas>
    <div>
    </div>
    <div>
      <button onclick="drawFillRect('Red')">drawFillRect('Red')</button>
      <button onclick="drawFillRect('Gray')">drawFillRect('Gray')</button>
      <button onclick="drawStrokeRect('Red')">drawStrokeRect('Red')</button>
      <button onclick="drawStrokeRect('Gray')">drawStrokeRect('Gray')</button>
    </div>
    <script>
      var  ce = document.getElementById("c");
      var ctx = ce.getContext('2d');
      ctx.globalAlpha = 1.0;
      function drawFillRect(color){ // GOOD: behaves like Alpha = 1.0
      ctx.fillStyle = color;
      ctx.fillRect(100,100,300,200);
      }
      function drawStrokeRect(color){ // BAD: behaves like Alpha = ~.5
      ctx.strokeStyle = color;
      ctx.strokeRect(100,100,300,200);
      }
    </script>
  </body> 
</html>

G

GFurnas
  • 101
  • 1
    The problem is that the rectangle is being drawn _inbetween_ two pixels. Adding `0.5` to the coordinates will resolve your issue. This is actually documented in MDN, I think… there’s probably a similar question here on StackOverflow as well. – Sebastian Simon Jun 15 '15 at 16:55
  • Here it is: [MDN: Canvas — A `lineWidth` example](https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Applying_styles_and_colors#A_lineWidth_example) — “Obtaining crisp lines requires understanding how paths are stroked…” – Sebastian Simon Jun 15 '15 at 17:03
  • Thanks. The between-pixel drawing was the problem. Interesting -- while the question already has an *answer*, this particular *question* (interpretion as an opacity effect: w multiple redrawing getting darker) has not been asked. That is, several effects of between pixels drawing might confuse people (like me) and all have the same resolution. From the standpoint of helping those who come with questions about the effects they are seeing, there might be some use in keeping the question, and pointing to the unifying answer. I'll will happily delete the question if others disagree. – GFurnas Jun 16 '15 at 19:15

0 Answers0