I don't know why but I'm unable to use a "degree" symbol (°) with fillText. I tried everything: ALT+248, ALT+0176, °, copy/paste from web... All I get is nothing or °. Works fine in code or on the same page in HTML - just not in canvas.
Anyone know how to fix this? Thanks.
Unicode Characters: To print the degrees symbol in JavaScript, we need to use the write \u00B0 to represent the unicode character for the degress symbol. Store a celsius temperature into a variable.
On your keyboard, press Alt + 0176.
This works for me, using a literal Unicode character in the text. See an example here: http://jsfiddle.net/W5d7D/
Is your source file:
Here's a standalone example showing Unicode working in HTML and on Canvas. You can see this online at http://phrogz.net/tmp/canvas_unicode_degree_symbol.html
<!DOCTYPE html>
<html><head>
<meta charset="utf-8">
<title>Unicode Degree Symbol on HTML5 Canvas</title>
<style type="text/css" media="screen">
body { margin:4em; background:#eee; text-align:center }
canvas { background:#fff; border:1px solid #ccc; display:block; margin:2em auto }
</style>
</head><body>
<canvas></canvas>
<p>Unicode characters 'just work' with a proper setup, e.g. "212° Fahrenheit"</p>
<script type="text/javascript" charset="utf-8">
var c = document.getElementsByTagName('canvas')[0];
c.width = c.height = 400;
var ctx = c.getContext('2d');
ctx.font = "bold 24px sans-serif";
ctx.fillText("212° Fahrenheit", 100, 100);
</script>
</body></html>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With