将HTML5 Canvas字体与Snapchat的字体完全匹配

时间:2014-04-11 20:04:17

标签: javascript html5 canvas fonts html5-canvas

我正在尝试使用HTML5 Canvas重新创建Snapchat“snap”。让我难过的部分是让字体看起来一样。 Snapchat使用的字体似乎是Helvetica,但我不能让我的文字看起来像真正的快照。

这是我尝试匹配字体。我在快照的实际屏幕截图上绘制了我的文字,因此很容易比较。您可以编辑JavaScript变量以尝试匹配文本(JSFiddle:http://jsfiddle.net/9RB88/1/)。

HTML:

<img src='http://i.imgur.com/tivQ8xJ.jpg'>
<canvas width='640' height='1136'></canvas>

CSS:

img {display:none}

JavaScript的:

//--- EDIT THESE ----------------
// text styles...
var fontFamily = 'Helvetica';
var fontSize = '35px';
var fontWeight = 'normal';
// http://css-tricks.com/almanac/properties/f/font-weight/

var onTop = false;
// set this to "true" to place our text on top of the real text (for more precise comparing)
var differentColours = false;
// draw our text/background different colours, making it easier to compare (use when "onTop" is "true")

// tweak where the text is drawn
var horOffset = 2;
var vertOffset = 0;
//-------------------------------



var can = document.getElementsByTagName('canvas')[0];
var c = can.getContext('2d');
c.fillStyle = 'white';
c.fillRect(0, 0, can.width, can.height);
var img = document.getElementsByTagName('img')[0];
c.drawImage(img, 0, 0);

var top;
if(onTop) top = 531;
else top = 450;

if(differentColours) c.fillStyle = 'orange';
else c.fillStyle = 'black';
c.globalAlpha = 0.6;
c.fillRect(0, top, can.width, 74);
c.globalAlpha = 1;
var text = 'Template example thingy $&@?! 123';
if(differentColours) c.fillStyle = 'red';
else c.fillStyle = 'white';
c.textAlign = 'center';
c.font = fontWeight + ' ' + fontSize + ' ' + fontFamily;
c.fillText(text, can.width/2 + horOffset, top + 50 + vertOffset);

JSFiddle:http://jsfiddle.net/9RB88/1/

似乎其中一个问题是字母间距,但在HTML5 Canvas中无法更改。

我该怎么办?

1 个答案:

答案 0 :(得分:0)

Snapchat可能会为Helvetica嵌入一个非常具体的变体或类似字体。这并不意味着用户加载页面(包括在您自己的机器上)将在系统上安装该字体。

如果用户没有安装字体,浏览器将查找类似的字体,例如,它将在Windows上使用Arial,如果不可用,则使用另一种sans-serif字体。在Mac和Linux上同样的事情。这意味着它可能会紧密匹配但不会是相同的字体。如果他们不使用网络字体,则可能会在不同的操作系统上拍摄,然后您使用的操作会导致呈现不同的字体。

或换句话说 - 当你指定&#34; Helvetica&#34;作为字体系列,系统将尝试在该系列中查找字体,但并未给出Helvetica是您将获得的字体。浏览器将尝试在正在使用的系统上找到最接近的匹配字体。

您必须将他们使用的字体作为网络字体嵌入,否则无法保证字体相同。

要找出他们使用的字体(如果有的话),请打开他们正在使用的样式表。

我的2美分..

相关问题