bhq@iemsoft.cn
2018-11-27 e2b48dac099e43f4b3243cdf19a7522e4b5eccbe
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
(function($)
{
    $.fn.wScratchPad = function(option, settings)
    {
        if(typeof option === 'object')
        {
            settings = option;
        }
        else if(typeof option == 'string')
        {
            var values = [];
 
            var elements = this.each(function()
            {
                var data = $(this).data('_wScratchPad');
 
                if(data)
                {
                    if(option === 'reset') { data.reset(); }
                    else if(option === 'clear') { data.clear(); }
                    else if(option === 'enabled') { data.enabled = settings === true; }
                    else if($.fn.wScratchPad.defaultSettings[option] !== undefined)
                    {
                        if(settings !== undefined) { data.settings[option] = settings; }
                        else { values.push(data.settings[option]); }
                    }
                }
            });
 
            if(values.length === 1) { return values[0]; }
            if(values.length > 0) { return values; }
            else { return elements; }
        }
        
        settings = $.extend({}, $.fn.wScratchPad.defaultSettings, settings || {});
 
        return this.each(function()
        {
            var elem = $(this);
            var $settings = jQuery.extend(true, {}, settings);
 
            //test for HTML5 canvas
            var test = document.createElement('canvas');
            if(!test.getContext)
            {
                elem.html("Browser does not support HTML5 canvas, please upgrade to a more modern browser.");
                return false;    
            }
 
            var sp = new ScratchPad($settings, elem);
            
            elem.append(sp.generate());
            
            //get number of pixels of canvas for percent calculations 
            sp.pixels = sp.canvas.width * sp.canvas.height;
            
            elem.data('_wScratchPad', sp);
            
            sp.init();
        });
    };
 
    $.fn.wScratchPad.defaultSettings =
    {
        width        : 210,                        // set width - best to match image width
        height        : 100,                        // set height - best to match image height
        image        : null,        // set image path
        image2        : null,                        // set overlay image path - if set color is not used
        color        : '#336699',                // set scratch color - if image2 is not set uses color
        overlay        : 'none',                    // set the type of overlay effect 'none', 'lighter' - only used with color
        size        : 10,                        // set size of scratcher
        scratchDown    : null,                        // scratchDown callback
        scratchUp    : null,                        // scratchUp callback
        scratchMove    : null,                        // scratcMove callback
        cursor        : null                        // Set path to custom cursor
    };
    
    function ScratchPad(settings, elem)
    {
        this.sp = null;
        this.settings = settings;
        this.$elem = elem;
        
        this.enabled = true;
        this.scratch = false;
        
        this.canvas = null;
        this.ctx = null;
        
        return this;
    }
    
    ScratchPad.prototype = 
    {
        generate: function()
        {
            var $this = this;
            
            this.canvas = document.createElement('canvas');
            this.ctx = this.canvas.getContext('2d');
            
            this.sp =
            $('<div></div>')
            .css({position: 'relative'})
            .append(
                $(this.canvas)
                .attr('width', this.settings.width + 'px')
                .attr('height', this.settings.height + 'px')
            )
            
            $(this.canvas)
            .mousedown(function(e)
            {
                if(!$this.enabled) return true;
 
                e.preventDefault();
                e.stopPropagation();
                
                //reset canvas offset in case it has moved
                $this.canvas_offset = $($this.canvas).offset();
                
                $this.scratch = true;
                $this.scratchFunc(e, $this, 'Down');
            })
            .mousemove(function(e)
            {
                e.preventDefault();
                e.stopPropagation();
                
                if($this.scratch) $this.scratchFunc(e, $this, 'Move');
            })
            .mouseup(function(e)
            {
                e.preventDefault();
                e.stopPropagation();
                
                //make sure we are in draw mode otherwise this will fire on any mouse up.
                if($this.scratch)
                {
                    $this.scratch = false;
                    $this.scratchFunc(e, $this, 'Up');
                }
            });
 
            this.bindMobile(this.sp);
            
            return this.sp;
        },
        
        bindMobile: function($el)
        {
            $el.bind('touchstart touchmove touchend touchcancel', function ()
            {
                var touches = event.changedTouches, first = touches[0], type = ""; 
 
                switch (event.type)
                {
                    case "touchstart": type = "mousedown"; break; 
                    case "touchmove": type = "mousemove"; 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);
                event.preventDefault();
            });
        },
 
        init: function()
        {
            this.sp.css('width', this.settings.width);
            this.sp.css('height', this.settings.height);
            this.sp.css('cursor', (this.settings.cursor ? 'url("' + this.settings.cursor + '"), default' : 'default'));
 
            $(this.canvas).css({cursor: (this.settings.cursor ? 'url("' + this.settings.cursor + '"), default' : 'default')});
            
            this.canvas.width = this.settings.width;
            this.canvas.height = this.settings.height;
            
            this.pixels = this.canvas.width * this.canvas.height;
            
            if(this.settings.image2)
            {
                this.drawImage(this.settings.image2);
            }
            else
            {
                if(this.settings.overlay != 'none')
                {
                    if(this.settings.image)
                    {
                        this.drawImage(this.settings.image);
                    }
                    this.ctx.globalCompositeOperation = this.settings.overlay;
                }
                else
                {
                    this.setBgImage();
                }
                
                this.ctx.fillStyle = this.settings.color;
                this.ctx.beginPath();
                this.ctx.rect(0, 0, this.settings.width, this.settings.height)
                this.ctx.fill();
            }
        },
 
        reset: function()
        {
            this.ctx.globalCompositeOperation = 'source-over';
            this.init();
        },
 
        clear: function()
        {
            this.ctx.clearRect(0, 0, this.settings.width, this.settings.height);
        },
 
        setBgImage: function()
        {
            if(this.settings.image)
            {
                this.sp.css({backgroundImage: 'url('+this.settings.image+')'});
            }
        },
 
        drawImage: function(imagePath)
        {
            var $this = this;
            var img = new Image();
              img.src = imagePath;
              $(img).load(function(){
                  $this.ctx.drawImage(img, 0, 0);
                  $this.setBgImage();
              })
        },
 
        scratchFunc: function(e, $this, event)
        {
            e.pageX = Math.floor(e.pageX - $this.canvas_offset.left);
            e.pageY = Math.floor(e.pageY - $this.canvas_offset.top);
            
            $this['scratch' + event](e, $this);
            
            if($this.settings['scratch' + event]) $this.settings['scratch' + event].apply($this, [e, $this.scratchPercentage($this)]);
        },
 
        scratchPercentage: function($this)
        {
            var hits = 0;
            var 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)
        {
            $this.ctx.globalCompositeOperation = 'destination-out';
            $this.ctx.lineJoin = "round";
            $this.ctx.lineCap = "round";
            $this.ctx.strokeStyle = $this.settings.color;
            $this.ctx.lineWidth = $this.settings.size;
            
            //draw single dot in case of a click without a move
            $this.ctx.beginPath();
            $this.ctx.arc(e.pageX, e.pageY, $this.settings.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)
        {
            $this.ctx.lineTo(e.pageX, e.pageY);
            $this.ctx.stroke();
        },
        
        scratchUp: function(e, $this)
        {
            $this.ctx.closePath();
        },
    }
})(jQuery);