-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdrawingTools.js
98 lines (79 loc) · 2.45 KB
/
drawingTools.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
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
"use strict";
function setColors(ctx,stroke,fill) {
if(typeof(stroke)==='undefined') stroke = 'black';
if(typeof(fill)==='undefined') fill = 'blue';
ctx.fillStyle = fill;
ctx.strokeStyle = stroke;
}
function drawLine(ctx, p1, p2) {
ctx.beginPath();
ctx.moveTo(p1.x, p1.y);
ctx.lineTo(p2.x, p2.y);
ctx.stroke();
}
function drawCircle(ctx, p, r) {
ctx.beginPath();
ctx.arc(p.x, p.y, r, 0, 2*Math.PI, false);
ctx.fill();
ctx.lineWidth = 1;
ctx.stroke();
}
function drawText(ctx,x,y,str) {
var oldFill = ctx.fillStyle;
ctx.font = '12pt Calibri';
ctx.fillStyle = 'rgb(10,70,160)';
ctx.fillText(str, x, y);
ctx.fillStyle = oldFill;
}
function getMousePos(evt) {
var rect = evt.target.getBoundingClientRect();
return {
x: evt.clientX - rect.left,
y: evt.clientY - rect.top
};
}
function drawCurve (ctx, curve) {
if (curve.nodes.length < 4) return
var samples = 500;
var v1 = curve.knots[2].value;
var v2 = curve.knots[curve.knots.length - 3].value;
var p1 = evaluateBSpline (v1, curve).p;
for (var i = 0; i < samples; i++) {
var t = v1 + (v2 - v1) / samples * i;
var p2 = evaluateBSpline (t, curve).p;
drawLine (ctx, p1, p2);
p1 = p2;
}
}
function drawConstruction (ctx, construction) {
setColors (ctx,'red', 'red');
drawCircle (ctx, construction.p10, 4);
drawCircle (ctx, construction.p20, 4);
drawCircle (ctx, construction.p30, 4);
drawCircle (ctx, construction.p40, 4);
drawLine (ctx, construction.p10, construction.p20);
drawLine (ctx, construction.p20, construction.p30);
drawLine (ctx, construction.p30, construction.p40);
setColors (ctx,'orange', 'orange');
drawCircle (ctx, construction.p21, 2);
drawCircle (ctx, construction.p31, 2);
drawCircle (ctx, construction.p41, 2);
drawLine (ctx, construction.p21, construction.p31);
drawLine (ctx, construction.p31, construction.p41);
setColors (ctx,'yellow', 'yellow');
drawCircle (ctx, construction.p32, 2);
drawCircle (ctx, construction.p42, 2);
drawLine (ctx, construction.p32, construction.p42);
setColors (ctx,'green', 'green');
drawCircle (ctx, construction.p, 2);
}
function drawBezier (ctx, p1, p2, p3, p4) {
setColors(ctx,'gray', 'gray');
drawCircle (ctx, p1, 4);
drawCircle (ctx, p2, 4);
drawCircle (ctx, p3, 4);
drawCircle (ctx, p4, 4);
drawLine (ctx, p1, p2);
drawLine (ctx, p2, p3);
drawLine (ctx, p3, p4);
}