├── .gitignore ├── LICENSE ├── README.md ├── deploy.sh ├── jsconfig.json ├── package-lock.json ├── package.json ├── public ├── avatar.png └── index.html ├── src ├── App.vue ├── assets │ └── logo.png ├── main.js ├── utils │ ├── eye_shape.js │ ├── face_shape.js │ ├── hair_lines.js │ └── mouth_shape.js └── views │ └── FaceGenerator.vue └── vue.config.js /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | /dist 4 | 5 | 6 | # local env files 7 | .env.local 8 | .env.*.local 9 | 10 | # Log files 11 | npm-debug.log* 12 | yarn-debug.log* 13 | yarn-error.log* 14 | pnpm-debug.log* 15 | 16 | # Editor directories and files 17 | .idea 18 | .vscode 19 | *.suo 20 | *.ntvs* 21 | *.njsproj 22 | *.sln 23 | *.sw? 24 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Attribution-NonCommercial 4.0 International 2 | 3 | ======================================================================= 4 | 5 | Creative Commons Corporation ("Creative Commons") is not a law firm and 6 | does not provide legal services or legal advice. Distribution of 7 | Creative Commons public licenses does not create a lawyer-client or 8 | other relationship. Creative Commons makes its licenses and related 9 | information available on an "as-is" basis. Creative Commons gives no 10 | warranties regarding its licenses, any material licensed under their 11 | terms and conditions, or any related information. Creative Commons 12 | disclaims all liability for damages resulting from their use to the 13 | fullest extent possible. 14 | 15 | Using Creative Commons Public Licenses 16 | 17 | Creative Commons public licenses provide a standard set of terms and 18 | conditions that creators and other rights holders may use to share 19 | original works of authorship and other material subject to copyright 20 | and certain other rights specified in the public license below. The 21 | following considerations are for informational purposes only, are not 22 | exhaustive, and do not form part of our licenses. 23 | 24 | Considerations for licensors: Our public licenses are 25 | intended for use by those authorized to give the public 26 | permission to use material in ways otherwise restricted by 27 | copyright and certain other rights. Our licenses are 28 | irrevocable. Licensors should read and understand the terms 29 | and conditions of the license they choose before applying it. 30 | Licensors should also secure all rights necessary before 31 | applying our licenses so that the public can reuse the 32 | material as expected. Licensors should clearly mark any 33 | material not subject to the license. This includes other CC- 34 | licensed material, or material used under an exception or 35 | limitation to copyright. More considerations for licensors: 36 | wiki.creativecommons.org/Considerations_for_licensors 37 | 38 | Considerations for the public: By using one of our public 39 | licenses, a licensor grants the public permission to use the 40 | licensed material under specified terms and conditions. If 41 | the licensor's permission is not necessary for any reason--for 42 | example, because of any applicable exception or limitation to 43 | copyright--then that use is not regulated by the license. Our 44 | licenses grant only permissions under copyright and certain 45 | other rights that a licensor has authority to grant. Use of 46 | the licensed material may still be restricted for other 47 | reasons, including because others have copyright or other 48 | rights in the material. A licensor may make special requests, 49 | such as asking that all changes be marked or described. 50 | Although not required by our licenses, you are encouraged to 51 | respect those requests where reasonable. More considerations 52 | for the public: 53 | wiki.creativecommons.org/Considerations_for_licensees 54 | 55 | ======================================================================= 56 | 57 | Creative Commons Attribution-NonCommercial 4.0 International Public 58 | License 59 | 60 | By exercising the Licensed Rights (defined below), You accept and agree 61 | to be bound by the terms and conditions of this Creative Commons 62 | Attribution-NonCommercial 4.0 International Public License ("Public 63 | License"). To the extent this Public License may be interpreted as a 64 | contract, You are granted the Licensed Rights in consideration of Your 65 | acceptance of these terms and conditions, and the Licensor grants You 66 | such rights in consideration of benefits the Licensor receives from 67 | making the Licensed Material available under these terms and 68 | conditions. 69 | 70 | 71 | Section 1 -- Definitions. 72 | 73 | a. Adapted Material means material subject to Copyright and Similar 74 | Rights that is derived from or based upon the Licensed Material 75 | and in which the Licensed Material is translated, altered, 76 | arranged, transformed, or otherwise modified in a manner requiring 77 | permission under the Copyright and Similar Rights held by the 78 | Licensor. For purposes of this Public License, where the Licensed 79 | Material is a musical work, performance, or sound recording, 80 | Adapted Material is always produced where the Licensed Material is 81 | synched in timed relation with a moving image. 82 | 83 | b. Adapter's License means the license You apply to Your Copyright 84 | and Similar Rights in Your contributions to Adapted Material in 85 | accordance with the terms and conditions of this Public License. 86 | 87 | c. Copyright and Similar Rights means copyright and/or similar rights 88 | closely related to copyright including, without limitation, 89 | performance, broadcast, sound recording, and Sui Generis Database 90 | Rights, without regard to how the rights are labeled or 91 | categorized. For purposes of this Public License, the rights 92 | specified in Section 2(b)(1)-(2) are not Copyright and Similar 93 | Rights. 94 | d. Effective Technological Measures means those measures that, in the 95 | absence of proper authority, may not be circumvented under laws 96 | fulfilling obligations under Article 11 of the WIPO Copyright 97 | Treaty adopted on December 20, 1996, and/or similar international 98 | agreements. 99 | 100 | e. Exceptions and Limitations means fair use, fair dealing, and/or 101 | any other exception or limitation to Copyright and Similar Rights 102 | that applies to Your use of the Licensed Material. 103 | 104 | f. Licensed Material means the artistic or literary work, database, 105 | or other material to which the Licensor applied this Public 106 | License. 107 | 108 | g. Licensed Rights means the rights granted to You subject to the 109 | terms and conditions of this Public License, which are limited to 110 | all Copyright and Similar Rights that apply to Your use of the 111 | Licensed Material and that the Licensor has authority to license. 112 | 113 | h. Licensor means the individual(s) or entity(ies) granting rights 114 | under this Public License. 115 | 116 | i. NonCommercial means not primarily intended for or directed towards 117 | commercial advantage or monetary compensation. For purposes of 118 | this Public License, the exchange of the Licensed Material for 119 | other material subject to Copyright and Similar Rights by digital 120 | file-sharing or similar means is NonCommercial provided there is 121 | no payment of monetary compensation in connection with the 122 | exchange. 123 | 124 | j. Share means to provide material to the public by any means or 125 | process that requires permission under the Licensed Rights, such 126 | as reproduction, public display, public performance, distribution, 127 | dissemination, communication, or importation, and to make material 128 | available to the public including in ways that members of the 129 | public may access the material from a place and at a time 130 | individually chosen by them. 131 | 132 | k. Sui Generis Database Rights means rights other than copyright 133 | resulting from Directive 96/9/EC of the European Parliament and of 134 | the Council of 11 March 1996 on the legal protection of databases, 135 | as amended and/or succeeded, as well as other essentially 136 | equivalent rights anywhere in the world. 137 | 138 | l. You means the individual or entity exercising the Licensed Rights 139 | under this Public License. Your has a corresponding meaning. 140 | 141 | 142 | Section 2 -- Scope. 143 | 144 | a. License grant. 145 | 146 | 1. Subject to the terms and conditions of this Public License, 147 | the Licensor hereby grants You a worldwide, royalty-free, 148 | non-sublicensable, non-exclusive, irrevocable license to 149 | exercise the Licensed Rights in the Licensed Material to: 150 | 151 | a. reproduce and Share the Licensed Material, in whole or 152 | in part, for NonCommercial purposes only; and 153 | 154 | b. produce, reproduce, and Share Adapted Material for 155 | NonCommercial purposes only. 156 | 157 | 2. Exceptions and Limitations. For the avoidance of doubt, where 158 | Exceptions and Limitations apply to Your use, this Public 159 | License does not apply, and You do not need to comply with 160 | its terms and conditions. 161 | 162 | 3. Term. The term of this Public License is specified in Section 163 | 6(a). 164 | 165 | 4. Media and formats; technical modifications allowed. The 166 | Licensor authorizes You to exercise the Licensed Rights in 167 | all media and formats whether now known or hereafter created, 168 | and to make technical modifications necessary to do so. The 169 | Licensor waives and/or agrees not to assert any right or 170 | authority to forbid You from making technical modifications 171 | necessary to exercise the Licensed Rights, including 172 | technical modifications necessary to circumvent Effective 173 | Technological Measures. For purposes of this Public License, 174 | simply making modifications authorized by this Section 2(a) 175 | (4) never produces Adapted Material. 176 | 177 | 5. Downstream recipients. 178 | 179 | a. Offer from the Licensor -- Licensed Material. Every 180 | recipient of the Licensed Material automatically 181 | receives an offer from the Licensor to exercise the 182 | Licensed Rights under the terms and conditions of this 183 | Public License. 184 | 185 | b. No downstream restrictions. You may not offer or impose 186 | any additional or different terms or conditions on, or 187 | apply any Effective Technological Measures to, the 188 | Licensed Material if doing so restricts exercise of the 189 | Licensed Rights by any recipient of the Licensed 190 | Material. 191 | 192 | 6. No endorsement. Nothing in this Public License constitutes or 193 | may be construed as permission to assert or imply that You 194 | are, or that Your use of the Licensed Material is, connected 195 | with, or sponsored, endorsed, or granted official status by, 196 | the Licensor or others designated to receive attribution as 197 | provided in Section 3(a)(1)(A)(i). 198 | 199 | b. Other rights. 200 | 201 | 1. Moral rights, such as the right of integrity, are not 202 | licensed under this Public License, nor are publicity, 203 | privacy, and/or other similar personality rights; however, to 204 | the extent possible, the Licensor waives and/or agrees not to 205 | assert any such rights held by the Licensor to the limited 206 | extent necessary to allow You to exercise the Licensed 207 | Rights, but not otherwise. 208 | 209 | 2. Patent and trademark rights are not licensed under this 210 | Public License. 211 | 212 | 3. To the extent possible, the Licensor waives any right to 213 | collect royalties from You for the exercise of the Licensed 214 | Rights, whether directly or through a collecting society 215 | under any voluntary or waivable statutory or compulsory 216 | licensing scheme. In all other cases the Licensor expressly 217 | reserves any right to collect such royalties, including when 218 | the Licensed Material is used other than for NonCommercial 219 | purposes. 220 | 221 | 222 | Section 3 -- License Conditions. 223 | 224 | Your exercise of the Licensed Rights is expressly made subject to the 225 | following conditions. 226 | 227 | a. Attribution. 228 | 229 | 1. If You Share the Licensed Material (including in modified 230 | form), You must: 231 | 232 | a. retain the following if it is supplied by the Licensor 233 | with the Licensed Material: 234 | 235 | i. identification of the creator(s) of the Licensed 236 | Material and any others designated to receive 237 | attribution, in any reasonable manner requested by 238 | the Licensor (including by pseudonym if 239 | designated); 240 | 241 | ii. a copyright notice; 242 | 243 | iii. a notice that refers to this Public License; 244 | 245 | iv. a notice that refers to the disclaimer of 246 | warranties; 247 | 248 | v. a URI or hyperlink to the Licensed Material to the 249 | extent reasonably practicable; 250 | 251 | b. indicate if You modified the Licensed Material and 252 | retain an indication of any previous modifications; and 253 | 254 | c. indicate the Licensed Material is licensed under this 255 | Public License, and include the text of, or the URI or 256 | hyperlink to, this Public License. 257 | 258 | 2. You may satisfy the conditions in Section 3(a)(1) in any 259 | reasonable manner based on the medium, means, and context in 260 | which You Share the Licensed Material. For example, it may be 261 | reasonable to satisfy the conditions by providing a URI or 262 | hyperlink to a resource that includes the required 263 | information. 264 | 265 | 3. If requested by the Licensor, You must remove any of the 266 | information required by Section 3(a)(1)(A) to the extent 267 | reasonably practicable. 268 | 269 | 4. If You Share Adapted Material You produce, the Adapter's 270 | License You apply must not prevent recipients of the Adapted 271 | Material from complying with this Public License. 272 | 273 | 274 | Section 4 -- Sui Generis Database Rights. 275 | 276 | Where the Licensed Rights include Sui Generis Database Rights that 277 | apply to Your use of the Licensed Material: 278 | 279 | a. for the avoidance of doubt, Section 2(a)(1) grants You the right 280 | to extract, reuse, reproduce, and Share all or a substantial 281 | portion of the contents of the database for NonCommercial purposes 282 | only; 283 | 284 | b. if You include all or a substantial portion of the database 285 | contents in a database in which You have Sui Generis Database 286 | Rights, then the database in which You have Sui Generis Database 287 | Rights (but not its individual contents) is Adapted Material; and 288 | 289 | c. You must comply with the conditions in Section 3(a) if You Share 290 | all or a substantial portion of the contents of the database. 291 | 292 | For the avoidance of doubt, this Section 4 supplements and does not 293 | replace Your obligations under this Public License where the Licensed 294 | Rights include other Copyright and Similar Rights. 295 | 296 | 297 | Section 5 -- Disclaimer of Warranties and Limitation of Liability. 298 | 299 | a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE 300 | EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS 301 | AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF 302 | ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS, 303 | IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION, 304 | WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR 305 | PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS, 306 | ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT 307 | KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT 308 | ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU. 309 | 310 | b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE 311 | TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION, 312 | NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT, 313 | INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES, 314 | COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR 315 | USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN 316 | ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR 317 | DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR 318 | IN PART, THIS LIMITATION MAY NOT APPLY TO YOU. 319 | 320 | c. The disclaimer of warranties and limitation of liability provided 321 | above shall be interpreted in a manner that, to the extent 322 | possible, most closely approximates an absolute disclaimer and 323 | waiver of all liability. 324 | 325 | 326 | Section 6 -- Term and Termination. 327 | 328 | a. This Public License applies for the term of the Copyright and 329 | Similar Rights licensed here. However, if You fail to comply with 330 | this Public License, then Your rights under this Public License 331 | terminate automatically. 332 | 333 | b. Where Your right to use the Licensed Material has terminated under 334 | Section 6(a), it reinstates: 335 | 336 | 1. automatically as of the date the violation is cured, provided 337 | it is cured within 30 days of Your discovery of the 338 | violation; or 339 | 340 | 2. upon express reinstatement by the Licensor. 341 | 342 | For the avoidance of doubt, this Section 6(b) does not affect any 343 | right the Licensor may have to seek remedies for Your violations 344 | of this Public License. 345 | 346 | c. For the avoidance of doubt, the Licensor may also offer the 347 | Licensed Material under separate terms or conditions or stop 348 | distributing the Licensed Material at any time; however, doing so 349 | will not terminate this Public License. 350 | 351 | d. Sections 1, 5, 6, 7, and 8 survive termination of this Public 352 | License. 353 | 354 | 355 | Section 7 -- Other Terms and Conditions. 356 | 357 | a. The Licensor shall not be bound by any additional or different 358 | terms or conditions communicated by You unless expressly agreed. 359 | 360 | b. Any arrangements, understandings, or agreements regarding the 361 | Licensed Material not stated herein are separate from and 362 | independent of the terms and conditions of this Public License. 363 | 364 | 365 | Section 8 -- Interpretation. 366 | 367 | a. For the avoidance of doubt, this Public License does not, and 368 | shall not be interpreted to, reduce, limit, restrict, or impose 369 | conditions on any use of the Licensed Material that could lawfully 370 | be made without permission under this Public License. 371 | 372 | b. To the extent possible, if any provision of this Public License is 373 | deemed unenforceable, it shall be automatically reformed to the 374 | minimum extent necessary to make it enforceable. If the provision 375 | cannot be reformed, it shall be severed from this Public License 376 | without affecting the enforceability of the remaining terms and 377 | conditions. 378 | 379 | c. No term or condition of this Public License will be waived and no 380 | failure to comply consented to unless expressly agreed to by the 381 | Licensor. 382 | 383 | d. Nothing in this Public License constitutes or may be interpreted 384 | as a limitation upon, or waiver of, any privileges and immunities 385 | that apply to the Licensor or You, including from the legal 386 | processes of any jurisdiction or authority. 387 | 388 | ======================================================================= 389 | 390 | Creative Commons is not a party to its public 391 | licenses. Notwithstanding, Creative Commons may elect to apply one of 392 | its public licenses to material it publishes and in those instances 393 | will be considered the “Licensor.” The text of the Creative Commons 394 | public licenses is dedicated to the public domain under the CC0 Public 395 | Domain Dedication. Except for the limited purpose of indicating that 396 | material is shared under a Creative Commons public license or as 397 | otherwise permitted by the Creative Commons policies published at 398 | creativecommons.org/policies, Creative Commons does not authorize the 399 | use of the trademark "Creative Commons" or any other trademark or logo 400 | of Creative Commons without its prior written consent including, 401 | without limitation, in connection with any unauthorized modifications 402 | to any of its public licenses or any other arrangements, 403 | understandings, or agreements concerning use of licensed material. For 404 | the avoidance of doubt, this paragraph does not form part of the 405 | public licenses. 406 | 407 | Creative Commons may be contacted at creativecommons.org. 408 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ugly-face 2 | 3 | This project is under Attribution-NonCommercial 4.0 International License. This means no commercial use for the project. I thought about this for a time. If you want to use this as part of your website that needs an avatar generator, you are free to use it. If you however, buid an app solely on this code as your main project, I am highly against that practice. I don't want to have the frustration of "oh someone copied my code and made money with it". 4 | 5 | Visit the website: https://txstc55.github.io/ugly-avatar 6 | 7 | I have seen a good amount of copycat websites. I am fairly disappointed. 8 | -------------------------------------------------------------------------------- /deploy.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | # abort on errors 4 | set -e 5 | 6 | # build 7 | npm run build 8 | 9 | # navigate into the build output directory 10 | cd dist 11 | 12 | # if you are deploying to a custom domain 13 | # echo 'www.example.com' > CNAME 14 | 15 | git init 16 | git add -A 17 | git commit -m 'Deploy' 18 | 19 | # if you are deploying to https://.github.io 20 | git push -f https://github.com/txstc55/ugly-avatar main:gh-pages 21 | 22 | # if you are deploying to https://.github.io/ 23 | # git push -f git@github.com:/.git master:gh-pages 24 | 25 | cd - -------------------------------------------------------------------------------- /jsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "module": "esnext", 5 | "baseUrl": "./", 6 | "moduleResolution": "node", 7 | "paths": { 8 | "@/*": [ 9 | "src/*" 10 | ] 11 | }, 12 | "lib": [ 13 | "esnext", 14 | "dom", 15 | "dom.iterable", 16 | "scripthost" 17 | ] 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ugly-face", 3 | "version": "0.1.0", 4 | "private": true, 5 | "scripts": { 6 | "serve": "vue-cli-service serve", 7 | "build": "vue-cli-service build" 8 | }, 9 | "dependencies": { 10 | "vue": "^3.2.13" 11 | }, 12 | "devDependencies": { 13 | "@vue/cli-service": "~5.0.0" 14 | }, 15 | "browserslist": [ 16 | "> 1%", 17 | "last 2 versions", 18 | "not dead", 19 | "not ie 11" 20 | ] 21 | } 22 | -------------------------------------------------------------------------------- /public/avatar.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/txstc55/ugly-avatar/220838e89c6e537c1b49337149ea273483f1f313/public/avatar.png -------------------------------------------------------------------------------- /public/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | Ugly Avatar 9 | 10 | 11 | 14 |
15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/App.vue: -------------------------------------------------------------------------------- 1 | 4 | 5 | 13 | 14 | 22 | -------------------------------------------------------------------------------- /src/assets/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/txstc55/ugly-avatar/220838e89c6e537c1b49337149ea273483f1f313/src/assets/logo.png -------------------------------------------------------------------------------- /src/main.js: -------------------------------------------------------------------------------- 1 | import { createApp } from 'vue' 2 | import App from './App.vue' 3 | 4 | createApp(App).mount('#app') 5 | -------------------------------------------------------------------------------- /src/utils/eye_shape.js: -------------------------------------------------------------------------------- 1 | function randomFromInterval(min, max) { 2 | // min and max included 3 | return Math.random() * (max - min) + min; 4 | } 5 | 6 | function cubicBezier(P0, P1, P2, P3, t) { 7 | var x = (1 - t) ** 3 * P0[0] + 3 * (1 - t) ** 2 * t * P1[0] + 3 * (1 - t) * t ** 2 * P2[0] + t ** 3 * P3[0]; 8 | var y = (1 - t) ** 3 * P0[1] + 3 * (1 - t) ** 2 * t * P1[1] + 3 * (1 - t) * t ** 2 * P2[1] + t ** 3 * P3[1]; 9 | return [x, y]; 10 | } 11 | 12 | function generateEyeParameters(width) { 13 | let height_upper = Math.random() * width / 1.2;// Less height for the upper eyelid to make it sharper 14 | let height_lower = Math.random() * width / 1.2;// More height for the lower eyelid to make it rounder and droopier 15 | let P0_upper_randX = Math.random() * 0.4 - 0.2; 16 | let P3_upper_randX = Math.random() * 0.4 - 0.2; 17 | let P0_upper_randY = Math.random() * 0.4 - 0.2; 18 | let P3_upper_randY = Math.random() * 0.4 - 0.2; 19 | let offset_upper_left_randY = Math.random(); 20 | let offset_upper_right_randY = Math.random(); 21 | let P0_upper = [-width / 2 + P0_upper_randX * width / 16, P0_upper_randY * height_upper / 16]; 22 | let P3_upper = [width / 2 + P3_upper_randX * width / 16, P3_upper_randY * height_upper / 16]; 23 | let P0_lower = P0_upper;// Starting at the same point as the upper eyelid 24 | let P3_lower = P3_upper;// Ending at the same point as the upper eyelid 25 | let eye_true_width = P3_upper[0] - P0_upper[0]; 26 | 27 | let offset_upper_left_x = randomFromInterval(-eye_true_width / 10.0, eye_true_width / 2.3);// Upper eyelid control point offset to create asymmetry 28 | let offset_upper_right_x = randomFromInterval(-eye_true_width / 10.0, eye_true_width / 2.3);// Upper eyelid control point offset to create asymmetry 29 | let offset_upper_left_y = offset_upper_left_randY * height_upper;// Upper eyelid control point offset to create asymmetry 30 | let offset_upper_right_y = offset_upper_right_randY * height_upper;// Upper eyelid control point offset to create asymmetry 31 | let offset_lower_left_x = randomFromInterval(offset_upper_left_x, eye_true_width / 2.1);// Lower eyelid control point offset 32 | let offset_lower_right_x = randomFromInterval(offset_upper_right_x, eye_true_width / 2.1);// Upper eyelid control point offset to create asymmetry 33 | let offset_lower_left_y = randomFromInterval(-offset_upper_left_y + 5, height_lower);// Upper eyelid control point offset to create asymmetry 34 | let offset_lower_right_y = randomFromInterval(-offset_upper_right_y + 5, height_lower);// Upper eyelid control point offset to create asymmetry 35 | // Generate points for the Bezier curves 36 | let left_converge0 = Math.random(); 37 | let right_converge0 = Math.random(); 38 | // Generate points for the Bezier curves 39 | let left_converge1 = Math.random(); 40 | let right_converge1 = Math.random(); 41 | return { 42 | height_upper: height_upper, 43 | height_lower: height_lower, 44 | P0_upper_randX: P0_upper_randX, 45 | P3_upper_randX: P3_upper_randX, 46 | P0_upper_randY: P0_upper_randY, 47 | P3_upper_randY: P3_upper_randY, 48 | offset_upper_left_randY: offset_upper_left_randY, 49 | offset_upper_right_randY: offset_upper_right_randY, 50 | eye_true_width: eye_true_width, 51 | offset_upper_left_x: offset_upper_left_x, 52 | offset_upper_right_x: offset_upper_right_x, 53 | offset_upper_left_y: offset_upper_left_y, 54 | offset_upper_right_y: offset_upper_right_y, 55 | offset_lower_left_x: offset_lower_left_x, 56 | offset_lower_right_x: offset_lower_right_x, 57 | offset_lower_left_y: offset_lower_left_y, 58 | offset_lower_right_y: offset_lower_right_y, 59 | left_converge0: left_converge0, 60 | right_converge0: right_converge0, 61 | left_converge1: left_converge1, 62 | right_converge1: right_converge1 63 | } 64 | } 65 | 66 | export function generateEyePoints(rands, width = 50) { 67 | 68 | let P0_upper = [-width / 2 + rands.P0_upper_randX * width / 16, rands.P0_upper_randY * rands.height_upper / 16]; 69 | let P3_upper = [width / 2 + rands.P3_upper_randX * width / 16, rands.P3_upper_randY * rands.height_upper / 16]; 70 | let P0_lower = P0_upper;// Starting at the same point as the upper eyelid 71 | let P3_lower = P3_upper;// Ending at the same point as the upper eyelid 72 | let eye_true_width = P3_upper[0] - P0_upper[0]; 73 | 74 | // Upper eyelid control points 75 | let P1_upper = [P0_upper[0] + rands.offset_upper_left_x, P0_upper[1] + rands.offset_upper_left_y]; // First control point 76 | let P2_upper = [P3_upper[0] - rands.offset_upper_right_x, P3_upper[1] + rands.offset_upper_right_y]; // Second control point 77 | 78 | 79 | // Lower eyelid control points 80 | let P1_lower = [P0_lower[0] + rands.offset_lower_left_x, P0_lower[1] - rands.offset_lower_left_y]; // First control point 81 | let P2_lower = [P3_lower[0] - rands.offset_lower_right_x, P3_lower[1] - rands.offset_lower_right_y]; // Second control point 82 | 83 | // now we generate the points for the upper eyelid 84 | let upper_eyelid_points = []; 85 | let upper_eyelid_points_left_control = []; 86 | let upper_eyelid_points_right_control = []; 87 | let upper_eyelid_left_control_point = [P0_upper[0] * (1 - rands.left_converge0) + P1_lower[0] * rands.left_converge0, P0_upper[1] * (1 - rands.left_converge0) + P1_lower[1] * rands.left_converge0]; 88 | let upper_eyelid_right_control_point = [P3_upper[0] * (1 - rands.right_converge0) + P2_lower[0] * rands.right_converge0, P3_upper[1] * (1 - rands.right_converge0) + P2_lower[1] * rands.right_converge0]; 89 | for (let t = 0; t < 100; t++) { 90 | upper_eyelid_points.push(cubicBezier(P0_upper, P1_upper, P2_upper, P3_upper, t / 100)); 91 | upper_eyelid_points_left_control.push(cubicBezier(upper_eyelid_left_control_point, P0_upper, P1_upper, P2_upper, t / 100)); 92 | upper_eyelid_points_right_control.push(cubicBezier(P1_upper, P2_upper, P3_upper, upper_eyelid_right_control_point, t / 100)); 93 | } 94 | 95 | for (let i = 0; i < 75; i++) { 96 | let weight = ((75.0 - i) / 75.0) ** 2 97 | upper_eyelid_points[i] = [upper_eyelid_points[i][0] * (1 - weight) + upper_eyelid_points_left_control[i + 25][0] * weight, upper_eyelid_points[i][1] * (1 - weight) + upper_eyelid_points_left_control[i + 25][1] * weight] 98 | upper_eyelid_points[i + 25] = [upper_eyelid_points[i + 25][0] * weight + upper_eyelid_points_right_control[i][0] * (1 - weight), upper_eyelid_points[i + 25][1] * weight + upper_eyelid_points_right_control[i][1] * (1 - weight)] 99 | } 100 | 101 | 102 | // now we generate the points for the upper eyelid 103 | let lower_eyelid_points = []; 104 | let lower_eyelid_points_left_control = []; 105 | let lower_eyelid_points_right_control = []; 106 | let lower_eyelid_left_control_point = [P0_lower[0] * (1 - rands.left_converge0) + P1_upper[0] * rands.left_converge0, P0_lower[1] * (1 - rands.left_converge0) + P1_upper[1] * rands.left_converge0]; 107 | let lower_eyelid_right_control_point = [P3_lower[0] * (1 - rands.right_converge1) + P2_upper[0] * rands.right_converge1, P3_lower[1] * (1 - rands.right_converge1) + P2_upper[1] * rands.right_converge1]; 108 | for (let t = 0; t < 100; t++) { 109 | lower_eyelid_points.push(cubicBezier(P0_lower, P1_lower, P2_lower, P3_lower, t / 100)); 110 | lower_eyelid_points_left_control.push(cubicBezier(lower_eyelid_left_control_point, P0_lower, P1_lower, P2_lower, t / 100)); 111 | lower_eyelid_points_right_control.push(cubicBezier(P1_lower, P2_lower, P3_lower, lower_eyelid_right_control_point, t / 100)); 112 | } 113 | 114 | for (let i = 0; i < 75; i++) { 115 | let weight = ((75.0 - i) / 75.0) ** 2 116 | lower_eyelid_points[i] = [lower_eyelid_points[i][0] * (1 - weight) + lower_eyelid_points_left_control[i + 25][0] * weight, lower_eyelid_points[i][1] * (1 - weight) + lower_eyelid_points_left_control[i + 25][1] * weight] 117 | lower_eyelid_points[i + 25] = [lower_eyelid_points[i + 25][0] * weight + lower_eyelid_points_right_control[i][0] * (1 - weight), lower_eyelid_points[i + 25][1] * weight + lower_eyelid_points_right_control[i][1] * (1 - weight)] 118 | } 119 | for (let i = 0; i < 100; i++) { 120 | lower_eyelid_points[i][1] = -lower_eyelid_points[i][1] 121 | upper_eyelid_points[i][1] = -upper_eyelid_points[i][1] 122 | } 123 | 124 | let eyeCenter = [upper_eyelid_points[50][0] / 2.0 + lower_eyelid_points[50][0] / 2.0, upper_eyelid_points[50][1] / 2.0 + lower_eyelid_points[50][1] / 2.0]; 125 | 126 | for (let i = 0; i < 100; i++) { 127 | // translate to center 128 | lower_eyelid_points[i][0] -= eyeCenter[0] 129 | lower_eyelid_points[i][1] -= eyeCenter[1] 130 | upper_eyelid_points[i][0] -= eyeCenter[0] 131 | upper_eyelid_points[i][1] -= eyeCenter[1] 132 | } 133 | eyeCenter = [0, 0]; 134 | 135 | // we switch the upper and lower eyelid points because in svg the bottom is y+ and top is y- 136 | return { upper: upper_eyelid_points, lower: lower_eyelid_points, center: [eyeCenter]} 137 | } 138 | 139 | export function generateBothEyes(width = 50) { 140 | let rands_left = generateEyeParameters(width) 141 | // Create a shallow copy of the object 142 | let rands_right = { ...rands_left }; 143 | 144 | // Iterate over the object's keys 145 | for (let key in rands_right) { 146 | // Check if the property value is a number 147 | if (typeof rands_right[key] === 'number') { 148 | // Add a random value to the number, for example, between -5 and 5 149 | rands_right[key] += randomFromInterval(-rands_right[key] / 2.0, rands_right[key] / 2.0); 150 | } 151 | } 152 | let left_eye = generateEyePoints(rands_left, width) 153 | let right_eye = generateEyePoints(rands_right, width) 154 | 155 | for (let key in left_eye) { 156 | if (typeof left_eye[key] === 'object') { 157 | for (let i = 0; i < left_eye[key].length; i++) { 158 | left_eye[key][i][0] = -left_eye[key][i][0] 159 | } 160 | } 161 | } 162 | return { left: left_eye, right: right_eye } 163 | } -------------------------------------------------------------------------------- /src/utils/face_shape.js: -------------------------------------------------------------------------------- 1 | function randomFromInterval(min, max) { 2 | // min and max included 3 | return Math.random() * (max - min) + min; 4 | } 5 | export function getEggShapePoints(a, b, k, segment_points) { 6 | // the function is x^2/a^2 * (1 + ky) + y^2/b^2 = 1 7 | var result = []; 8 | // var pointString = ""; 9 | for (var i = 0; i < segment_points; i++) { 10 | // x positive, y positive 11 | // first compute the degree 12 | var degree = 13 | (Math.PI / 2 / segment_points) * i + 14 | randomFromInterval( 15 | -Math.PI / 1.1 / segment_points, 16 | Math.PI / 1.1 / segment_points, 17 | ); 18 | var y = Math.sin(degree) * b; 19 | var x = 20 | Math.sqrt(((1 - (y * y) / (b * b)) / (1 + k * y)) * a * a) + 21 | randomFromInterval(-a / 200.0, a / 200.0); 22 | // pointString += x + "," + y + " "; 23 | result.push([x, y]); 24 | } 25 | for (var i = segment_points; i > 0; i--) { 26 | // x is negative, y is positive 27 | var degree = 28 | (Math.PI / 2 / segment_points) * i + 29 | randomFromInterval( 30 | -Math.PI / 1.1 / segment_points, 31 | Math.PI / 1.1 / segment_points, 32 | ); 33 | var y = Math.sin(degree) * b; 34 | var x = 35 | -Math.sqrt(((1 - (y * y) / (b * b)) / (1 + k * y)) * a * a) + 36 | randomFromInterval(-a / 200.0, a / 200.0); 37 | // pointString += x + "," + y + " "; 38 | result.push([x, y]); 39 | } 40 | for (var i = 0; i < segment_points; i++) { 41 | // x is negative, y is negative 42 | var degree = 43 | (Math.PI / 2 / segment_points) * i + 44 | randomFromInterval( 45 | -Math.PI / 1.1 / segment_points, 46 | Math.PI / 1.1 / segment_points, 47 | ); 48 | var y = -Math.sin(degree) * b; 49 | var x = 50 | -Math.sqrt(((1 - (y * y) / (b * b)) / (1 + k * y)) * a * a) + 51 | randomFromInterval(-a / 200.0, a / 200.0); 52 | // pointString += x + "," + y + " "; 53 | result.push([x, y]); 54 | } 55 | for (var i = segment_points; i > 0; i--) { 56 | // x is positive, y is negative 57 | var degree = 58 | (Math.PI / 2 / segment_points) * i + 59 | randomFromInterval( 60 | -Math.PI / 1.1 / segment_points, 61 | Math.PI / 1.1 / segment_points, 62 | ); 63 | var y = -Math.sin(degree) * b; 64 | var x = 65 | Math.sqrt(((1 - (y * y) / (b * b)) / (1 + k * y)) * a * a) + 66 | randomFromInterval(-a / 200.0, a / 200.0); 67 | // pointString += x + "," + y + " "; 68 | result.push([x, y]); 69 | } 70 | return result; 71 | } 72 | 73 | function findIntersectionPoints(radian, a, b) { 74 | if (radian < 0) { 75 | radian = 0; 76 | } 77 | if (radian > Math.PI / 2) { 78 | radian = Math.PI / 2; 79 | } 80 | // a is width, b is height 81 | // Slope of the line 82 | const m = Math.tan(radian); 83 | // check if radian is close to 90 degrees 84 | if (Math.abs(radian - Math.PI / 2) < 0.0001) { 85 | return { x: 0, y: b }; 86 | } 87 | // only checks the first quadrant 88 | const y = m * a; 89 | if (y < b) { 90 | // it intersects with the left side 91 | return { x: a, y: y }; 92 | } else { 93 | // it intersects with the top side 94 | // console.log(m); 95 | const x = b / m; 96 | // console.log(x, b); 97 | return { x: x, y: b }; 98 | } 99 | } 100 | 101 | export function generateRectangularFaceContourPoints(a, b, segment_points) { 102 | // a is width, b is height, segment_points is the number of points 103 | 104 | var result = []; 105 | for (var i = 0; i < segment_points; i++) { 106 | var degree = 107 | (Math.PI / 2 / segment_points) * i + 108 | randomFromInterval( 109 | -Math.PI / 11 / segment_points, 110 | Math.PI / 11 / segment_points, 111 | ); 112 | var intersection = findIntersectionPoints(degree, a, b); 113 | result.push([intersection.x, intersection.y]); 114 | } 115 | for (var i = segment_points; i > 0; i--) { 116 | // x is negative, y is positive 117 | var degree = 118 | (Math.PI / 2 / segment_points) * i + 119 | randomFromInterval( 120 | -Math.PI / 11 / segment_points, 121 | Math.PI / 11 / segment_points, 122 | ); 123 | var intersection = findIntersectionPoints(degree, a, b); 124 | result.push([-intersection.x, intersection.y]); 125 | } 126 | for (var i = 0; i < segment_points; i++) { 127 | // x is negative, y is negative 128 | // first compute the degree 129 | var degree = 130 | (Math.PI / 2 / segment_points) * i + 131 | randomFromInterval( 132 | -Math.PI / 11 / segment_points, 133 | Math.PI / 11 / segment_points, 134 | ); 135 | var intersection = findIntersectionPoints(degree, a, b); 136 | result.push([-intersection.x, -intersection.y]); 137 | } 138 | for (var i = segment_points; i > 0; i--) { 139 | // x is positive, y is negative 140 | // first compute the degree 141 | var degree = 142 | (Math.PI / 2 / segment_points) * i + 143 | randomFromInterval( 144 | -Math.PI / 11 / segment_points, 145 | Math.PI / 11 / segment_points, 146 | ); 147 | var intersection = findIntersectionPoints(degree, a, b); 148 | result.push([intersection.x, -intersection.y]); 149 | } 150 | return result; 151 | } 152 | 153 | export function generateFaceCountourPoints(numPoints = 100) { 154 | var faceSizeX0 = randomFromInterval(50, 100); 155 | var faceSizeY0 = randomFromInterval(70, 100); 156 | 157 | var faceSizeY1 = randomFromInterval(50, 80); 158 | var faceSizeX1 = randomFromInterval(70, 100); 159 | var faceK0 = 160 | randomFromInterval(0.001, 0.005) * (Math.random() > 0.5 ? 1 : -1); 161 | var faceK1 = 162 | randomFromInterval(0.001, 0.005) * (Math.random() > 0.5 ? 1 : -1); 163 | var face0TranslateX = randomFromInterval(-5, 5); 164 | var face0TranslateY = randomFromInterval(-15, 15); 165 | 166 | var face1TranslateY = randomFromInterval(-5, 5); 167 | var face1TranslateX = randomFromInterval(-5, 25); 168 | var eggOrRect0 = Math.random() > 0.1; 169 | var eggOrRect1 = Math.random() > 0.3; 170 | 171 | var results0 = eggOrRect0 172 | ? getEggShapePoints(faceSizeX0, faceSizeY0, faceK0, numPoints) 173 | : generateRectangularFaceContourPoints(faceSizeX0, faceSizeY0, numPoints); 174 | var results1 = eggOrRect1 175 | ? getEggShapePoints(faceSizeX1, faceSizeY1, faceK1, numPoints) 176 | : generateRectangularFaceContourPoints(faceSizeX1, faceSizeY1, numPoints); 177 | for (var i = 0; i < results0.length; i++) { 178 | results0[i][0] += face0TranslateX; 179 | results0[i][1] += face0TranslateY; 180 | results1[i][0] += face1TranslateX; 181 | results1[i][1] += face1TranslateY; 182 | } 183 | var results = []; 184 | let center = [0, 0]; 185 | for (var i = 0; i < results0.length; i++) { 186 | results.push([ 187 | results0[i][0] * 0.7 + 188 | results1[(i + results0.length / 4) % results0.length][1] * 0.3, 189 | results0[i][1] * 0.7 - 190 | results1[(i + results0.length / 4) % results0.length][0] * 0.3, 191 | ]); 192 | center[0] += results[i][0]; 193 | center[1] += results[i][1]; 194 | } 195 | center[0] /= results.length; 196 | center[1] /= results.length; 197 | // center the face 198 | for (var i = 0; i < results.length; i++) { 199 | results[i][0] -= center[0]; 200 | results[i][1] -= center[1]; 201 | } 202 | 203 | let width = results[0][0] - results[results.length / 2][0]; 204 | let height = 205 | results[results.length / 4][1] - results[(results.length * 3) / 4][1]; 206 | // add the first point to the end to close the shape 207 | results.push(results[0]); 208 | results.push(results[1]); 209 | // console.log(results); 210 | return { face: results, width: width, height: height, center: [0, 0] }; 211 | } 212 | -------------------------------------------------------------------------------- /src/utils/hair_lines.js: -------------------------------------------------------------------------------- 1 | function randomFromInterval(min, max) { 2 | // min and max included 3 | return Math.random() * (max - min) + min; 4 | } 5 | function factorial(n) { 6 | if (n <= 1) return 1; 7 | return n * factorial(n - 1); 8 | } 9 | 10 | function binomialCoefficient(n, k) { 11 | return factorial(n) / (factorial(k) * factorial(n - k)); 12 | } 13 | 14 | function calculateBezierPoint(t, controlPoints) { 15 | let x = 0, y = 0; 16 | const n = controlPoints.length - 1; 17 | 18 | for (let i = 0; i <= n; i++) { 19 | let binCoeff = binomialCoefficient(n, i); 20 | let a = Math.pow(1 - t, n - i); 21 | let b = Math.pow(t, i); 22 | x += binCoeff * a * b * controlPoints[i].x; 23 | y += binCoeff * a * b * controlPoints[i].y; 24 | } 25 | 26 | return [x, y]; 27 | } 28 | 29 | function computeBezierCurve(controlPoints, numberOfPoints) { 30 | let curve = []; 31 | for (let i = 0; i <= numberOfPoints; i++) { 32 | let t = i / numberOfPoints; 33 | let point = calculateBezierPoint(t, controlPoints); 34 | curve.push(point); 35 | } 36 | return curve; 37 | } 38 | 39 | export function generateHairLines0(faceCountour, numHairLines = 100) { 40 | var faceCountourCopy = faceCountour.slice(0, faceCountour.length - 2); 41 | var results = []; 42 | for (var i = 0; i < numHairLines; i++){ 43 | var numHairPoints = 20 + Math.floor(randomFromInterval(-5, 5)); 44 | // we generate some hair lines 45 | var hair_line = []; 46 | var index_offset = Math.floor(randomFromInterval(30, 140)); 47 | for (var j = 0; j < numHairPoints; j++){ 48 | hair_line.push({x: faceCountourCopy[(faceCountourCopy.length - (j + index_offset)) % faceCountourCopy.length][0], y:faceCountourCopy[(faceCountourCopy.length - (j + index_offset)) % faceCountourCopy.length][1]}); 49 | } 50 | var d0 = computeBezierCurve(hair_line, numHairPoints); 51 | hair_line = [] 52 | index_offset = Math.floor(randomFromInterval(30, 140)); 53 | for (var j = 0; j < numHairPoints; j++){ 54 | hair_line.push({x: faceCountourCopy[(faceCountourCopy.length - (-j + index_offset)) % faceCountourCopy.length][0], y:faceCountourCopy[(faceCountourCopy.length - (-j + index_offset)) % faceCountourCopy.length][1]}); 55 | } 56 | var d1 = computeBezierCurve(hair_line, numHairPoints); 57 | var d = []; 58 | for (var j = 0; j < numHairPoints; j++){ 59 | d.push([d0[j][0] * (j * (1 / numHairPoints)) ** 2 + d1[j][0] * (1 - (j * (1 / numHairPoints)) ** 2), d0[j][1] * (j * (1 / numHairPoints)) ** 2 + d1[j][1] * (1 - (j * (1 / numHairPoints)) ** 2)]); 60 | } 61 | 62 | results.push(d); 63 | } 64 | return results; 65 | } 66 | export function generateHairLines1(faceCountour, numHairLines = 100) { 67 | var faceCountourCopy = faceCountour.slice(0, faceCountour.length - 2); 68 | var results = []; 69 | for (var i = 0; i < numHairLines; i++){ 70 | var numHairPoints = 20 + Math.floor(randomFromInterval(-5, 5)); 71 | // we generate some hair lines 72 | var hair_line = []; 73 | var index_start = Math.floor(randomFromInterval(20, 160)); 74 | hair_line.push({x: faceCountourCopy[(faceCountourCopy.length - index_start) % faceCountourCopy.length][0], y:faceCountourCopy[(faceCountourCopy.length - index_start) % faceCountourCopy.length][1]}); 75 | 76 | for (var j = 1; j < numHairPoints + 1; j++){ 77 | index_start = Math.floor(randomFromInterval(20, 160)); 78 | hair_line.push({x: faceCountourCopy[(faceCountourCopy.length - index_start) % faceCountourCopy.length][0], y:faceCountourCopy[(faceCountourCopy.length - index_start) % faceCountourCopy.length][1]}); 79 | } 80 | var d = computeBezierCurve(hair_line, numHairPoints); 81 | 82 | results.push(d); 83 | } 84 | return results; 85 | } 86 | 87 | 88 | export function generateHairLines2(faceCountour, numHairLines = 100) { 89 | 90 | var faceCountourCopy = faceCountour.slice(0, faceCountour.length - 2); 91 | var results = []; 92 | var pickedIndices = []; 93 | for (var i = 0; i < numHairLines; i++){ 94 | pickedIndices.push(Math.floor(randomFromInterval(10, 180))); 95 | } 96 | pickedIndices.sort(); 97 | for (var i = 0; i < numHairLines; i++){ 98 | var numHairPoints = 20 + Math.floor(randomFromInterval(-5, 5)); 99 | // we generate some hair lines 100 | var hair_line = []; 101 | var index_offset = pickedIndices[i]; 102 | var lower = randomFromInterval(0.8 , 1.4); 103 | var reverse = Math.random() > 0.5 ? 1 : -1; 104 | for (var j = 0; j < numHairPoints; j++){ 105 | var powerscale = randomFromInterval(0.1, 3); 106 | var portion = (1 - (j / numHairPoints) ** powerscale) * (1 - lower) + lower; 107 | hair_line.push({x: faceCountourCopy[(faceCountourCopy.length - (reverse * j + index_offset)) % faceCountourCopy.length][0] * portion, y:faceCountourCopy[(faceCountourCopy.length - (reverse * j + index_offset)) % faceCountourCopy.length][1] * portion}); 108 | } 109 | var d = computeBezierCurve(hair_line, numHairPoints); 110 | if (Math.random() > 0.7) d = d.reverse(); 111 | if (results.length == 0){ 112 | results.push(d); 113 | continue; 114 | } 115 | var lastHairPoint = results[results.length - 1][results[results.length - 1].length - 1]; 116 | var lastPointsDistance = Math.sqrt((d[0][0] - lastHairPoint[0]) ** 2 + (d[0][1] - lastHairPoint[1]) ** 2); 117 | if (Math.random() > 0.5 && lastPointsDistance < 100){ 118 | results[results.length - 1] = results[results.length - 1].concat(d); 119 | }else{ 120 | results.push(d); 121 | } 122 | } 123 | return results; 124 | } 125 | 126 | export function generateHairLines3(faceCountour, numHairLines = 100) { 127 | var faceCountourCopy = faceCountour.slice(0, faceCountour.length - 2); 128 | var results = []; 129 | var pickedIndices = []; 130 | for (var i = 0; i < numHairLines; i++){ 131 | pickedIndices.push(Math.floor(randomFromInterval(10, 180))); 132 | } 133 | pickedIndices.sort(); 134 | var splitPoint = Math.floor(randomFromInterval(0, 200)); 135 | for (var i = 0; i < numHairLines; i++){ 136 | var numHairPoints = 30 + Math.floor(randomFromInterval(-8, 8)); 137 | // we generate some hair lines 138 | var hair_line = []; 139 | var index_offset = pickedIndices[i]; 140 | var lower = randomFromInterval(1 , 2.3); 141 | if (Math.random() > 0.9) lower = randomFromInterval(0 , 1.); 142 | var reverse = index_offset > splitPoint ? 1 : -1; 143 | for (var j = 0; j < numHairPoints; j++){ 144 | var powerscale = randomFromInterval(0.1, 3); 145 | var portion = (1 - (j / (numHairPoints)) ** powerscale) * (1 - lower) + lower; 146 | hair_line.push({x: faceCountourCopy[(faceCountourCopy.length - (reverse * j * 2 + index_offset)) % faceCountourCopy.length][0] * portion, y:faceCountourCopy[(faceCountourCopy.length - (reverse * j * 2 + index_offset)) % faceCountourCopy.length][1]}); 147 | } 148 | var d = computeBezierCurve(hair_line, numHairPoints); 149 | results.push(d); 150 | } 151 | return results; 152 | } -------------------------------------------------------------------------------- /src/utils/mouth_shape.js: -------------------------------------------------------------------------------- 1 | function randomFromInterval(min, max) { 2 | // min and max included 3 | return Math.random() * (max - min) + min; 4 | } 5 | function cubicBezier(P0, P1, P2, P3, t) { 6 | var x = (1 - t) ** 3 * P0[0] + 3 * (1 - t) ** 2 * t * P1[0] + 3 * (1 - t) * t ** 2 * P2[0] + t ** 3 * P3[0]; 7 | var y = (1 - t) ** 3 * P0[1] + 3 * (1 - t) ** 2 * t * P1[1] + 3 * (1 - t) * t ** 2 * P2[1] + t ** 3 * P3[1]; 8 | return [x, y]; 9 | } 10 | function getEggShapePoints(a, b, k, segment_points) { 11 | // the function is x^2/a^2 * (1 + ky) + y^2/b^2 = 1 12 | var result = []; 13 | // var pointString = ""; 14 | for (var i = 0; i < segment_points; i++) { 15 | // x positive, y positive 16 | // first compute the degree 17 | var degree = 18 | (Math.PI / 2 / segment_points) * i + 19 | randomFromInterval( 20 | -Math.PI / 1.1 / segment_points, 21 | Math.PI / 1.1 / segment_points 22 | ); 23 | var y = Math.sin(degree) * b; 24 | var x = 25 | Math.sqrt(((1 - (y * y) / (b * b)) / (1 + k * y)) * a * a) + 26 | randomFromInterval(-a / 200.0, a / 200.0); 27 | // pointString += x + "," + y + " "; 28 | result.push([x, y]); 29 | } 30 | for (var i = segment_points; i > 0; i--) { 31 | // x is negative, y is positive 32 | var degree = 33 | (Math.PI / 2 / segment_points) * i + 34 | randomFromInterval( 35 | -Math.PI / 1.1 / segment_points, 36 | Math.PI / 1.1 / segment_points 37 | ); 38 | var y = Math.sin(degree) * b; 39 | var x = 40 | -Math.sqrt(((1 - (y * y) / (b * b)) / (1 + k * y)) * a * a) + 41 | randomFromInterval(-a / 200.0, a / 200.0); 42 | // pointString += x + "," + y + " "; 43 | result.push([x, y]); 44 | } 45 | for (var i = 0; i < segment_points; i++) { 46 | // x is negative, y is negative 47 | var degree = 48 | (Math.PI / 2 / segment_points) * i + 49 | randomFromInterval( 50 | -Math.PI / 1.1 / segment_points, 51 | Math.PI / 1.1 / segment_points 52 | ); 53 | var y = -Math.sin(degree) * b; 54 | var x = 55 | -Math.sqrt(((1 - (y * y) / (b * b)) / (1 + k * y)) * a * a) + 56 | randomFromInterval(-a / 200.0, a / 200.0); 57 | // pointString += x + "," + y + " "; 58 | result.push([x, y]); 59 | } 60 | for (var i = segment_points; i > 0; i--) { 61 | // x is positive, y is negative 62 | var degree = 63 | (Math.PI / 2 / segment_points) * i + 64 | randomFromInterval( 65 | -Math.PI / 1.1 / segment_points, 66 | Math.PI / 1.1 / segment_points 67 | ); 68 | var y = -Math.sin(degree) * b; 69 | var x = 70 | Math.sqrt(((1 - (y * y) / (b * b)) / (1 + k * y)) * a * a) + 71 | randomFromInterval(-a / 200.0, a / 200.0); 72 | // pointString += x + "," + y + " "; 73 | result.push([x, y]); 74 | } 75 | return result; 76 | } 77 | 78 | export function generateMouthShape0(faceCountour, faceHeight, faceWidth) { 79 | // the first one is a a big smile U shape 80 | var faceCountourCopy = faceCountour.slice(0, faceCountour.length - 2); 81 | // choose one point on face at bottom side 82 | var mouthRightY = randomFromInterval(faceHeight / 7, faceHeight / 3.5) 83 | var mouthLeftY = randomFromInterval(faceHeight / 7, faceHeight / 3.5) 84 | var mouthRightX = randomFromInterval(faceWidth / 10, faceWidth / 2) 85 | var mouthLeftX = -mouthRightX + randomFromInterval(-faceWidth / 20, faceWidth / 20) 86 | var mouthRight = [mouthRightX, mouthRightY] 87 | var mouthLeft = [mouthLeftX, mouthLeftY] 88 | 89 | var controlPoint0 = [randomFromInterval(0, mouthRightX), randomFromInterval(mouthLeftY + 5, faceHeight / 1.5)] 90 | var controlPoint1 = [randomFromInterval(mouthLeftX, 0), randomFromInterval(mouthLeftY + 5, faceHeight / 1.5)] 91 | 92 | var mouthPoints = [] 93 | for (var i = 0; i < 1; i += 0.01) { 94 | mouthPoints.push(cubicBezier(mouthLeft, controlPoint1, controlPoint0, mouthRight, i)) 95 | } 96 | if (Math.random() > 0.5) { 97 | for (var i = 0; i < 1; i += 0.01) { 98 | mouthPoints.push(cubicBezier(mouthRight, controlPoint0, controlPoint1, mouthLeft, i)) 99 | } 100 | }else{ 101 | var y_offset_portion = randomFromInterval(0, 0.8); 102 | for (var i = 0; i < 100; i += 1) { 103 | mouthPoints.push([mouthPoints[99][0] * (1 - i / 100.0) + mouthPoints[0][0] * i / 100.0, (mouthPoints[99][1] * (1 - i / 100.0) + mouthPoints[0][1] * i / 100.0) * (1 - y_offset_portion) + mouthPoints[99 - i][1] * y_offset_portion]) 104 | } 105 | } 106 | return mouthPoints; 107 | } 108 | 109 | export function generateMouthShape1(faceCountour, faceHeight, faceWidth) { 110 | // the first one is a a big smile U shape 111 | var faceCountourCopy = faceCountour.slice(0, faceCountour.length - 2); 112 | // choose one point on face at bottom side 113 | var mouthRightY = randomFromInterval(faceHeight / 7, faceHeight / 4) 114 | var mouthLeftY = randomFromInterval(faceHeight / 7, faceHeight / 4) 115 | var mouthRightX = randomFromInterval(faceWidth / 10, faceWidth / 2) 116 | var mouthLeftX = -mouthRightX + randomFromInterval(-faceWidth / 20, faceWidth / 20) 117 | var mouthRight = [mouthRightX, mouthRightY] 118 | var mouthLeft = [mouthLeftX, mouthLeftY] 119 | 120 | var controlPoint0 = [randomFromInterval(0, mouthRightX), randomFromInterval(mouthLeftY + 5, faceHeight / 1.5)] 121 | var controlPoint1 = [randomFromInterval(mouthLeftX, 0), randomFromInterval(mouthLeftY + 5, faceHeight / 1.5)] 122 | 123 | var mouthPoints = [] 124 | for (var i = 0; i < 1; i += 0.01) { 125 | mouthPoints.push(cubicBezier(mouthLeft, controlPoint1, controlPoint0, mouthRight, i)) 126 | } 127 | 128 | var center = [(mouthRight[0] + mouthLeft[0]) / 2, mouthPoints[25][1] / 2 + mouthPoints[75][1] / 2]; 129 | if (Math.random() > 0.5) { 130 | for (var i = 0; i < 1; i += 0.01) { 131 | mouthPoints.push(cubicBezier(mouthRight, controlPoint0, controlPoint1, mouthLeft, i)) 132 | } 133 | }else{ 134 | var y_offset_portion = randomFromInterval(0, 0.8); 135 | for (var i = 0; i < 100; i += 1) { 136 | mouthPoints.push([mouthPoints[99][0] * (1 - i / 100.0) + mouthPoints[0][0] * i / 100.0, (mouthPoints[99][1] * (1 - i / 100.0) + mouthPoints[0][1] * i / 100.0) * (1 - y_offset_portion) + mouthPoints[99 - i][1] * y_offset_portion]) 137 | } 138 | } 139 | // translate to center 140 | for (var i = 0; i < mouthPoints.length; i++) { 141 | mouthPoints[i][0] -= center[0] 142 | mouthPoints[i][1] -= center[1] 143 | // rotate 180 degree 144 | mouthPoints[i][1] = -mouthPoints[i][1] 145 | // scale smaller 146 | mouthPoints[i][0] = mouthPoints[i][0] * 0.6 147 | mouthPoints[i][1] = mouthPoints[i][1] * 0.6 148 | // translate back 149 | mouthPoints[i][0] += center[0] 150 | mouthPoints[i][1] += center[1] * 0.8 151 | } 152 | return mouthPoints; 153 | } 154 | 155 | export function generateMouthShape2(faceCountour, faceHeight, faceWidth) { 156 | // generate a random center 157 | var center = [randomFromInterval(-faceWidth / 8, faceWidth / 8), randomFromInterval(faceHeight / 4, faceHeight / 2.5)] 158 | 159 | var mouthPoints = getEggShapePoints(randomFromInterval(faceWidth / 4, faceWidth / 10), randomFromInterval(faceHeight / 10, faceHeight / 20), 0.001, 50); 160 | var randomRotationDegree = randomFromInterval(-Math.PI / 9.5, Math.PI / 9.5) 161 | for (var i = 0; i < mouthPoints.length; i++) { 162 | // rotate the point 163 | var x = mouthPoints[i][0] 164 | var y = mouthPoints[i][1] 165 | mouthPoints[i][0] = x * Math.cos(randomRotationDegree) - y * Math.sin(randomRotationDegree) 166 | mouthPoints[i][1] = x * Math.sin(randomRotationDegree) + y * Math.cos(randomRotationDegree) 167 | mouthPoints[i][0] += center[0] 168 | mouthPoints[i][1] += center[1] 169 | } 170 | return mouthPoints; 171 | } -------------------------------------------------------------------------------- /src/views/FaceGenerator.vue: -------------------------------------------------------------------------------- 1 | 274 | 275 | 663 | 664 | 708 | -------------------------------------------------------------------------------- /vue.config.js: -------------------------------------------------------------------------------- 1 | const { defineConfig } = require('@vue/cli-service') 2 | module.exports = defineConfig({ 3 | transpileDependencies: true, 4 | publicPath: process.env.NODE_ENV === 'production' 5 | ? '/ugly-avatar/' 6 | : '/' 7 | }) 8 | --------------------------------------------------------------------------------