JQuery和HTML5 Canvas實現(xiàn)彈幕效果
更新時間:2017年01月04日 11:40:16 作者:http://blog.csdn.net/phpfer/article/details/49764047
這篇文章主要介紹了JQuery和HTML5 Canvas兩種方法實現(xiàn)彈幕效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
JQuery和HTML5 Canvas兩種方法實現(xiàn)彈幕效果:

方法一,JQuery實現(xiàn)。
源碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--
<link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
<link href="favicon.ico" rel="Bookmark" type="image/x-icon" />
-->
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>JQuery彈幕</title>
<link href="" rel="stylesheet" />
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
<style type="text/css">
.ctxt{
background:#666;
width:1000px;
height:400px;
overflow:hidden;
margin:0 auto;
}
.ctxt p{
position:relative;
left:1000px;
margin:0;
padding:0;
}
</style>
</head>
<body>
<div id="" class="ctxt"></div>
<br />
<form method="post" action="" align="center">
<input type="text" id="msg" style="height:24px;width:200px;" /> <button type="button" id="submitBut">發(fā)布</button>
</form>
<script type="text/javascript">
$(document).ready(function(){
$("#submitBut").click(function(){
var msgtxt=$("#msg").val();
var colortxt = getReandomColor();
var topPos = generateMixed(3);
if (topPos > 300)
{
topPos = 30;
}
var newtxt = '<p style="top:'+topPos+'px; color:'+colortxt+'">'+$("#msg").val()+'</p>';
$(".ctxt").prepend(newtxt);
var addTextW = $(".ctxt").find("p").width();
$(".ctxt p").animate({left: '-'+addTextW+"px"}, 30000,function(){
$(this).hide();
});
});
});
//隨機獲取顏色值
function getReandomColor(){
return '#'+(function(h){
return new Array(7-h.length).join("0")+h
})((Math.random()*0x1000000<<0).toString(16))
}
//生成隨機數(shù)據(jù)。n表示位數(shù)
var jschars = ['0','1','2','3','4','5','6','7','8','9'];
function generateMixed(n) {
var res = "";
for(var i = 0; i < n ; i ++) {
var id = Math.ceil(Math.random()*9);
res += jschars[id];
}
return res;
}
</script>
</body>
</html>
方法二,HTML5 Canvas實現(xiàn)。
源碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--
<link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
<link href="favicon.ico" rel="Bookmark" type="image/x-icon" />
-->
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Canvas彈幕</title>
<link href="" rel="stylesheet" />
<style type="text/css">
#c1{background:#f1f1f1;}
span{color:#FFFFFF;}
</style>
</head>
<body>
<canvas id="c1" width="600" height="300" >
<span>該瀏覽器不支持html5</span>
</canvas>
<br />
<input type="text" name='smsg' value="" id="smsg" placeholder="請輸入內(nèi)容" /> <button id="send">發(fā)送</button>
<!--<button id='start' onclick="startFun('test')">Start</button>
<button id='stop' onclick="stopFun()">Stop</button>-->
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
var sv;
var sId;
var oC;
var oGC;
var numW;
var numH;
var maxTxt = 600;
$(function(){
$("#send").click(function(){
clearInterval(sId);
var m = $("#smsg").val();
//alert(m);
startFun(m);
$("#smsg").val('')
});
})
function doDraw(msg){
oC = document.getElementById('c1');
oGC = oC.getContext('2d');
numW = oC.width;
numH = oC.height;
console.log(numW+'-'+numH);
oGC.fillStyle="red";
//oGC.fillRect(0,0,numW,100);
oGC.fillText(msg,numW,100);
}
function startFun(msg){
doDraw(msg);
sId = setInterval(function(){
if (numW > -610)
{
numW--;
console.log(numW);
oGC.clearRect(0,0,oC.width,oC.height);
oGC.fillText(msg,numW,100);
} else {
oGC.clearRect(0,0,oC.width,oC.height);
clearInterval(sId);
}
},10);
}
</script>
</body>
</html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
省市區(qū)三級聯(lián)動jquery實現(xiàn)代碼
這篇文章主要為大家詳細紹了省市區(qū)三級聯(lián)動jquery實現(xiàn)代碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
ie8模式下click無反應(yīng)點擊option無反應(yīng)的解決方法
點擊select里面的option,將其賦值到上面的input,直接用jQuery寫的,問題是在用IE8打開的時候,點擊option沒有任何反應(yīng)2014-10-10
JQuery Ajax執(zhí)行跨域請求數(shù)據(jù)的解決方案
今天小編就為大家分享一篇關(guān)于JQuery Ajax執(zhí)行跨域請求數(shù)據(jù)的解決方案,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12
JQuery的html(data)方法與<script>腳本塊的解決方法
在使用Jquery的html(data)方法執(zhí)行寫數(shù)據(jù)到Dom元素時遇到一個問題:在data參數(shù)中包含script腳本塊的時候,html(data)方法的執(zhí)行結(jié)果與預(yù)期不符2010-03-03

