-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCls.js
67 lines (58 loc) · 1.55 KB
/
Cls.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
'use strict';
function Cls (dom, src) {
var $dom = $(dom);
var top = $dom.offset().top;
var left = $dom.offset().left;
var width = $dom.width();
var height = $dom.height();
var $img = $('<img>');
var img = $img[0];
var roundRect = function (context, x, y, w, h, r) {
var min_size = Math.min(w, h);
if (r > min_size / 2) r = min_size / 2;
// 开始绘制
context.beginPath();
context.moveTo(x + r, y);
context.arcTo(x + w, y, x + w, y + h, r);
context.arcTo(x + w, y + h, x, y + h, r);
context.arcTo(x, y + h, x, y, r);
context.arcTo(x, y, x + w, y, r);
context.closePath();
return this;
}
$img.css({
display: 'none',
width: width,
height: height
}).attr({
src: src
});
$img.appendTo($dom.parent());
$img.on('load', function () {
var $canvas = $('<canvas>');
var context = $canvas[0].getContext('2d');
$canvas.css({
position: 'absolute',
top: top,
left: left
}).attr({
width: width,
height: height
})
$canvas.appendTo($dom.parent())
$canvas.on('mousemove', function (e) {
var radius = 100;
var top = e.offsetY - radius;
var left = e.offsetX - radius;
var width = radius * 2;
var height = radius * 2;
var pattern = context.createPattern(img, "no-repeat");
roundRect(context, left, top, width, height, 100);
context.fillStyle = pattern;
context.fill()
})
})
}
$(function () {
var eraser = new Eraser($('.bg'), 'http://sandbox.runjs.cn/uploads/rs/85/jud8ox3q/br.jpg');
})