Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
110
rated 0 times [  112] [ 2]  / answers: 1 / hits: 25156  / 12 Years ago, thu, july 12, 2012, 12:00:00

I am new to html5 development could anyone tell me how to make text to move one side to other horizontally inside canvas..


More From » html

 Answers
11

Here's an example of how to animate text back and forth across the screen:



<html>
<head>
<title>HTML 5 Animated Text</title>
<script src=https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js></script>
<script type=text/javascript>
var context;
var text = ;
var textDirection =;

$(function()
{
context = document.getElementById(cvs).getContext(2d);
setInterval(animate(), 30);

textDirection =right;
textXpos = 5;
text = Animation!;
});

function animate() {
// Clear screen
context.clearRect(0, 0, 500, 500);
context.globalAlpha = 1;
context.fillStyle = '#fff';
context.fillRect(0, 0, 500, 500);

var metrics = context.measureText(text);
var textWidth = metrics.width;

if (textDirection == right) {
textXpos += 10;

if (textXpos > 500 - textWidth) {
textDirection = left;
}
}
else {
textXpos -= 10;

if (textXpos < 10) {
textDirection = right;
}
}

context.font = '20px _sans';
context.fillStyle = '#FF0000';
context.textBaseline = 'top';
context.fillText ( text, textXpos, 180);
}
</script>
</head>
<body>
<div id=page>
<canvas id=cvs width=500 height=500>
Your browser does not support the HTML 5 Canvas.
</canvas>
</div>
</body>
</html>


In action: http://jsfiddle.net/bS79G/


[#84310] Wednesday, July 11, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
madalyngriseldas

Total Points: 167
Total Questions: 92
Total Answers: 85

Location: Iceland
Member since Sat, Sep 17, 2022
2 Years ago
;