Drawing text with an outer stroke with HTML5's canvas

前端 未结 4 668
灰色年华
灰色年华 2020-12-30 01:57

I\'m currently using HTML5\'s canvas to render a number of strings using the fillText method. This works fine, but I\'d also like to give each string a 1px black outer strok

相关标签:
4条回答
  • 2020-12-30 02:21

    What's wrong with stroke? Since half the stroke will be outside of the shape, you can always draw the stroke first with a line width of double what you want. So if you wanted a 4px outer stroke you could do:

    function drawStroked(text, x, y) {
        ctx.font = '80px Sans-serif';
        ctx.strokeStyle = 'black';
        ctx.lineWidth = 8;
        ctx.strokeText(text, x, y);
        ctx.fillStyle = 'white';
        ctx.fillText(text, x, y);
    }
    
    
    drawStroked("37°", 50, 150);
    

    Which makes:

    enter image description here

    live fiddle here: http://jsfiddle.net/vNWn6/


    IF that happens to not look as accurate at smaller text rendering scales, you can always draw it large but scale it down (in the above case you'd do ctx.scale(0.25, 0.25))

    0 讨论(0)
  • 2020-12-30 02:29

    For a smooth shadow you can try this

    ctx.beginPath();
    ctx.fillStyle = 'white';
    ctx.font = "bold 9pt Tahoma";
    ctx.shadowBlur = 3;
    ctx.textAlign = "center";
    ctx.shadowColor = "#000000";
    ctx.shadowOffs = 0;                 
    ctx.fillText('www.ifnotpics.com', 100, 50);        
    ctx.closePath();
    
    0 讨论(0)
  • 2020-12-30 02:32

    Simon's answer is a good solution, yet it may have mitering glitches in some cases, especially with capital 'M', 'V', & 'W':

    drawStroked("MVW", 50, 150);
    

    http://jsfiddle.net/hwG42/1/

    In this case, it's best to utilize:

    ctx.miterLimit=2;
    

    http://jsfiddle.net/hwG42/3/

    Best of luck!

    0 讨论(0)
  • The above answers are great, using some of these solutions* and some of my own ideas, I made a quick reference and some creative alternatives in the below fiddle.

    *All credits given where due in the fiddle code

    drawStrokedText   ( text, x, y );
    drawShadowedText  ( text, x, y, shadowBlur);
    drawGlowingText   ( text, x, y, glowColorHex, glowDistance);
    drawBlurredText   ( text, x, y, blurAmount);
    drawReflectedText ( text, x, y, reflectionScale, reflectionOpacity);
    

    https://jsfiddle.net/vtmnyea8/

    Output of the fiddle:

    What it supports:

    • Outline text
    • Shadow text
    • Glowing text
    • Blurred text
    • Reflected text

    Some performance metrics:

    Considering using this in a game or at high frame rates? Check out this jsperf using the above methods.

    https://jsperf.com/various-text-effects-html5-2d-context

    0 讨论(0)
提交回复
热议问题