简体   繁体   English

HTML5 canvas中的fillText字体未更改为默认值

[英]fillText Font in HTML5 canvas doesn't change from default

What I am trying to do seems fairly simple, but I just can't get it to work properly. 我正在尝试做的事情似乎很简单,但是我无法使其正常工作。 I am trying to change the font type of an element drawn in a HTML5 canvas. 我正在尝试更改HTML5画布中绘制的元素的字体类型。 What I have is this: 我所拥有的是:

function drawStaticElements(){
    /*Few other non text elements here*/

    gameScreenCtx.font = "18px Buxton Sketch";
    //gameScreenCtx.font = '18px Comic Sans MS';
    gameScreenCtx.fillStyle = "white";
    gameScreenCtx.fillText(gameScreenCtx.font, 5, 20);

    gameScreenCtx.font = "18px Buxton Sketch";
    gameScreenCtx.fillStyle = "white";
    gameScreenCtx.fillText("Number of victories: "+numberOfVictories, 5, 40);

I don't know if this can be a browser version thing or something else, but in the PC I used to develop it, the code works fine, even when uploaded to webserver. 我不知道这是否可以是浏览器版本的东西,但是在我用来开发它的PC中,即使将代码上传到Web服务器,代码也可以正常工作。 If I try to access it from another PC it just always default to a same font, I tried some more common fonts and not even Comic Sans works. 如果尝试从另一台PC访问它,则它总是默认使用相同的字体,那么我尝试了一些更常见的字体,甚至连Comic Sans都没有。 The text shows the font name correctly, but it won't render in that font. 文本正确显示了字体名称,但不会以该字体呈现。

As a sample of what is happening I uploaded to a Bitballoon webserver and accessed it from 2 different computers, the result from the development computer is this: 作为所发生事件的示例,我上传到Bitballoon Web服务器并从2台不同的计算机上访问了它,开发计算机的结果是:

在此处输入图片说明

And the result from a different computer is this: 来自另一台计算机的结果是:

在此处输入图片说明

Am I doing something wrong here? 我在这里做错什么了吗?

Well, under normal circumstances a computer can only render fonts that are installed on the system, and even then I imagine there could be problems with their exact names and so on. 好吧,在通常情况下,计算机只能渲染系统上安装的字体,即使这样,我仍可以想象它们的确切名称可能存在问题,等等。 As for "common fonts", these differ between operating systems -- my Linux computer doesn't have Comic Sans MS, for instance. 至于“通用字体”,这在操作系统之间是不同的-例如,我的Linux计算机没有Comic Sans MS。

It certainly seems unlikely that every user (or even a large share of users) coming to your site will have your specific font installed. 当然,访问您网站的每个用户(甚至大量用户)似乎都不会安装您的特定字体。

A common solution to achieve a consistent look across all user devices is to use CSS3 Web Fonts that tell the browser where to download the actual font face before loading the page. 在所有用户设备上实现一致外观的常见解决方案是使用CSS3 Web字体 ,该字体告诉浏览器在加载页面之前在何处下载实际字体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM