Avatar generated by name text based on svg.
Fill <svg>
as data uri into the <img>
src, no added element.
如果用户没有个性头像,就填充用户名生成的<svg>
头像。
程序会在 <img src>
上填充data URI,没有额外添加元素。
npm install namedavatar --save
namedavatar is a UMD module, support Browser, Requirejs, Vue2 directive, miniprogram(小程序).
NOTE that the
<img>
width
andborder-radius: 100%
requires you additional settings, the program is not set.
<img id="avatar1" src="./invalid.jpg" alt="Tom Hanks" width="40" style="border-radius: 100%">
<script src="/dist/namedavatar.min.js"></script>
<!-- also support requirejs
<script>
requirejs.config({
paths: {
namedavatar: '/dist/namedavatar'
}
})
requirejs('namedavatar', function(namedavatar){
// ...
})
</script>
-->
<script>
namedavatar.config({
nameType: 'initials'
})
// fill single <img>
var img = document.getElementById('avatar1')
namedavatar.setImg(img, img.alt)
// fill multi <img>
var imgs = document.querySelectorAll('img[data-name]')
namedavatar.setImgs(imgs, 'data-name')
</script>
if img.src
invalid, img#avatar1
will be:
<img id="avatar1" src="data:image/svg+xml,<svg xmlns="https://www.w3.org/2000/svg" width="32" height="32"><rect fill="#9C27B0" x="0" y="0" width="100%" height="100%"></rect><text fill="#FFF" x="50%" y="50%" text-anchor="middle" alignment-baseline="central" font-size="16" font-family="Verdana, Geneva, sans-serif">Hanks</text></svg>">
support like miniprogram(小程序)
import namedavatar from 'namedavatar'
// create svg html string without DOM
const svg = namedavatar.getSVGString('李连杰')
const uri = namedavatar.getDataURI(svg)
console.log(uri)
// data:image/svg+xml,%3Csvg%20...
online example main.js
import { directive } from 'namedavatar/vue'
// register v-avatar="Tom Hanks", show firstName (default)
Vue.directive('avatar', directive);
// or set options and same as above
import namedavatarVue from 'namedavatar/vue'
// register v-avatar="Tom Hanks", show lastName
Vue.use(namedavatarVue, {
nameType: 'lastName'
})
in vue template
<template>
<img v-avatar="'Tom Hanks'" width="36"/>
</template>
filed | type | default | description |
---|---|---|---|
nameType | string | 'firstName' |
pick from: firstName , lastName , initials |
fontFamily | string | 'Verdana, Geneva, sans-serif' |
font family |
backgroundColors | Array | Material Design colors *-500 | random background color list |
textColor | string | '#FFF' |
name text color |
minFontSize | number | 8 |
min font size limit |
maxFontSize | number | 16 |
max font size limit |
name
the full name valueoptions
extended options
return <svg>
node, get string by svg.outerHTML
added at 1.1.0 version, special for without DOM like miniprogram(小程序)
name
the full name valueoptions
extended options
return <svg>
html string
img
<img>
itemname
the full name value
create svg by name
, and fill to <img src="data:image/svg+xml,<svg>...">
imgs
<img>
listattr
pick full name value from special attr, eg'alt'
,'data-name'
create svg by attr
value, batch processing setImg()
html
<svg>
node html string
get data uri (rfc2397) of svg html
- IE > 8 (based on svg)
- Continuous improvement, welcome review and suggest
npm install -g watchify
npm run dev
npm install -g browserify uglifyjs
# build UMD bundle and minify
npm run build && npm run minify
# build for vue
npm run build:vue
npm install -g mocha
npm run test