Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

corners

Package Overview
Dependencies
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

corners - npm Package Compare versions

Comparing version 0.0.0 to 0.0.1

dist/index.js

25

package.json
{
"name": "corners",
"version": "0.0.0",
"version": "0.0.1",
"private": false,
"main": "dist/index.js",
"types": "src/index.tsx",
"scripts": {
"serve": "nodemon ./example/dist/server.js --watch",
"serve": "nodemon ./example/dist/server.js",
"build:server": "node ./example/esbuild.config.server.js",
"build:client": "node ./example/esbuild.config.client.js",
"prebuild": "rm -rf example/dist && mkdir example/dist && cp example/src/index.html example/dist",
"build": "concurrently \"yarn build:server\" \"yarn build:client\"",
"dev": "concurrently \"yarn build\" \"yarn serve\"",
"prebuild:example": "rm -rf example/dist && mkdir example/dist && cp example/src/index.html example/dist",
"build:example": "concurrently \"yarn build:server\" \"yarn build:client\"",
"dev": "concurrently \"yarn build:example\" \"yarn serve\"",
"build": "node ./esbuild.config.js",
"pretest": "tsc --noEmit",

@@ -25,2 +28,3 @@ "test": "jest --watch --no-coverage",

"@banka/eslint-config": "^1.0.0",
"@emotion/react": "^11.7.1",
"@emotion/styled": "^11.6.0",

@@ -50,2 +54,4 @@ "@ls-lint/ls-lint": "^1.10.0",

"prettier": "^2.4.1",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"ts-node": "^10.2.1",

@@ -67,7 +73,8 @@ "tsconfig-paths": "^3.11.0",

"dependencies": {
"@emotion/react": "^11.7.1",
"@react-hook/resize-observer": "^1.2.5",
"react": "^17.0.2",
"react-dom": "^17.0.2"
"@react-hook/resize-observer": "^1.2.5"
},
"peerDependencies": {
"react": "<=17.0.0",
"react-dom": "<=17.0.0"
}
}

@@ -1,5 +0,4 @@

Thanks @rafaph/ts-app-template
Let's face it: `border-radius` is terrible.
## License
MIT

@@ -0,1 +1,3 @@

import { interpolate } from "./interpolate"
type SvgCommandCode = `C` | `L` | `M` | `Q` | `S`

@@ -7,3 +9,3 @@

interface Point2d {
type Point2d = {
x: number

@@ -13,9 +15,10 @@ y: number

const drawAngledCorner = (p1: Point2d, p2: Point2d, idx: number) =>
[
writePathPoint(p1.x, p1.y, idx === 0 ? `M` : `L`),
writePathPoint(p2.x, p2.y, `L`),
].join(`\n`) + `\n`
export type DrawCorner = (p1: Point2d, p2: Point2d, idx: number) => string[]
const drawRoundedCornerNaively = (p1: Point2d, p2: Point2d, idx: number) => {
export const chamfer: DrawCorner = (p1, p2, idx) => [
writePathPoint(p1.x, p1.y, idx === 0 ? `M` : `L`),
writePathPoint(p2.x, p2.y, `L`),
]
export const straight: DrawCorner = (p1, p2, idx) => {
const isEven = idx % 2 === 0

@@ -26,18 +29,20 @@ const ex = {

}
return (
[
writePathPoint(p1.x, p1.y, idx === 0 ? `M` : `L`),
writePathPoint(ex.x, ex.y, `C`),
writePathPoint(ex.x, ex.y),
writePathPoint(p2.x, p2.y),
].join(`\n`) + `\n`
)
return [writePathPoint(ex.x, ex.y, idx === 0 ? `M` : `L`)]
}
type Interpolate = (from: number, to: number, completionRatio: number) => number
export const roundedNaive: DrawCorner = (p1, p2, idx) => {
const isEven = idx % 2 === 0
const ex = {
x: isEven ? p2.x : p1.x,
y: isEven ? p1.y : p2.y,
}
return [
writePathPoint(p1.x, p1.y, idx === 0 ? `M` : `L`),
writePathPoint(ex.x, ex.y, `C`),
writePathPoint(ex.x, ex.y),
writePathPoint(p2.x, p2.y),
]
}
const interpolate: Interpolate = (from, to, completionRatio = 0.5) =>
from + completionRatio * (to - from)
const drawRoundedCorner = (p1: Point2d, p2: Point2d, idx: number): string => {
export const round: DrawCorner = (p1, p2, idx) => {
const isEven = idx % 2 === 0

@@ -64,29 +69,24 @@ const axis1 = isEven ? `x` : `y`

return (
[
writePathPoint(p1.x, p1.y, idx === 0 ? `M` : `L`),
writePathPoint(c0.x, c0.y, `C`),
writePathPoint(c1.x, c1.y),
writePathPoint(c2.x, c2.y),
writePathPoint(s0.x, s0.y, `S`),
writePathPoint(p2.x, p2.y),
].join(`\n`) + `\n`
)
return [
writePathPoint(p1.x, p1.y, idx === 0 ? `M` : `L`),
writePathPoint(c0.x, c0.y, `C`),
writePathPoint(c1.x, c1.y),
writePathPoint(c2.x, c2.y),
writePathPoint(s0.x, s0.y, `S`),
writePathPoint(p2.x, p2.y),
]
}
export const drawClipPath = (
// export type DrawCorners = cornerPoints
export const drawCorners = (
height: number,
width: number,
cornerSize: number,
cornerFunction: (
p1: Point2d,
p2: Point2d,
idx: number,
cornerSize: number
) => string
corners: [DrawCorner, DrawCorner, DrawCorner, DrawCorner]
): string => {
if (!height || !width || !cornerSize) return ``
const realCornerSize = Math.min(cornerSize, Math.min(height, width) / 2)
const cornerHeight = realCornerSize / height
const cornerWidth = realCornerSize / width
const maxCornerSize = Math.min(cornerSize, Math.min(height, width) / 2)
const cornerHeight = maxCornerSize / height
const cornerWidth = maxCornerSize / width

@@ -101,22 +101,67 @@ const cornerPoints = [

const path =
cornerPoints.reduce(
(acc, { p1, p2 }, idx) => acc + cornerFunction(p1, p2, idx, cornerSize),
``
) + `Z`
cornerPoints.reduce((acc, { p1, p2 }, idx) => {
const drawCorner = corners[idx]
return acc + drawCorner(p1, p2, idx).join(`\n`) + `\n`
}, ``) + `Z`
return path
}
export type DrawPath = (
type SvgPath = string
export type Pathfinder = (
height: number,
width: number,
cornerSize: number
) => string
cornerSize?: number
) => SvgPath
export const drawRoundedBoxNaively: DrawPath = (height, width, cornerRadius) =>
drawClipPath(height, width, cornerRadius, drawRoundedCornerNaively)
export type CreatePathfinder = (
defaultCornerSize: number,
...corners: (DrawCorner | null)[]
) => Pathfinder
export const drawChamferedBox: DrawPath = (height, width, cornerRadius) =>
drawClipPath(height, width, cornerRadius, drawAngledCorner)
export const drawRoundedBox: DrawPath = (height, width, cornerRadius) =>
drawClipPath(height, width, cornerRadius, drawRoundedCorner)
/* eslint-disable no-case-declarations */
export const createPathfinder: CreatePathfinder = (
defaultCornerSize,
...corners
) => {
switch (corners.length) {
case 0:
throw new Error(`No corners provided`)
case 1:
const corner = corners[0] ?? straight
return (height: number, width: number, cornerSize?: number) =>
drawCorners(height, width, cornerSize || defaultCornerSize, [
corner,
corner,
corner,
corner,
])
case 2:
const cornerX = corners[0] ?? straight
const cornerY = corners[1] ?? straight
return (height: number, width: number, cornerSize?: number) =>
drawCorners(height, width, cornerSize || defaultCornerSize, [
cornerX,
cornerY,
cornerX,
cornerY,
])
case 3:
throw new Error(`pass 1, 2, or 4 corners`)
case 4:
const cornerA = corners[0] ?? straight
const cornerB = corners[1] ?? straight
const cornerC = corners[2] ?? straight
const cornerD = corners[3] ?? straight
return (height: number, width: number, cornerSize?: number) =>
drawCorners(height, width, cornerSize || defaultCornerSize, [
cornerA,
cornerB,
cornerC,
cornerD,
])
default:
throw new Error(`pass 2 or 4 corners`)
}
}
/* eslint-enable no-case-declarations */

@@ -32,3 +32,10 @@ {

},
"include": ["src", "test", "example", "jest.config.js", "babel.config.js"]
"include": [
"src",
"test",
"example",
"esbuild.config.js",
"jest.config.js",
"babel.config.js"
]
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc