【问题标题】:Canvas text not showing? - JavaScript画布文字未显示? - JavaScript
【发布时间】:2015-03-21 00:33:13
【问题描述】:

我正在使用ctx.fillText 函数在我的屏幕上绘制文本,但它似乎没有出现。

overlay("rgb(50, 50, 200)", "This is the beginning screen", 48, "black", "center", centerx, centery)

var overlay = function(colour, text, font, size, fontColour, oreintation, x, y){
    ctx.font = size + "px " + font + " " + fontColour
    ctx.textAlign = oreintation
    ctx.fillStyle = colour
    ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height)
    ctx.fillText(text, x, y)
}

【问题讨论】:

    标签: javascript canvas text


    【解决方案1】:
    • 您必须先定义您的函数var overlay,然后才能使用 `overlay(...);

    • centerx & centery 未定义

    • overlay(...) 中缺少字体 font

    • 背景填充与文本填充颜色相同,因此文本不显示

    工作演示:

    var canvas=document.getElementById("canvas");
    var ctx=canvas.getContext("2d");
    var cw=canvas.width;
    var ch=canvas.height;
    
    
    var overlay = function(colour, text, font, size, fontColour, oreintation, x, y){
      ctx.font = size + "px " + font + " " + fontColour
      ctx.textAlign = oreintation
      ctx.fillStyle = colour
      ctx.fillStyle='gold';
      ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height)
      ctx.fillStyle=fontColour,
      ctx.fillText(text, x, y)
    }
    
    overlay(
      "rgb(50, 50, 200)", 
      "This is the beginning screen",
      'verdana', 
      48, 
      "black", 
      "center", 
      25,
      50
    );
    body{ background-color: ivory; }
    #canvas{border:1px solid red;}
    <canvas id="canvas" width=300 height=300></canvas>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-04
      • 1970-01-01
      相关资源
      最近更新 更多