学习整理fabric.js更换画布文字元素字体样式
原图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3efd3d64ad1e763521b0b7b26dde8788.png)
效果图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8be8aa4807d1734f070da78f0732d87f.gif#pic_center)
实现代码
index.html
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Fabric.js 下拉选择文字更换字体</title>
<script src="../fabric5.2.1.js"></script>
<script src="../jquery-2.1.4.min.js"></script>
<style>
div#container {
padding: 30px;
font-family: 'verdana', lucida;
}
a {
color: #777;
display: block;
background-color: #ccc;
width: 300px;
padding: 0;
margin-top: 2px;
text-decoration: none;
}
@font-face {
font-family: 'SourceHanSansCN-Medium';
src: url("https://www.qipa250.com/fonts/SourceHanSansCN-Medium.otf");
}
@font-face {
font-family: 'HanYiYaKuHei85w';
src: url("https://www.qipa250.com/fonts/HanYiYaKuHei-85W.ttf");
}
@font-face {
font-family: 'SourceHanSansSongTiCN-Medium';
src: url("https://www.qipa250.com/fonts/SourceHanSansSongTiCN-Medium.otf");
}
@font-face {
font-family: 'YouSheBiaoTiHei-2';
src: url("https://www.qipa250.com/fonts/YouSheBiaoTiHei-2.ttf");
}
</style>
</head>
<body>
<div id="container">
<select style="font-family:'SourceHanSansCN-Medium';" name="certificate_title_font" onchange="getfontlib(this,0)">
<option value="SourceHanSansCN-Medium" style="font-family:'SourceHanSansCN-Medium'; ">思源黑体</option>
<option value="HanYiYaKuHei85w" style="font-family:'HanYiYaKuHei85w'; ">汉仪雅酷黑-85W</option>
<option value="SourceHanSansSongTiCN-Medium" style="font-family:'SourceHanSansSongTiCN-Medium'; ">
思源宋体
</option>
<option value="YouSheBiaoTiHei-2" style="font-family:'YouSheBiaoTiHei-2'; ">优设标题黑</option>
</select>
<canvas id="imageCanvas" width="300" height="300"></canvas>
<a id="lnkDownload" href="#">Save image</a>
</div>
<script src="script.js"></script>
</body>
</html>
script.js
var canvas = new fabric.Canvas('imageCanvas', {
backgroundColor: 'rgb(240,240,240)',
includeDefaultValues: false,
perPixelTargetFind: true,
hasBorders: false,
});
canvas.setWidth(500);
canvas.setHeight(500);
var text = new fabric.IText(
'奇葩呀,www.qipa250.com',
{
left: 10,
top: 10,
fontSize: 20,
fontFamily: 'Comic Sans',
}
)
canvas.add(text);
function getfontlib(that) {
var index = that.selectedIndex;
var font_val = that.options[index].value;
var text = that.options[index].text;
console.log('index==', index);
console.log('font_val==', font_val);
console.log('text==', text);
$(that).attr("style", "font-family:'" + font_val + "';");
let activeTxt = canvas.getActiveObject();
if (!activeTxt) return;
if (activeTxt.isEditing) {
activeTxt.setSelectionStyles({'fontFamily': font_val});
} else {
activeTxt.fontFamily = font_val;
let s = activeTxt.styles;
for (let i in s) {
for (let j in s[i]) {
s[i][j].fontFamily = font_val;
}
}
activeTxt.dirty = true;
}
canvas.renderAll();
}
var imageSaver = document.getElementById('lnkDownload');
imageSaver.addEventListener('click', saveImage, false);
function saveImage(e) {
console.log('toJSON==', canvas.toJSON());
console.log('toObject==', canvas.toObject());
this.href = canvas.toDataURL({
format: 'png',
quality: 0.8
});
this.download = 'canvas.png';
}
生成本地图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/c00ac5ca4944582a1273b614d21ffe5b.png#pic_center)