UNPKG

@tldraw/editor

Version:

tldraw infinite canvas SDK (editor).

8 lines (7 loc) 3.98 kB
{ "version": 3, "sources": ["../../../../../src/lib/editor/shapes/shared/getPerfectDashProps.ts"], "sourcesContent": ["import { TLDefaultDashStyle } from '@tldraw/tlschema'\n\n/** @public */\nexport type PerfectDashTerminal = 'skip' | 'outset' | 'none'\n\n/** @public */\nexport function getPerfectDashProps(\n\ttotalLength: number,\n\tstrokeWidth: number,\n\topts: {\n\t\tstyle?: TLDefaultDashStyle\n\t\tsnap?: number\n\t\tend?: PerfectDashTerminal\n\t\tstart?: PerfectDashTerminal\n\t\tlengthRatio?: number\n\t\tclosed?: boolean\n\t\tforceSolid?: boolean\n\t} = {}\n): {\n\tstrokeDasharray: string\n\tstrokeDashoffset: string\n} {\n\tconst {\n\t\tclosed = false,\n\t\tsnap = 1,\n\t\tstart = 'outset',\n\t\tend = 'outset',\n\t\tlengthRatio = 2,\n\t\tstyle = 'dashed',\n\t\tforceSolid = false,\n\t} = opts\n\n\tlet dashLength = 0\n\tlet dashCount = 0\n\tlet ratio = 1\n\tlet gapLength = 0\n\tlet strokeDashoffset = 0\n\n\tif (forceSolid) {\n\t\treturn {\n\t\t\tstrokeDasharray: 'none',\n\t\t\tstrokeDashoffset: 'none',\n\t\t}\n\t}\n\n\tswitch (style) {\n\t\tcase 'dashed': {\n\t\t\tratio = 1\n\t\t\tdashLength = Math.min(strokeWidth * lengthRatio, totalLength / 4)\n\t\t\tbreak\n\t\t}\n\t\tcase 'dotted': {\n\t\t\tratio = 100\n\t\t\tdashLength = strokeWidth / ratio\n\t\t\tbreak\n\t\t}\n\t\tdefault: {\n\t\t\treturn {\n\t\t\t\tstrokeDasharray: 'none',\n\t\t\t\tstrokeDashoffset: 'none',\n\t\t\t}\n\t\t}\n\t}\n\n\tif (!closed) {\n\t\tif (start === 'outset') {\n\t\t\ttotalLength += dashLength / 2\n\t\t\tstrokeDashoffset += dashLength / 2\n\t\t} else if (start === 'skip') {\n\t\t\ttotalLength -= dashLength\n\t\t\tstrokeDashoffset -= dashLength\n\t\t}\n\n\t\tif (end === 'outset') {\n\t\t\ttotalLength += dashLength / 2\n\t\t} else if (end === 'skip') {\n\t\t\ttotalLength -= dashLength\n\t\t}\n\t}\n\n\tdashCount = Math.floor(totalLength / dashLength / (2 * ratio))\n\tdashCount -= dashCount % snap\n\n\tif (dashCount < 3 && style === 'dashed') {\n\t\tif (totalLength / strokeWidth < 4) {\n\t\t\tdashLength = totalLength\n\t\t\tdashCount = 1\n\t\t\tgapLength = 0\n\t\t} else {\n\t\t\tdashLength = totalLength * (1 / 3)\n\t\t\tgapLength = totalLength * (1 / 3)\n\t\t}\n\t} else {\n\t\tdashLength = totalLength / dashCount / (2 * ratio)\n\n\t\tif (closed) {\n\t\t\tstrokeDashoffset = dashLength / 2\n\t\t\tgapLength = (totalLength - dashCount * dashLength) / dashCount\n\t\t} else {\n\t\t\tgapLength = (totalLength - dashCount * dashLength) / Math.max(1, dashCount - 1)\n\t\t}\n\t}\n\n\treturn {\n\t\tstrokeDasharray: [dashLength, gapLength].join(' '),\n\t\tstrokeDashoffset: strokeDashoffset.toString(),\n\t}\n}\n"], "mappings": "AAMO,SAAS,oBACf,aACA,aACA,OAQI,CAAC,GAIJ;AACD,QAAM;AAAA,IACL,SAAS;AAAA,IACT,OAAO;AAAA,IACP,QAAQ;AAAA,IACR,MAAM;AAAA,IACN,cAAc;AAAA,IACd,QAAQ;AAAA,IACR,aAAa;AAAA,EACd,IAAI;AAEJ,MAAI,aAAa;AACjB,MAAI,YAAY;AAChB,MAAI,QAAQ;AACZ,MAAI,YAAY;AAChB,MAAI,mBAAmB;AAEvB,MAAI,YAAY;AACf,WAAO;AAAA,MACN,iBAAiB;AAAA,MACjB,kBAAkB;AAAA,IACnB;AAAA,EACD;AAEA,UAAQ,OAAO;AAAA,IACd,KAAK,UAAU;AACd,cAAQ;AACR,mBAAa,KAAK,IAAI,cAAc,aAAa,cAAc,CAAC;AAChE;AAAA,IACD;AAA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