Tag Archives: canvas

WordPress Admin Bar Gotcha

To get the javascript code in my previous post to work correctly while logged in to WordPress I had to put in a ‘hack’ that checked for the presence of the admin bar and accounted for its height when calculating mouse coordinates. I was asked why this was necessary, and I didn’t have a very good answer… I just knew that the presence of the admin bar made a certain value 28px too large. But I have a better answer now:

The problem is that the admin bar includes the following css:

html {
    margin-top:28px !important;
}

The admin bar is then placed in the margin with position: fixed, which is what causes it to stay at the top of the page even after you scroll. This margin is a problem because we need two values to calculate the position of a mouse click relative to the canvas: the mouse event’s pageX/pageY coordinates, and the canvas element’s offsets (top and left). The two canvas offsets are relative to the body element, so the margin, which is outside of the body, doesn’t affect this number. However pageX/pageY are relative to the entire document, which includes that additional margin. So if the admin bar is present, pageY will be 28px larger than we expect it to be.

HTML5 Canvas


Sorry, it doesn’t look like your browser supports canvas!

Our group is constantly learning about and evaluating new technologies to use in our products. It’s important to be proactive in experimenting with these technologies so that we have them in our tool belt; if we aren’t aware of or don’t know enough about some emerging technology, we very well may miss an opportunity to use it.

HTML5 includes a lot of new features, and one of these is the <canvas> element. The basic idea is that an area of some fixed size is defined in a web page, then javascript is used to draw on, animate, and interact with that area. We haven’t had a chance to use canvas in any of our projects, but the box above is a very simple demo that was written to get familiar with this feature. If your browser supports canvas, you can click in the box to interact with it. For some (much) more complex demos, check out this list.