I've used Canvas to create a progress bar that takes a slider value and fills in the progress. This uses an overlay image to create the fill. My code is listed below.
I'm running into a problem, though, because I don't want the progress bar to use a slider. I just used the slider for testing purposes. What I really want to do is use this as an XP (experience) indicator. I have php running on my Joomla site which gets the current user id, group id, and their current xp value. I'd like to be able to access this user information to fill the progress bar based on the current users xp. Even further, I'd like to tell the bar that if the user group id is 11 (level 1) the xp range to fill should be 0xp-9xp. If the users group id is 12 (level 2) the xp range should be 10xp-99xp, and so on.
I could definitely use some direction to at least get me started. I'm thinking something along the lines of: if the users group is 11, then the xpRange = 9 and that the progress = xpValue / xpRange
HTML:
<body onload='init();'>
<form id="drawForm">
<input id="slider" type="range" min="0" max="100" onchange="drawImage();"
onkeyup="drawImage();" />
</form>
<canvas id="progress" width="670" height="161"></canvas>
Javascript:
<script language="javascript" type="text/javascript">
function init() {
canvas = document.getElementById('progress');
// Create the image resource
img = new Image();
// Canvas supported?
if (canvas.getContext) {
ctx = canvas.getContext('2d');
slider = document.getElementById('slider');
// Setup the onload event
img.onload = drawImage;
// Load the image
img.src = '../images/xpBar.png';
} else {
alert("Canvas not supported!");
}
}
function drawImage() {
// Draw the base image - no progress
drawBase(ctx);
// Draw the progress segment level
drawProgress(ctx);
}
var img,
iHEIGHT = 50,
iWIDTH = 240,
canvas = null,
ctx = null;
slider = null;
function drawBase(ctx) {
ctx.drawImage(img, 0, 0, iWIDTH, iHEIGHT, 0, 0, iWIDTH, iHEIGHT);
}
function drawProgress(ctx) {
var x1 = 0, // X position where the progress segment starts
x2 = 240, // X position where the progress segment ends
s = slider.value,
x3 = 0,
// Calculated x position where the overlayed image should end
x3 = (x1 + ((x2 - x1) / 100) * s);
ctx.drawImage(img, 0, iHEIGHT, x3, iHEIGHT, 0, 0, x3, iHEIGHT);
}
</script>
Any help would be greatly appreciated.