Skip to content

Commit d50c8cd

Browse files
committed
feat: blobs dot type
1 parent 53b6d29 commit d50c8cd

3 files changed

Lines changed: 131 additions & 2 deletions

File tree

src/figures/dot-elements.ts

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,98 @@ export const DotElements = {
274274
z`
275275
)
276276

277+
return element
278+
},
279+
blob: (args: BasicFigureDrawArgs, corners: Record<'tl' | 'tr' | 'bl' | 'br', 'round' | 'out' | 'square'>): SVGElement => {
280+
const { size, x, y, document } = args
281+
const dotSize = size / 7
282+
283+
const element = document.createElementNS('http://www.w3.org/2000/svg', 'path')
284+
285+
const parts = [
286+
svgPath`M ${x} ${y + 2.5 * dotSize}`
287+
]
288+
289+
let l = 2 * dotSize
290+
291+
if (corners.bl == 'round') {
292+
parts.push(
293+
svgPath`v ${l}
294+
a ${2.5 * dotSize} ${2.5 * dotSize} 0 0 0 ${dotSize * 2.5} ${dotSize * 2.5}`
295+
)
296+
l = 2 * dotSize
297+
} else if (corners.bl == 'out') {
298+
parts.push(
299+
svgPath`v ${5 * dotSize + l}
300+
a ${2.5 * dotSize} ${2.5 * dotSize} 0 0 1 ${dotSize * 2.5} ${dotSize * -2.5}`
301+
)
302+
l = 2 * dotSize
303+
} else {
304+
parts.push(
305+
svgPath`v ${2.5 * dotSize + l}`
306+
)
307+
l = 4.5 * dotSize
308+
}
309+
310+
if (corners.br == 'round') {
311+
parts.push(
312+
svgPath`h ${l}
313+
a ${2.5 * dotSize} ${2.5 * dotSize} 0 0 0 ${dotSize * 2.5} ${dotSize * -2.5}`
314+
)
315+
l = -2 * dotSize
316+
} else if (corners.br == 'out') {
317+
parts.push(
318+
svgPath`h ${5 * dotSize + l}
319+
a ${2.5 * dotSize} ${2.5 * dotSize} 0 0 1 ${dotSize * -2.5} ${dotSize * -2.5}`
320+
)
321+
l = -2 * dotSize
322+
} else {
323+
parts.push(
324+
svgPath`h ${2.5 * dotSize + l}`
325+
)
326+
l = -4.5 * dotSize
327+
}
328+
329+
if (corners.tr == 'round') {
330+
parts.push(
331+
svgPath`v ${l}
332+
a ${2.5 * dotSize} ${2.5 * dotSize} 0 0 0 ${dotSize * -2.5} ${dotSize * -2.5}`
333+
)
334+
l = -2 * dotSize
335+
} else if (corners.tr == 'out') {
336+
parts.push(
337+
svgPath`v ${-5 * dotSize + l}
338+
a ${2.5 * dotSize} ${2.5 * dotSize} 0 0 1 ${dotSize * -2.5} ${dotSize * 2.5}`
339+
)
340+
l = -2 * dotSize
341+
} else {
342+
parts.push(
343+
svgPath`v ${-2.5 * dotSize + l}`
344+
)
345+
l = -4.5 * dotSize
346+
}
347+
348+
if (corners.tl == 'round') {
349+
parts.push(
350+
svgPath`h ${l}
351+
a ${2.5 * dotSize} ${2.5 * dotSize} 0 0 0 ${dotSize * -2.5} ${dotSize * 2.5}`
352+
)
353+
} else if (corners.tl == 'out') {
354+
parts.push(
355+
svgPath`h ${-5 * dotSize + l}
356+
a ${2.5 * dotSize} ${2.5 * dotSize} 0 0 1 ${dotSize * 2.5} ${dotSize * 2.5}`
357+
)
358+
} else {
359+
parts.push(
360+
svgPath`h ${-2.5 * dotSize + l}`
361+
)
362+
}
363+
364+
element.setAttribute(
365+
'd',
366+
parts.join(' ') + ' z'
367+
)
368+
277369
return element
278370
}
279371
}

src/figures/dot.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,43 @@ const qrDotFigures: { [type in DotType]: (args: DrawArgs) => SVGElement } = {
111111
const y = args.y + (args.size - height) * rnd()
112112
return DotElements.square({ ..._args, y }, { height })
113113
}
114-
})
114+
}),
115+
[DotType.blobs]: ({ getNeighbor, ...args }) => {
116+
const tl = getNeighbor?.(-1, -1)
117+
const t = getNeighbor?.(0, -1)
118+
const tr = getNeighbor?.(1, -1)
119+
const r = getNeighbor?.(1, 0)
120+
const br = getNeighbor?.(1, 1)
121+
const b = getNeighbor?.(0, 1)
122+
const bl = getNeighbor?.(-1, 1)
123+
const l = getNeighbor?.(-1, 0)
124+
const corners: Record<'tl' | 'tr' | 'bl' | 'br', 'round' | 'out' | 'square'> = {
125+
tl: 'square',
126+
tr: 'square',
127+
bl: 'square',
128+
br: 'square'
129+
}
130+
131+
if (!l && tl)
132+
corners.tl = 'out'
133+
if (!t && tr)
134+
corners.tr = 'out'
135+
if (!r && br)
136+
corners.br = 'out'
137+
if (!b && bl)
138+
corners.bl = 'out'
139+
140+
if (!l && !tl && !t)
141+
corners.tl = 'round'
142+
if (!t && !tr && !r)
143+
corners.tr = 'round'
144+
if (!r && !br && !b)
145+
corners.br = 'round'
146+
if (!b && !bl && !l)
147+
corners.bl = 'round'
148+
149+
return DotElements.blob(args, corners)
150+
}
115151
}
116152

117153
export function getQrDotFigure(type: `${DotType}`, plugins?: Plugin[]) {

src/utils/options.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ export enum DotType {
2525
zebraHorizontal = 'zebra-horizontal',
2626
zebraVertical = 'zebra-vertical',
2727
blocksHorizontal = 'blocks-horizontal',
28-
blocksVertical = 'blocks-vertical'
28+
blocksVertical = 'blocks-vertical',
29+
blobs = 'blobs'
2930
}
3031

3132
export enum CornerDotType {

0 commit comments

Comments
 (0)