這篇文章主要介紹canvas如何實(shí)現(xiàn)彈幕效果,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡(jiǎn)單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、網(wǎng)站空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、欽南網(wǎng)站維護(hù)、網(wǎng)站推廣。
代碼如下
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <canvas >你的瀏覽器不支持canvas</canvas> </body> <script src="http://www.jq22.com/jquery/jquery-1.10.2.js"></script> <script> (function ($) { function Barrager(dom) { this.canvas = dom.get(0); this.ctx = this.canvas.getContext("2d"); this.msgs = new Array(300);//緩沖池,長(zhǎng)度越大,屏幕上顯示的就越多 this.width = 1280;//canvas分辨率1280*720 this.height = 720; this.canvas.width=this.width;//上邊的兩步可以省略,直接在這里賦值 this.canvas.height=this.height; this.font = "30px 黑體";//字體和字體大小 this.ctx.font=this.font; //顏色數(shù)組,在繪制過程中隨機(jī)從這里取出顏色 this.colorArr=["Olive","OliveDrab","Orange","OrangeRed","Orchid","PaleGoldenRod","PaleGreen","PaleTurquoise","PaleVioletRed","PapayaWhip","PeachPuff","Peru","Pink","Plum","PowderBlue","Purple","Red","RosyBrown","RoyalBlue","SaddleBrown","Salmon","SandyBrown","SeaGreen","SeaShell","Sienna","Silver","SkyBlue"]; this.interval = ""; this.draw = function () {//繪制方法 if (this.interval != "")return; var _this=this; this.interval = setInterval(function () {//每隔20毫秒重新繪制一次,間隔最好小于40,要不然效果就跟播放圖片差不多 //1,清除屏幕 _this.ctx.clearRect(0, 0, _this.width, _this.height); _this.ctx.save(); //2,循環(huán)緩沖區(qū)域,把沒有設(shè)置Left,Top,Speed,Color先賦值,賦值的就改變left值(產(chǎn)生移動(dòng)效果),left值小于200就會(huì)從緩沖區(qū)移除 for (var i = 0; i < _this.msgs.length; i++) { if (!(_this.msgs[i] == null || _this.msgs[i] == "" || typeof(_this.msgs[i]) == "undefined")) { if(_this.msgs[i].L==null || typeof(_this.msgs[i].L)=="undefined"){ _this.msgs[i].L=_this.width; _this.msgs[i].T=parseInt(Math.random() * 700); _this.msgs[i].S=parseInt(Math.random() * (10 - 4) + 4); _this.msgs[i].C=_this.colorArr[Math.floor(Math.random() * _this.colorArr.length)]; }else{ if(_this.msgs[i].L<-200){ _this.msgs[i]=null; }else { _this.msgs[i].L=parseInt(_this.msgs[i].L-_this.msgs[i].S); _this.ctx.fillStyle =_this.msgs[i].C; _this.ctx.fillText(_this.msgs[i].msg,_this.msgs[i].L,_this.msgs[i].T); _this.ctx.restore(); } } } } }, 20); }; //添加數(shù)據(jù),數(shù)據(jù)格式[{"msg":"nihao"}] this.putMsg = function (datas) {//循環(huán)緩沖區(qū),把位置是空的裝填上數(shù)據(jù) for (var j = 0; j < datas.length; j++) { for (var i = 0; i < this.msgs.length; i++) { if (this.msgs[i] == null || this.msgs[i] == "" || typeof(this.msgs[i]) == "undefined") { this.msgs[i] = datas[j]; break; } } } this.draw(); }; this.clear = function () {//清除定時(shí)器,清除屏幕,清空緩沖區(qū) clearInterval(this.interval); this.interval=""; this.ctx.clearRect(0, 0, this.width, this.height); this.ctx.save(); for(var i=0;i<this.msgs.length;i++){ this.msgs[i]=null; } }; } $.fn.barrager = function (para) { if (typeof(para)=="string") { try{ var api = $(this).data('barrager_api'); api[para].apply(api); }catch (e){} } else if (typeof para == 'object' || !para) { $this = $(this); if ($this.data('barrager_api') != null && $this.data('barrager_api') != ''){ var api = $this.data('barrager_api'); api.putMsg(para); }else{ var api = new Barrager($this); $this.data('barrager_api', api); api.putMsg(para); } } else { $.error('Method ' + method + ' does not exist on jQuery.slidizle'); } return this; } })(jQuery); </script> <script> // $('canvas').barrager([{"msg":"這是我發(fā)的。。。哈哈哈"}]);// 發(fā)送彈幕 $('canvas').barrager([{"msg":"看著不錯(cuò)。。。。"},{"msg":"哈哈哈。。。。"},{"msg":"不錯(cuò)不錯(cuò)。。"},{"msg":"真好看。。。。"}]);//多條發(fā)送方式 // $('canvas').barrager("clear"); //清除/關(guān)閉彈幕 </script> </html>
以上是“canvas如何實(shí)現(xiàn)彈幕效果”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
文章名稱:canvas如何實(shí)現(xiàn)彈幕效果
本文網(wǎng)址:http://m.rwnh.cn/article30/jdgcso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、用戶體驗(yàn)、靜態(tài)網(wǎng)站、小程序開發(fā)、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)