Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML5 canvas text-shadow equivalent?

I'm attempting to create the same effect I can create in css's text-shadow, with HTML5's canvas.

I can do this with CSS:

font: 36px Impact;
text-shadow: 2px 2px 2px #000,
            -2px -2px 2px #000,
             2px -2px 2px #000,
            -2px 2px 2px #000,
             2px -2px 2px #000,
             2px 2px 2px #000,
            -2px -2px 0 #000,
             2px -2px 0 #000,
            -2px 2px 0 #000,
             2px -2px 0 #000;

Which creates characters that look like this:

http://i.imgur.com/LBZKxL9.png

But with HTML5's canvas, the stroke is too thin, and the shadows don't allow me to specify more than one, unlike text-shadow.

Has anyone figured out how to accomplish this?

like image 543
Troy Payne Avatar asked Jan 03 '14 17:01

Troy Payne


People also ask

What does text shadow mean in HTML?

The text-shadow CSS property adds shadows to text. It accepts a comma-separated list of shadows to be applied to the text and any of its decorations . Each shadow is described by some combination of X and Y offsets from the element, blur radius, and color.

How do I add a shadow to text in canvas?

To add shadow to text, use the shadowOffsetX and shadowOffsetY attributes. shadowOffsetX = number of pixels offset in the x direction; shadowOffsetY = number of pixels offset in the y direction; A blur effect added to the shadows will make the text beveled.


2 Answers

The html canvas command to apply shadows to text (or any drawing) is context.shadowBlur

enter image description here

Here's a starting spot towards your shadowed text effect:

Make sure you fill the text after stroking it or else the shadow will intrude into the fill.

ctx.font="75px verdana";
ctx.shadowColor="black";
ctx.shadowBlur=7;
ctx.lineWidth=5;
ctx.strokeText("HELLO",25,100);
ctx.shadowBlur=0;
ctx.fillStyle="white";
ctx.fillText("HELLO",25,100);
like image 87
markE Avatar answered Sep 19 '22 15:09

markE


You said the stroke is too thin, have you tried increasing the context's lineWidth property? I think a value of 4 with a shadow would be an appropriate value to match your image.

To draw multiple shadows in an HTML5 canvas, you could draw the element multiple times in the same place, with a different shadow each time.

like image 36
Alexander O'Mara Avatar answered Sep 18 '22 15:09

Alexander O'Mara