Debian Codegraph @

texmf-dist/doc/latex/figput/example/externalcode.js

raw
1
2 // This file demonstrates two things: how to use an external js
3 // file, and use of the DraggableDrawWidget.
4
5 // Functions passed to DraggableDrawWidgets
6
7 function getRawSquare() {
8
9 // The 'radius' of a square.
10 let r = 3;
11 let p = new FPath();
12 p.moveTo(-r,-r);
13 p.lineTo(r,-r);
14 p.lineTo(r,r);
15 p.lineTo(-r,r);
16 p.closePath();
17
18 return p;
19 }
20
21 function drawSquareDot(ctx) {
22
23 let p = getRawSquare();
24 ctx.fillStyle='red';
25 ctx.fill(p);
26 return p;
27 }
28
29 function drawSquareDotSelect(ctx) {
30
31 let p = getRawSquare();
32 ctx.fillStyle='green';
33 ctx.fill(p);
34 return p;
35 }
36
37 function squareLocationOK(x,y,w,ha,hb) {
38
39 // A bit of padding so that we don't leave "crumbs" around the edges.
40 let pad = 5;
41
42 if (x < pad) return false;
43 if (x > w - pad) return false;
44
45 // Note the minus sign. We need to compare to y, which may be negative.
46 if (y < -hb + pad ) return false;
47 if (y > ha - pad ) return false;
48 return true;
49 }
50
51
52 // The top-level figure-drawing function.
53
54 function bezier(ctx) {
55
56 let w1 = DraggableDotWidget.register(ctx,70,165,'d1');
57 let w2 = DraggableDrawWidget.register(ctx,30,115,
58 drawSquareDot,drawSquareDotSelect,squareLocationOK,'d2');
59 let w3 = DraggableDrawWidget.register(ctx,120,25,
60 drawSquareDot,drawSquareDotSelect,squareLocationOK,'d3');
61 let w4 = DraggableDotWidget.register(ctx,270,105,'d4');
62
63 let p = new FPath();
64
65 // Straight lines from point to point.
66 p.moveTo(w1.widgetX,w1.widgetY);
67 p.lineTo(w2.widgetX,w2.widgetY);
68 p.lineTo(w3.widgetX,w3.widgetY);
69 p.lineTo(w4.widgetX,w4.widgetY);
70 ctx.strokeStyle = 'black';
71 ctx.lineWidth = 0.4;
72 ctx.stroke(p);
73
74 // Bezier curve determined by these points.
75 p = new FPath();
76 p.moveTo(w1.widgetX,w1.widgetY);
77 p.bezierCurveTo(w2.widgetX,w2.widgetY,
78 w3.widgetX,w3.widgetY,w4.widgetX,w4.widgetY);
79 ctx.strokeStyle = 'blue';
80 ctx.lineWidth = 1.5;
81 ctx.stroke(p);
82
83 // In this case (unlike most others), the widgets must be explicitly drawn.
84 w1.draw(ctx);
85 w2.draw(ctx);
86 w3.draw(ctx);
87 w4.draw(ctx);
88 }
89