Unicode characters not rendering properly in HTML5 canvas(Unicode 字符无法在 HTML5 画布中正确呈现)
问题描述
我正在尝试使用 HTML5 画布元素呈现 unicode 高音谱号.使用正确的字符代码(特别是 1D120)时,它在 HTML 中呈现良好,但是当我尝试在画布内使用它时,会出现一个奇怪的字符
I am trying to render a unicode treble clef using the HTML5 canvas element. When using the correct character code (specifically 1D120), it renders fine in HTML, but when I try to use it inside of a canvas a weird character appears
以下代码在我的 javascript 文件中,它在画布上发挥了它的魔力...
The following code is in my javascript file which works its magic on the canvas...
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
context.font = "48px serif";
context.strokeText("u1D120", 10, 50);
<h1>𝄠</h1>
<canvas id="canvas" width="100" height="100">
</canvas>
很遗憾,我不能放角色的照片,因为我的声望还太低.
Unfortunately I can't put a picture of the character because my rep is too low as of yet.
感谢您深入了解可能导致此问题的原因.提前致谢!
Any insight into what might be causing this problem is appreciated. Thanks in advance!
推荐答案
JavaScript 字符串使用 UTF-16 编码.您的字符需要两部分转义,因为它是一个需要 2 个 UTF-16 字符的 3 字节 UTF-8 序列 代码点.
JavaScript strings use UTF-16 encoding. Your character requires a two-part escape because it's a 3-byte UTF-8 sequence codepoint that requires 2 UTF-16 characters.
从比我聪明的人的博客文章中盗取方便的功能:
function toUTF16(codePoint) {
var TEN_BITS = parseInt('1111111111', 2);
function u(codeUnit) {
return '\u'+codeUnit.toString(16).toUpperCase();
}
if (codePoint <= 0xFFFF) {
return u(codePoint);
}
codePoint -= 0x10000;
// Shift right to get to most significant 10 bits
var leadSurrogate = 0xD800 + (codePoint >> 10);
// Mask to get least significant 10 bits
var tailSurrogate = 0xDC00 + (codePoint & TEN_BITS);
return u(leadSurrogate) + u(tailSurrogate);
}
当你用你的代码调用它时:
When you invoke that with your code:
var treble = toUTF16(0x1D120);
你回来了 "uD834uDD20".
再次感谢 Axel Rauschmayer 博士提供上述代码 —阅读优秀的链接博客文章了解更多信息.
Thanks again to Dr. Axel Rauschmayer for the code above — read the excellent linked blog post for more information.
这篇关于Unicode 字符无法在 HTML5 画布中正确呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Unicode 字符无法在 HTML5 画布中正确呈现
基础教程推荐
- 如何在特定日期之前获取消息? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
