├── .github └── workflows │ └── gh-page.yml ├── .gitignore ├── LICENSE ├── LRUCache.d.ts ├── LRUCache.js ├── LRUCache.ts ├── README.md ├── example ├── .gitignore ├── README.md ├── deploy.js ├── example.png ├── package-lock.json ├── package.json ├── public │ ├── favicon.ico │ ├── index.html │ ├── ios.png │ ├── logo192.png │ ├── logo512.png │ ├── manifest.json │ └── robots.txt ├── src │ ├── App.css │ ├── App.test.tsx │ ├── App.tsx │ ├── index.css │ ├── index.tsx │ ├── logo.svg │ ├── react-app-env.d.ts │ ├── serviceWorker.ts │ └── setupTests.ts ├── tsconfig.json └── yarn.lock ├── index.d.ts ├── index.js ├── index.tsx ├── package-lock.json ├── package.json └── tsconfig.json /.github/workflows/gh-page.yml: -------------------------------------------------------------------------------- 1 | # This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions 3 | 4 | name: gh-page 5 | 6 | on: 7 | push: 8 | branches: [ master ] 9 | pull_request: 10 | branches: [ master ] 11 | 12 | jobs: 13 | build: 14 | 15 | runs-on: ubuntu-latest 16 | 17 | strategy: 18 | matrix: 19 | node-version: [12.x] 20 | 21 | steps: 22 | - uses: actions/checkout@v2 23 | - name: Use Node.js ${{ matrix.node-version }} 24 | uses: actions/setup-node@v1 25 | with: 26 | node-version: ${{ matrix.node-version }} 27 | - run: npm i 28 | - name: git setup 29 | run: | 30 | git config --global user.name 'QuadFlask' 31 | git config --global user.email 'pop9310@gmail.com' 32 | - name: deploy gh-page 33 | working-directory: ./example 34 | run: | 35 | npm i 36 | npm run deploy 37 | env: 38 | CI: true 39 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | .idea 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 QuadFlask 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LRUCache.d.ts: -------------------------------------------------------------------------------- 1 | export default class LRUCache { 2 | max: number; 3 | cache: Map; 4 | constructor(max?: number); 5 | get(key: string): T | undefined; 6 | set(key: string, val: T): T; 7 | _first(): any; 8 | } 9 | -------------------------------------------------------------------------------- /LRUCache.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | Object.defineProperty(exports, "__esModule", { value: true }); 3 | var LRUCache = /** @class */ (function () { 4 | function LRUCache(max) { 5 | if (max === void 0) { max = 10; } 6 | this.max = 10; 7 | this.cache = new Map(); 8 | this.max = max; 9 | } 10 | LRUCache.prototype.get = function (key) { 11 | var item = this.cache.get(key); 12 | if (item) { 13 | this.cache.delete(key); 14 | this.cache.set(key, item); 15 | } 16 | return item; 17 | }; 18 | LRUCache.prototype.set = function (key, val) { 19 | if (this.cache.has(key)) 20 | this.cache.delete(key); 21 | else if (this.cache.size == this.max) 22 | this.cache.delete(this._first()); 23 | this.cache.set(key, val); 24 | return val; 25 | }; 26 | LRUCache.prototype._first = function () { 27 | return this.cache.keys().next().value; 28 | }; 29 | return LRUCache; 30 | }()); 31 | exports.default = LRUCache; 32 | -------------------------------------------------------------------------------- /LRUCache.ts: -------------------------------------------------------------------------------- 1 | export default class LRUCache { 2 | max = 10; 3 | cache: Map = new Map(); 4 | 5 | constructor(max = 10) { 6 | this.max = max; 7 | } 8 | 9 | get(key: string) { 10 | const item = this.cache.get(key); 11 | if (item) { 12 | this.cache.delete(key); 13 | this.cache.set(key, item); 14 | } 15 | return item; 16 | } 17 | 18 | set(key: string, val: T): T { 19 | if (this.cache.has(key)) 20 | this.cache.delete(key); 21 | else if (this.cache.size == this.max) 22 | this.cache.delete(this._first()); 23 | this.cache.set(key, val); 24 | return val; 25 | } 26 | 27 | _first() { 28 | return this.cache.keys().next().value; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | react-superellipse [![npm version](https://badge.fury.io/js/react-superellipse.svg)](https://badge.fury.io/js/react-superellipse) 2 | ----- 3 | 4 | [example](https://quadflask.github.io/react-superellipse/) 5 | 6 | ![](https://raw.githubusercontent.com/QuadFlask/react-superellipse/master/example/example.png) 7 | 8 | 9 | ## What is this? 10 | 11 | React component for create [Superellipse](https://en.wikipedia.org/wiki/Superellipse) (or [Squircle](https://en.wikipedia.org/wiki/Squircle)) mask using svg and css `mask-image` attribute. 12 | 13 | iOS app icon is Squircle and KakaoTalk profile is Superellipse shape. 14 | 15 | 16 | ## example 17 | 18 | ```tsx 19 | import SuperEllipse from "react-superellipse"; 20 | 21 | 22 | {/* children */} 23 | 24 | 25 | 26 | {/* children */} 27 | 28 | 29 | 30 | // or using preset 31 | 32 | import {Preset} from "react-superellipse"; 33 | // preset for iOS squircle or Kakaotalk superellipse 34 | 35 | 36 | {/* children */} 37 | 38 | ``` 39 | 40 | 41 | ## component 42 | 43 | ### `SuperEllipse` 44 | 45 | > Wrapper div component 46 | 47 | ```ts 48 | interface SuperEllipseProps { 49 | style?: CSSProperties; 50 | r1?: number; // [0 ~ 0.5] width ratio 51 | r2?: number; // [0 ~ 0.5] width ratio 52 | p1?: number; // [0 ~ width/2] in pixel 53 | p2?: number; // [0 ~ width/2] in pixel 54 | } 55 | ``` 56 | > r1,r2 or p1,p2 57 | 58 | ### `SuperEllipseImg` 59 | 60 | ```ts 61 | export interface SuperEllipseImgProps { 62 | width: number; 63 | height: number; 64 | href: string; 65 | style?: CSSProperties; 66 | r1?: number; 67 | r2?: number; 68 | backgroundColor?: string; 69 | strokeColor?: string; 70 | strokeWidth?: number; 71 | } 72 | ``` 73 | -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. 2 | 3 | # dependencies 4 | /node_modules 5 | /.pnp 6 | .pnp.js 7 | 8 | # testing 9 | /coverage 10 | 11 | # production 12 | /build 13 | 14 | # misc 15 | .DS_Store 16 | .env.local 17 | .env.development.local 18 | .env.test.local 19 | .env.production.local 20 | 21 | npm-debug.log* 22 | yarn-debug.log* 23 | yarn-error.log* 24 | -------------------------------------------------------------------------------- /example/README.md: -------------------------------------------------------------------------------- 1 | This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). 2 | 3 | ## Available Scripts 4 | 5 | In the project directory, you can run: 6 | 7 | ### `yarn start` 8 | 9 | Runs the app in the development mode.
10 | Open [http://localhost:3000](http://localhost:3000) to view it in the browser. 11 | 12 | The page will reload if you make edits.
13 | You will also see any lint errors in the console. 14 | 15 | ### `yarn test` 16 | 17 | Launches the test runner in the interactive watch mode.
18 | See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. 19 | 20 | ### `yarn build` 21 | 22 | Builds the app for production to the `build` folder.
23 | It correctly bundles React in production mode and optimizes the build for the best performance. 24 | 25 | The build is minified and the filenames include the hashes.
26 | Your app is ready to be deployed! 27 | 28 | See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. 29 | 30 | ### `yarn eject` 31 | 32 | **Note: this is a one-way operation. Once you `eject`, you can’t go back!** 33 | 34 | If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. 35 | 36 | Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. 37 | 38 | You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. 39 | 40 | ## Learn More 41 | 42 | You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). 43 | 44 | To learn React, check out the [React documentation](https://reactjs.org/). 45 | -------------------------------------------------------------------------------- /example/deploy.js: -------------------------------------------------------------------------------- 1 | var ghpages = require('gh-pages'); 2 | 3 | ghpages.publish('build', function (err) { 4 | console.error(err); 5 | }); 6 | -------------------------------------------------------------------------------- /example/example.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/QuadFlask/react-superellipse/5633888b046bed0ef688f88978fb844de9be465a/example/example.png -------------------------------------------------------------------------------- /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "example", 3 | "version": "0.1.0", 4 | "private": true, 5 | "homepage": ".", 6 | "dependencies": { 7 | "@testing-library/jest-dom": "^5.3.0", 8 | "@testing-library/react": "^10.0.2", 9 | "@testing-library/user-event": "^10.0.1", 10 | "@types/jest": "^25.2.1", 11 | "@types/node": "^13.11.0", 12 | "@types/react": "^16.9.32", 13 | "@types/react-dom": "^16.9.6", 14 | "react": "^16.13.1", 15 | "react-dom": "^16.13.1", 16 | "react-scripts": "3.4.1", 17 | "react-superellipse": "file:../", 18 | "typescript": "~3.8.3" 19 | }, 20 | "scripts": { 21 | "start": "react-scripts start", 22 | "build": "react-scripts build", 23 | "test": "react-scripts test", 24 | "eject": "react-scripts eject", 25 | "deploy": "npm run build; node deploy.js" 26 | }, 27 | "eslintConfig": { 28 | "extends": "react-app" 29 | }, 30 | "browserslist": { 31 | "production": [ 32 | ">0.2%", 33 | "not dead", 34 | "not op_mini all" 35 | ], 36 | "development": [ 37 | "last 1 chrome version", 38 | "last 1 firefox version", 39 | "last 1 safari version" 40 | ] 41 | }, 42 | "devDependencies": { 43 | "gh-pages": "^2.2.0" 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /example/public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/QuadFlask/react-superellipse/5633888b046bed0ef688f88978fb844de9be465a/example/public/favicon.ico -------------------------------------------------------------------------------- /example/public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | React App 28 | 29 | 30 | 31 |
32 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /example/public/ios.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/QuadFlask/react-superellipse/5633888b046bed0ef688f88978fb844de9be465a/example/public/ios.png -------------------------------------------------------------------------------- /example/public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/QuadFlask/react-superellipse/5633888b046bed0ef688f88978fb844de9be465a/example/public/logo192.png -------------------------------------------------------------------------------- /example/public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/QuadFlask/react-superellipse/5633888b046bed0ef688f88978fb844de9be465a/example/public/logo512.png -------------------------------------------------------------------------------- /example/public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "React App", 3 | "name": "Create React App Sample", 4 | "icons": [ 5 | { 6 | "src": "favicon.ico", 7 | "sizes": "64x64 32x32 24x24 16x16", 8 | "type": "image/x-icon" 9 | }, 10 | { 11 | "src": "logo192.png", 12 | "type": "image/png", 13 | "sizes": "192x192" 14 | }, 15 | { 16 | "src": "logo512.png", 17 | "type": "image/png", 18 | "sizes": "512x512" 19 | } 20 | ], 21 | "start_url": ".", 22 | "display": "standalone", 23 | "theme_color": "#000000", 24 | "background_color": "#ffffff" 25 | } 26 | -------------------------------------------------------------------------------- /example/public/robots.txt: -------------------------------------------------------------------------------- 1 | # https://www.robotstxt.org/robotstxt.html 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /example/src/App.css: -------------------------------------------------------------------------------- 1 | .App { 2 | text-align: center; 3 | /*background-color: #282c34;*/ 4 | /*color: #61dafb;*/ 5 | } 6 | 7 | .super-ellipse { 8 | position: relative; 9 | margin: 12px; 10 | } 11 | 12 | .preset-name { 13 | position: absolute; 14 | display: inline-flex; 15 | left: 0; 16 | width: 100%; 17 | height: 100%; 18 | align-items: center; 19 | justify-content: center; 20 | color: white; 21 | font-size: 50px; 22 | font-weight: bold; 23 | flex-direction: column; 24 | } 25 | 26 | .preset-name > small { 27 | display: block; 28 | font-size: 0.5em; 29 | } 30 | -------------------------------------------------------------------------------- /example/src/App.test.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { render } from '@testing-library/react'; 3 | import App from './App'; 4 | 5 | test('renders learn react link', () => { 6 | const { getByText } = render(); 7 | const linkElement = getByText(/learn react/i); 8 | expect(linkElement).toBeInTheDocument(); 9 | }); 10 | -------------------------------------------------------------------------------- /example/src/App.tsx: -------------------------------------------------------------------------------- 1 | import React, {FC, useState} from 'react'; 2 | import './App.css'; 3 | import SuperEllipse, {Preset, SuperEllipseImg} from "react-superellipse"; 4 | 5 | const size = 512; 6 | const imgSrc = "https://images.unsplash.com/photo-1585572336833-4f34f2101c18?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=400&q=60"; 7 | 8 | function App() { 9 | const [r1, setR1] = useState(Preset.KakaoTalk.r1); 10 | const [r2, setR2] = useState(Preset.KakaoTalk.r2); 11 | const p1 = 4; 12 | const p2 = 24; 13 | 14 | return
15 |
16 |
26 | 27 | 28 | round rect 29 | border-radius: 120px 30 | 31 |
32 | 33 | 34 | 35 | iOS 36 | r1: {Preset.iOS.r1}, r2: {Preset.iOS.r2} 37 | 38 | 39 | 40 | 41 | 42 | 카카오톡 43 | r1: {Preset.KakaoTalk.r1}, r2: {Preset.KakaoTalk.r2} 44 | 45 | 46 |
47 |
48 | 49 | 50 | r1: {r1}
r2: {r2}
51 |
52 |
53 |
r1: setR1(parseFloat(e.target.value))} style={{width: size}}/>
54 |
r2: setR2(parseFloat(e.target.value))} style={{width: size}}/>
55 |
56 |
57 | 58 |
59 | 60 |
61 | 62 | 63 | p1: {p1}
p2: {p2}
64 |
65 | 66 | 67 | p1: {p1}
p2: {p2}
68 |
69 | 70 | 71 | p1: {p1}
p2: {p2}
72 |
73 | 74 | 75 | p1: {p1}
p2: {p2}
76 |
77 |
78 | 79 |
80 | 81 |
82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 |
92 |
93 | 94 |
95 |
96 |

103 | iOS icon squircle(equation: |x/60|^5+|y/60|^5=1)
104 | react-superellipse Preset.iOS
105 | rounded rect(120px)
106 |

107 | { 115 | if (!ref) return; 116 | 117 | let ctx = ref.getContext('2d')!; 118 | ctx.clearRect(0, 0, ref.width, ref.height); 119 | ctx.strokeStyle = 'rgba(255,0,0,0.5)'; 120 | ctx.lineWidth = 2; 121 | ctx.beginPath(); 122 | for (let t = 0; t < Math.PI * 2; t += 0.01) { 123 | let {x, y} = squirclePolar(t); 124 | x *= 8.52; 125 | y *= 8.52; 126 | x += size; 127 | y += size; 128 | ctx.lineTo(x * 2, y * 2); 129 | } 130 | ctx.closePath(); 131 | ctx.stroke(); 132 | }}/> 133 | 141 |
153 |
154 |
155 |
156 |
157 | } 158 | 159 | function AppIcon(props: { x: number; y: number; }) { 160 | return
161 | 162 |

월요일

167 |

6

172 |
173 |

캘린더

180 |
181 | } 182 | 183 | function RoundAppIcon(props: { x: number; y: number; }) { 184 | return
185 |
186 |

월요일

191 |

6

196 |
197 |

캘린더

204 |
205 | } 206 | 207 | function squircleCartesian(x: number, y: number): boolean { 208 | return Math.pow(Math.abs(x / 60), 5) + Math.pow(Math.abs(y / 60), 5) < 512 * 60 * 1.5; 209 | } 210 | 211 | function squirclePolar(t: number): { x: number, y: number } { 212 | return { 213 | x: Math.pow(Math.abs(Math.cos(t)), 2 / 5) * 60 * sgn(Math.cos(t)), 214 | y: Math.pow(Math.abs(Math.sin(t)), 2 / 5) * 60 * sgn(Math.sin(t)), 215 | } 216 | } 217 | 218 | function sgn(n: number) { 219 | return n > 0 ? 1 : -1; 220 | } 221 | 222 | const PresetName: FC<{ sm?: boolean }> = ({children, sm = false}) => {children} 223 | 224 | export default App; 225 | -------------------------------------------------------------------------------- /example/src/index.css: -------------------------------------------------------------------------------- 1 | body { 2 | margin: 0; 3 | font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 4 | 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 5 | sans-serif; 6 | -webkit-font-smoothing: antialiased; 7 | -moz-osx-font-smoothing: grayscale; 8 | } 9 | 10 | code { 11 | font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', 12 | monospace; 13 | } 14 | -------------------------------------------------------------------------------- /example/src/index.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import ReactDOM from 'react-dom'; 3 | import './index.css'; 4 | import App from './App'; 5 | import * as serviceWorker from './serviceWorker'; 6 | 7 | ReactDOM.render( 8 | 9 | 10 | , 11 | document.getElementById('root') 12 | ); 13 | 14 | // If you want your app to work offline and load faster, you can change 15 | // unregister() to register() below. Note this comes with some pitfalls. 16 | // Learn more about service workers: https://bit.ly/CRA-PWA 17 | serviceWorker.unregister(); 18 | -------------------------------------------------------------------------------- /example/src/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /example/src/react-app-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /example/src/serviceWorker.ts: -------------------------------------------------------------------------------- 1 | // This optional code is used to register a service worker. 2 | // register() is not called by default. 3 | 4 | // This lets the app load faster on subsequent visits in production, and gives 5 | // it offline capabilities. However, it also means that developers (and users) 6 | // will only see deployed updates on subsequent visits to a page, after all the 7 | // existing tabs open on the page have been closed, since previously cached 8 | // resources are updated in the background. 9 | 10 | // To learn more about the benefits of this model and instructions on how to 11 | // opt-in, read https://bit.ly/CRA-PWA 12 | 13 | const isLocalhost = Boolean( 14 | window.location.hostname === 'localhost' || 15 | // [::1] is the IPv6 localhost address. 16 | window.location.hostname === '[::1]' || 17 | // 127.0.0.0/8 are considered localhost for IPv4. 18 | window.location.hostname.match( 19 | /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ 20 | ) 21 | ); 22 | 23 | type Config = { 24 | onSuccess?: (registration: ServiceWorkerRegistration) => void; 25 | onUpdate?: (registration: ServiceWorkerRegistration) => void; 26 | }; 27 | 28 | export function register(config?: Config) { 29 | if (process.env.NODE_ENV === 'production' && 'serviceWorker' in navigator) { 30 | // The URL constructor is available in all browsers that support SW. 31 | const publicUrl = new URL( 32 | process.env.PUBLIC_URL, 33 | window.location.href 34 | ); 35 | if (publicUrl.origin !== window.location.origin) { 36 | // Our service worker won't work if PUBLIC_URL is on a different origin 37 | // from what our page is served on. This might happen if a CDN is used to 38 | // serve assets; see https://github.com/facebook/create-react-app/issues/2374 39 | return; 40 | } 41 | 42 | window.addEventListener('load', () => { 43 | const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; 44 | 45 | if (isLocalhost) { 46 | // This is running on localhost. Let's check if a service worker still exists or not. 47 | checkValidServiceWorker(swUrl, config); 48 | 49 | // Add some additional logging to localhost, pointing developers to the 50 | // service worker/PWA documentation. 51 | navigator.serviceWorker.ready.then(() => { 52 | console.log( 53 | 'This web app is being served cache-first by a service ' + 54 | 'worker. To learn more, visit https://bit.ly/CRA-PWA' 55 | ); 56 | }); 57 | } else { 58 | // Is not localhost. Just register service worker 59 | registerValidSW(swUrl, config); 60 | } 61 | }); 62 | } 63 | } 64 | 65 | function registerValidSW(swUrl: string, config?: Config) { 66 | navigator.serviceWorker 67 | .register(swUrl) 68 | .then(registration => { 69 | registration.onupdatefound = () => { 70 | const installingWorker = registration.installing; 71 | if (installingWorker == null) { 72 | return; 73 | } 74 | installingWorker.onstatechange = () => { 75 | if (installingWorker.state === 'installed') { 76 | if (navigator.serviceWorker.controller) { 77 | // At this point, the updated precached content has been fetched, 78 | // but the previous service worker will still serve the older 79 | // content until all client tabs are closed. 80 | console.log( 81 | 'New content is available and will be used when all ' + 82 | 'tabs for this page are closed. See https://bit.ly/CRA-PWA.' 83 | ); 84 | 85 | // Execute callback 86 | if (config && config.onUpdate) { 87 | config.onUpdate(registration); 88 | } 89 | } else { 90 | // At this point, everything has been precached. 91 | // It's the perfect time to display a 92 | // "Content is cached for offline use." message. 93 | console.log('Content is cached for offline use.'); 94 | 95 | // Execute callback 96 | if (config && config.onSuccess) { 97 | config.onSuccess(registration); 98 | } 99 | } 100 | } 101 | }; 102 | }; 103 | }) 104 | .catch(error => { 105 | console.error('Error during service worker registration:', error); 106 | }); 107 | } 108 | 109 | function checkValidServiceWorker(swUrl: string, config?: Config) { 110 | // Check if the service worker can be found. If it can't reload the page. 111 | fetch(swUrl, { 112 | headers: { 'Service-Worker': 'script' } 113 | }) 114 | .then(response => { 115 | // Ensure service worker exists, and that we really are getting a JS file. 116 | const contentType = response.headers.get('content-type'); 117 | if ( 118 | response.status === 404 || 119 | (contentType != null && contentType.indexOf('javascript') === -1) 120 | ) { 121 | // No service worker found. Probably a different app. Reload the page. 122 | navigator.serviceWorker.ready.then(registration => { 123 | registration.unregister().then(() => { 124 | window.location.reload(); 125 | }); 126 | }); 127 | } else { 128 | // Service worker found. Proceed as normal. 129 | registerValidSW(swUrl, config); 130 | } 131 | }) 132 | .catch(() => { 133 | console.log( 134 | 'No internet connection found. App is running in offline mode.' 135 | ); 136 | }); 137 | } 138 | 139 | export function unregister() { 140 | if ('serviceWorker' in navigator) { 141 | navigator.serviceWorker.ready 142 | .then(registration => { 143 | registration.unregister(); 144 | }) 145 | .catch(error => { 146 | console.error(error.message); 147 | }); 148 | } 149 | } 150 | -------------------------------------------------------------------------------- /example/src/setupTests.ts: -------------------------------------------------------------------------------- 1 | // jest-dom adds custom jest matchers for asserting on DOM nodes. 2 | // allows you to do things like: 3 | // expect(element).toHaveTextContent(/react/i) 4 | // learn more: https://github.com/testing-library/jest-dom 5 | import '@testing-library/jest-dom/extend-expect'; 6 | -------------------------------------------------------------------------------- /example/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": [ 5 | "dom", 6 | "dom.iterable", 7 | "esnext" 8 | ], 9 | "allowJs": true, 10 | "skipLibCheck": true, 11 | "esModuleInterop": true, 12 | "allowSyntheticDefaultImports": true, 13 | "strict": true, 14 | "forceConsistentCasingInFileNames": true, 15 | "module": "esnext", 16 | "moduleResolution": "node", 17 | "resolveJsonModule": true, 18 | "isolatedModules": true, 19 | "noEmit": true, 20 | "jsx": "react" 21 | }, 22 | "include": [ 23 | "src" 24 | ] 25 | } 26 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | import React, { CSSProperties } from "react"; 2 | import { Preset } from "superellipsejs"; 3 | export interface SuperEllipseProps { 4 | r1?: number; 5 | r2?: number; 6 | p1?: number; 7 | p2?: number; 8 | } 9 | interface Bounds { 10 | readonly width: number; 11 | readonly height: number; 12 | } 13 | declare function SuperEllipse(props: SuperEllipseProps & React.HTMLAttributes): JSX.Element; 14 | export declare function getMaskStyle(bounds: Bounds, props: SuperEllipseProps): CSSProperties; 15 | export interface SuperEllipseImgProps { 16 | width: number; 17 | height: number; 18 | href: string; 19 | style?: CSSProperties; 20 | r1?: number; 21 | r2?: number; 22 | backgroundColor?: string; 23 | strokeColor?: string; 24 | strokeWidth?: number; 25 | } 26 | export declare const SuperEllipseImg: (props: SuperEllipseImgProps) => JSX.Element; 27 | export { Preset }; 28 | export default SuperEllipse; 29 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var __assign = (this && this.__assign) || function () { 3 | __assign = Object.assign || function(t) { 4 | for (var s, i = 1, n = arguments.length; i < n; i++) { 5 | s = arguments[i]; 6 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) 7 | t[p] = s[p]; 8 | } 9 | return t; 10 | }; 11 | return __assign.apply(this, arguments); 12 | }; 13 | var __importDefault = (this && this.__importDefault) || function (mod) { 14 | return (mod && mod.__esModule) ? mod : { "default": mod }; 15 | }; 16 | Object.defineProperty(exports, "__esModule", { value: true }); 17 | var react_1 = __importDefault(require("react")); 18 | var superellipsejs_1 = require("superellipsejs"); 19 | exports.Preset = superellipsejs_1.Preset; 20 | var react_use_measure_1 = __importDefault(require("react-use-measure")); 21 | function SuperEllipse(props) { 22 | var _a = react_use_measure_1.default(), ref = _a[0], bounds = _a[1]; 23 | return react_1.default.createElement("div", __assign({}, props, { ref: ref, style: __assign(__assign({}, props.style), getMaskStyle(bounds, props)) }), props.children); 24 | } 25 | function getMaskStyle(bounds, props) { 26 | var w = bounds.width; 27 | var h = bounds.height; 28 | var _a = props.r1, r1 = _a === void 0 ? superellipsejs_1.Preset.iOS.r1 : _a, _b = props.r2, r2 = _b === void 0 ? superellipsejs_1.Preset.iOS.r2 : _b, p1 = props.p1, p2 = props.p2; 29 | var dataUri = superellipsejs_1.getSuperEllipsePathAsDataUri(w, h, p1 !== undefined ? p1 : r1 * Math.min(w, h), p2 !== undefined ? p2 : r2 * Math.min(w, h)).dataUri; 30 | return { 31 | maskImage: "url(\"" + dataUri + "\")", 32 | maskPosition: 'center', 33 | maskRepeat: 'no-repeat', 34 | // maskSize: 'contain', 35 | WebkitMaskImage: "url(\"" + dataUri + "\")", 36 | WebkitMaskPosition: 'center', 37 | WebkitMaskRepeat: 'no-repeat', 38 | }; 39 | } 40 | exports.getMaskStyle = getMaskStyle; 41 | exports.SuperEllipseImg = function (props) { 42 | var w = props.width; 43 | var h = props.height; 44 | var _a = props.r1, r1 = _a === void 0 ? superellipsejs_1.Preset.iOS.r1 : _a, _b = props.r2, r2 = _b === void 0 ? superellipsejs_1.Preset.iOS.r2 : _b; 45 | var _c = props.strokeWidth, strokeWidth = _c === void 0 ? 0 : _c, _d = props.strokeColor, strokeColor = _d === void 0 ? 'rgba(255,255,255,0.5)' : _d, backgroundColor = props.backgroundColor; 46 | var path = superellipsejs_1.calcSuperEllipsePath(w, h, r1 * Math.min(w, h), r2 * Math.min(w, h)); 47 | var id = "super-ellipse-" + w + "-" + h + "-" + r1 + "-" + r2; 48 | return react_1.default.createElement("svg", { width: w, height: h, viewBox: "0 0 " + w + " " + h, style: props.style }, 49 | react_1.default.createElement("defs", null, 50 | react_1.default.createElement("clipPath", { id: id }, 51 | react_1.default.createElement("path", { fill: "#000", stroke: "none", d: path }))), 52 | react_1.default.createElement("g", { clipPath: "url(#" + id + ")" }, 53 | backgroundColor && 54 | react_1.default.createElement("rect", { width: w, height: h, fill: backgroundColor }), 55 | react_1.default.createElement("image", { href: props.href, width: w, height: h, preserveAspectRatio: "none" }), 56 | strokeWidth > 0 && 57 | react_1.default.createElement("path", { stroke: strokeColor, strokeWidth: strokeWidth * 2, fill: "none", d: path }))); 58 | }; 59 | exports.default = SuperEllipse; 60 | -------------------------------------------------------------------------------- /index.tsx: -------------------------------------------------------------------------------- 1 | import React, {CSSProperties} from "react"; 2 | import {calcSuperEllipsePath, getSuperEllipsePathAsDataUri, Preset} from "superellipsejs"; 3 | import useMeasure from 'react-use-measure'; 4 | 5 | export interface SuperEllipseProps { 6 | r1?: number; 7 | r2?: number; 8 | p1?: number; 9 | p2?: number; 10 | } 11 | 12 | interface Bounds { 13 | readonly width: number; 14 | readonly height: number; 15 | } 16 | 17 | function SuperEllipse(props: SuperEllipseProps & React.HTMLAttributes) { 18 | const [ref, bounds] = useMeasure(); 19 | 20 | return
{props.children}
; 24 | } 25 | 26 | export function getMaskStyle(bounds: Bounds, props: SuperEllipseProps): CSSProperties { 27 | const w = bounds.width; 28 | const h = bounds.height; 29 | const {r1 = Preset.iOS.r1, r2 = Preset.iOS.r2, p1, p2} = props; 30 | const {dataUri} = getSuperEllipsePathAsDataUri(w, h, p1 !== undefined ? p1 : r1 * Math.min(w, h), p2 !== undefined ? p2 : r2 * Math.min(w, h)); 31 | return { 32 | maskImage: `url("${dataUri}")`, 33 | maskPosition: 'center', 34 | maskRepeat: 'no-repeat', 35 | // maskSize: 'contain', 36 | WebkitMaskImage: `url("${dataUri}")`, 37 | WebkitMaskPosition: 'center', 38 | WebkitMaskRepeat: 'no-repeat', 39 | // WebkitMaskSize: 'contain' 40 | }; 41 | } 42 | 43 | export interface SuperEllipseImgProps { 44 | width: number; 45 | height: number; 46 | href: string; 47 | style?: CSSProperties; 48 | r1?: number; 49 | r2?: number; 50 | backgroundColor?: string; 51 | strokeColor?: string; 52 | strokeWidth?: number; 53 | } 54 | 55 | export const SuperEllipseImg = (props: SuperEllipseImgProps) => { 56 | const w = props.width; 57 | const h = props.height; 58 | const {r1 = Preset.iOS.r1, r2 = Preset.iOS.r2} = props; 59 | const {strokeWidth = 0, strokeColor = 'rgba(255,255,255,0.5)', backgroundColor} = props; 60 | const path = calcSuperEllipsePath(w, h, r1 * Math.min(w, h), r2 * Math.min(w, h)); 61 | const id = `super-ellipse-${w}-${h}-${r1}-${r2}`; 62 | 63 | return 64 | 65 | 66 | 67 | 68 | 69 | 70 | { 71 | backgroundColor && 72 | 73 | } 74 | 75 | { 76 | strokeWidth > 0 && 77 | 78 | } 79 | 80 | 81 | } 82 | 83 | export {Preset} 84 | 85 | export default SuperEllipse; 86 | -------------------------------------------------------------------------------- /package-lock.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-superellipse", 3 | "version": "0.0.6", 4 | "lockfileVersion": 1, 5 | "requires": true, 6 | "dependencies": { 7 | "@types/prop-types": { 8 | "version": "15.7.3", 9 | "resolved": "https://registry.npmjs.org/@types/prop-types/-/prop-types-15.7.3.tgz", 10 | "integrity": "sha512-KfRL3PuHmqQLOG+2tGpRO26Ctg+Cq1E01D2DMriKEATHgWLfeNDmq9e29Q9WIky0dQ3NPkd1mzYH8Lm936Z9qw==", 11 | "dev": true 12 | }, 13 | "@types/react": { 14 | "version": "16.9.31", 15 | "resolved": "https://registry.npmjs.org/@types/react/-/react-16.9.31.tgz", 16 | "integrity": "sha512-NpYJpNMWScFXtx3A2BJMeew2G3+9SEslVWMdxNJ6DLvxIuxWjY1bizK9q5Y1ujhln31vtjmhjOAYDr9Xx3k9FQ==", 17 | "dev": true, 18 | "requires": { 19 | "@types/prop-types": "*", 20 | "csstype": "^2.2.0" 21 | } 22 | }, 23 | "csstype": { 24 | "version": "2.6.10", 25 | "resolved": "https://registry.npmjs.org/csstype/-/csstype-2.6.10.tgz", 26 | "integrity": "sha512-D34BqZU4cIlMCY93rZHbrq9pjTAQJ3U8S8rfBqjwHxkGPThWFjzZDQpgMJY0QViLxth6ZKYiwFBo14RdN44U/w==", 27 | "dev": true 28 | }, 29 | "debounce": { 30 | "version": "1.2.0", 31 | "resolved": "https://registry.npmjs.org/debounce/-/debounce-1.2.0.tgz", 32 | "integrity": "sha512-mYtLl1xfZLi1m4RtQYlZgJUNQjl4ZxVnHzIR8nLLgi4q1YT8o/WM+MK/f8yfcc9s5Ir5zRaPZyZU6xs1Syoocg==" 33 | }, 34 | "react-use-measure": { 35 | "version": "2.0.0", 36 | "resolved": "https://registry.npmjs.org/react-use-measure/-/react-use-measure-2.0.0.tgz", 37 | "integrity": "sha512-aYosukh38+eqjgNExd/OKEM0n5YSbl5niYYq3OAUWywf3deUjpnVfGMnwTmjF32+Owqr1sc3KKZku9n72lAaPA==", 38 | "requires": { 39 | "debounce": "^1.2.0" 40 | } 41 | }, 42 | "resize-observer-polyfill": { 43 | "version": "1.5.1", 44 | "resolved": "https://registry.npmjs.org/resize-observer-polyfill/-/resize-observer-polyfill-1.5.1.tgz", 45 | "integrity": "sha512-LwZrotdHOo12nQuZlHEmtuXdqGoOD0OhaxopaNFxWzInpEgaLWoVuAMbTzixuosCx2nEG58ngzW3vxdWoxIgdg==" 46 | }, 47 | "superellipsejs": { 48 | "version": "0.0.6", 49 | "resolved": "https://registry.npmjs.org/superellipsejs/-/superellipsejs-0.0.6.tgz", 50 | "integrity": "sha512-l6v/YtKBTt+3oM0ZrqgO+omkDvwJuKu+6emMzjfolcBcjfi+68T8HEzsca13wt2pYmr3wBoL5qR627j5WLD+ig==" 51 | }, 52 | "typescript": { 53 | "version": "3.8.3", 54 | "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.8.3.tgz", 55 | "integrity": "sha512-MYlEfn5VrLNsgudQTVJeNaQFUAI7DkhnOjdpAp4T+ku1TfQClewlbSuTVHiA+8skNBgaf02TL/kLOvig4y3G8w==", 56 | "dev": true 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-superellipse", 3 | "version": "0.0.6", 4 | "description": "superellipse react component", 5 | "author": "QuadFlask", 6 | "license": "MIT", 7 | "main": "index.js", 8 | "homepage": "https://github.com/QuadFlask/react-superellipse", 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/QuadFlask/react-superellipse" 12 | }, 13 | "keywords": [ 14 | "react", 15 | "svg", 16 | "squircle", 17 | "superellipse", 18 | "round", 19 | "roundrect", 20 | "border-radius", 21 | "mask-image", 22 | "mask" 23 | ], 24 | "scripts": { 25 | "build": "tsc -p ./tsconfig.json", 26 | "deploy": "npm version patch; npm run build; npm publish" 27 | }, 28 | "dependencies": { 29 | "superellipsejs": "^0.0.6", 30 | "react-use-measure": "^2.0.0", 31 | "resize-observer-polyfill": "^1.5.1" 32 | }, 33 | "devDependencies": { 34 | "@types/react": "^16.9.31", 35 | "typescript": "^3.8.3" 36 | }, 37 | "peerDependencies": { 38 | "react": ">=16.11", 39 | "react-dom": ">=16.11" 40 | }, 41 | "files": [ 42 | "index.tsx", 43 | "index.js", 44 | "index.d.ts", 45 | "LRUCache.ts", 46 | "LRUCache.js", 47 | "LRUCache.d.ts" 48 | ] 49 | } 50 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | /* Basic Options */ 4 | "target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */ 5 | "module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */ 6 | "lib": ["esnext"], /* Specify library files to be included in the compilation. */ 7 | // "allowJs": true, /* Allow javascript files to be compiled. */ 8 | // "checkJs": true, /* Report errors in .js files. */ 9 | "jsx": "react", /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */ 10 | "declaration": true, /* Generates corresponding '.d.ts' file. */ 11 | // "declarationMap": true, /* Generates a sourcemap for each corresponding '.d.ts' file. */ 12 | // "sourceMap": true, /* Generates corresponding '.map' file. */ 13 | // "outFile": "./", /* Concatenate and emit output to single file. */ 14 | // "outDir": "./", /* Redirect output structure to the directory. */ 15 | // "rootDir": "./", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */ 16 | // "composite": true, /* Enable project compilation */ 17 | // "removeComments": true, /* Do not emit comments to output. */ 18 | // "noEmit": true, /* Do not emit outputs. */ 19 | // "importHelpers": true, /* Import emit helpers from 'tslib'. */ 20 | // "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */ 21 | // "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */ 22 | 23 | /* Strict Type-Checking Options */ 24 | "strict": true, /* Enable all strict type-checking options. */ 25 | // "noImplicitAny": true, /* Raise error on expressions and declarations with an implied 'any' type. */ 26 | // "strictNullChecks": true, /* Enable strict null checks. */ 27 | // "strictFunctionTypes": true, /* Enable strict checking of function types. */ 28 | // "strictBindCallApply": true, /* Enable strict 'bind', 'call', and 'apply' methods on functions. */ 29 | // "strictPropertyInitialization": true, /* Enable strict checking of property initialization in classes. */ 30 | // "noImplicitThis": true, /* Raise error on 'this' expressions with an implied 'any' type. */ 31 | // "alwaysStrict": true, /* Parse in strict mode and emit "use strict" for each source file. */ 32 | 33 | /* Additional Checks */ 34 | // "noUnusedLocals": true, /* Report errors on unused locals. */ 35 | // "noUnusedParameters": true, /* Report errors on unused parameters. */ 36 | // "noImplicitReturns": true, /* Report error when not all code paths in function return a value. */ 37 | // "noFallthroughCasesInSwitch": true, /* Report errors for fallthrough cases in switch statement. */ 38 | 39 | /* Module Resolution Options */ 40 | // "moduleResolution": "node", /* Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6). */ 41 | // "baseUrl": "./", /* Base directory to resolve non-absolute module names. */ 42 | // "paths": {}, /* A series of entries which re-map imports to lookup locations relative to the 'baseUrl'. */ 43 | // "rootDirs": [], /* List of root folders whose combined content represents the structure of the project at runtime. */ 44 | // "typeRoots": [], /* List of folders to include type definitions from. */ 45 | // "types": [], /* Type declaration files to be included in compilation. */ 46 | // "allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */ 47 | "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */ 48 | // "preserveSymlinks": true, /* Do not resolve the real path of symlinks. */ 49 | 50 | /* Source Map Options */ 51 | // "sourceRoot": "", /* Specify the location where debugger should locate TypeScript files instead of source locations. */ 52 | // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */ 53 | // "inlineSourceMap": true, /* Emit a single file with source maps instead of having a separate file. */ 54 | // "inlineSources": true, /* Emit the source alongside the sourcemaps within a single file; requires '--inlineSourceMap' or '--sourceMap' to be set. */ 55 | 56 | /* Experimental Options */ 57 | // "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */ 58 | // "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */ 59 | }, 60 | "include": [ 61 | "index.tsx" 62 | ], 63 | "exclude": [ 64 | "node_modules", 65 | "example" 66 | ] 67 | } 68 | --------------------------------------------------------------------------------