feat: add overrides option

This commit is contained in:
Anthony Fu
2024-02-22 14:10:55 +01:00
parent f86983354e
commit 0f0a7279b9
3 changed files with 16 additions and 5 deletions

View File

@@ -6,4 +6,6 @@ Changes in this fork:
- Expose config and data types - Expose config and data types
- Support custom `class` option - Support custom `class` option
- Support `overrides` option for customizing RoughJS
- Improved build assets with `.cjs` and `.mjs` - Improved build assets with `.cjs` and `.mjs`
- `annotation.show()` returns a promise that resolves when the animation is done

View File

@@ -16,7 +16,7 @@ function getDefaultOptions(): ResolvedOptions {
return defaultOptions return defaultOptions
} }
function getOptions(type: RoughOptionsType, seed: number): ResolvedOptions { function getOptions(type: RoughOptionsType, seed: number, overrides?: Partial<ResolvedOptions>): ResolvedOptions {
return { return {
...getDefaultOptions(), ...getDefaultOptions(),
maxRandomnessOffset: 2, maxRandomnessOffset: 2,
@@ -38,6 +38,7 @@ function getOptions(type: RoughOptionsType, seed: number): ResolvedOptions {
disableMultiStroke: type !== 'double', disableMultiStroke: type !== 'double',
disableMultiStrokeFill: false, disableMultiStrokeFill: false,
seed, seed,
...overrides,
} }
} }
@@ -82,7 +83,7 @@ export function renderAnnotation(
const animate = (config.animate === undefined) ? true : (!!config.animate) const animate = (config.animate === undefined) ? true : (!!config.animate)
const iterations = config.iterations || 2 const iterations = config.iterations || 2
const rtl = config.rtl ? 1 : 0 const rtl = config.rtl ? 1 : 0
const o = getOptions('single', seed) const o = getOptions('single', seed, config.overrides)
switch (config.type) { switch (config.type) {
case 'underline': { case 'underline': {
@@ -182,7 +183,7 @@ export function renderAnnotation(
break break
} }
case 'circle': { case 'circle': {
const doubleO = getOptions('double', seed) const doubleO = getOptions('double', seed, config.overrides)
const width = rect.w + (padding[1] + padding[3]) const width = rect.w + (padding[1] + padding[3])
const height = rect.h + (padding[0] + padding[2]) const height = rect.h + (padding[0] + padding[2])
const x = rect.x - padding[3] + (width / 2) const x = rect.x - padding[3] + (width / 2)
@@ -198,7 +199,7 @@ export function renderAnnotation(
break break
} }
case 'highlight': { case 'highlight': {
const o = getOptions('highlight', seed) const o = getOptions('highlight', seed, config.overrides)
strokeWidth = rect.h * 0.95 strokeWidth = rect.h * 0.95
const y = rect.y + (rect.h / 2) const y = rect.y + (rect.h / 2)
for (let i = rtl; i < iterations + rtl; i++) { for (let i = rtl; i < iterations + rtl; i++) {

View File

@@ -1,3 +1,5 @@
import type { ResolvedOptions } from 'roughjs/bin/core'
export interface Rect { export interface Rect {
x: number x: number
y: number y: number
@@ -24,8 +26,14 @@ export interface RoughAnnotationConfigBase {
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' brackets?: BracketType | BracketType[] // defaults to 'right'
// Additional class added to the annotation /**
* Additional class to add to the root SVG element
*/
class?: string class?: string
/**
* RoughJS options
*/
overrides?: Partial<ResolvedOptions>
} }
export interface RoughAnnotation extends RoughAnnotationConfigBase { export interface RoughAnnotation extends RoughAnnotationConfigBase {