I would like to draw "user-supplied text" on an arc shape in HTML canvas using Javascript. This was the closest example I've seen for a soultion: Create an arc text using canvas
However in my code, I keep getting a "Uncaught TypeError: Cannot read property 'getContext' of null" Where am I going wrong?
HTML
<!DOCTYPE HTML>
<html>
<head>
<style>
body {
margin: 0px;
padding: 0px;
}
#canvas{
display:block;
margin:0 auto;
}
</style>
<script src="textOnPath3.js"></script>
</head>
<body>
<canvas id="myCanvas" width="900" height="400" style="border:1px solid #ff0000;"></canvas>
</body>
</html>
JAVASCRIPT
window.addEventListener("load", eventWindowLoaded, false);
var canvas = document.getElementById('myCanvas'),
context = canvas.getContext('2d'),
centerX = canvas.width / 2,
centerY = canvas.height - 30,
angle = Math.PI * 0.8,
radius = 150;
context.font = '30pt helvetica';
context.textAlign = 'center';
context.fillStyle = 'red';
context.strokeStyle = 'blue';
context.lineWidth = 4;
drawTextAlongArc(context, 'Arc Canvas Text', centerX, centerY, radius, angle);
// draw circle underneath text
context.arc(centerX, centerY, radius - 10, 0, 2 * Math.PI, false);
context.stroke();
function eventWindowLoaded () {
drawTextAlongArc();
}
function drawTextAlongArc(context, str, centerX, centerY, radius, angle) {
var len = str.length, s;
context.save();
context.translate(centerX, centerY);
context.rotate(-1 * angle / 2);
context.rotate(-1 * (angle / len) / 2);
for(var n = 0; n < len; n++) {
context.rotate(angle / len);
context.save();
context.translate(0, -1 * radius);
s = str[n];
context.fillText(s, 0, 0);
context.restore();
}
context.restore();
}