'How to draw rectangles with different colors on a canvas?

The following code draws two rectangles:

const demoCanvas = document.getElementById('canvas-demo').getContext('2d');

window.onload = function() {
  demoCanvas.fillStyle = 'red';
  demoCanvas.rect(10, 10, 60, 60);
  demoCanvas.fill(); // first rectangle

  demoCanvas.fillStyle = 'blue';
  demoCanvas.rect(10, 110, 60, 60);
  demoCanvas.fill();
}
<canvas id="canvas-demo" width="300" height="300">

The output is two blue rectangles. I tried adding begin and close path as well, but the rectangles take only one color for some reason. In this case, it's blue. How can I fix this?



Solution 1:[1]

I think, you are approaching it from a wrong direction, and that's the root of your problem. Why bother to covert everything ahead of time, without even knowing if you are ever going to need the result (maybe you are only interested in closing price to begin with, so why bother converting the other values)?

Rather that convert(date)(using cc): Money just create get(date)(cc): BigDecimal. This way, you can carry your Money fields around in the "canonical" form, and convert them on demand to whatever currency you need at the call site.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dima