您可以使用html2canvas.js http://html2canvas.hertzen.com/.
以下代码可能对您有帮助:
HTML
<span class="glyphicon glyphicon-align-left"></span>
<canvas id="canvas" width="300" height="300"></canvas>
JS
html2canvas(document.querySelector('.glyphicon'), {
onrendered: function(canvas) {
var myCanvas = document.querySelector('#canvas');
var ctx = myCanvas.getContext('2d');
var img = new Image;
img.onload = function(){
ctx.drawImage(img,0,0);
};
img.src = canvas.toDataURL();
document.querySelector('.glyphicon').style.display = 'none';
}
});
这是小提琴:http://jsfiddle.net/k7moorthi/qpyj02k8/ http://jsfiddle.net/k7moorthi/qpyj02k8/
在这里我使用了html2canvas.js http://html2canvas.hertzen.com/将 html 代码渲染到画布上,画布将动态创建并返回内存中的画布元素(如果需要,可以将其附加到某些元素)。我不想使用动态创建的画布,因为当我想更新画布内容时它会导致一些问题。所以我只是获取画布的 dataurl,将其转换为图像对象并将其绘制到我已经存在的画布元素中。
EDIT:
As @帕特里克·埃文斯 https://stackoverflow.com/users/560593/patrick-evans提到了,可以直接使用fillText()
将 gylphicon 渲染到画布上的方法。
HTML
<canvas id="canvas" width="300" height="300"></canvas>
CSS
@font-face {
font-family: 'glyphicon';
src: url('glyphicons-halflings-regular.eot'); /* IE9 Compat Modes */
src: url('glyphicons-halflings-regular.eot?#iefix') format('embedded-opentype'), /* IE6-IE8 */
url('glyphicons-halflings-regular.woff2') format('woff2'), /* Super Modern Browsers */
url('glyphicons-halflings-regular.woff') format('woff'), /* Pretty Modern Browsers */
url('glyphicons-halflings-regular.ttf') format('truetype'), /* Safari, Android, iOS */
url('glyphicons-halflings-regular.svg#svgFontName') format('svg'); /* Legacy iOS */
}
JS
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.font = '20px glyphicon';
ctx.fillText(String.fromCharCode(0x2a), 10, 50);
这是更新的小提琴:http://jsfiddle.net/k7moorthi/qpyj02k8/3/ http://jsfiddle.net/k7moorthi/qpyj02k8/3/
要获取图标的字符代码,请使用字形备忘单 http://glyphicons.bootstrapcheatsheets.com/。单击备忘单上每个图标下方的副本下拉列表,然后单击Unicode HTML 实体。现在 unicode 值将被复制到剪贴板。它看起来像这样*
。代替&#
with 0
并删除;
获取字符代码。