use*_*899 5 html canvas emoji html5-canvas
我想在 html 画布上打印一个表情符号。
可以使用图像来完成,但是很麻烦。有没有更好的办法?
const canvas = document.querySelector("#canvas");
const contex = canvas.getContext("2d");
var img = new Image();
img.src = "emoji.jpg";
img.addEventListener(
"load",
() => {
context.drawImage(img, 0, 0, 200, 200);
}
);
img.src = "img.jpg";
Run Code Online (Sandbox Code Playgroud)
#canvas {
background: red;
}
Run Code Online (Sandbox Code Playgroud)
<canvas id="canvas"></canvas>
Run Code Online (Sandbox Code Playgroud)
好吧,我认为我的问题被误解了。我能够在画布上将表情符号绘制为图像。我不想那样做。因为在画布上打印之前我必须对所有表情符号进行截图并裁剪它们,这很麻烦。我正在寻找一种更有效的方法。
您可以fillText
用来绘制 unicode 表情符号。
您可以从emojipedia复制和粘贴表情符号。
我当然不是这方面的专家,所以我不知道这样做是否有很大的缺点,但无论如何,这里有一些事情需要考虑。
const canvas = document.querySelector("#canvas");
const contex = canvas.getContext("2d");
// The size of the emoji is set with the font
contex.font = '100px serif'
// use these alignment properties for "better" positioning
contex.textAlign = "center";
contex.textBaseline = "middle";
// draw the emoji
contex.fillText('', canvas.width / 2, canvas.height / 2)
Run Code Online (Sandbox Code Playgroud)
#canvas {
background: #ccc;
}
Run Code Online (Sandbox Code Playgroud)
<canvas id="canvas"></canvas>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4638 次 |
最近记录: |