-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtxbb-scratch.js
More file actions
300 lines (271 loc) · 9.24 KB
/
txbb-scratch.js
File metadata and controls
300 lines (271 loc) · 9.24 KB
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
/**
* Txbb.Scratch 组件
*
* 同学帮帮移动端刮刮卡组件
* 0.2.2
* by zhangyang
*/
(function(factory) {
'use strict';
if (typeof define !== 'undefined' && define.amd) {
define('Txbb/Scratch', function() {
return factory.call(null);
});
} else {
if (!window.Txbb) window.Txbb = {};
window.Txbb.Scratch = factory.call(null);
}
}(function() {
'use strict';
// helps
function q(s) {return document.querySelector(s);}
function elem(nodeType, attrs) {
var node = document.createElement(nodeType);
if (attrs) {
for (var k in attrs) {
node[k] = attrs[k];
}
}
return node;
}
function extend(origin, extendObj) {
var back = {};
if (origin) {
for (var k1 in origin) {
back[k1] = origin[k1];
}
}
if (extendObj) {
for (var k2 in extendObj) {
back[k2] = extendObj[k2];
}
}
return back;
}
Element.prototype._css = function (attrs) {
if (!this) return this;
if (attrs) {
for (var k in attrs) {
if (this.style.hasOwnProperty(k))
this.style[k] = attrs[k];
}
}
return this;
};
Element.prototype._attr = function (attrs) {
if (!this) return this;
if (attrs && typeof attrs === 'object') {
for (var k in attrs) {
this.setAttribute(k, attrs[k]);
}
}
if (attrs && typeof attrs === 'string') return this.getAttribute(attrs);
return this;
};
Element.prototype._remove = function() {
this.parentNode.removeChild(this);
};
Element.prototype._show = function() {
this.style.display = 'block';
};
Element.prototype._hide = function() {
this.style.display = 'none';
};
Element.prototype._addEvent = function(eventNames, handler) {
var events = eventNames.split(/\s+/);
var _this = this;
events.forEach(function(evt) {
_this.addEventListener(evt, handler, false);
});
return _this;
};
Element.prototype._offset = function() {
var offset = {
left: 0,
top: 0
};
var dom = this;
while (dom && dom !== document.body) {
offset.left += dom.offsetLeft;
offset.top += dom.offsetTop;
dom = dom.offsetParent; // 0.1.1 修复 _offset 方法错误
}
return offset;
};
Element.prototype._parents = function() {
var parent = this.parentNode;
var back = [];
while (parent != document.body) {
back.push(parent);
parent = parent.parentNode;
}
back.push(document.body);
return back;
};
/*-------------- 华丽丽的分割线 ---------------*/
var ua = window.navigator.userAgent;
// 是否支持 canvas
var supportCanvas = elem('canvas').getContext;
if (!supportCanvas) {
alert('您的设备不支持刮刮卡,请及时升级');
return;
}
// 实例化后的 Scratch 实例缓存
var cache = {};
// 实例序号
var id = 0;
// 类定义
function Scratch(elem, options, id) {
this.elem = elem;
this.id = id;
this.options = options;
this.elem._attr({'scratch' : id});
this._render();
}
// 实例方法定义
Scratch.prototype = {
_render : function() {
this.elem._css({
backgroundColor : this.options.bg,
position: 'relative'
});
this._createCanvas();
this.reset();
this._createMiddle();
this._eventBind();
},
_createCanvas: function() {
var canvas = elem('canvas');
canvas.width = this.elem.offsetWidth;
canvas.height = this.elem.offsetHeight;
canvas._css({
position: 'absolute',
zIndex: 2,
top: 0,
left: 0
});
this.elem.appendChild(canvas);
this.canvas = canvas;
this.ctx = canvas.getContext('2d');
this.offset = canvas._offset();
// 修复 android 4.1 webview 双重 canvas 错误
if (ua.indexOf('534.30') > 0) {
this.canvas._parents().forEach(function(item) {
item._css({overflow : 'visible'});
});
}
},
_createMiddle: function() {
var div;
if (this.elem.lastChild.className.indexOf('txbb-scratch-middle') === -1) {
div = elem('div', {className : 'txbb-scratch-middle'});
div._css({
position: 'absolute',
zIndex: 1,
color: '#333',
textAlign: 'center',
lineHeight: this.height + 'px',
height: this.height + 'px',
width: this.width + 'px',
fontSize: '20px',
top: 0,
left: 0
}).innerHTML = this.options.middle;
this.elem.appendChild(div);
} else {
div = this.elem.lastChild;
div.innerHTML = this.options.middle;
}
},
_eventBind: function() {
var _this = this;
_this.canvas._addEvent('touchstart', function(e) {
var x = Math.floor((e.touches[0].pageX + document.body.scrollLeft) - _this.offset.left);
var y = Math.floor((e.touches[0].pageY + document.body.scrollTop) - _this.offset.top);
_this.ctx.globalCompositeOperation = 'destination-out';
_this.ctx.lineJoin = 'round';
_this.ctx.lineCap = 'round';
_this.ctx.strokeStyle = _this.options.color;
_this.ctx.lineWidth = _this.options.size;
_this.ctx.beginPath();
_this.ctx.arc(x, y, _this.options.size/2, 0, Math.PI*2, true);
_this.ctx.closePath();
_this.ctx.fill();
_this.ctx.beginPath();
_this.ctx.moveTo(x, y);
if (_this.options.onStart && !_this.onStartCalled) {
_this.options.onStart.call(this);
_this.onStartCalled = true;
}
})._addEvent('touchmove', function(e) {
var x = Math.floor(e.touches[0].pageX - _this.offset.left);
var y = Math.floor(e.touches[0].pageY - _this.offset.top);
// 修复 android 4.1 webview canvas destination-out 失效错误
if (ua.indexOf('534.30') > 0) {
_this.canvas.style.display = 'none';
_this.canvas.offsetHeight;
_this.canvas.style.display = 'inherit';
}
_this.ctx.lineTo(x, y);
_this.ctx.stroke();
e.preventDefault();
})._addEvent('touchend', function(e) {
_this.ctx.closePath();
var percent = _this._percent();
if (percent >= 50) _this._clear();
});
},
reset: function(options) {
if (options && typeof options === 'object')
this.options = extend(this.options, options);
this.canvas._show();
this.width = this.canvas.width;
this.height = this.canvas.height;
this.ctx.globalCompositeOperation = 'source-over';
this.pixels = this.width * this.height;
this.ctx.fillStyle = this.options.fg;
this.ctx.beginPath();
this.ctx.rect(0, 0, this.width, this.height);
this.ctx.fill();
this.ctx.closePath();
this._createMiddle();
this.onStartCalled = false;
},
_percent: function() {
var hits = 0,
imageData = this.ctx.getImageData(0,0, this.width, this.height);
for (var i=0, ii=imageData.data.length; i<ii; i=i+4) {
if (imageData.data[i] === 0 && imageData.data[i+1] === 0 && imageData.data[i+2] === 0 && imageData.data[i+3] === 0) {
hits++;
}
}
return (hits / this.pixels) * 100;
},
_clear: function() {
this.ctx.clearRect(0, 0, this.width, this.height);
this.canvas._hide();
if (this.options.onEnd) this.options.onEnd.call(this);
}
};
// 默认参数
var defaults = {
bg: '#f00',
fg: '#888',
middle: '谢谢惠顾,嘿嘿',
size: 20,
onEnd: function(){},
onStart: function(){}
};
return {
attach:function(elem, willOptions){
var options = extend(defaults, willOptions);
var existedId = elem._attr('scratch');
if (!existedId) {
id++;
cache['TxbbScratch' + id] = new Scratch(elem, options, id);
return cache['TxbbScratch' + id];
}
else return cache['TxbbScratch' + existedId];
}
};
}));