forked from paperjs/paper.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBezierTool.html
More file actions
90 lines (85 loc) · 2.16 KB
/
Copy pathBezierTool.html
File metadata and controls
90 lines (85 loc) · 2.16 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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Bezier Tool</title>
<link rel="stylesheet" href="../css/style.css">
<script type="text/javascript" src="../../dist/paper.js"></script>
<script type="text/paperscript" canvas="canvas">
var path;
var types = ['point', 'handleIn', 'handleOut'];
function findHandle(point) {
for (var i = 0, l = path.segments.length; i < l; i++) {
for (var j = 0; j < 3; j++) {
var type = types[j];
var segment = path.segments[i];
var segmentPoint = type == 'point'
? segment.point
: segment.point + segment[type];
var distance = (point - segmentPoint).length;
if (distance < 3) {
return {
type: type,
segment: segment
};
}
}
}
return null;
}
var currentSegment, mode, type;
function onMouseDown(event) {
if (currentSegment)
currentSegment.selected = false;
mode = type = currentSegment = null;
if (!path) {
path = new Path();
path.fillColor = {
hue: 360 * Math.random(),
saturation: 1,
brightness: 1,
alpha: 0.5
};
}
var result = findHandle(event.point);
if (result) {
currentSegment = result.segment;
type = result.type;
if (path.segments.length > 1 && result.type == 'point'
&& result.segment.index == 0) {
mode = 'close';
path.closed = true;
path.selected = false;
path = null;
}
}
if (mode != 'close') {
mode = currentSegment ? 'move' : 'add';
if (!currentSegment)
currentSegment = path.add(event.point);
currentSegment.selected = true;
}
}
function onMouseDrag(event) {
if (mode == 'move' && type == 'point') {
currentSegment.point = event.point;
} else if (mode != 'close') {
var delta = event.delta.clone();
if (type == 'handleOut' || mode == 'add')
delta = -delta;
currentSegment.handleIn += delta;
currentSegment.handleOut -= delta;
}
}
</script>
</head>
<body>
<p>
An emulation of a vector pen tool.
Click and drag to add a points.<br/>
Drag segment handles and points to manipulate them.
Close the path to start a new one.
</p>
<canvas id="canvas" resize></canvas>
</body>
</html>