draw.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import * as React from "react";
  2. import { pathOf, type Flat, type KeyMark, type LegendKey, type LineTone, type SolidPaths, type Tone } from "../iso-kit";
  3. import { Stage } from "./live";
  4. interface SolidProps {
  5. paths: SolidPaths;
  6. tone?: Tone;
  7. crease?: Tone | "none";
  8. halo?: boolean;
  9. lit?: boolean;
  10. flat?: boolean;
  11. className?: string;
  12. inner?: React.ReactNode;
  13. children?: React.ReactNode;
  14. }
  15. export function Solid({ paths, tone = "hi", crease = "faint", halo = false, lit = false, flat = false, className, inner, children }: SolidProps) {
  16. return (
  17. <g className={className ? `iso-solid ${className}` : "iso-solid"} data-lit={lit ? "" : undefined}>
  18. {halo ? <path className="iso-halo" d={paths.fill} /> : null}
  19. <path className="iso-fill" d={paths.fill} />
  20. {flat ? null : paths.shades.map((d, shade) => (d ? <path key={shade} className="iso-shade" data-shade={shade} d={d} /> : null))}
  21. {flat ? null : <path className="iso-top" d={paths.top} />}
  22. {crease === "none" || !paths.crease ? null : <path className="iso-line iso-crease" data-tone={crease} d={paths.crease} />}
  23. {crease === "none" || !paths.bevel ? null : <path className="iso-line iso-bevel" data-tone={crease} d={paths.bevel} />}
  24. {inner}
  25. <path className="iso-line iso-edge" data-tone={tone} d={paths.outline} />
  26. {children}
  27. </g>
  28. );
  29. }
  30. interface LineProps {
  31. d: string;
  32. tone?: LineTone;
  33. dotted?: boolean;
  34. dashed?: boolean;
  35. flow?: boolean;
  36. className?: string;
  37. free?: boolean | "start" | "end";
  38. }
  39. export function Line({ d, tone = "lo", dotted = false, dashed = false, flow = false, className, free }: LineProps) {
  40. if (!d) return null;
  41. return (
  42. <path
  43. className={className ? `iso-line ${className}` : "iso-line"}
  44. data-tone={tone}
  45. data-dotted={dotted ? "" : undefined}
  46. data-dashed={dashed ? "" : undefined}
  47. data-flow={flow ? "" : undefined}
  48. data-free={free === true ? "" : free || undefined}
  49. d={d}
  50. />
  51. );
  52. }
  53. interface WireProps {
  54. from: Flat;
  55. to: Flat;
  56. via?: Flat[];
  57. tone?: LineTone;
  58. dotted?: boolean;
  59. dashed?: boolean;
  60. flow?: boolean;
  61. ends?: "both" | "from" | "to" | "none";
  62. size?: number;
  63. }
  64. const tenth = (value: number) => Math.round(value * 10) / 10;
  65. export function Wire({ from, to, via = [], tone = "lo", dotted = false, dashed = false, flow = false, ends = "both", size = 1.2 }: WireProps) {
  66. return (
  67. <g className="iso-wire" data-tone={tone}>
  68. <Line d={pathOf([from, ...via, to])} tone={tone} dotted={dotted} dashed={dashed} flow={flow} />
  69. {ends === "both" || ends === "from" ? <circle className="iso-wire-end" cx={tenth(from[0])} cy={tenth(from[1])} r={size} /> : null}
  70. {ends === "both" || ends === "to" ? <circle className="iso-wire-end" cx={tenth(to[0])} cy={tenth(to[1])} r={size} /> : null}
  71. </g>
  72. );
  73. }
  74. export function Dots({ points, size = 0.5, tone = "mid", pulse = false }: { points: Flat[]; size?: number; tone?: Tone; pulse?: boolean }) {
  75. return (
  76. <g className="iso-dots" data-tone={tone}>
  77. {points.map(([x, y], index) => (
  78. <circle
  79. key={index}
  80. cx={tenth(x)}
  81. cy={tenth(y)}
  82. r={size}
  83. data-pulse={pulse ? "" : undefined}
  84. style={pulse ? ({ "--pulse-at": `${(index * 137) % 1600}ms` } as React.CSSProperties) : undefined}
  85. />
  86. ))}
  87. </g>
  88. );
  89. }
  90. export function FaceText({
  91. transform,
  92. children,
  93. size = 3,
  94. tone = "mid",
  95. anchor = "start",
  96. }: {
  97. transform: string;
  98. children: React.ReactNode;
  99. size?: number;
  100. tone?: Tone;
  101. anchor?: "start" | "middle" | "end";
  102. }) {
  103. return (
  104. <text className="iso-face-text" data-tone={tone} transform={transform} fontSize={size} textAnchor={anchor}>
  105. {children}
  106. </text>
  107. );
  108. }
  109. interface IsoFigureProps {
  110. width: number;
  111. height: number;
  112. label: string;
  113. className?: string;
  114. children: React.ReactNode;
  115. }
  116. export function IsoFigure({ width, height, label, className, children }: IsoFigureProps) {
  117. return (
  118. <Stage className={className ? `iso-stage ${className}` : "iso-stage"}>
  119. <svg className="iso-svg" viewBox={`0 0 ${width} ${height}`} role="img" aria-label={label} focusable="false">
  120. {children}
  121. </svg>
  122. </Stage>
  123. );
  124. }
  125. function Swatch({ mark }: { mark: KeyMark }) {
  126. if (mark === "raised" || mark === "flat" || mark === "lit") {
  127. return (
  128. <svg className="iso-swatch" data-mark={mark} viewBox="0 0 16 10" aria-hidden="true" focusable="false">
  129. <path d={mark === "flat" ? "M2 6.5 8 3.5 14 6.5 8 9.5Z" : "M2 4.5 8 1.5 14 4.5 14 6 8 9 2 6Z"} />
  130. {mark === "flat" ? null : <path className="iso-swatch-crease" d="M2 4.5 8 7.5 14 4.5" />}
  131. </svg>
  132. );
  133. }
  134. return (
  135. <svg className="iso-swatch" data-mark={mark} viewBox="0 0 16 10" aria-hidden="true" focusable="false">
  136. <path d="M1.5 5h13" />
  137. </svg>
  138. );
  139. }
  140. interface PlateProps {
  141. fig: string;
  142. title?: string;
  143. hint?: React.ReactNode;
  144. readout?: React.ReactNode;
  145. keys?: LegendKey[];
  146. caption?: React.ReactNode;
  147. children: React.ReactNode;
  148. }
  149. export function Plate({ fig, title, hint, readout, keys = [], caption, children }: PlateProps) {
  150. return (
  151. <figure className="iso-figure">
  152. <div className="iso-plate">
  153. <div className="iso-plate-corners" aria-hidden="true">
  154. <span className="iso-plate-corner" data-corner="fig">
  155. {fig}
  156. </span>
  157. {title ? (
  158. <span className="iso-plate-corner" data-corner="title">
  159. {title}
  160. </span>
  161. ) : null}
  162. {hint ? (
  163. <span className="iso-plate-corner" data-corner="hint">
  164. {hint}
  165. </span>
  166. ) : null}
  167. <span className="iso-plate-corner" data-corner="readout" data-readout="">
  168. {readout}
  169. </span>
  170. </div>
  171. {children}
  172. </div>
  173. {keys.length || caption ? (
  174. <figcaption className="iso-legend">
  175. {keys.length ? (
  176. <span className="iso-keys">
  177. {keys.map((key) => (
  178. <span className="iso-key" key={key.label}>
  179. <Swatch mark={key.mark} />
  180. {key.label}
  181. </span>
  182. ))}
  183. </span>
  184. ) : null}
  185. {caption ? <span className="iso-caption">{caption}</span> : null}
  186. </figcaption>
  187. ) : null}
  188. </figure>
  189. );
  190. }