canvas 彈幕效果

来源:http://www.cnblogs.com/cynthia-wuqian/archive/2017/01/11/6272516.html
-Advertisement-
Play Games

Document 你的瀏覽器不支持canvas ...


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <canvas style="width: 1280px;height: 720px;background-color: rgba(0,0,0,0.2)">你的瀏覽器不支持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);//緩衝池,長度越大,屏幕上顯示的就越多
        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;
        //顏色數組,在繪製過程中隨機從這裡取出顏色
        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,迴圈緩衝區域,把沒有設置Left,Top,Speed,Color先賦值,賦值的就改變left值(產生移動效果),left值小於200就會從緩衝區移除
                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);
        };
        //添加數據,數據格式[{"msg":"nihao"}]
        this.putMsg = function (datas) {//迴圈緩衝區,把位置是空的裝填上數據
            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 () {//清除定時器,清除屏幕,清空緩衝區
            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":"這是我發的。。。哈哈哈"}]);// 發送彈幕
    
    $('canvas').barrager([{"msg":"看著不錯。。。。"},{"msg":"哈哈哈。。。。"},{"msg":"不錯不錯。。"},{"msg":"真好看。。。。"}]);//多條發送方式

    // $('canvas').barrager("clear"); //清除/關閉彈幕
</script>
</html>

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 集合技術 作業 作業 猜數字游戲 猜數字游戲 /* * 猜數字游戲 */ public class HomeWork1 { public static void main(String[] args) { // 獲取被猜的那個數字,需要使用隨機數類產生 Random r = new Random() ...
  • Memory leak patterns in JavaScript Handling circular references in JavaScript applications Abhijeet Bhattacharya and Kiran Shivarama SundarPublished o ...
  • 以下分析均採取沙箱模式 js (function (window) { //為了提高性能把需要的變數統一提前聲明 var arr = [], push = arr.push; //為區別jQuery,此文章以iQuery來定義封裝的函數 function iQuery( selector ) { r ...
  • echarts是百度的一個圖表插件,確實好用美觀。 之前實習接觸到的頁面大多是下麵這種調用方式 這次有一個頁面需要計時器反覆載入新數據,然後重繪echarts圖表。一開始我還是使用了上面這種方式,沒有發現太大問題。第二天早上來調試的時候,網頁運行了一段時間,我的電腦記憶體達到了56%,我是8G的記憶體, ...
  • 今天博客園註冊成功了,很是欣喜,趕緊在博客園留下我的足記。 半年前,我還是剛走出校園的一枚IT小白,對於IT行業什麼都不懂,我大學的專業是採礦工程,接觸過的編程語言只有VB,但說實話,那時候啥也不懂,考試嗎,相信大家也都知道,在考試前老師一般會畫原題,你死記硬背就行了,根本不用懂那行代碼什麼意思,只 ...
  • 經測試,相容IE8 ...
  • 1.Html區塊元素 HTML可以通過<div>和<span>將元素組合起來 大多數HTML元素被定義為塊級元素或內聯元素, 而塊級元素在瀏覽器顯示時,通常會以新行來開始(或結束)。如:<h1>,<p>,<ul>,<table> 內聯元素在顯示時通常不會以新行開始。如:<b>,<td>,<a>,<i ...
  • 一、Bootstrap彈出框使用過JQuery UI應該知道,它裡面有一個dialog的彈出框組件,功能也很豐富。與jQuery UI的dialog類似,Bootstrap裡面也內置了彈出框組件。打開bootstrap 文檔可以看到它的dialog是直接嵌入到bootstrap.js和bootstr ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...