-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.js
429 lines (373 loc) · 13.7 KB
/
demo.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
"use strict";
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
/**
* Actor 类
* @class
* @param {Game} game 游戏实例
* @return {Actor} Instance
*/
var Actor = function () {
/** Create a actor instance */
function Actor(ctx, item, pre, opt) {
_classCallCheck(this, Actor);
this.ctx = ctx;
/* 角色 x 坐标值 */
this.x = 0;
/* 角色 y 坐标值 */
this.y = 0;
/* 角色 宽度 */
var measureText = ctx.measureText(item.valueDisplay);
this.w = measureText.width;
/* 角色 高度 */
this.h = parseInt(opt.font, 10);
this.item = item;
this.pre = pre;
this.opt = opt;
}
/**
* 渲染自己
*
* @return {void}
*/
_createClass(Actor, [{
key: "render",
value: function render() {
var ctx = this.ctx,
item = this.item,
pre = this.pre,
opt = this.opt,
x = this.x,
y = this.y,
w = this.w,
h = this.h;
// 圆角半径
var radius = h / 2 + opt.padding;
// 左X坐标
var leftX = x - w / 2 - radius - 5;
// 上Y坐标
var topY = y - radius;
// 绘制连接线
if (pre) {
ctx.beginPath();
ctx.moveTo(pre.x, pre.y);
ctx.lineTo(x, y);
ctx.strokeStyle = opt.color;
ctx.lineWidth = opt.lineWidth;
ctx.stroke();
}
// 绘制圆角背景
this.roundRect(leftX, topY, w + radius * 2 + 10, radius * 2, radius);
// 设置填充色
ctx.fillStyle = opt.color;
ctx.fill();
// 绘制文字
ctx.textAlign = "center";
ctx.fillStyle = opt.valueColor;
ctx.fillText(item.valueDisplay, x, y + h / 3);
}
}, {
key: "roundRect",
value: function roundRect(x, y, w, h, r) {
if (w < 2 * r) {
r = w / 2;
}
if (h < 2 * r) {
r = h / 2;
}
this.ctx.beginPath();
this.ctx.moveTo(x + r, y);
this.ctx.arcTo(x + w, y, x + w, y + h, r);
this.ctx.arcTo(x + w, y + h, x, y + h, r);
this.ctx.arcTo(x, y + h, x, y, r);
this.ctx.arcTo(x, y, x + w, y, r);
this.ctx.closePath();
}
}]);
return Actor;
}();
module.exports = Actor;
},{}],2:[function(require,module,exports){
"use strict";
var _slicedToArray = function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; }();
var List2graph = require("./list2graph");
var params = function () {
var hash = {};
var pairs = window.location.search.slice(1).split("&");
var _iteratorNormalCompletion = true;
var _didIteratorError = false;
var _iteratorError = undefined;
try {
for (var _iterator = pairs[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
var x = _step.value;
var _x$split = x.split("="),
_x$split2 = _slicedToArray(_x$split, 2),
k = _x$split2[0],
v = _x$split2[1];
hash[k] = decodeURIComponent(v);
}
} catch (err) {
_didIteratorError = true;
_iteratorError = err;
} finally {
try {
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
} finally {
if (_didIteratorError) {
throw _iteratorError;
}
}
}
return hash;
}();
var num = Math.max(1, params.num | 0);
var randomItem = function randomItem() {
return {
name: Math.random().toString().slice(2, 8),
value: (Math.random() * 400 | 0) / 100
};
};
var main = function main() {
var canvas = document.getElementById("myCanvas");
var addBtn = document.getElementById("add-item");
var resetBtn = document.getElementById("reset-btn");
var splitBtn = document.getElementById("split-enter");
var splitInput = document.getElementById("split-value");
var list = [];
for (var i = 0; i < num; i += 1) {
list.push(randomItem());
}var list2graph = new List2graph(canvas, list, {
valueFormat: function valueFormat(x) {
return "" + Math.floor(x * 100) / 100;
}
});
addBtn.onclick = function () {
list2graph.add(randomItem());
};
splitBtn.onclick = function () {
list2graph.split(splitInput.value | 0);
};
resetBtn.onclick = function () {
var ls = [];
for (var _i = 0; _i < num; _i += 1) {
ls.push(randomItem());
}list2graph.reset(ls);
};
};
main();
},{"./list2graph":3}],3:[function(require,module,exports){
"use strict";
var Actor = require("./actor");
var defineOption = {
range: [1, Infinity], // 值的区间
split: 2, // 区间分割点
width: 1200, // canvas 的宽度,高度是动态计算的
leftWidth: 120, // 左侧边栏的宽度
bottomHeight: 60, // 底部高度
maxLength: 20, // 最大长度
valueFormat: function valueFormat(x) {
// 值格式化函数
return x;
},
nameFormat: function nameFormat(x) {
// 名称格式化函数
return x;
},
tableSurplus: 10, // 表格左侧和底部露头的长度
tableBorderWidth: 1, // 表格边框接线宽度
animationMS: 1000, // 动态效果时长,单位毫秒
color: "#43e28b", // item以及连线的颜色
tableColor: "#363e63", // 表格颜色, 巧妙的利用表格颜色,可以做出表格不存在的效果
nameFont: "24px serif", // 左侧名称字体设定
rangeFont: "24px serif", // 底部区间显示的颜色
nameColor: "#7685bc", // 左侧name显示的颜色
rangeColor: "#7685bc", // 底部区间显示的颜色
valueColor: "#ffffff", // 值的显示颜色
padding: 10, // 文字内容和容器的内间距
font: "24px serif", // 图标文字大小以及字体设定
lineHeight: 60, // 每一个对象占用的行高
lineWidth: 4 // 连接线的宽度
};
/**
* @param {element} canvas 绘图的canvas元素对象
* @param {Array.Object} list 初始的绘图数据 {name: 'xxx', value: 121}
* @param {Object} [option] 可选参数,用来控制某些绘图细节
*
* @class
* @return {List2graph} Instance
*/
function List2graph(canvas, list, option) {
var opt = Object.assign({}, defineOption, option);
var ctx = canvas.getContext("2d");
// 每一行数据的处理函数
var trans = function trans(item) {
var obj = {};
obj.name = item.name;
obj.value = item.value;
obj.nameDisplay = opt.nameFormat(item.name, item);
obj.valueDisplay = opt.valueFormat(item.value, item);
return obj;
};
// 数据
var data = list.slice(0, opt.maxLength).map(trans);
// 角色
var actors = [];
// 状态,stable: 稳定,ready: 准备动态状态, animation: 动画
var status = "ready";
var lastRequestAnimationAt = Date.now();
var dt = 1; // 两次动画之间的时间间隔,单位毫秒
var distance = opt.width - opt.leftWidth >> 1; // 左侧到右侧的距离
// 加速度, 根据公式 s = 1 / 2 * a * t² 求得
var aVelocity = distance * 2 / (opt.animationMS * opt.animationMS);
var velocity = aVelocity * opt.animationMS; // 运动的初始速度,v = at;
var beginX = opt.leftWidth + (distance >> 1); // 开始的 X 坐标
var height = opt.lineHeight * opt.maxLength + opt.bottomHeight; // 计算画本应该的高度
var nameHeight = parseInt(opt.nameFont, 10); // 计算左侧名称显示的高度
var rangeHeight = parseInt(opt.rangeFont, 10); // 计算底部区间显示的高度
var ready = function ready() {
height = opt.lineHeight * data.length + opt.bottomHeight; // 计算画本应该的高度
canvas.width = opt.width;
canvas.height = height;
canvas.style.width = opt.width >> 1;
canvas.style.height = height >> 1;
// 清空角色
actors.length = 0;
for (var i = 0; i < data.length; i += 1) {
var actor = new Actor(ctx, data[i], actors[i - 1], opt);
actor.x = beginX;
actor.y = i * opt.lineHeight + (opt.lineHeight >> 1);
actor.isRight = opt.split <= data[i].value;
actors.push(actor);
}
status = "animation";
dx = 0;
velocity = aVelocity * opt.animationMS;
};
var dx = 0;
var update = function update() {
dx += velocity * dt;
velocity -= aVelocity * dt;
if (velocity <= 0) dx = distance;
var x = beginX + dx;
for (var i = 0; i < data.length; i += 1) {
var actor = actors[i];
if (!actor.isRight) continue;
actor.x = x;
}
};
var drawExtra = function drawExtra() {
ctx.save();
ctx.strokeStyle = opt.tableColor;
ctx.lineWidth = opt.tableBorderWidth;
ctx.beginPath();
// 绘制表格
var leftX = opt.leftWidth;
// 绘制横线
for (var i = 0; i <= data.length; i += 1) {
ctx.moveTo(leftX - opt.tableSurplus, i * opt.lineHeight + opt.tableBorderWidth / 2);
ctx.lineTo(opt.width, i * opt.lineHeight + opt.tableBorderWidth / 2);
}
// 绘制纵线
ctx.moveTo(leftX, 0);
ctx.lineTo(leftX, opt.lineHeight * data.length + opt.tableSurplus);
ctx.moveTo(opt.leftWidth + distance, 0);
ctx.lineTo(opt.leftWidth + distance, opt.lineHeight * data.length + opt.tableSurplus);
ctx.moveTo(opt.width - (opt.tableBorderWidth >> 1), 0);
ctx.lineTo(opt.width - (opt.tableBorderWidth >> 1), opt.lineHeight * data.length + opt.tableSurplus);
// 绘制底部指向区间范围的小短线
ctx.moveTo(beginX, data.length * opt.lineHeight);
ctx.lineTo(beginX, data.length * opt.lineHeight + opt.tableSurplus);
ctx.moveTo(beginX + distance, data.length * opt.lineHeight);
ctx.lineTo(beginX + distance, data.length * opt.lineHeight + opt.tableSurplus);
// 绘制左侧文字
ctx.textAlign = "right";
ctx.font = opt.nameFont;
ctx.fillStyle = opt.nameColor;
for (var _i = 0; _i < data.length; _i += 1) {
ctx.fillText(data[_i].nameDisplay, opt.leftWidth - opt.tableSurplus - 6, (_i + 1) * opt.lineHeight + nameHeight / 3 - (opt.lineHeight >> 1));
}
// 绘制底部区间文字
ctx.fillStyle = opt.rangeColor;
ctx.font = opt.rangeFont;
ctx.textAlign = "center";
ctx.fillText("[" + opt.range[0] + ", " + opt.split + ")", beginX, data.length * opt.lineHeight + opt.tableSurplus + rangeHeight); // 左侧区间
ctx.fillText("[" + opt.split + ", " + (opt.range[1] === Infinity ? "♾" : opt.range[1]) + ")", beginX + distance, data.length * opt.lineHeight + opt.tableSurplus + rangeHeight); // 右侧区间
ctx.stroke();
ctx.restore();
};
var render = function render() {
requestAnimationFrame(render);
var now = Date.now();
dt = now - lastRequestAnimationAt;
lastRequestAnimationAt = now;
if (status === "stable") return;
if (status === "ready") {
ready();
return;
}
update();
// 清空画布
ctx.clearRect(0, 0, opt.width, height);
// 绘制表格以及其他辅助信息
drawExtra();
ctx.save();
ctx.font = opt.font;
for (var i = actors.length - 1; 0 <= i; i -= 1) {
actors[i].render();
}
ctx.restore();
if (velocity <= 0) {
// 再次进入稳定态
velocity = aVelocity * opt.animationMS;
dx = 0;
status = "stable";
}
};
requestAnimationFrame(render);
/**
* 重置数据
* @memberof List2graph
* @instance
*
* @param {Object} item 新增的数据 {name: 'xxx', value: 121}
* @return {void}
*/
var add = function add(item) {
data.unshift(trans(item));
if (opt.maxLength < data.length) data.length = opt.maxLength;
status = "ready";
};
/**
* 重置数据
* @memberof List2graph
* @instance
*
* @param {Array.Object} list 初始的绘图数据 {name: 'xxx', value: 121}
* @return {void}
*/
var reset = function reset(ls) {
data = ls.slice(0, opt.maxLength).map(trans);
status = "ready";
};
/**
* 读取/重置split
* @memberof List2graph
* @instance
*
* @param {number} [value] 分隔区间的值, 不指定值则为读取
* @return {number}
*/
var split = function split(value) {
if (value == null) return opt.split;
if (value < opt.range[0] || opt.range[1] < value) throw Error("\u5206\u5272\u70B9\u4E0D\u5408\u6CD5, \u5FC5\u987B\u5728 " + opt.range + " \u4E4B\u95F4");
opt.split = value;
status = "ready";
return value;
};
return { add: add, split: split, reset: reset };
}
module.exports = List2graph;
},{"./actor":1}]},{},[2]);