This repository was archived by the owner on Aug 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathwScratchPad.js
More file actions
320 lines (256 loc) · 9.21 KB
/
wScratchPad.js
File metadata and controls
320 lines (256 loc) · 9.21 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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
(function ($) {
'use strict';
function ScratchPad(el, options) {
this.$el = $(el);
this.options = options;
this.init = false;
this.enabled = true;
this._generate();
}
ScratchPad.prototype = {
_generate: function () {
// Throw message if canvas is not supported.
if (!$.support.canvas) {
this.$el.append('Canvas is not supported in this browser.');
return true;
}
// Setup canvas and context.
this.canvas = document.createElement('canvas');
this.ctx = this.canvas.getContext('2d');
// Make sure it's at least relative.
if (this.$el.css('position') === 'static') {
this.$el.css('position', 'relative');
}
this.$img = $('<img src=""/>').attr('crossOrigin', '').css({position: 'absolute', width: '100%', height: '100%'});
// Make sure we sett style width height here for elastic stretch
// and better support for mobile if we are resizing the scratch pad.
this.$scratchpad = $(this.canvas).css({position: 'absolute', width: '100%', height: '100%'});
this.$scratchpad.bindMobileEvents();
// Setup event handlers.
this.$scratchpad
.mousedown($.proxy(function (e) {
// If disabled we just return true which menas
// our our this.scratch will remain as false.
if (!this.enabled) {
return true;
}
this.canvasOffset = $(this.canvas).offset();
this.scratch = true;
this._scratchFunc(e, 'Down');
}, this))
.mousemove($.proxy(function (e) {
if (this.scratch) {
this._scratchFunc(e, 'Move');
}
}, this))
.mouseup($.proxy(function (e) {
if (this.scratch) {
this.scratch = false;
this._scratchFunc(e, 'Up');
}
}, this));
// Run options
this._setOptions();
// Apepnd items
this.$el.append(this.$img).append(this.$scratchpad);
// Initialize and reset
this.init = true;
this.reset();
},
reset: function () {
var _this = this,
width = Math.ceil(this.$el.innerWidth()),
height = Math.ceil(this.$el.innerHeight()),
devicePixelRatio = window.devicePixelRatio || 1;
// Set number of pixels required for getting scratch percentage.
this.pixels = width * height;
// We'll do a hard reset for the height here in case
// we need to run this at differnt sizes.
this.$scratchpad.attr('width', width).attr('height', height);
this.canvas.setAttribute('width', width * devicePixelRatio);
this.canvas.setAttribute('height', height * devicePixelRatio);
this.ctx.scale(devicePixelRatio, devicePixelRatio);
this.pixels = width * devicePixelRatio * height * devicePixelRatio;
// Default to image hidden in case no bg or color is set.
this.$img.hide();
// Set bg.
if (this.options.bg) {
if (this.options.bg.charAt(0) === '#') {
this.$el.css('backgroundColor', this.options.bg);
}
else {
this.$el.css('backgroundColor', '');
this.$img.attr('src', this.options.bg);
}
}
// Set fg.
if (this.options.fg) {
if (this.options.fg.charAt(0) === '#') {
this.ctx.fillStyle = this.options.fg;
this.ctx.beginPath();
this.ctx.rect(0, 0, width, height);
this.ctx.fill();
this.$img.show();
}
else {
// Have to load image before we can use it.
$(new Image())
.attr('crossOrigin', '')
.attr('src', this.options.fg)
.on('load', function () {
_this.ctx.drawImage(this, 0, 0, width, height);
_this.$img.show();
});
}
}
},
clear: function () {
this.ctx.clearRect(0, 0, Math.ceil(this.$el.innerWidth()), Math.ceil(this.$el.innerHeight()));
},
enable: function (enabled) {
this.enabled = enabled === true ? true : false;
},
destroy: function () {
this.$el.children().remove();
$.removeData(this.$el, 'wScratchPad');
},
_setOptions: function () {
var opt, func;
for (opt in this.options) {
this.options[opt] = this.$el.attr('data-' + opt) || this.options[opt];
func = 'set' + opt.charAt(0).toUpperCase() + opt.substring(1);
if (this[func]) {
this[func](this.options[opt]);
}
}
},
setBg: function () {
if (this.init) {
this.reset();
}
},
setFg: function () {
this.setBg();
},
setCursor: function (cursor) {
this.$el.css('cursor', cursor);
},
_scratchFunc: function (e, event) {
e.pageX = Math.floor(e.pageX - this.canvasOffset.left);
e.pageY = Math.floor(e.pageY - this.canvasOffset.top);
this['_scratch' + event](e);
if (this.options.realtime || event === 'Up') {
if (this.options['scratch' + event]) {
this.options['scratch' + event].apply(this, [e, this._scratchPercent()]);
}
}
},
_scratchPercent: function() {
var hits = 0,
imageData = this.ctx.getImageData(0,0, this.canvas.width, this.canvas.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;
},
_scratchDown: function (e) {
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;
//draw single dot in case of a click without a move
this.ctx.beginPath();
this.ctx.arc(e.pageX, e.pageY, this.options.size/2, 0, Math.PI*2, true);
this.ctx.closePath();
this.ctx.fill();
//start the path for a drag
this.ctx.beginPath();
this.ctx.moveTo(e.pageX, e.pageY);
},
_scratchMove: function (e) {
this.ctx.lineTo(e.pageX, e.pageY);
this.ctx.stroke();
},
_scratchUp: function () {
this.ctx.closePath();
}
};
$.support.canvas = (document.createElement('canvas')).getContext;
$.fn.wScratchPad = function (options, value) {
function get() {
var wScratchPad = $.data(this, 'wScratchPad');
if (!wScratchPad) {
wScratchPad = new ScratchPad(this, $.extend(true, {}, options));
$.data(this, 'wScratchPad', wScratchPad);
}
return wScratchPad;
}
if (typeof options === 'string') {
var wScratchPad,
values = [],
func = (value !== undefined ? 'set' : 'get') + options.charAt(0).toUpperCase() + options.substring(1),
setOpt = function () {
if (wScratchPad.options[options]) { wScratchPad.options[options] = value; }
if (wScratchPad[func]) { wScratchPad[func].apply(wScratchPad, [value]); }
},
getOpt = function () {
if (wScratchPad[func]) { return wScratchPad[func].apply(wScratchPad, [value]); }
else if (wScratchPad.options[options]) { return wScratchPad.options[options]; }
else { return undefined; }
},
runOpt = function () {
wScratchPad = $.data(this, 'wScratchPad');
if (wScratchPad) {
if (wScratchPad[options]) { wScratchPad[options].apply(wScratchPad, [value]); }
else if (value !== undefined) { setOpt(); }
else { values.push(getOpt()); }
}
};
this.each(runOpt);
return values.length ? (values.length === 1 ? values[0] : values) : this;
}
options = $.extend({}, $.fn.wScratchPad.defaults, options);
return this.each(get);
};
$.fn.wScratchPad.defaults = {
size : 5, // The size of the brush/scratch.
bg : '#cacaca', // Background (image path or hex color).
fg : '#6699ff', // Foreground (image path or hex color).
realtime : true, // Calculates percentage in realitime
scratchDown : null, // Set scratchDown callback.
scratchUp : null, // Set scratchUp callback.
scratchMove : null, // Set scratcMove callback.
cursor : 'crosshair' // Set cursor.
};
$.fn.bindMobileEvents = function () {
$(this).on('touchstart touchmove touchend touchcancel', function (event) {
var touches = (event.changedTouches || event.originalEvent.targetTouches),
first = touches[0],
type = '';
switch (event.type) {
case 'touchstart':
type = 'mousedown';
break;
case 'touchmove':
type = 'mousemove';
event.preventDefault();
break;
case 'touchend':
type = 'mouseup';
break;
default:
return;
}
var simulatedEvent = document.createEvent('MouseEvent');
simulatedEvent.initMouseEvent(
type, true, true, window, 1,
first.screenX, first.screenY, first.clientX, first.clientY,
false, false, false, false, 0/*left*/, null
);
first.target.dispatchEvent(simulatedEvent);
});
};
})(jQuery);