bracket annotation support (#50)

This commit is contained in:
Preet
2020-06-22 20:28:41 -07:00
committed by GitHub
parent e1d7e1b930
commit 212bfa06bd
2 changed files with 55 additions and 3 deletions

View File

@@ -9,9 +9,10 @@ export interface Rect {
h: number; h: number;
} }
export type RoughAnnotationType = 'underline' | 'box' | 'circle' | 'highlight' | 'strike-through' | 'crossed-off'; export type RoughAnnotationType = 'underline' | 'box' | 'circle' | 'highlight' | 'strike-through' | 'crossed-off' | 'bracket';
export type FullPadding = [number, number, number, number]; export type FullPadding = [number, number, number, number];
export type RoughPadding = number | [number, number] | FullPadding; export type RoughPadding = number | [number, number] | FullPadding;
export type BracketType = 'left' | 'right' | 'top' | 'bottom';
export interface RoughAnnotationConfig extends RoughAnnotationConfigBase { export interface RoughAnnotationConfig extends RoughAnnotationConfigBase {
type: RoughAnnotationType; type: RoughAnnotationType;
@@ -25,6 +26,7 @@ export interface RoughAnnotationConfigBase {
strokeWidth?: number; // default based on type strokeWidth?: number; // default based on type
padding?: RoughPadding; // defaults to 5px padding?: RoughPadding; // defaults to 5px
iterations?: number; // defaults to 2 iterations?: number; // defaults to 2
brackets?: BracketType | BracketType[]; // defaults to 'right'
} }
export interface RoughAnnotation extends RoughAnnotationConfigBase { export interface RoughAnnotation extends RoughAnnotationConfigBase {

View File

@@ -1,6 +1,7 @@
import { Rect, RoughAnnotationConfig, SVG_NS, FullPadding } from './model.js'; import { Rect, RoughAnnotationConfig, SVG_NS, FullPadding, BracketType } from './model.js';
import { ResolvedOptions, OpSet } from 'roughjs/bin/core'; import { ResolvedOptions, OpSet } from 'roughjs/bin/core';
import { line, rectangle, ellipse } from 'roughjs/bin/renderer'; import { line, rectangle, ellipse, linearPath } from 'roughjs/bin/renderer';
import { Point } from 'roughjs/bin/geometry';
type RoughOptionsType = 'highlight' | 'single' | 'double'; type RoughOptionsType = 'highlight' | 'single' | 'double';
@@ -97,6 +98,55 @@ export function renderAnnotation(svg: SVGSVGElement, rect: Rect, config: RoughAn
} }
break; break;
} }
case 'bracket': {
const o = getOptions('single', seed);
const brackets: BracketType[] = Array.isArray(config.brackets) ? config.brackets : (config.brackets ? [config.brackets] : ['right']);
const lx = rect.x - padding[3] * 2;
const rx = rect.x + rect.w + padding[1] * 2;
const ty = rect.y - padding[0] * 2;
const by = rect.y + rect.h + padding[2] * 2;
for (const br of brackets) {
let points: Point[];
switch (br) {
case 'bottom':
points = [
[lx, rect.y + rect.h],
[lx, by],
[rx, by],
[rx, rect.y + rect.h]
];
break;
case 'top':
points = [
[lx, rect.y],
[lx, ty],
[rx, ty],
[rx, rect.y]
];
break;
case 'left':
points = [
[rect.x, ty],
[lx, ty],
[lx, by],
[rect.x, by]
];
break;
case 'right':
points = [
[rect.x + rect.w, ty],
[rx, ty],
[rx, by],
[rect.x + rect.w, by]
];
break;
}
if (points) {
opList.push(linearPath(points, false, o));
}
}
break;
}
case 'crossed-off': { case 'crossed-off': {
const o = getOptions('single', seed); const o = getOptions('single', seed);
const x = rect.x; const x = rect.x;