by David Pallmann via Fire & Ice: David Pallmann's Web & Cloud Blog on 10/31/2011 6:30:00 PM
For my third experiment in JavaScript, I created a ticker that you could use to scroll stock information, news, or pretty much any textual content that has a label and a value. It looks like this and animates by scrolling to the left in a loop,. You can see a running version here. All the source is right there in the page, and there's a full listing at the end of this post as well.
<div id="ticker" style="width:1920px"> <canvas id="tape" width="4096" height="40">Your browser doesn't support canvas</canvas></div>
<body onload="OnLoad();"><div id="ticker" style="width:1920px"> <canvas id="tape" width="4096" height="40">Your browser doesn't support canvas</canvas></div>...<script> function OnLoad() { var items = []; items.push({ Symbol: 'AAA', Value: '1' }); items.push({ Symbol: 'BBB', Value: '22' }); items.push({ Symbol: 'CCC', Value: '3.3' }); items.push({ Symbol: 'DDD', Value: '44 1/4' }); items.push({ Symbol: 'EEE', Value: '5,000' }); items.push({ Symbol: 'FFF', Value: '-66' }); items.push({ Symbol: 'GGG', Value: '700' }); items.push({ Symbol: 'HHH', Value: '88' }); items.push({ Symbol: 'III', Value: '9999' }); items.push({ Symbol: 'JJJ', Value: '10' }); Ticker("ticker", "tape", items); }</script></body>
var TickerState = { canvas: undefined, ctx: undefined, tickerX: 0, dataWidth: 0, interval: 10 /* Fast: 10 Medium: 50 Slow: 100 */ }; function Ticker(divId, canvasId, items) { ticker = document.getElementById(divId); maxWidth = parseInt(ticker.style.width); TickerState.canvas = document.getElementById(canvasId); TickerState.ctx = TickerState.canvas.getContext('2d'); TickerState.ctx.font = "20px sans-serif"; TickerState.ctx.font = "20px Nova Flat, 20px sans-serif"; TickerState.ctx.textAlign = "left"; var x = 10; var y = 20; var firstTime = true; while (x < maxWidth * 4) { for (var i = 0; i < items.length; i++) { TickerState.ctx.fillStyle = "Blue"; TickerState.ctx.fillText(items[i].Symbol, x, y); x += TickerState.ctx.measureText(items[i].Symbol).width; TickerState.ctx.fillStyle = "Green"; TickerState.ctx.fillText(items[i].Value, x, y + 10); x += TickerState.ctx.measureText(items[i].Value).width; x += 10 + 6; } if (firstTime) { TickerState.dataWidth = x; firstTime = false; } } setTimeout(MoveTicker, TickerState.interval); } function MoveTicker() { TickerState.tickerX += 1; if (TickerState.tickerX >= TickerState.dataWidth) { TickerState.tickerX = 10; } TickerState.canvas.style.marginLeft = "-" + TickerState.tickerX.toString() + "px"; TickerState.canvas.style.clip = "rect(0px " + (maxWidth + TickerState.tickerX).toString() + "px 40px -" + TickerState.tickerX.toString() + "px)"; setTimeout(MoveTicker, TickerState.interval); }
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head> <title>Ticker</title> <link rel="stylesheet" type="text/css" href="http://fonts.googleapis.com/css?family=Nova+Flat"> <style> #ticker { font-family: 'Nova Flat', cursive; font-size: 20px; } #tape { font-family: 'Nova Flat', cursive; font-size: 20px; position: absolute; clip: rect(0px 2048px 40px 0px); vertical-align: top; margin-left: 0px; background-color: LightYellow; } </style><script> var TickerState = { canvas: undefined, ctx: undefined, tickerX: 0, dataWidth: 0, interval: 10 /* Fast: 10 Medium: 50 Slow: 100 */ }; function Ticker(divId, canvasId, items) { ticker = document.getElementById(divId); maxWidth = parseInt(ticker.style.width); TickerState.canvas = document.getElementById(canvasId); TickerState.ctx = TickerState.canvas.getContext('2d'); TickerState.ctx.font = "20px sans-serif"; TickerState.ctx.font = "20px Nova Flat, 20px sans-serif"; TickerState.ctx.textAlign = "left"; var x = 10; var y = 20; var firstTime = true; while (x < maxWidth * 4) { for (var i = 0; i < items.length; i++) { TickerState.ctx.fillStyle = "Blue"; TickerState.ctx.fillText(items[i].Symbol, x, y); x += TickerState.ctx.measureText(items[i].Symbol).width; TickerState.ctx.fillStyle = "Green"; TickerState.ctx.fillText(items[i].Value, x, y + 10); x += TickerState.ctx.measureText(items[i].Value).width; x += 10 + 6; } if (firstTime) { TickerState.dataWidth = x; firstTime = false; } } setTimeout(MoveTicker, TickerState.interval); } function MoveTicker() { TickerState.tickerX += 1; if (TickerState.tickerX >= TickerState.dataWidth) { TickerState.tickerX = 10; } TickerState.canvas.style.marginLeft = "-" + TickerState.tickerX.toString() + "px"; TickerState.canvas.style.clip = "rect(0px " + (maxWidth + TickerState.tickerX).toString() + "px 40px -" + TickerState.tickerX.toString() + "px)"; setTimeout(MoveTicker, TickerState.interval); }</script></head><body onload="OnLoad();"><div id="ticker" style="width:1920px"> <canvas id="tape" width="4096" height="40">Your browser doesn't support canvas</canvas></div> <br /> <br /> <br />Hello<script> var loaded = false; function OnLoad() { loaded = true; var items = []; items.push({ Symbol: 'AAA', Value: '1' }); items.push({ Symbol: 'BBB', Value: '22' }); items.push({ Symbol: 'CCC', Value: '3.3' }); items.push({ Symbol: 'DDD', Value: '44 1/4' }); items.push({ Symbol: 'EEE', Value: '5,000' }); items.push({ Symbol: 'FFF', Value: '-66' }); items.push({ Symbol: 'GGG', Value: '700' }); items.push({ Symbol: 'HHH', Value: '88' }); items.push({ Symbol: 'III', Value: '9999' }); items.push({ Symbol: 'JJJ', Value: '10' }); Ticker("ticker", "tape", items); }</script></body></html>