forked from iamjoel/front-end-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.js
142 lines (127 loc) · 4.25 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
$(document).ready(function() {
var tool = {
$el: $('#tags'),
init: function() {
this.$dataWrap = this.$el.find('ul');
},
make: function(makeNum) {
makeNum = makeNum || this.MAKENUM;
var html = [];
for (var i = 0; i < makeNum; i++) {
html.push(this.makeItem(false, false, i));
}
this.$dataWrap.html(html.join(''));
},
makeItem: function(isNew, url, index) {
var imgPrefix;
var picIndex;
if (isNew) {
// 新人加入 todo just for test
picIndex = Math.floor(Math.random() * 2) + 1;
url = 'assets/img/avatar/n-' + picIndex + '.png';
} else {
picIndex = Math.floor(Math.random() * this.PIC_NUM) + 1;
url = 'assets/img/avatar/p-' + picIndex + '.png';
}
return '<li>' +
// '<a href="http://www.google.com" target="_blank"><img class="avatar" width=50 height=50 src="' + url + '"><p>' + index + '</p></a>' +
'<a href="http://www.google.com" target="_blank"><img class="avatar" width=50 height=50 src="' + url + '"></a>' +
'</li>';
},
PIC_NUM: 4,
MAKENUM: 50 // 图案数量
}
tool.init();
tool.make();
var $canvasWrap = $('#canvas-wrap');
var $canvas = $('#canvas');
function start() {
var canRun = $('#canvas').tagcanvas({
textColour: '#ff0000',
// outlineThickness: 1,
maxSpeed: 0.03,
minSpeed: 0.01,
initial: [0.01, 0.01], // 开始的时候自动
depth: 0.75,
imageMode: 'both',
wheelZoom: false
}, 'tags');
if (!canRun) {
$('#canvas-wrap').hide();
}
}
start();
$('#pause-btn').click(function() {
$canvas.tagcanvas("pause");
});
$('#resume-btn').click(function() {
$canvas.tagcanvas("resume");
});
$('#add-btn').click(function() {
addOne().done(function() {
start();
$('#total-num').text(tool.$dataWrap.find('li').length);
setTimeout(function() {
$canvas.tagcanvas("tagtofront", {
index: tool.$dataWrap.find('li').length - 1,
callback: function() {
$canvas.tagcanvas("setspeed", [0.01, 0.01]);
}
});
}, 0);
});
});
$('#add-multi-btn').click(function() {
multiAdd(20, function(){
start();
setTimeout(function() {
$canvas.tagcanvas("tagtofront", {
index: tool.$dataWrap.find('li').length - 1,
callback: function() {
$canvas.tagcanvas("setspeed", [0.01, 0.01]);
}
});
}, 0);
})
});
function multiAdd(times, done) {
if (times > 0) {
addOne().done(function () {
multiAdd(times - 1, done);
});
} else {
done();
}
}
function addOne(url) {
var dfd = $.Deferred();
$canvas.tagcanvas("pause");
var $newItem = $(tool.makeItem(true, '', tool.$dataWrap.find('li').length));
$animItem = $newItem.clone();
$animItem.addClass('anim-start');
$canvasWrap.append($animItem);
$animItem.animate({
'margin-left': 0,
'margin-top': 0,
opacity: 0,
height: 0,
width: 0
}, 2000, function() {
$animItem.remove();
tool.$dataWrap.append($newItem);
dfd.resolve();
});
return dfd.promise();
// 修改某个图标
// var toUpdateIndex = Math.floor(Math.random() * tool.MAKENUM) + 1;
// var $targetImg = tool.$dataWrap.find('li').eq(toUpdateIndex).find('img');
// $targetImg.attr('src', 'img/avatar/n-special.jpg');
// $canvas.tagcanvas("tagtofront", {
// index: toUpdateIndex,
// callback: function() {
// start();
// }
// });
// $canvas.tagcanvas("setspeed", [0.01, 0.01]);
}
});