项目作者: MatthewCallis

项目描述 :
Library for showing Gravatars or generating user avatars.
高级语言: JavaScript
项目地址: git://github.com/MatthewCallis/avatar.git
创建时间: 2014-12-18T22:43:06Z
项目社区:https://github.com/MatthewCallis/avatar

开源协议:MIT License

下载


view on npm
npm module downloads
Build Status
Coverage Status
Tree-Shaking Support
Dependency Count
Minified + GZip
Minified

Avatar

Avatar is a JavaScript library & React component for showing Gravatars or generating user avatars.

Examples

There are several examples on the website.

  1. import Avatar from 'avatar-initials';
  2. // or
  3. const Avatar = require('avatar-initials');
  4. // Add an avatar to an <img>
  5. const avatar = Avatar.from(document.getElementById('avatar'), {
  6. 'useGravatar': false,
  7. 'initials': 'MC',
  8. });
  9. // If you just want the URL / string:
  10. const github_avatar_url = Avatar.githubAvatar({
  11. id: '12345'
  12. });
  13. const gravatar_url_from_email = Avatar.gravatarUrl({
  14. email: 'test@test.test'
  15. });
  16. const gravatar_url_from_hash = Avatar.gravatarUrl({
  17. hash: '12929016fffb0b3af98bc440acf0bfe2'
  18. });
  19. const initial_png = Avatar.initialAvatar({
  20. initials: 'MC',
  21. initial_fg: '#888888',
  22. initial_bg: '#f4f6f7',
  23. initial_size: 0, // Defaults to height / 2
  24. initial_weight: 100,
  25. initial_font_family: "'Lato', 'Lato-Regular', 'Helvetica Neue'",
  26. });

This example will render an avatar with my initials “MC” as the image.

Avatar Example

A simple React example:

  1. import { AvatarComponent } from 'avatar-initials';
  2. export default function Example() {
  3. // ...
  4. return (
  5. <div className={`w-full flex self-center items-center justify-between relative ${classes}`}>
  6. <button type="button" onClick={() => setShowMenu(true)} title="Open Menu">
  7. <AvatarComponent
  8. classes="rounded-full"
  9. useGravatar={false}
  10. size={44}
  11. primarySource={currentUser.Avatar}
  12. color="#000000"
  13. background="#f1f1f1"
  14. fontSize={16}
  15. fontWeight={400}
  16. offsetY={24}
  17. initials={`${currentUser.FirstName[0]}${currentUser.LastName[0]}`}
  18. ></AvatarComponent>
  19. </button>
  20. </div>
  21. );
  22. }

Options

Avatar is highly customizable and most options are self explanatory:

  1. {
  2. primarySource: '', // A source image to be used by default before attempting any other sources.
  3. fallbackImage: '', // URL or Data URI used when no initials are provided and not using Gravatars.
  4. size: 80, // Size in pixels, fallback for hidden images and Gravatar
  5. setSourceCallback: () => {}, // Callback called when image source is set (useful to cache avatar sources provided by third parties such as Gravatar)
  6. // Initial Avatars Specific
  7. initials: '', // Initials to be used
  8. color: '#888888', // Text Color
  9. background: '#f4f6f7', // Background Color
  10. fontSize: 0, // Text Size in pixels
  11. fontWeight: 100, // Font weight (numeric value for light, bold, etc.)
  12. fontFamily: "'Lato', 'Lato-Regular', 'Helvetica Neue'",
  13. offsetX: undefined, // Text X position in pixels, defaults to: width / 2
  14. offsetY: undefined, // Text Y position in pixels, defaults to: height / 2
  15. width: undefined, // The width of the output image, size is used it not provided
  16. height: undefined, // The height of the output image, size is used if not provided
  17. // Gravatar Specific
  18. useGravatar: true, // Allow Gravatars as source
  19. useGravatarFallback: false, // Use Gravatars fallback, not fallbackImage
  20. hash: '', // Precalculated MD5 string of an email address
  21. email: '', // Email used for the Gravatar
  22. fallback: 'mm', // Fallback Type
  23. rating: 'x', // Gravatar Rating
  24. forcedefault: false, // Force Gravatar Defaults
  25. // GitHub Specific
  26. githubId: null, // GitHub User ID.
  27. }

Installation

  1. npm install --save avatar-initials

or copy the minified build from browser/

  1. <script src="browser/avatar.js"></script>

The browser build is built with the following @babel/preset-env targets:

  1. {
  2. "android": "109",
  3. "chrome": "109",
  4. "edge": "109",
  5. "firefox": "109",
  6. "ios": "16.2",
  7. "opera": "92",
  8. "safari": "16.2",
  9. "samsung": "19"
  10. }

jQuery Wrapper for Avatar

  1. if (typeof jQuery !== 'undefined') {
  2. jQuery.fn.avatar = function avatar(options) {
  3. return this.each(() => {
  4. if (!jQuery.data(this, 'plugin_avatar')) {
  5. jQuery.data(this, 'plugin_avatar', new Avatar(this, options));
  6. }
  7. });
  8. };
  9. }

Migrating to v6

The settings have changed keys and some new logic was introduced, the logic should not interefere but the keys will need to be updated:

  1. initial_fg => color
  2. initial_bg => background
  3. initial_size => fontSize
  4. initial_weight => fontWeight
  5. initial_font_family => fontFamily
  6. allowGravatarFallback => useGravatarFallback
  7. github_id => github_id

Thanks

Pretty styles and design support kindly provided by Andrew Crocker.
Sun Knudsen for providing a PR with setSourceCallback.

License

Avatar is MIT licensed.