annotate web/drawing.ts @ 2418:9bb0eb587d5b

640x480 camera res
author drewp@bigasterisk.com
date Tue, 21 May 2024 11:58:13 -0700
parents 4556eebe5d73
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1621
db84c1ee6b09 refactor to drawing.coffee
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
1
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 2060
diff changeset
2 export function svgPathFromPoints(pts: { forEach: (arg0: (p: any) => void) => void }) {
2060
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
3 let out = "";
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
4 pts.forEach(function (p: Number[] | { elements: Number[] }) {
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
5 let x, y;
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
6 if ((p as any).elements) {
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
7 // for vec2
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
8 [x, y] = (p as any).elements;
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
9 } else {
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
10 [x, y] = p as Number[];
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
11 }
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
12 if (out.length === 0) {
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
13 out = "M ";
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
14 } else {
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
15 out += "L ";
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
16 }
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
17 out += "" + x + "," + y + " ";
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
18 });
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
19 return out;
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
20 };
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
21
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 2060
diff changeset
22 export function line(
2060
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
23 ctx: { moveTo: (arg0: any, arg1: any) => void; lineTo: (arg0: any, arg1: any) => any },
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
24 p1: { e: (arg0: number) => any },
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
25 p2: { e: (arg0: number) => any }
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
26 ) {
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
27 ctx.moveTo(p1.e(1), p1.e(2));
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
28 return ctx.lineTo(p2.e(1), p2.e(2));
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
29 };
1621
db84c1ee6b09 refactor to drawing.coffee
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
30
2060
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
31 // http://stackoverflow.com/a/4959890
2062
d991f7c3485a WIP rough porting of coffee to ts
drewp@bigasterisk.com
parents: 2060
diff changeset
32 export function roundRect(
2060
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
33 ctx: {
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
34 beginPath: () => void;
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
35 moveTo: (arg0: any, arg1: any) => void;
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
36 lineTo: (arg0: number, arg1: number) => void;
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
37 arc: (arg0: number, arg1: number, arg2: any, arg3: number, arg4: number, arg5: boolean) => void;
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
38 closePath: () => any;
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
39 },
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
40 sx: number,
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
41 sy: number,
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
42 ex: number,
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
43 ey: number,
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
44 r: number
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
45 ) {
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
46 const d2r = Math.PI / 180;
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
47 if (ex - sx - 2 * r < 0) {
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
48 r = (ex - sx) / 2;
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
49 } // ensure that the radius isn't too large for x
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
50 if (ey - sy - 2 * r < 0) {
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
51 r = (ey - sy) / 2;
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
52 } // ensure that the radius isn't too large for y
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
53 ctx.beginPath();
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
54 ctx.moveTo(sx + r, sy);
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
55 ctx.lineTo(ex - r, sy);
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
56 ctx.arc(ex - r, sy + r, r, d2r * 270, d2r * 360, false);
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
57 ctx.lineTo(ex, ey - r);
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
58 ctx.arc(ex - r, ey - r, r, d2r * 0, d2r * 90, false);
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
59 ctx.lineTo(sx + r, ey);
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
60 ctx.arc(sx + r, ey - r, r, d2r * 90, d2r * 180, false);
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
61 ctx.lineTo(sx, sy + r);
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
62 ctx.arc(sx + r, sy + r, r, d2r * 180, d2r * 270, false);
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
63 return ctx.closePath();
905cf827d750 port to ts (aside from exporting the funcs correctly)
drewp@bigasterisk.com
parents: 1753
diff changeset
64 };