views:

100

answers:

2

I'm trying to write some text to a canvas element, but it seems that the font options I put in are being completely ignored. No matter what I change them to, it all comes out the same, which I believe to be the default 10px sans-serif. Heres what I have (this function runs on load)

function start() {
                canvas = document.getElementById('c');
                ctx = canvas.getContext('2d');
                ctx.fillStyle = "white";
                ctx.font = "12px monospace";
                ctx.textBaseline = "top";
            }

It doesn't work in either Firefox or Chrome.

A: 

Your font specified "monospace", which is a category of font. I found this example: context.font = "bold 60px 'Times New Roman', Times, serif"; Can you try that? Let's hope the user has 'Times New Roman' on his or her machine, since you can't embed fonts with HTML.

So basically, you may want to consider specifying an actual font name. Also, since HTML requests every single little image and piece of code and content as a separate bloated HTML request, and it doesn't let you embed fonts (let alone fine-grained control over which characters to embed), and they're rendered differently on every browser anyway, you may want to just try using FLASH.

EDIT: The real answer is that you probably have to set the font each time you acquire the 2D graphics context and draw something. That sounds like a pain in the ass, because it is. You're performing low-level drawing operations to render text. We are SOOOO beyond that today with the availability of a platform like Flash. Anyway, if you set the font, discard the context, then re-acquire it later somewhere else and try to draw again... the font setting will be lost, and you'll have to set the font again and draw before you discard the context. Basically, set the font and draw all your text with it before you discard the context.

Triynko
This is not a place for you to complain about the fact that you don't like HTML 5.
The.Anti.9
I'm not complaining, I'm commenting on a technical limitation and on the inconsistent implementations of the HTML5/JavaScript/CSS3 platform, and suggesting a legitimate alternative platform (Adobe Flash), which offers a fully integrated rendering and strongly-typed scripting architecture with a nice IDE, capable of embedding fonts and all contents into a single compressed file accessible with a single HTML request, that will operate and display consistently on any web browser on any OS.
Triynko
Here's an example of the inconsistencies you're going to have to deal with on all the different HTML/JavaScript/CSS implementations made by all the different browsers developers. http://www.williammalone.com/articles/html5-canvas-example/ At least the Flash Platform is developed by a single-entity, so you have highly-consistent plug-in implementations across browsers and operating systems... a TRUE standard, not just on paper.
Triynko
+1  A: 

As it turns out, the ctx.font change needs to be used in the same function that is doing the fillText()

This makes it work like a charm.

The.Anti.9
I suspect that each time you re-acquire the graphics context with canvas.getContext('2d') you get a new context that's reset to the default font. This makes sense, because you're performing drawing operations, and probably going to render all kinds of text in all different fonts, and cycle through them as you render each frame... so (re)setting the font for each new context makes sense.
Triynko