You need to specify the canvas size in pixels. You can then scale the canvas using cm
as units.
canvas.width = 500; // px
canvas.height = 300; // px
canvas.style.width="5cm";
canvas.style.height="3cm";
The number of pixels in a canvas has to be explicitly/absolutely defined. Centimeters are a relative value.
solved CSS canvas width and height setting