shadowBlur controls how soft the Canvas shadow appears. The value is a non-negative number. It is not defined as a number of pixels and is not affected by the current transformation matrix.
Negative values, Infinity and NaN are ignored.
const canvas = document.getElementById('shadow_demo');
const ctx = canvas.getContext('2d');
ctx.shadowColor = 'rgb(0 0 0 / 55%)';
ctx.shadowBlur = 18;
ctx.shadowOffsetX = 20;
ctx.shadowOffsetY = 20;
ctx.fillStyle = '#4b8bf4';
ctx.fillRect(50, 35, 170, 90);
A shadow needs a non-transparent shadowColor. At least one of shadowBlur, shadowOffsetX, or shadowOffsetY must also be non-zero. Shadow settings affect later drawing operations until changed or restored.
ctx.save();
ctx.shadowColor = 'rgb(0 0 0 / 45%)';
ctx.shadowBlur = 12;
ctx.shadowOffsetX = 8;
ctx.shadowOffsetY = 8;
ctx.fillRect(30, 30, 120, 70);
ctx.restore(); // later drawing has the previous state
shadowColor transparent and expecting blur or offsets alone to show a shadow.shadowBlur is an exact pixel radius.The default shadowBlur is 0.
The shadow blur and offset values are not affected by the current transformation matrix.
Set shadowColor to a non-transparent color and make at least one blur or offset value non-zero.
Yes. The same shadow state can affect fillText() and strokeText() as well as shapes.
Reset the shadow properties or wrap shadowed drawing in save() and restore().
shadowBlur | shadowColor | shadowOffsetX | shadowOffsetY | Canvas tutorial
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.