Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

release: v1.2.0 #53

Merged
merged 6 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion common/stories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const meta = {
shapes: {
body: {
control: 'select',
options: ['square', 'circle', 'leaf', 'diamond', 'heart', 'triangle'],
options: ['square', 'circle', 'leaf', 'diamond', 'heart', 'triangle', 'rounded'],
},
eyeball: {
control: 'select',
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
"storybook": "^7.6.20",
"storybook-solidjs": "1.0.0-beta.2",
"storybook-solidjs-vite": "1.0.0-beta.2",
"turbo": "2.0.4",
"turbo": "2.1.3",
"typescript": "^5.5.3",
"vite": "^5.3.3"
"vite": "5.3.3"
},
"devDependencies": {
"@types/node": "^20.14.9",
Expand Down
19 changes: 17 additions & 2 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,10 @@ export function getSVGData({ data, shapes, gradient, ...options }: Omit<Options,
switch (true) {
case isEyeArea:
return ''
case isON:
return bodyShapes[$shapes.body](i, j)
case isON:{
const getNeighbor = getNeighborHOF(i, j, modules);
return bodyShapes[$shapes.body](i,j,getNeighbor);
}
default:
return ''
}
Expand Down Expand Up @@ -86,3 +88,16 @@ export function getSVGData({ data, shapes, gradient, ...options }: Omit<Options,
$gradient: gradient ? parseGradient({ id: `gradient-${id}`, ...gradient }) : undefined,
}
}

function getNeighborHOF(x:number, y:number, modules:boolean[][]) {
return function (
xOffset:number, yOffset:number
) {
const count = modules.length;
const isOn = (r:number, c:number) => modules[r] && modules[r][c];
// if outside qr
if (x + xOffset < 0 || y + yOffset < 0 || x + xOffset >= count || y + yOffset >= count) return false;

return isOn(x+xOffset, y+yOffset);
}
}
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@qr-x/core",
"license": "MIT",
"version": "1.0.0",
"version": "1.2.0",
"description": "",
"main": "dist/index.cjs.js",
"types": "dist/index.d.ts",
Expand Down
95 changes: 86 additions & 9 deletions packages/core/src/shapes.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
type GetPath = (x: number, y: number) => string
type GetNeighbor = (xOffset:number, yOffset:number) => boolean
type GetPath = (x: number, y: number, getNeighbor?: GetNeighbor) => string
type Corner = "topLeft" | "topRight" | "bottomRight" | "bottomLeft"
type Corners = Record<Corner,string>
type Side = "top" | "right" | "bottom" | "left"
type Sides = Record<Side,string>

export const bodyShapes = {
circle: ((x, y) => {
const r = 0.45
return `M ${x + r * 2}, ${y + r}
a ${r},${r} 45 1,0 -${r * 2},0,
a ${r},${r} 45 1,0 ${r * 2},0`
// rotation 45deg is to smooth the edges of the circle (bug in chromium)
}) satisfies GetPath,
square: ((x, y) => `M ${x} ${y} h 1 v 1 h -1 v -1`) satisfies GetPath,
circle,
square,
rounded,
diamond: ((x, y) => `M ${x + 0.5} ${y} l 0.5 0.5 l -0.5 0.5 l -0.5 -0.5 Z`) satisfies GetPath,
triangle: ((x, y) => `M ${x} ${y + 1} l 0.5 -1 l 0.5 1 Z`) satisfies GetPath,
heart: ((x, y) => `M ${x + 0.5} ${y + 0.5}
Expand Down Expand Up @@ -41,3 +41,80 @@ export const eyeframeShapes = {
0 + 2
}a2,2,0,0,1,2,-2h3a2,2,0,0,1,2,2v3a2,2,0,0,1,-2,2h-3a2,2,0,0,1,-2,-2v-3zm2,-1a1,1,0,0,0,-1,1v3a1,1,0,0,0,1,1h3a1,1,0,0,0,1,-1v-3a1,1,0,0,0,-1,-1h-3z`,
}

function rounded(x:number, y:number, getNeighbor?:GetNeighbor) {
const leftNeighbor = getNeighbor ? +getNeighbor(-1, 0) : 0;
const rightNeighbor = getNeighbor ? +getNeighbor(1, 0) : 0;
const topNeighbor = getNeighbor ? +getNeighbor(0, -1) : 0;
const bottomNeighbor = getNeighbor ? +getNeighbor(0, 1) : 0;

const neighborsCount = leftNeighbor + rightNeighbor + topNeighbor + bottomNeighbor;

if (neighborsCount === 0) {
return circle(x, y);
}

if (neighborsCount > 2 || (leftNeighbor && rightNeighbor) || (topNeighbor && bottomNeighbor)) {
return square(x, y);
}

if (neighborsCount === 2) {
let corner:Corner = "topRight";

if (leftNeighbor && topNeighbor) {
corner = "bottomRight";
} else if (topNeighbor && rightNeighbor) {
corner = "bottomLeft";
} else if (rightNeighbor && bottomNeighbor) {
corner = "topLeft";
}

return basicCornerRounded(x, y, corner);
}

if (neighborsCount === 1) {
let side:Side = "right";

if (topNeighbor) {
side = "bottom";
} else if (rightNeighbor) {
side = "left";
} else if (bottomNeighbor) {
side = "top";
}

return basicSideRounded(x, y, side);
}
return ``
}

function circle(x:number, y:number){
const r = 0.45
return `M ${x + r * 2}, ${y + r}
a ${r},${r} 45 1,0 -${r * 2},0,
a ${r},${r} 45 1,0 ${r * 2},0`
}

function square(x:number, y:number){
return `M ${x} ${y} h 1 v 1 h -1 v -1`
}

function basicCornerRounded(x:number, y:number, corner:Corner) {
const corners:Corners = {
topLeft: `M ${x + 1} ${y} h -0.5 a 0.5 0.5 0 0 0 -0.5 0.5 v 0.5 h 1 v -1 h -0.5`,
topRight: `M ${x} ${y} h 0 v 1 h 1 v -0.5 a 0.5 0.5 0 0 0, -0.5 -0.5 h -0.5`,
bottomRight: `M ${x} ${y} h 1 v 0.5 a 0.5 0.5, 0, 0 1, -0.5 0.5 h -0.5`,
bottomLeft: `M ${x} ${y} h 1 v 1 h -0.5 a 0.5 0.5 0 0 1 -0.5 -0.5`
}
return corners[corner]
}

function basicSideRounded(x:number, y:number, side:Side) {
const sides:Sides = {
left: `M ${x + 0.5} ${y} h 0.5 v 1 h -0.5 a 0.5 0.5 0 0 1 0 -1`,
right: `M ${x} ${y}v 1h 0.5a 0.5 0.5, 0, 0, 0, 0 -1`,
top: `M ${x} ${y + 0.5} v 0.5 h 1 v -0.5 a 0.5 0.5 0 0 0 -1 0`,
bottom: `M ${x} ${y} h 1 v 0.5 a 0.5 0.5 0 0 1 -1 0`
}
return sides[side]
}
24 changes: 12 additions & 12 deletions packages/react-native/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ function App() {

# Props

| Name | Type | Default |
| :--------------- | :--------------------------------------------------------------- | :--------- |
| data | `string` | |
| level | `'L' \| 'M' \|'Q' \| 'H'` | `'L'` |
| shapes.body | `'square' \| 'circle' \| 'leaf' \| 'rounded'` | `'square'` |
| shapes.eyeball | `'square' \| 'circle' \| 'leaf' \| 'rounded'` | `'square'` |
| shapes.eyeframe | `'square' \| 'circle' \| 'leaf' \| 'rounded'` | `'square'` |
| gradient.type | `'linear' \| 'radial'` | |
| gradients.colors | `string[] \| {value: string, stop: number}` | |
| gradients.rotate | `number` (This property only exist if gradient.type is 'radial') | `45` |
| fillImage | `string` | |
| brand | `ComponentProps<'img'>` \| `ReactNode` | |
| Name | Type | Default |
| :--------------- | :---------------------------------------------------------------------------------- | :--------- |
| data | `string` | |
| level | `'L' \| 'M' \|'Q' \| 'H'` | `'L'` |
| shapes.body | `'square' \| 'circle' \| 'leaf' \| 'rounded' \| 'diamond' \| 'triangle' \| 'heart'` | `'square'` |
| shapes.eyeball | `'square' \| 'circle' \| 'leaf' \| 'rounded'` | `'square'` |
| shapes.eyeframe | `'square' \| 'circle' \| 'leaf' \| 'rounded'` | `'square'` |
| gradient.type | `'linear' \| 'radial'` | |
| gradients.colors | `string[] \| {value: string, stop: number}` | |
| gradients.rotate | `number` (This property only exist if gradient.type is 'radial') | `45` |
| fillImage | `string` | |
| brand | `ComponentProps<'img'>` \| `ReactNode` | |
2 changes: 1 addition & 1 deletion packages/react-native/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@qr-x/react-native",
"version": "1.1.1",
"version": "1.2.0",
"license": "MIT",
"description": "Create elegant QR codes",
"main": "dist/index.cjs.js",
Expand Down
24 changes: 12 additions & 12 deletions packages/react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ function App() {

# Props

| Name | Type | Default |
| :--------------- | :--------------------------------------------------------------- | :--------- |
| data | `string` | |
| level | `'L' \| 'M' \|'Q' \| 'H'` | `'L'` |
| shapes.body | `'square' \| 'circle' \| 'leaf' \| 'rounded'` | `'square'` |
| shapes.eyeball | `'square' \| 'circle' \| 'leaf' \| 'rounded'` | `'square'` |
| shapes.eyeframe | `'square' \| 'circle' \| 'leaf' \| 'rounded'` | `'square'` |
| gradient.type | `'linear' \| 'radial'` | |
| gradients.colors | `string[] \| {value: string, stop: number}` | |
| gradients.rotate | `number` (This property only exist if gradient.type is 'radial') | `45` |
| fillImage | `string` | |
| brand | `ComponentProps<'img'>` \| `ReactNode` | |
| Name | Type | Default |
| :--------------- | :---------------------------------------------------------------------------------- | :--------- |
| data | `string` | |
| level | `'L' \| 'M' \|'Q' \| 'H'` | `'L'` |
| shapes.body | `'square' \| 'circle' \| 'leaf' \| 'rounded' \| 'diamond' \| 'triangle' \| 'heart'` | `'square'` |
| shapes.eyeball | `'square' \| 'circle' \| 'leaf' \| 'rounded'` | `'square'` |
| shapes.eyeframe | `'square' \| 'circle' \| 'leaf' \| 'rounded'` | `'square'` |
| gradient.type | `'linear' \| 'radial'` | |
| gradients.colors | `string[] \| {value: string, stop: number}` | |
| gradients.rotate | `number` (This property only exist if gradient.type is 'radial') | `45` |
| fillImage | `string` | |
| brand | `ComponentProps<'img'>` \| `ReactNode` | |
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@qr-x/react",
"version": "1.1.1",
"version": "1.2.0",
"license": "MIT",
"description": "Create elegant QR codes",
"main": "dist/index.cjs.js",
Expand Down
24 changes: 12 additions & 12 deletions packages/solid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,15 @@ function App() {

# Props

| Name | Type | Default |
| :--------------- | :--------------------------------------------------------------- | :--------- |
| data | `string` | |
| level | `'L' \| 'M' \|'Q' \| 'H'` | `'L'` |
| shapes.body | `'square' \| 'circle' \| 'leaf' \| 'rounded'` | `'square'` |
| shapes.eyeball | `'square' \| 'circle' \| 'leaf' \| 'rounded'` | `'square'` |
| shapes.eyeframe | `'square' \| 'circle' \| 'leaf' \| 'rounded'` | `'square'` |
| gradient.type | `'linear' \| 'radial'` | |
| gradients.colors | `string[] \| {value: string, stop: number}` | |
| gradients.rotate | `number` (This property only exist if gradient.type is 'radial') | `45` |
| fillImage | `string` | |
| brand | `ComponentProps<'img'>` \| `JSX.Element` | |
| Name | Type | Default |
| :--------------- | :---------------------------------------------------------------------------------- | :--------- |
| data | `string` | |
| level | `'L' \| 'M' \|'Q' \| 'H'` | `'L'` |
| shapes.body | `'square' \| 'circle' \| 'leaf' \| 'rounded' \| 'diamond' \| 'triangle' \| 'heart'` | `'square'` |
| shapes.eyeball | `'square' \| 'circle' \| 'leaf' \| 'rounded'` | `'square'` |
| shapes.eyeframe | `'square' \| 'circle' \| 'leaf' \| 'rounded'` | `'square'` |
| gradient.type | `'linear' \| 'radial'` | |
| gradients.colors | `string[] \| {value: string, stop: number}` | |
| gradients.rotate | `number` (This property only exist if gradient.type is 'radial') | `45` |
| fillImage | `string` | |
| brand | `ComponentProps<'img'>` \| `JSX.Element` | |
2 changes: 1 addition & 1 deletion packages/solid/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@qr-x/solid",
"version": "1.1.1",
"version": "1.2.0",
"license": "MIT",
"main": "dist/index.cjs.js",
"types": "dist/index.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@qr-x/svelte",
"version": "1.1.1",
"version": "1.2.0",
"license": "MIT",
"type": "module",
"main": "dist/index.js",
Expand Down
24 changes: 12 additions & 12 deletions packages/vanilla/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,15 @@ const qrx = createQRX({

# Props

| Name | Type | Default |
| :--------------- | :--------------------------------------------------------------- | :--------- |
| data | `string` | |
| level | `'L' \| 'M' \|'Q' \| 'H'` | `'L'` |
| shapes.body | `'square' \| 'circle' \| 'leaf' \| 'rounded'` | `'square'` |
| shapes.eyeball | `'square' \| 'circle' \| 'leaf' \| 'rounded'` | `'square'` |
| shapes.eyeframe | `'square' \| 'circle' \| 'leaf' \| 'rounded'` | `'square'` |
| gradient.type | `'linear' \| 'radial'` | |
| gradients.colors | `string[] \| {value: string, stop: number}` | |
| gradients.rotate | `number` (This property only exist if gradient.type is 'radial') | `45` |
| fillImage | `string` | |
| brand | HTML Img Attributes \| `Element` | |
| Name | Type | Default |
| :--------------- | :---------------------------------------------------------------------------------- | :--------- |
| data | `string` | |
| level | `'L' \| 'M' \|'Q' \| 'H'` | `'L'` |
| shapes.body | `'square' \| 'circle' \| 'leaf' \| 'rounded' \| 'diamond' \| 'triangle' \| 'heart'` | `'square'` |
| shapes.eyeball | `'square' \| 'circle' \| 'leaf' \| 'rounded'` | `'square'` |
| shapes.eyeframe | `'square' \| 'circle' \| 'leaf' \| 'rounded'` | `'square'` |
| gradient.type | `'linear' \| 'radial'` | |
| gradients.colors | `string[] \| {value: string, stop: number}` | |
| gradients.rotate | `number` (This property only exist if gradient.type is 'radial') | `45` |
| fillImage | `string` | |
| brand | HTML Img Attributes \| `Element` | |
2 changes: 1 addition & 1 deletion packages/vanilla/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@qr-x/vanilla",
"version": "1.1.1",
"version": "1.2.0",
"license": "MIT",
"description": "Create elegant QR codes",
"main": "dist/index.cjs.js",
Expand Down
24 changes: 12 additions & 12 deletions packages/vue/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ import QRX from '@qr-x/vue'

# Props

| Name | Type | Default |
| :--------------- | :--------------------------------------------------------------- | :--------- |
| data | `string` | |
| level | `'L' \| 'M' \|'Q' \| 'H'` | `'L'` |
| shapes.body | `'square' \| 'circle' \| 'leaf' \| 'rounded'` | `'square'` |
| shapes.eyeball | `'square' \| 'circle' \| 'leaf' \| 'rounded'` | `'square'` |
| shapes.eyeframe | `'square' \| 'circle' \| 'leaf' \| 'rounded'` | `'square'` |
| gradient.type | `'linear' \| 'radial'` | |
| gradients.colors | `string[] \| {value: string, stop: number}` | |
| gradients.rotate | `number` (This property only exist if gradient.type is 'radial') | `45` |
| fillImage | `string` | |
| brand | `ImgHTMLAttributes` | |
| Name | Type | Default |
| :--------------- | :---------------------------------------------------------------------------------- | :--------- |
| data | `string` | |
| level | `'L' \| 'M' \|'Q' \| 'H'` | `'L'` |
| shapes.body | `'square' \| 'circle' \| 'leaf' \| 'rounded' \| 'diamond' \| 'triangle' \| 'heart'` | `'square'` |
| shapes.eyeball | `'square' \| 'circle' \| 'leaf' \| 'rounded'` | `'square'` |
| shapes.eyeframe | `'square' \| 'circle' \| 'leaf' \| 'rounded'` | `'square'` |
| gradient.type | `'linear' \| 'radial'` | |
| gradients.colors | `string[] \| {value: string, stop: number}` | |
| gradients.rotate | `number` (This property only exist if gradient.type is 'radial') | `45` |
| fillImage | `string` | |
| brand | `ImgHTMLAttributes` | |
Loading
Loading