闲得蛋疼,JavaScript版本BadApple
参考Vim版本的BadApple改写而成。由于加载数据比较大,可能网速不给力的童鞋效果不太好,多刷新几次就好了,^_^。
运行环境:支持HTML5 Canvas的浏览器。
1. 代码:
$(function() {
var BadApple = {
is_running: false,
data_index: 0,
data_count: 67,
is_loading: {},
progress: $('#progress'),
init: function() {
var me = this,
stage = $('#stage'),
i;
me.data_matrix = [];
for (i = 0; i < me.frame_row; ++i) {
me.data_matrix[i] = Array(me.frame_col).join(' ').split('');
}
me.canvas = stage.get(0);
me.ctx = me.canvas.getContext('2d');
me.canvas.width = me.frame_col * 7;
me.canvas.height = me.frame_row * 16;
me.ctx.fillStyle = 'rgba(0, 0, 0, .05)';
me.ctx.fillRect(0, 0, me.canvas.width, me.canvas.height);
me.ctx.fillStyle = '#7E7E7E';
me.ctx.font = '12px monospace';
},
load_data: function (index, count, fn) {
var me = this;
if (!me.is_loading[index]) {
// mark is loading
me.is_loading[index] = true;
$.get('data/BadApple/data_' + index + '.txt', function (data) {
// finish loading
me.is_loading[index] = null;
me.data_index = index;
data = data.split('\n');
// update progress
me.progress.html((index / me.data_count * 100).toFixed(2) + '%');
if (me.data) {
me.data = me.data.concat(data);
}
// first time
else {
me.data = data;
var rc = data[0].split(' ');
me.frame_row = parseInt(rc[0]);
me.frame_col = parseInt(rc[1]);
$('#div_loading').remove();
me.init();
}
if (count > 1 && index < me.data_count) {
me.load_data(index + 1, count - 1, fn);
}
else {
fn && fn();
}
});
}
},
draw_frame: function(data) {
var me = this, i;
for (i = 0; i < data.length; ++i) {
me.data_matrix[data[i][0] - 1][data[i][1] - 1] = data[i][2];
}http://www.huiyi8.com/jianbihua/
me.ctx.clearRect(0, 0, me.canvas.width, me.canvas.height);
for (i = 0; i < me.frame_row; ++i) {
me.ctx.fillText(me.data_matrix[i].join(''), 4, i * 14 + 16);
}简笔画大全
},
play: function() {
var me = this,
i = 0,
j,
l,
r;
me.is_running = true;
me.interval || (me.interval = setInterval(function () {
if (me.is_running && i < me.data.length) {
l = me.data[++i];
r = [];
if (l && !/^\s*$/.test(l)) {
l = l.split('|');
for (j = 0; j < l.length; ++j) {
r[j] = l[j].split('_');
}
}
me.draw_frame(r);
// load data
if (i + 16 > me.data_index * 16 &&
me.data_index < me.data_count) {
me.load_data(me.data_index + 1, 2);
}
}
}, 70));
},
start: function() {
var me = this;
if (me.data) {
me.play();
}
else {
me.load_data(0, 1, function() {
me.play();
});
}
},
stop: function() {
this.is_running = false;
},
terminal: function () {
this.stop();
clearInterval(this.interval);
}
};
$('#stage').click(function() {
BadApple.is_running ?
BadApple.stop() :
BadApple.start();
}).click();
});
闲得蛋疼,JavaScript版本BadApple的更多相关文章
- 小王子浅读Effective javascript(一)了解javascript版本
哈哈,各位园友新年快乐!愚安好久没在园子里写东西了,这次决定针对javascript做一个系列,叫做<小王子浅读Effective javascript>,主要是按照David Herma ...
- [闲的蛋疼系列]从零开始用TypeScript写React的UI组件(0)-先写一个Button??
0.咸鱼要说的 一入前端深似海,咸鱼入海更加咸. 最近闲的蛋疼,手上年前的事也完成了7788了,借助[PG1]的话来说,我们要keep real. 咸鱼肯定不real 了,因为我们都活在梦里,所以咱们 ...
- 介绍一种在ABAP内核态进行内表高效拷贝的方法,和对应的Java和JavaScript版本的伪实现
内表操作是ABAP开发人员几乎在每个ABAP程序里都会遇到的. 看一个例子:有两个行结构不一样的内表,每个内表的行结构有三列,除了name这一列名字一致外,其他两列的名称都不同,下图用红色和蓝色标注出 ...
- 经典算法:快排的Javascript版本
function swap(arr,l,r){ var temp=arr[l]; arr[l]=arr[r]; arr[r]=temp; } function partition(arr,camp,l ...
- JavaScript 版本的 RSA加密库文件
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- 伟大的通信使者——JSON(JavaScript版本)
JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式.它基于 ECMAScript 规范的一个子集,采用完全独立于编程语言的文本格式来存储和表 ...
- Struts2-Ajax整合之纯JavaScript版本
1.Ajax的作用:能够在不重新加载页面的情况下,用异步的方式与后台服务器进行数据交互 2.Struts2-Json的jar包(包含阿里巴巴自己的jar包) commons-beanutils-1.7 ...
- 生日礼物网页Javascript版本与锚点版本
<style> #dv1{ width:60px; height:36px; margin:0 auto; background-color:orange; display:none; } ...
- BF算法和KMP算法(javascript版本)
var str="abcbababcbababcbababcabcbaba";//主串 var ts="bcabcbaba";//子串 function BF( ...
随机推荐
- luogu2234 [HNOI2002]营业额统计
treap水过 #include <iostream> #include <cstdlib> #include <cstdio> using namespace s ...
- js总结(三):面向对象,prototype ,oo模拟
http://aralejs.org/class/docs/competitors.html http://javascript.crockford.com/prototypal.html proto ...
- UVALive 4015 树形dp
题目大意: 从一个根节点出发,走最多 x 的长度,问最多能走过多少个节点,图保证是一棵树 dp[0][i][j] , 表示走从i点为根的子树走过了j个点最后回到 i 最少需要多少时间dp[1][i][ ...
- POJ 1523 网络连通
题目大意: 给你一个网络组,每台机子与其他机子的关系,让你找到所有的割点,如果没有割点,输出无 这道题目就是最直接的求割点问题,我在这里用的是邻接矩阵来存储机子之间的关系 割点问题的求解需要对深度优先 ...
- codeforces 303 div2 E
赤裸裸的最短路,需要注意下枚举过程就好了.直接贴上别人的代码,发现他的代码挺符合我的风格,以后就这样写了. #include <bits/stdc++.h> ]; ]; ]; ]; vec ...
- 【数学+枚举】OpenJ_POJ - C17J Pairs
https://vjudge.net/contest/171652#problem/J [题意] 问有多少个正整数对(x,y),使得存在正整数p,q满足 1 <= T <= 15 1 &l ...
- 45个android实例源码分享
分享45个android实例源码,很好很强大 http://www.apkbus.com/android-20978-1-1.html andriod闹钟源代码 http://www.apkbus.c ...
- 【ZJOI2017 Round1练习】D2T2 iqtest(排列组合)
题意: 思路: 根据欧拉定理,a^(phi(n)-1)为a mod n的逆元 ..]of longint; s,ans,x,mo,k,phi,tmp:int64; i,m,n,j:longint; f ...
- 最长上升子序列(LIS)长度的O(nlogn)算法
最长上升子序列(LIS)的典型变形,熟悉的n^2的动归会超时.LIS问题可以优化为nlogn的算法.定义d[k]:长度为k的上升子序列的最末元素,若有多个长度为k的上升子序列,则记录最小的那个最末元素 ...
- Meteor在手机上运行
在本章中,我们将学习如何在Android设备上运行你的应用程序.最近Meteor刚刚添加此功能适用于Windows操作系统,所以我们需要更新 Meteor 应用到 1.3测试版. 注 在写的时候本教程 ...