gpt4 book ai didi

javascript - 如何使用 javascript 将 Bootstrap 图标添加到 Canvas ?如果有办法

转载 作者:行者123 更新时间:2023-11-29 10:38:33 26 4
gpt4 key购买 nike

这是我到目前为止尝试过的方法。

     ctx.setAttribute("class", "glyphicon glyphicon-time");

还有;

    var icon = document.createElement("span");

icon.className ="glyphicon glyphicon-time";
this.appendChild(icon);

最佳答案

您可以使用 html2canvas.js .

以下代码可能对您有所帮助:

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';
}
});

这是 fiddle :http://jsfiddle.net/k7moorthi/qpyj02k8/

这里我使用了html2canvas.js将 html 代码渲染到 Canvas 上, Canvas 将动态创建并返回内存中的 Canvas 元素(如果需要,您可以将其附加到某些元素)。我不想使用动态创建的 Canvas ,因为当我想更新 Canvas 内容时它会导致一些问题。所以我只获取 Canvas 的 dataurl,将其转换为图像对象并将其绘制到我现有的 Canvas 元素中。

编辑:

作为@Patrick Evans提到,你可以直接使用fillText()方法将gylphicon渲染到canvas中。

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);

这是更新后的 fiddle :http://jsfiddle.net/k7moorthi/qpyj02k8/3/

要获取图标的字符代码,请使用 gylphicon cheat sheet .单击备忘单上每个图标下方的复制下拉菜单,然后单击 Unicode HTML 实体。现在 unicode 值将被复制到您的剪贴板。它看起来像这样 *。将 &# 替换为 0 并删除 ; 以获取字符代码。

关于javascript - 如何使用 javascript 将 Bootstrap 图标添加到 Canvas ?如果有办法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32976961/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com