-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (48 loc) · 1.06 KB
/
index.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
function drawOutline(out, ctx, offset) {
ctx.strokeStyle = "blue";
ctx.fillStyle = "blue";
ctx.save();
ctx.globalCompositeOperation = 'xor';
ctx.translate(offset,0);
for(var i = 0; i < out.polys.length; i++) {
drawPoly(out.polys[i], ctx);
}
ctx.restore();
}
function drawPoly(p, ctx) {
var s = p.start;
ctx.beginPath();
ctx.moveTo(s.x, s.y);
for(var i = 0; i < p.lines.length; i++) {
s = drawLine(s, p.lines[i], ctx);
}
ctx.moveTo(s.x, s.y);
ctx.closePath();
ctx.fill();
}
function drawLine(s, line, ctx) {
var points = line.points, len = points.length;
if(line.type === 'line') {
for(var i = 0; i < len; i++) {
ctx.lineTo(points[i].x, points[i].y);
}
} else {
for(var i = 0; i < len - 1; i++) {
var pb = points[i], pc = {x:0,y:0};
if(i<len-2){
pc.x = (pb.x + points[i+1].x) / 2;
pc.y = (pb.y + points[i+1].y) / 2;
} else {
pc = points[i+1];
}
ctx.quadraticCurveTo(pb.x, pb.y, pc.x, pc.y);
}
}
return points[len-1];
}
function getMiddlePoint(p1, p2) {
return {
x : (p1.x + p2.x) / 2,
y : (p1.y + p2.y) / 2
}
}