Jen*_*ens 7 javascript css fonts fabricjs
我正在使用Fabric JS开发一个大型自定义应用程序,我已经做得很好.但我使用webfont的init加载文本对象有问题.
只要该字体在客户端的计算机上是本地的,我工作正常,因为未加载webfont并且画布上的文本对象以默认的sans-serif字体系列呈现.
简而言之,就是我所做的(在这个例子中,我使用"allstar"作为我的webfont):
CSS: 在fabric.js之前,css被加载到头部的fonts.css中
@font-face{
font-family:'allstar';
src:
url('/path-to-fonts/all_star-webfont.eot');
src:
url('/path-to-fonts/all_star-webfont.eot?#iefix') format('embedded-opentype'),
url('/path-to-fonts/all_star-webfont.woff') format('woff'),
url('/path-to-fonts/all_star-webfont.ttf') format('truetype'),
url('/path-to-fonts/all_star-webfont.svg#allstarregular') format('svg');
font-weight:normal;
font-style:normal
}
Run Code Online (Sandbox Code Playgroud)
Javascript: 这是在$(document).ready(function(){})里面的页面底部加载的
var textSample = new fabric.Text(text, {
fontFamily: "allstar",
});
canvas.add(textSample);
canvas.renderAll();
Run Code Online (Sandbox Code Playgroud)
如果我在页面上的其他地方使用该字体(例如:在带有点和加载字体的透明span标签中),它可以正常工作.但我不认为这是一种正确的编码方式.
我使用fabric.js版本1.3.0
使用fabric.js和Google Web Fonts,以下代码对我有用:
小提琴: http: //jsfiddle.net/DanBrown180/vvL6f/
CSS
<Style>
@font-face {
font-family: 'Jolly Lodger';
font-style: normal;
font-weight: 400;
src: local('Jolly Lodger'), local('JollyLodger'),
url(http://themes.googleusercontent.com/static/fonts/jollylodger/v1/RX8HnkBgaEKQSHQyP9itiaRDOzjiPcYnFooOUGCOsRk.woff) format('woff');
}
<style>
Run Code Online (Sandbox Code Playgroud)
JavaScript
<script type = "text/javascript">
canvas = new fabric.Canvas('c');
var text = new fabric.Text("Web Font Example", {
left: 200,
top: 30,
fontFamily: 'Jolly Lodger',
fill: '#000',
fontSize: 60
});
canvas.add(text);
</script>
Run Code Online (Sandbox Code Playgroud)
超文本标记语言
<canvas id="c" height="200px" width="400px"></canvas>
Run Code Online (Sandbox Code Playgroud)
看起来是因为 Google Web Fonts css 代码使用:
src: local('Jolly Lodger'), local('JollyLodger')
Run Code Online (Sandbox Code Playgroud)
在CSS中
| 归档时间: |
|
| 查看次数: |
4940 次 |
| 最近记录: |