├── .gitignore ├── LICENSE ├── README.md ├── doc ├── generated_mask.png └── tutorial.gif ├── images └── labeling-system.png ├── package-lock.json ├── package.json ├── public ├── favicon.ico ├── index.html ├── logo192.png ├── logo512.png ├── manifest.json └── robots.txt ├── src ├── App.tsx ├── common │ ├── colors.tsx │ ├── config.tsx │ └── example.story.jpg ├── components │ ├── BrushPreview.tsx │ ├── ClassSelectionMenu.tsx │ ├── ImageCanvas.tsx │ ├── SideBarBoxContainer.tsx │ ├── ToolSelectionMenu.tsx │ └── Toolbar.tsx ├── hooks │ ├── useImageScale.tsx │ ├── usePainter.tsx │ └── useSelectedClass.tsx ├── index.tsx ├── react-app-env.d.ts ├── style.css └── utils │ └── ColorConversion.tsx └── tsconfig.json /.gitignore: -------------------------------------------------------------------------------- 1 | # Ignore build 2 | build/ 3 | 4 | # Logs 5 | logs 6 | *.log 7 | npm-debug.log* 8 | yarn-debug.log* 9 | yarn-error.log* 10 | lerna-debug.log* 11 | 12 | # Diagnostic reports (https://nodejs.org/api/report.html) 13 | report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json 14 | 15 | # Runtime data 16 | pids 17 | *.pid 18 | *.seed 19 | *.pid.lock 20 | 21 | # Directory for instrumented libs generated by jscoverage/JSCover 22 | lib-cov 23 | 24 | # Coverage directory used by tools like istanbul 25 | coverage 26 | *.lcov 27 | 28 | # nyc test coverage 29 | .nyc_output 30 | 31 | # Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files) 32 | .grunt 33 | 34 | # Bower dependency directory (https://bower.io/) 35 | bower_components 36 | 37 | # node-waf configuration 38 | .lock-wscript 39 | 40 | # Compiled binary addons (https://nodejs.org/api/addons.html) 41 | build/Release 42 | 43 | # Dependency directories 44 | node_modules/ 45 | jspm_packages/ 46 | 47 | # TypeScript v1 declaration files 48 | typings/ 49 | 50 | # TypeScript cache 51 | *.tsbuildinfo 52 | 53 | # Optional npm cache directory 54 | .npm 55 | 56 | # Optional eslint cache 57 | .eslintcache 58 | 59 | # Microbundle cache 60 | .rpt2_cache/ 61 | .rts2_cache_cjs/ 62 | .rts2_cache_es/ 63 | .rts2_cache_umd/ 64 | 65 | # Optional REPL history 66 | .node_repl_history 67 | 68 | # Output of 'npm pack' 69 | *.tgz 70 | 71 | # Yarn Integrity file 72 | .yarn-integrity 73 | 74 | # dotenv environment variables file 75 | .env 76 | .env.test 77 | 78 | # parcel-bundler cache (https://parceljs.org/) 79 | .cache 80 | 81 | # Next.js build output 82 | .next 83 | 84 | # Nuxt.js build / generate output 85 | .nuxt 86 | dist 87 | 88 | # Gatsby files 89 | .cache/ 90 | # Comment in the public line in if your project uses Gatsby and *not* Next.js 91 | # https://nextjs.org/blog/next-9-1#public-directory-support 92 | # public 93 | 94 | # vuepress build output 95 | .vuepress/dist 96 | 97 | # Serverless directories 98 | .serverless/ 99 | 100 | # FuseBox cache 101 | .fusebox/ 102 | 103 | # DynamoDB Local files 104 | .dynamodb/ 105 | 106 | # TernJS port file 107 | .tern-port 108 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Pixel Annotation Tool in React 2 | 3 | ## Introduction 4 | It's a React version of [PixelAnnotationTool](https://github.com/abreheret/PixelAnnotationTool). The implementation borrows ideas from different repos, including: 5 | 6 | * [react-magic-painter](https://github.com/codeAdrian/react-magic-painter) 7 | * [react-image-annotate](https://github.com/UniversalDataTool/react-image-annotate) 8 | 9 | The tool also uses many excellent packages, such as: 10 | 11 | * [react-konva](https://github.com/konvajs/react-konva) 12 | * [use-image](https://github.com/konvajs/use-image) 13 | * [opencv-js](https://github.com/TechStark/opencv-js) 14 | 15 | ## Demo 16 | 17 | [Try the pixel annotation tool here](https://pixel-annotation-tool.netlify.app/) 18 | 19 | ### UI Overview 20 | ![alt text](doc/generated_mask.png "UI Overview") 21 | 22 | ### Draw and Generate Mask 23 | ![Alt Text](doc/tutorial.gif) 24 | 25 | ## How to use 26 | 27 | ### Installation 28 | ``` 29 | npm install 30 | ``` 31 | 32 | ### Run the App 33 | 34 | ``` 35 | npm start 36 | ``` 37 | 38 | ## Next steps (Roadmap) 39 | 40 | I supervised a few engineers in Deltron Intelligence to extend this app for production use. See the figure below: 41 | 42 | ![alt text](images/labeling-system.png "LabelingSystem") 43 | 44 | Key technical challenges have been addressed in another of my repo: [emscripten-vision-react-example](https://github.com/randxie/emscripten-vision-react-example) 45 | 46 | ## Changelog 47 | 48 | - [x] Solve anti-aliasing issue by using findContours and drawContours to handle pixels at edge. 49 | 50 | ## References 51 | 52 | ### OpenCV related 53 | 1. https://aralroca.com/blog/opencv-in-the-web 54 | 1. https://github.com/echamudi/opencv-wasm 55 | 56 | ### Anti-aliasing related 57 | 1. https://medium.com/@kozo002/how-to-draw-without-antialiasing-on-html5-canvas-cf13294a8e58 58 | 1. https://github.com/konvajs/konva/issues/695 59 | 60 | ### Memory-leak related 61 | 62 | 1. https://github.com/opencv/opencv/issues/15060 63 | -------------------------------------------------------------------------------- /doc/generated_mask.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/randxie/react-pixel-annotation-tool/2eecb94fb6e9516faebe32942165a308d59988eb/doc/generated_mask.png -------------------------------------------------------------------------------- /doc/tutorial.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/randxie/react-pixel-annotation-tool/2eecb94fb6e9516faebe32942165a308d59988eb/doc/tutorial.gif -------------------------------------------------------------------------------- /images/labeling-system.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/randxie/react-pixel-annotation-tool/2eecb94fb6e9516faebe32942165a308d59988eb/images/labeling-system.png -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "react-pixel-annotation-tool", 3 | "version": "0.1.0", 4 | "private": true, 5 | "dependencies": { 6 | "@fortawesome/fontawesome-svg-core": "^1.2.32", 7 | "@fortawesome/free-solid-svg-icons": "^5.15.1", 8 | "@fortawesome/react-fontawesome": "^0.1.14", 9 | "@material-ui/core": "^4.11.3", 10 | "@techstark/opencv-js": "^4.5.2", 11 | "@types/node": "^12.0.0", 12 | "@types/react": "^16.9.53", 13 | "@types/react-dom": "^16.9.8", 14 | "as-bind": "^0.6.1", 15 | "buffer": "^6.0.3", 16 | "is-svg": ">=4.2.2", 17 | "konva": "^7.2.5", 18 | "normalize.css": "^8.0.1", 19 | "react": "^17.0.1", 20 | "react-app-rewired": "^2.1.8", 21 | "react-dom": "^17.0.1", 22 | "react-images-upload": "^1.2.8", 23 | "react-konva": "^17.0.0-rc.1", 24 | "react-material-workspace-layout": "^1.0.9", 25 | "react-remove-scroll": "^2.4.1", 26 | "react-scripts": "4.0.1", 27 | "react-use": "^17.2.1", 28 | "reactstrap": "^8.9.0", 29 | "transformation-matrix-js": "^2.7.6", 30 | "typescript": "^4.0.3", 31 | "use-event-callback": "^0.1.0", 32 | "use-image": "^1.0.7" 33 | }, 34 | "scripts": { 35 | "start": "react-scripts start", 36 | "build": "react-scripts build", 37 | "eject": "react-scripts eject" 38 | }, 39 | "eslintConfig": { 40 | "extends": [ 41 | "react-app" 42 | ] 43 | }, 44 | "browserslist": { 45 | "production": [ 46 | ">0.2%", 47 | "not dead", 48 | "not op_mini all" 49 | ], 50 | "development": [ 51 | "last 1 chrome version", 52 | "last 1 firefox version", 53 | "last 1 safari version" 54 | ] 55 | }, 56 | "devDependencies": { 57 | "wasm-loader": "^1.3.0" 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /public/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/randxie/react-pixel-annotation-tool/2eecb94fb6e9516faebe32942165a308d59988eb/public/favicon.ico -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 12 | 13 | 17 | 18 | 27 | Pixel Annotation Tool 28 | 29 | 33 | 34 | 35 | 36 |
37 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /public/logo192.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/randxie/react-pixel-annotation-tool/2eecb94fb6e9516faebe32942165a308d59988eb/public/logo192.png -------------------------------------------------------------------------------- /public/logo512.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/randxie/react-pixel-annotation-tool/2eecb94fb6e9516faebe32942165a308d59988eb/public/logo512.png -------------------------------------------------------------------------------- /public/manifest.json: -------------------------------------------------------------------------------- 1 | { 2 | "short_name": "Pixel Annotation Tool", 3 | "name": "React Pixel Annotation Tool with Watershed support in browser", 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 | -------------------------------------------------------------------------------- /public/robots.txt: -------------------------------------------------------------------------------- 1 | User-agent: * 2 | Disallow: 3 | -------------------------------------------------------------------------------- /src/App.tsx: -------------------------------------------------------------------------------- 1 | import React, { useCallback, useState } from 'react'; 2 | 3 | import Grid from '@material-ui/core/Grid'; 4 | 5 | import { ImageCanvas } from './components/ImageCanvas'; 6 | import { Toolbar } from './components/Toolbar'; 7 | import { usePainter } from './hooks/usePainter'; 8 | import { useSelectedClass } from './hooks/useSelectedClass'; 9 | import { useImageScale } from './hooks/useImageScale'; 10 | 11 | const App = () => { 12 | // States used in multiple components 13 | const [canvasSize, setCanvasSize] = useState([window.innerWidth * 0.6, window.innerHeight * 0.8]); 14 | 15 | // References shared across components 16 | const maskRef = React.useRef(); 17 | const imageRef = React.useRef(); 18 | const toolbarGridRef = React.useRef(); 19 | 20 | // Component properties 21 | const [{ ...painterState }, { ...painterApi }] = usePainter(); 22 | const [{ ...selectedClassState }, { ...selectedClassApi }] = useSelectedClass(); 23 | const [{ ...imageScaleState }, { ...imageScaleApi }] = useImageScale(); 24 | 25 | const toolbarProps = { 26 | ...painterState, ...selectedClassState, ...imageScaleState, 27 | ...painterApi, ...selectedClassApi, ...imageScaleApi 28 | }; 29 | const imageCanvasProps = { 30 | ...painterState, ...imageScaleState, ...selectedClassState, 31 | ...painterApi, ...imageScaleApi, canvasSize 32 | }; 33 | 34 | return ( 35 | <> 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | ); 46 | }; 47 | 48 | export default App; 49 | -------------------------------------------------------------------------------- /src/common/colors.tsx: -------------------------------------------------------------------------------- 1 | 2 | import * as muiColors from '@material-ui/core/colors'; 3 | 4 | export const colors = [ 5 | muiColors.red[500], 6 | muiColors.blue[500], 7 | muiColors.green[500], 8 | muiColors.orange[800], 9 | muiColors.brown[500], 10 | muiColors.lightGreen[700], 11 | muiColors.pink[500], 12 | muiColors.purple[500], 13 | muiColors.indigo[500], 14 | muiColors.teal[500], 15 | muiColors.lime[500], 16 | muiColors.blueGrey[500], 17 | ] 18 | 19 | const transparency = 0x88000000 20 | 21 | function reverseParseColor(rrggbb) { 22 | rrggbb = rrggbb.replace("#", "") 23 | const bbggrr = rrggbb.substr(4, 2) + rrggbb.substr(2, 2) + rrggbb.substr(0, 2) 24 | return parseInt(bbggrr, 16) 25 | } 26 | 27 | export const colorInts: Array = colors.map( 28 | (c) => (reverseParseColor(c) | transparency) >>> 0 29 | ) 30 | 31 | export default colors -------------------------------------------------------------------------------- /src/common/config.tsx: -------------------------------------------------------------------------------- 1 | const ALL_CLASSES = [ 2 | "foreground", 3 | "background", 4 | "tree", 5 | "road", 6 | "traffic-light" 7 | ]; 8 | 9 | export default ALL_CLASSES -------------------------------------------------------------------------------- /src/common/example.story.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/randxie/react-pixel-annotation-tool/2eecb94fb6e9516faebe32942165a308d59988eb/src/common/example.story.jpg -------------------------------------------------------------------------------- /src/components/BrushPreview.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | 3 | interface Props { 4 | currentColor: string; 5 | currentWidth: number; 6 | } 7 | 8 | export const BrushPreview: React.FC = ({ 9 | currentColor, 10 | currentWidth, 11 | }) => { 12 | const styles = { 13 | backgroundColor: currentColor, 14 | width: `${currentWidth}px`, 15 | height: `${currentWidth}px`, 16 | }; 17 | return ( 18 |
19 |
20 |
21 | ); 22 | }; 23 | -------------------------------------------------------------------------------- /src/components/ClassSelectionMenu.tsx: -------------------------------------------------------------------------------- 1 | 2 | import classnames from 'classnames'; 3 | import capitalize from 'lodash/capitalize'; 4 | import React, { useEffect } from 'react'; 5 | 6 | import BallotIcon from '@material-ui/icons/Ballot'; 7 | import Box from '@material-ui/core/Box'; 8 | import * as muiColors from '@material-ui/core/colors'; 9 | import { styled } from '@material-ui/core/styles'; 10 | 11 | import colors from '../common/colors'; 12 | import SidebarBoxContainer from './SideBarBoxContainer'; 13 | 14 | const LabelContainer = styled("div")({ 15 | display: "flex", 16 | paddingTop: 4, 17 | paddingBottom: 4, 18 | paddingLeft: 16, 19 | paddingRight: 16, 20 | alignItems: "center", 21 | cursor: "pointer", 22 | opacity: 0.7, 23 | backgroundColor: "#fff", 24 | "&:hover": { 25 | opacity: 1, 26 | }, 27 | "&.selected": { 28 | opacity: 1, 29 | fontWeight: "bold", 30 | }, 31 | }) 32 | const Circle = styled("div")({ 33 | width: 12, 34 | height: 12, 35 | borderRadius: 12, 36 | marginRight: 8, 37 | }) 38 | const Label = styled("div")({ 39 | fontSize: 11, 40 | }) 41 | const DashSep = styled("div")({ 42 | flexGrow: 1, 43 | borderBottom: `2px dotted ${muiColors.grey[300]}`, 44 | marginLeft: 8, 45 | marginRight: 8, 46 | }) 47 | const Number = styled("div")({ 48 | fontSize: 11, 49 | textAlign: "center", 50 | minWidth: 14, 51 | paddingTop: 2, 52 | paddingBottom: 2, 53 | fontWeight: "bold", 54 | color: muiColors.grey[700], 55 | }) 56 | 57 | export const ClassSelectionMenu = ({ 58 | selectedCls, 59 | regionClsList, 60 | onSelectCls, 61 | }) => { 62 | useEffect(() => { 63 | const keyMapping = {} 64 | for (let i = 0; i < 9 && i < regionClsList.length; i++) { 65 | keyMapping[i + 1] = () => onSelectCls(regionClsList[i]) 66 | } 67 | const onKeyDown = (e) => { 68 | if (keyMapping[e.key]) { 69 | keyMapping[e.key]() 70 | e.preventDefault() 71 | e.stopPropagation() 72 | } 73 | } 74 | window.addEventListener("keydown", onKeyDown) 75 | return () => window.removeEventListener("keydown", onKeyDown) 76 | }, [regionClsList, selectedCls, onSelectCls]) 77 | 78 | return ( 79 |
80 | {regionClsList.map((label, index) => ( 81 | onSelectCls(label)} 84 | > 85 | 86 | 89 | 90 | 91 | {index < 9 ? `Key [${index + 1}]` : ""} 92 | 93 | 94 | ))} 95 |
96 | ) 97 | } 98 | 99 | export default ClassSelectionMenu -------------------------------------------------------------------------------- /src/components/ImageCanvas.tsx: -------------------------------------------------------------------------------- 1 | import React from 'react'; 2 | import { Image, Layer, Line, Stage } from 'react-konva'; 3 | import useImage from 'use-image'; 4 | 5 | import exampleImage from '../common/example.story.jpg'; 6 | 7 | export const ImageCanvas: React.FC = ({ 8 | currentWidth, 9 | currentColor, 10 | currentTool, 11 | currentMaskOpacity, 12 | selectedClass, 13 | imageScale, 14 | lines, 15 | handleImageScale, 16 | setLines, 17 | imageRef, 18 | maskRef, 19 | }) => { 20 | let width = currentWidth 21 | let widthHalf = currentWidth / 2 22 | 23 | const isDrawing = React.useRef(false); 24 | const [image, status] = useImage(exampleImage, "Anonymous"); 25 | 26 | /* Handle mouse events */ 27 | const handleMouseEnter = (e) => { 28 | const stage = e.target.getStage(); 29 | stage.container().style.cursor = `url('data:image/svg+xml;utf8,') ${widthHalf} ${widthHalf}, auto`; 30 | } 31 | 32 | const handleMouseLeave = (e) => { 33 | const stage = e.target.getStage(); 34 | stage.container().style.cursor = "default"; 35 | isDrawing.current = false; 36 | } 37 | 38 | const handleMouseDown = (e) => { 39 | if (currentTool !== "none") { 40 | isDrawing.current = true; 41 | } 42 | 43 | const pos = e.target.getStage().getPointerPosition(); 44 | let tool = currentTool 45 | let scale = imageScale 46 | setLines([...lines, { tool, scale, selectedClass, currentWidth, currentColor, points: [pos.x, pos.y] }]); 47 | }; 48 | 49 | const handleMouseMove = (e) => { 50 | // no drawing - skipping 51 | if (!isDrawing.current) { 52 | return; 53 | } 54 | const stage = e.target.getStage(); 55 | 56 | const point = stage.getPointerPosition(); 57 | let lastLine = lines[lines.length - 1]; 58 | 59 | // add point 60 | lastLine.points = lastLine.points.concat([point.x, point.y]); 61 | 62 | // replace last 63 | lines.splice(lines.length - 1, 1, lastLine); 64 | setLines(lines.concat()); 65 | }; 66 | 67 | const handleMouseUp = () => { 68 | isDrawing.current = false; 69 | }; 70 | 71 | const scaleEachScroll = 1.02; 72 | const handleWheel = (e) => { 73 | e.evt.preventDefault(); 74 | const stage = e.target.getStage(); 75 | var newScale = 76 | e.evt.deltaY > 0 ? imageScale * scaleEachScroll : imageScale / scaleEachScroll; 77 | 78 | handleImageScale(newScale) 79 | stage.batchDraw(); 80 | }; 81 | 82 | return ( 83 |
87 | 98 | 102 | 109 | 110 | 114 | {lines.map((line, i) => ( 115 | 132 | ))} 133 | 134 | 135 |
136 | ); 137 | }; 138 | -------------------------------------------------------------------------------- /src/components/SideBarBoxContainer.tsx: -------------------------------------------------------------------------------- 1 | 2 | import classnames from 'classnames'; 3 | import React, { memo, useState } from 'react'; 4 | import SidebarBox from 'react-material-workspace-layout/SidebarBox'; 5 | import useEventCallback from 'use-event-callback'; 6 | 7 | import { grey } from '@material-ui/core/colors'; 8 | import { makeStyles } from '@material-ui/core/styles'; 9 | 10 | const useStyles = makeStyles({ 11 | container: { margin: 8 }, 12 | header: { 13 | display: "flex", 14 | flexDirection: "row", 15 | alignItems: "center", 16 | padding: 8, 17 | paddingLeft: 16, 18 | paddingRight: 16, 19 | }, 20 | title: { 21 | fontSize: 14, 22 | fontWeight: "bold", 23 | flexGrow: 1, 24 | paddingLeft: 8, 25 | color: grey[800], 26 | "& span": { 27 | color: grey[600], 28 | fontSize: 12, 29 | }, 30 | }, 31 | expandButton: { 32 | padding: 0, 33 | width: 30, 34 | height: 30, 35 | "& .icon": { 36 | marginTop: -6, 37 | width: 20, 38 | height: 20, 39 | transition: "500ms transform", 40 | "&.expanded": { 41 | transform: "rotate(180deg)", 42 | }, 43 | }, 44 | }, 45 | expandedContent: { 46 | maxHeight: 300, 47 | overflowY: "auto", 48 | "&.noScroll": { 49 | overflowY: "visible", 50 | overflow: "visible", 51 | }, 52 | }, 53 | }) 54 | 55 | export const SidebarBoxContainer = ({ 56 | icon, 57 | title, 58 | children, 59 | }) => { 60 | 61 | return ( 62 | 63 | {children} 64 | 65 | ) 66 | } 67 | 68 | export default memo( 69 | SidebarBoxContainer, 70 | (prev, next) => prev.title === next.title && prev.children === next.children 71 | ) -------------------------------------------------------------------------------- /src/components/ToolSelectionMenu.tsx: -------------------------------------------------------------------------------- 1 | 2 | import classnames from 'classnames'; 3 | import capitalize from 'lodash/capitalize'; 4 | import React, { useEffect } from 'react'; 5 | 6 | import * as muiColors from '@material-ui/core/colors'; 7 | import { styled } from '@material-ui/core/styles'; 8 | 9 | const LabelContainer = styled("div")({ 10 | display: "flex", 11 | paddingTop: 4, 12 | paddingBottom: 4, 13 | paddingLeft: 16, 14 | paddingRight: 16, 15 | alignItems: "center", 16 | cursor: "pointer", 17 | opacity: 0.7, 18 | backgroundColor: "#fff", 19 | "&:hover": { 20 | opacity: 1, 21 | }, 22 | "&.selected": { 23 | opacity: 1, 24 | fontWeight: "bold", 25 | }, 26 | }) 27 | 28 | const Label = styled("div")({ 29 | fontSize: 11, 30 | }) 31 | const DashSep = styled("div")({ 32 | flexGrow: 1, 33 | borderBottom: `2px dotted ${muiColors.grey[300]}`, 34 | marginLeft: 8, 35 | marginRight: 8, 36 | }) 37 | const Number = styled("div")({ 38 | fontSize: 11, 39 | textAlign: "center", 40 | minWidth: 14, 41 | paddingTop: 2, 42 | paddingBottom: 2, 43 | fontWeight: "bold", 44 | color: muiColors.grey[700], 45 | }) 46 | const ToolList = ["pen", "eraser"] 47 | const ToolKeys = ["a", "s"] 48 | 49 | export const ClassSelectionMenu = ({ 50 | selectedTool, 51 | onSelectTool, 52 | }) => { 53 | 54 | useEffect(() => { 55 | const keyMapping = {} 56 | for (let i = 0; i < ToolList.length; i++) { 57 | keyMapping[ToolKeys[i]] = () => onSelectTool(ToolList[i]) 58 | } 59 | const onKeyDown = (e) => { 60 | if (keyMapping[e.key]) { 61 | keyMapping[e.key]() 62 | e.preventDefault() 63 | e.stopPropagation() 64 | } 65 | } 66 | window.addEventListener("keydown", onKeyDown) 67 | return () => window.removeEventListener("keydown", onKeyDown) 68 | }, [onSelectTool]) 69 | 70 | return ( 71 |
72 | { 73 | ToolList.map((label, index) => ( 74 | onSelectTool(label)} 77 | > 78 | 81 | 82 | 83 | {`Key [${ToolKeys[index]}]`} 84 | 85 | 86 | )) 87 | } 88 |
89 | ) 90 | } 91 | 92 | export default ClassSelectionMenu -------------------------------------------------------------------------------- /src/components/Toolbar.tsx: -------------------------------------------------------------------------------- 1 | import React, { Ref, useCallback, useState } from 'react'; 2 | 3 | import Button from '@material-ui/core/Button'; 4 | import ButtonGroup from '@material-ui/core/ButtonGroup'; 5 | import * as muiColors from '@material-ui/core/colors'; 6 | import Grid from '@material-ui/core/Grid'; 7 | import BallotIcon from '@material-ui/icons/Ballot'; 8 | import cv, { map } from '@techstark/opencv-js'; 9 | 10 | import colors from '../common/colors'; 11 | import ALL_CLASSES from '../common/config'; 12 | import { hexToRgb, rgbToHex } from '../utils/ColorConversion'; 13 | import { BrushPreview } from './BrushPreview'; 14 | import ClassSelectionMenu from './ClassSelectionMenu'; 15 | import SidebarBoxContainer from './SideBarBoxContainer'; 16 | import ToolSelectionMenu from './ToolSelectionMenu'; 17 | 18 | const maskPixelMultiplier: number = 5; 19 | 20 | export const Toolbar: React.FC = ({ 21 | currentWidth, 22 | currentColor, 23 | currentMaskOpacity, 24 | currentTool, 25 | selectedClass, 26 | imageScale, 27 | handleColor, 28 | handleWidth, 29 | handleTool, 30 | handleMaskOpacity, 31 | handleClearMask, 32 | handleSelectedClass, 33 | handleImageScale, 34 | imageRef, 35 | maskRef, 36 | toolbarGridRef, 37 | }) => { 38 | const canvasRef = React.useRef() 39 | 40 | // Change mask for watershed algorithm and return a reset func 41 | // to go back to previous states. It behaves like context manager. 42 | const makeMaskMutationResetFn = (mask, changeColor) => { 43 | var originOpacity = currentMaskOpacity; 44 | var shouldChangeColor = changeColor; 45 | 46 | mask?.getChildren().each((node) => { 47 | node.opacity(1); 48 | if (shouldChangeColor) { 49 | let maskIdx = ALL_CLASSES.indexOf(node.name()) + 1 50 | if (node.name() !== 'eraser') { 51 | // We can not turn off anti-aliasing in canvas shape. Offset the maskIdx so that 52 | // it's easier to post-process. 53 | let pixelValue = maskPixelMultiplier * maskIdx 54 | node.stroke(rgbToHex(pixelValue, pixelValue, pixelValue)) 55 | } else { 56 | node.stroke(rgbToHex(0, 0, 0)) 57 | } 58 | } 59 | }) 60 | 61 | function resetMask() { 62 | // Set mask back to original state 63 | mask?.getChildren().each((node) => { 64 | if (shouldChangeColor) { 65 | let index = ALL_CLASSES.indexOf(node.name()) 66 | node.stroke(colors[index % colors.length]) 67 | } 68 | node.opacity(originOpacity); 69 | }) 70 | } 71 | return resetMask 72 | } 73 | 74 | const handleWatershed = () => { 75 | let currentMask = maskRef?.current 76 | if (currentMask === null) { 77 | return 78 | } 79 | 80 | if (imageRef?.current) { 81 | // Load both image and mask 82 | let imageMat = cv.imread(imageRef?.current?.toCanvas()); 83 | cv.cvtColor(imageMat, imageMat, cv.COLOR_RGBA2RGB) 84 | 85 | let resetMaskFn = makeMaskMutationResetFn(currentMask, true) 86 | let maskMat = cv.imread(currentMask?.toCanvas()); 87 | cv.cvtColor(maskMat, maskMat, cv.COLOR_RGBA2RGB) 88 | resetMaskFn() 89 | 90 | let rgbPlanes = new cv.MatVector(); 91 | maskMat.convertTo(maskMat, cv.CV_32S) 92 | cv.split(maskMat, rgbPlanes); 93 | maskMat.delete() 94 | 95 | // Create marker for watershed algorithm by summing all channels 96 | let marker = new cv.Mat(); 97 | cv.add(rgbPlanes.get(0), rgbPlanes.get(1), marker) 98 | cv.add(marker, rgbPlanes.get(2), marker) 99 | rgbPlanes.delete() 100 | 101 | // Use find contour to handle anti-aliasing issue. 102 | let contours = new cv.MatVector() 103 | let hierachy = new cv.Mat() 104 | cv.findContours(marker, contours, hierachy, cv.RETR_CCOMP, cv.CHAIN_APPROX_NONE) 105 | cv.drawContours(marker, contours, -1, new cv.Scalar(0), 4) 106 | contours.delete() 107 | hierachy.delete() 108 | 109 | // Use watershed to fill unknown label in the marker Mat. 110 | cv.watershed(imageMat, marker) 111 | imageMat.delete() 112 | 113 | if (canvasRef) { 114 | marker.convertTo(marker, -1, 1.0 / (maskPixelMultiplier * 3)) 115 | marker.convertTo(marker, cv.CV_8UC1) 116 | 117 | let coloredMask = new cv.Mat() 118 | let mergedPlanes = new cv.MatVector(); 119 | mergedPlanes.push_back(marker); 120 | mergedPlanes.push_back(marker); 121 | mergedPlanes.push_back(marker); 122 | cv.merge(mergedPlanes, coloredMask) 123 | 124 | marker.delete() 125 | mergedPlanes.delete() 126 | 127 | // Render the marker into colored mask. The color has been predefined for different classes. 128 | // let coloredMask = new cv.Mat(marker.rows, marker.cols, cv.CV_8UC3, new cv.Scalar(0, 0, 0)); 129 | let colorMap = ALL_CLASSES.map((cls) => { 130 | let index = ALL_CLASSES.indexOf(cls) 131 | return hexToRgb(colors[index % colors.length]) 132 | }) 133 | 134 | // Performance warning!! This part be slow. 135 | for (let x = 0; x < coloredMask.rows; x++) { 136 | let row = coloredMask.row(x) 137 | for (let y = 0; y < coloredMask.cols; y++) { 138 | let elem = row.col(y) 139 | let cls = elem.data[0] 140 | if (cls > 0 && cls <= ALL_CLASSES.length) { 141 | let colorVec = colorMap[cls - 1] 142 | if (colorVec) { 143 | elem.data[0] = colorVec[0] 144 | elem.data[1] = colorVec[1] 145 | elem.data[2] = colorVec[2] 146 | } 147 | } 148 | } 149 | } 150 | cv.imshow(canvasRef?.current, coloredMask) 151 | coloredMask.delete() 152 | } 153 | } 154 | } 155 | 156 | const handleClear = () => { 157 | handleClearMask() 158 | } 159 | 160 | const handleDownload = (mask, name) => { 161 | if (mask?.current === null) { 162 | return 163 | } 164 | 165 | let link = Array.from(document.getElementsByTagName("a")).find( 166 | (a) => a.download === name 167 | ) 168 | 169 | let resetMaskFn = makeMaskMutationResetFn(mask.current, false) 170 | 171 | if (!link) { 172 | const link = document.createElement('a'); 173 | link.download = name; 174 | link.href = mask.current.toDataURL(); 175 | document.body.appendChild(link); 176 | link.click(); 177 | document.body.removeChild(link); 178 | } else { 179 | document.body.appendChild(link); 180 | link.click(); 181 | document.body.removeChild(link); 182 | } 183 | 184 | resetMaskFn() 185 | } 186 | 187 | return ( 188 | 323 | ); 324 | }; 325 | -------------------------------------------------------------------------------- /src/hooks/useImageScale.tsx: -------------------------------------------------------------------------------- 1 | import { useState } from 'react'; 2 | 3 | import ALL_CLASSES from '../common/config'; 4 | 5 | export const useImageScale = () => { 6 | const [imageScale, setImageScale] = useState(1); 7 | 8 | const handleImageScale = (e: any) => { 9 | if (typeof e === 'number') { 10 | console.log(e) 11 | setImageScale(e) 12 | } else { 13 | setImageScale(e.currentTarget.value) 14 | } 15 | 16 | }; 17 | 18 | return [ 19 | { 20 | imageScale, 21 | }, 22 | { 23 | handleImageScale 24 | }, 25 | ] as any; 26 | }; 27 | -------------------------------------------------------------------------------- /src/hooks/usePainter.tsx: -------------------------------------------------------------------------------- 1 | import { useCallback, useRef, useState } from "react"; 2 | import colors from '../common/colors'; 3 | 4 | export const usePainter = () => { 5 | // Brush settings 6 | const [currentTool, setCurrentTool] = useState("pen"); 7 | const [currentColor, setCurrentColor] = useState(colors[0]); 8 | const [currentWidth, setCurrentWidth] = useState(50); 9 | 10 | // For drawing mask 11 | const [lines, setLines] = useState([]); 12 | const [currentMaskOpacity, setCurrentMaskOpacity] = useState(0.4); 13 | 14 | const handleColor = (e: any) => { 15 | setCurrentColor(e) 16 | }; 17 | 18 | const handleWidth = (e: any) => { 19 | setCurrentWidth(e.currentTarget.value); 20 | }; 21 | 22 | const handleMaskOpacity = (e: any) => { 23 | setCurrentMaskOpacity(e.currentTarget.value / 100.0); 24 | }; 25 | 26 | const handleTool = (e: string) => { 27 | setCurrentTool(e); 28 | }; 29 | 30 | const handleClearMask = () => { 31 | setLines([]) 32 | } 33 | 34 | return [ 35 | { 36 | currentWidth, 37 | currentColor, 38 | currentTool, 39 | currentMaskOpacity, 40 | lines 41 | }, 42 | { 43 | handleColor, 44 | handleWidth, 45 | handleTool, 46 | handleMaskOpacity, 47 | handleClearMask, 48 | setLines 49 | }, 50 | ] as any; 51 | }; 52 | -------------------------------------------------------------------------------- /src/hooks/useSelectedClass.tsx: -------------------------------------------------------------------------------- 1 | import { useState } from 'react'; 2 | 3 | import ALL_CLASSES from '../common/config'; 4 | 5 | export const useSelectedClass = () => { 6 | // Selected classes to draw mask 7 | const [selectedClass, setSelectedClass] = useState(ALL_CLASSES[0]); 8 | 9 | const handleSelectedClass = (e: any) => { 10 | setSelectedClass(e) 11 | }; 12 | 13 | return [ 14 | { 15 | selectedClass, 16 | }, 17 | { 18 | handleSelectedClass 19 | }, 20 | ] as any; 21 | }; 22 | -------------------------------------------------------------------------------- /src/index.tsx: -------------------------------------------------------------------------------- 1 | import React from "react"; 2 | import ReactDOM from "react-dom"; 3 | import App from "./App"; 4 | 5 | import "normalize.css"; 6 | import "./style.css"; 7 | 8 | ReactDOM.render( 9 | 10 | 11 | , 12 | document.getElementById("root"), 13 | ); 14 | -------------------------------------------------------------------------------- /src/react-app-env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /src/style.css: -------------------------------------------------------------------------------- 1 | :root { 2 | --font-size-base: 18px; 3 | --line-height-base: 1.5; 4 | 5 | --color-text: #1d1d1d; 6 | --color-background: #ffffff; 7 | } 8 | 9 | html { 10 | box-sizing: border-box; 11 | } 12 | 13 | *, 14 | *:before, 15 | *:after { 16 | box-sizing: inherit; 17 | } 18 | 19 | body { 20 | min-width: 1100px; 21 | color: var(--color-text); 22 | font-size: var(--font-size-base); 23 | line-height: var(--line-height-base); 24 | font-family: "Lato", Avenir, Adobe Heiti Std, Segoe UI, Trebuchet MS, 25 | sans‑serif; 26 | } 27 | 28 | html, 29 | body, 30 | canvas, 31 | section, 32 | main { 33 | width: 100%; 34 | height: 100%; 35 | } 36 | 37 | section { 38 | flex-grow: 1; 39 | height: 100%; 40 | } 41 | 42 | input { 43 | outline: 0; 44 | border: 0; 45 | } 46 | 47 | input[type="range"] { 48 | display: block; 49 | width: 100%; 50 | } 51 | 52 | aside { 53 | flex-basis: 196px; 54 | background-color: #f1f1f1; 55 | padding: 1.2em; 56 | display: flex; 57 | flex-direction: column; 58 | overflow: auto; 59 | } 60 | 61 | aside > div:first-of-type { 62 | flex-grow: 1; 63 | } 64 | 65 | main { 66 | display: flex; 67 | } 68 | 69 | header { 70 | min-width: 1100px; 71 | position: fixed; 72 | background-color: #f5f5f5; 73 | top: 0; 74 | left: 0; 75 | width: 100vw; 76 | height: 100vh; 77 | z-index: 5; 78 | display: flex; 79 | justify-content: center; 80 | align-items: center; 81 | text-align: center; 82 | padding: 4rem; 83 | border: 2rem solid; 84 | border-image-slice: 1; 85 | border-image-source: repeating-conic-gradient( 86 | hsl(0, 100%, 50%), 87 | hsl(36, 100%, 50%), 88 | hsl(108, 100%, 50%), 89 | hsl(211, 100%, 50%), 90 | hsl(247, 100%, 50%), 91 | hsl(277, 100%, 50%), 92 | hsl(320, 100%, 50%), 93 | hsl(0, 100%, 50%) 94 | ); 95 | } 96 | 97 | header.hidden { 98 | animation: fadeOut 1s ease-in-out forwards; 99 | pointer-events: none; 100 | } 101 | 102 | header a { 103 | padding: 0 1rem; 104 | font-weight: 700; 105 | color: var(--color-text); 106 | text-decoration: none; 107 | opacity: 0.8; 108 | transition: opacity 0.3s ease; 109 | } 110 | 111 | header a:nth-of-type(1) { 112 | color: hsl(36, 100%, 40%); 113 | } 114 | 115 | header a:nth-of-type(2) { 116 | color: hsl(108, 100%, 25%); 117 | } 118 | 119 | header a:nth-of-type(3) { 120 | color: hsl(211, 100%, 40%); 121 | } 122 | 123 | header a:nth-of-type(4) { 124 | color: hsl(277, 100%, 40%); 125 | } 126 | 127 | header a:hover, 128 | header a:active { 129 | opacity: 1; 130 | } 131 | 132 | h1 { 133 | font-weight: 400; 134 | font-family: "Finger Paint"; 135 | font-size: 8rem; 136 | letter-spacing: -0.3rem; 137 | margin: 0 0 3rem; 138 | background: linear-gradient( 139 | 90deg, 140 | hsl(0, 100%, 50%), 141 | hsl(211, 100%, 50%) 50%, 142 | hsl(108, 100%, 40%) 143 | ); 144 | -webkit-background-clip: text; 145 | -webkit-text-fill-color: transparent; 146 | } 147 | 148 | .blob-btn { 149 | z-index: 1; 150 | position: relative; 151 | padding: 1.5rem 4rem; 152 | margin-bottom: 30px; 153 | font-size: 1.25rem; 154 | text-align: center; 155 | background-color: transparent; 156 | outline: none; 157 | border: none; 158 | cursor: pointer; 159 | border-radius: 3rem; 160 | animation: colorShift 13s ease infinite forwards; 161 | box-shadow: 0 0px 15px currentColor; 162 | margin-bottom: 3rem; 163 | display: inline-flex; 164 | text-decoration: none; 165 | font-weight: 400; 166 | } 167 | .blob-text { 168 | font-family: "Finger Paint"; 169 | letter-spacing: 1px; 170 | color: currentColor; 171 | } 172 | .blob-btn:before { 173 | content: ""; 174 | z-index: 1; 175 | position: absolute; 176 | left: 0; 177 | top: 0; 178 | width: 100%; 179 | height: 100%; 180 | border: 4px solid currentColor; 181 | border-radius: 30px; 182 | } 183 | .blob-btn:after { 184 | content: ""; 185 | z-index: -2; 186 | position: absolute; 187 | left: 3px; 188 | top: 3px; 189 | width: 100%; 190 | height: 100%; 191 | transition: all 0.3s 0.2s; 192 | border-radius: 30px; 193 | } 194 | .blob-btn:hover .blob-text { 195 | color: #ffffff; 196 | border-radius: 30px; 197 | } 198 | .blob-btn:hover:after { 199 | transition: all 0.3s; 200 | left: 0; 201 | top: 0; 202 | border-radius: 30px; 203 | } 204 | .blob-btn__inner { 205 | z-index: -1; 206 | overflow: hidden; 207 | position: absolute; 208 | left: 0; 209 | top: 0; 210 | width: 100%; 211 | height: 100%; 212 | border-radius: 30px; 213 | background: #ffffff; 214 | } 215 | .blob-btn__blobs { 216 | position: relative; 217 | display: block; 218 | height: 100%; 219 | filter: url("#goo"); 220 | } 221 | 222 | .blob-btn__blob { 223 | position: absolute; 224 | top: 2px; 225 | width: 25%; 226 | height: 100%; 227 | background: currentColor; 228 | border-radius: 100%; 229 | transform: translate3d(0, 150%, 0) scale(1.7); 230 | transition: transform 0.4s ease; 231 | } 232 | @supports (filter: url("#goo")) { 233 | .blob-btn__blob { 234 | transform: translate3d(0, 150%, 0) scale(1.4); 235 | } 236 | } 237 | .blob-btn__blob:nth-child(1) { 238 | left: 0%; 239 | transition-delay: 0s; 240 | } 241 | .blob-btn__blob:nth-child(2) { 242 | left: 30%; 243 | transition-delay: 0.08s; 244 | } 245 | .blob-btn__blob:nth-child(3) { 246 | left: 60%; 247 | transition-delay: 0.16s; 248 | } 249 | .blob-btn__blob:nth-child(4) { 250 | left: 90%; 251 | transition-delay: 0.24s; 252 | } 253 | .blob-btn:hover .blob-btn__blob { 254 | transform: translateZ(0) scale(1.7); 255 | } 256 | @supports (filter: url("#goo")) { 257 | .blob-btn:hover .blob-btn__blob { 258 | transform: translateZ(0) scale(1.4); 259 | } 260 | } 261 | 262 | .preview { 263 | width: 130px; 264 | height: 130px; 265 | border: 2px solid var(--color-text); 266 | position: relative; 267 | } 268 | 269 | .preview__brush { 270 | position: absolute; 271 | top: 50%; 272 | left: 50%; 273 | transform: translate3d(-50%, -50%, 0); 274 | border-radius: 100%; 275 | } 276 | 277 | @keyframes colorShift { 278 | 0% { 279 | color: hsl(0, 100%, 40%); 280 | } 281 | 10% { 282 | color: hsl(36, 100%, 40%); 283 | } 284 | 20% { 285 | color: hsl(72, 100%, 30%); 286 | } 287 | 30% { 288 | color: hsl(108, 100%, 30%); 289 | } 290 | 40% { 291 | color: hsl(144, 100%, 30%); 292 | } 293 | 50% { 294 | color: hsl(180, 100%, 20%); 295 | } 296 | 60% { 297 | color: hsl(211, 100%, 40%); 298 | } 299 | 70% { 300 | color: hsl(247, 100%, 40%); 301 | } 302 | 80% { 303 | color: hsl(277, 100%, 50%); 304 | } 305 | 90% { 306 | color: hsl(301, 100%, 40%); 307 | } 308 | 100% { 309 | color: hsl(320, 100%, 30%); 310 | } 311 | 100% { 312 | color: hsl(350, 100%, 40%); 313 | } 314 | } 315 | 316 | @keyframes fadeOut { 317 | from { 318 | opacity: 1; 319 | } 320 | 321 | to { 322 | opacity: 0; 323 | } 324 | } 325 | 326 | .btn { 327 | display: inline-flex; 328 | cursor: pointer; 329 | justify-content: center; 330 | align-items: center; 331 | border: 0; 332 | outline: 0; 333 | border-radius: 0; 334 | text-decoration: none; 335 | padding: 0.5em; 336 | color: var(--color-text); 337 | background-color: #bbb; 338 | height: 45px; 339 | } 340 | 341 | .btn--main { 342 | background-color: hsl(211, 100%, 75%); 343 | margin-bottom: 0.75rem; 344 | } 345 | 346 | .btn--block { 347 | display: flex; 348 | width: 100%; 349 | } 350 | 351 | .btn--tool { 352 | background-color: #c1c1c1; 353 | width: 100%; 354 | height: 100%; 355 | padding: 0.25em 0.3em; 356 | } 357 | 358 | .tool-grid { 359 | display: grid; 360 | grid-template-columns: 1fr 1fr 1fr 1fr; 361 | grid-template-rows: 1fr; 362 | grid-gap: 0.25rem; 363 | margin-bottom: 0.5em; 364 | } 365 | 366 | .btn--color { 367 | -webkit-appearance: none; 368 | border: none; 369 | width: 100%; 370 | height: 28px; 371 | padding: 0; 372 | cursor: pointer; 373 | } 374 | 375 | input[type="color"]::-webkit-color-swatch-wrapper { 376 | padding: 0; 377 | } 378 | input[type="color"]::-webkit-color-swatch { 379 | border: none; 380 | } 381 | 382 | .btn--main { 383 | } 384 | 385 | input[type="checkbox"] { 386 | display: none; 387 | } 388 | 389 | .tool-section { 390 | padding-bottom: 0.5rem; 391 | } 392 | 393 | .tool-section--lrg { 394 | padding-bottom: 1rem; 395 | } 396 | 397 | .btn--active { 398 | background-color: hsl(211, 100%, 70%); 399 | } 400 | 401 | .btn--dream-active { 402 | background-image: repeating-conic-gradient( 403 | hsl(0, 100%, 70%), 404 | hsl(36, 100%, 70%), 405 | hsl(108, 100%, 70%), 406 | hsl(211, 100%, 70%), 407 | hsl(247, 100%, 70%), 408 | hsl(277, 100%, 70%), 409 | hsl(320, 100%, 70%), 410 | hsl(0, 100%, 70%) 411 | ); 412 | } 413 | 414 | .btn--eraser-active { 415 | background-color: hsl(108, 100%, 70%); 416 | } 417 | 418 | .btn--width-active { 419 | background-color: hsl(0, 100%, 70%); 420 | } 421 | 422 | *[disabled], 423 | *[disabled] + label { 424 | opacity: 0.6; 425 | cursor: not-allowed; 426 | } 427 | -------------------------------------------------------------------------------- /src/utils/ColorConversion.tsx: -------------------------------------------------------------------------------- 1 | // Conversion between RGB and HEX 2 | 3 | function componentToHex(c: number) { 4 | var hex = c.toString(16); 5 | return hex.length === 1 ? "0" + hex : hex; 6 | } 7 | 8 | function rgbToHex(r: number, g: number, b: number) { 9 | return "#" + componentToHex(r) + componentToHex(g) + componentToHex(b); 10 | } 11 | 12 | function hexToRgb(hex: string) { 13 | var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); 14 | return result ? [ 15 | parseInt(result[1], 16), 16 | parseInt(result[2], 16), 17 | parseInt(result[3], 16) 18 | ] : null; 19 | } 20 | 21 | export { 22 | rgbToHex, 23 | hexToRgb 24 | } -------------------------------------------------------------------------------- /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": false, 14 | "forceConsistentCasingInFileNames": true, 15 | "noFallthroughCasesInSwitch": true, 16 | "module": "esnext", 17 | "moduleResolution": "node", 18 | "resolveJsonModule": true, 19 | "isolatedModules": true, 20 | "noEmit": true, 21 | "jsx": "react-jsx", 22 | "noImplicitAny": false 23 | }, 24 | "include": [ 25 | "src" 26 | ], 27 | "exclude": [ 28 | "node_modules", 29 | "build" 30 | ] // *** The files to not type check *** 31 | } --------------------------------------------------------------------------------