-
Notifications
You must be signed in to change notification settings - Fork 0
/
maskcard.js
254 lines (229 loc) · 9.18 KB
/
maskcard.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
; (function (global, factory) {
if (typeof exports === 'object' && typeof module !== 'undefined') {
module.exports = factory()
} else {
if (typeof define === 'function' && define.amd) {
define(factory)
} else {
global.Maskcard = factory()
}
}
})(this, function () {
'use strict'
var Maskcard = function (element, config) {
if (!element) {
console.error('no element')
return
}
if (typeof element === 'object') {
if (element.nodeName !== 'CANVAS') {
console.error('element is not canvas element')
return
}
} else {
console.error('element is not element')
return
}
// 默认配置
var initConfig = {
fill: {
type: 'color', // 填充的方式 color | image
content: 'silver' // 填充的内容 如果type=color则填颜色值 如果type=image则填图片地址或base64
},
percent: 100, // 涂抹了x%之后自动消除
radius: 20, // 涂抹笔触的半径
disable: false, // 是否封锁 默认不封锁
beforeInit: null, // 初始化之前执行
inited: null, // 初始化之后执行
touchstart: null, // 手指按下的时候执行
touchmove: null, // 手指移动的时候执行
touchend: null, // 手指离开的时候执行
complete: null // 刮完之后执行
}
this.config = extend(initConfig, (config || {}))
this.element = element
this.ctx = this.element.getContext('2d')
this.width = this.element.clientWidth
this.height = this.element.clientHeight
this.init()
}
Maskcard.version = '1.0.2'
function extend(sourceObj, extendObj) {
var newObj = JSON.parse(JSON.stringify(sourceObj))
for (var x in extendObj) {
newObj[x] = extendObj[x]
}
return newObj
}
function loadImage(url, callback) {
var img = new Image()
img.src = url
if (img.complete) {
callback(img)
return
}
img.onload = function () {
callback(img)
}
}
Maskcard.prototype = {
init: function () {
var _this = this
var config = _this.config
var element = _this.element
var ctx = _this.ctx
var width = _this.width
var height = _this.height
config.beforeInit && config.beforeInit.call(_this)
element.width = width
element.height = height
// 先遮挡后再清除背景 以防初始化刮刮卡之前被看到底部的内容
ctx.fillStyle = 'silver'
ctx.fillRect(0, 0, width, height)
ctx.globalCompositeOperation = 'source-over'
var ctxInit = function () {
ctx.globalCompositeOperation = 'destination-out'
config.inited && config.inited.call(_this)
}
if (config.fill.type == 'color') {
ctx.fillStyle = config.fill.content
ctx.fillRect(0, 0, width, height)
ctxInit()
} else if (config.fill.type == 'image') {
loadImage(config.fill.content, function (img) {
// 循环repeat
// 后期更新cover contain
for (var j = 0; j < height; j += img.height) {
for (var i = 0; i < width; i += img.width) {
ctx.drawImage(img, i, j, img.width, img.height)
}
}
ctxInit()
})
} else {
console.error('fill.type error')
return false
}
function scratching(e) {
var offsetX = element.getBoundingClientRect().left
var offsetY = element.getBoundingClientRect().top
var x = e.clientX - offsetX
var y = e.clientY - offsetY
ctx.beginPath()
ctx.arc(x, y, parseInt(config.radius), 0, Math.PI * 2)
ctx.fill()
}
_this.isTouching = false // 是否正在刮
_this.touchCount = 0 // 刮的次数
_this.isComplete = false // 是否刮完
if (!this.isReset) {
var isCanTouch = 'ontouchstart' in window ? true : false // 判断系统是否支持touch事件
var tapstart = isCanTouch ? 'touchstart' : 'mousedown'
var tapend = isCanTouch ? 'touchend' : 'mouseup'
var tapmove = isCanTouch ? 'touchmove' : 'mousemove'
element.addEventListener(tapstart, function (e) {
e.preventDefault()
if (config.disable) return false // 如果设置了不可刮 则返回false
if (_this.isComplete) return false // 已经完成
_this.isTouching = true
_this.touchCount++
if (e.changedTouches) {
e = e.changedTouches[e.changedTouches.length - 1]
}
scratching(e)
config.touchstart && config.touchstart.call(_this, e)
})
element.addEventListener(tapmove, function (e) {
e.preventDefault()
if (config.disable) return false // 如果设置了不可刮 则返回false
if (_this.isComplete) return false // 已经完成
if (_this.isTouching) {
if (e.changedTouches) {
e = e.changedTouches[e.changedTouches.length - 1]
}
scratching(e)
config.touchmove && config.touchmove.call(_this, e)
}
})
element.addEventListener(tapend, function (e) {
e.preventDefault()
if (config.disable) return false // 如果设置了不可刮 则返回false
if (_this.isComplete) return false // 已经完成
if (e.changedTouches) {
e = e.changedTouches[e.changedTouches.length - 1]
}
_this.isTouching = false
config.touchend && config.touchend.call(_this, e)
if (_this.getProgress() >= parseFloat(config.percent)) {
_this.clear()
}
})
}
},
reset: function () {
this.isReset = true
this.init()
},
// 获取刮刮卡进度
getProgress: function () {
var _this = this
var width = _this.width
var height = _this.height
try {
var data = _this.ctx.getImageData(0, 0, width, height).data
var area = 0 // 被刮去的面积
for (var i = 0; i < data.length; i += 4) {
if (data[i + 3] == 0) {
area++
}
}
var progress = area / (width * height) * 100
progress = Math.round(progress * 100) / 100
return progress
} catch (error) {
console.error(error)
var totalCount = parseFloat(_this.config.percent) / 10
// 所引用图片地址不属于本域名 canvas中getImageData方法不支持跨域 改为刮一次返回10%
return _this.touchCount * 10
}
},
changeConfig: function (config) {
for (var x in config) {
this.config[x] = config[x]
}
},
disable: function () {
this.changeConfig({
disable: true
})
},
enable: function () {
this.changeConfig({
disable: false
})
},
// 清除画布
clear: function () {
var _this = this
_this.isComplete = true
var config = _this.config
_this.element.style.webkitTransitionDuration = '500ms'
_this.element.style.MozTransitionDuration = '500ms'
_this.element.style.msTransitionDuration = '500ms'
_this.element.style.OTransitionDuration = '500ms'
_this.element.style.transitionDuration = '500ms'
_this.element.style.opacity = 0
config.complete && config.complete.call(_this)
setTimeout(function () {
_this.ctx.clearRect(0, 0, _this.width, _this.height)
_this.element.style.webkitTransitionDuration = '0ms'
_this.element.style.MozTransitionDuration = '0ms'
_this.element.style.msTransitionDuration = '0ms'
_this.element.style.OTransitionDuration = '0ms'
_this.element.style.transitionDuration = '0ms'
_this.element.style.opacity = 1
}, 500)
}
}
return Maskcard
})