char-to-avatar
Advanced tools
Comparing version 0.1.0 to 0.1.1
@@ -13,6 +13,9 @@ // **Github:** https://github.com/teambition/char-to-avatar | ||
else { | ||
var character = this.path.slice(1, 2) || 'T' | ||
console.log(this.path, character) | ||
var avatar = new CharToAvatar(character, 'circle', '#fff', 'rgba(255, 0, 0, 0.8)') | ||
var character = decodeURIComponent(this.path).slice(1, 2) || 't' | ||
var avatar = new CharToAvatar(character, { | ||
shape: 'circle', | ||
backgroundColor: 'rgba(255, 0, 0, 0.8)' | ||
}) | ||
var dataURL = yield avatar.toDataURL() | ||
console.log(dataURL.length) | ||
this.type = 'html' | ||
@@ -19,0 +22,0 @@ this.body = `<img src="${dataURL}" />` |
21
index.js
@@ -10,17 +10,20 @@ // **Github:** https://github.com/teambition/char-to-avatar | ||
module.exports = CharToAvatar | ||
CharToAvatar.font = '120px \'Microsoft Yahei\'' | ||
CharToAvatar.font = '60px \'Microsoft Yahei\'' | ||
CharToAvatar.textAlign = 'center' | ||
CharToAvatar.textBaseline = 'middle' | ||
CharToAvatar.textFactor = 0.95 | ||
function CharToAvatar (character, shape, charColor, backgroundColor, size) { | ||
size = size > 0 ? +size : 200 | ||
function CharToAvatar (character, options) { | ||
options = options || {} | ||
var size = options.size > 0 ? +options.size : 100 | ||
this.canvas = new Canvas(size, size) | ||
var ctx = this.canvas.getContext('2d') | ||
ctx.fillStyle = backgroundColor | ||
ctx.fillStyle = options.backgroundColor || '#383838' | ||
var radius = size / 2 | ||
switch (shape) { | ||
switch (options.shape) { | ||
case 'circle': | ||
ctx.beginPath() | ||
ctx.arc(radius, radius, radius, 0, 2 * Math.PI, true) | ||
ctx.arc(radius, radius, radius, 0, Math.PI * 2, true) | ||
ctx.fill() | ||
@@ -32,3 +35,3 @@ break | ||
ctx.fillStyle = charColor | ||
ctx.fillStyle = options.color || '#fff' | ||
ctx.font = CharToAvatar.font | ||
@@ -40,4 +43,4 @@ ctx.textAlign = CharToAvatar.textAlign | ||
var fix = radius * 0.9 | ||
ctx.fillText(character, fix, fix, fix * 2) | ||
var fix = Math.floor(radius * CharToAvatar.textFactor) | ||
ctx.fillText(character, Math.floor(fix * CharToAvatar.textFactor), fix, fix) | ||
} | ||
@@ -44,0 +47,0 @@ |
@@ -7,3 +7,3 @@ { | ||
], | ||
"version": "0.1.0", | ||
"version": "0.1.1", | ||
"main": "index.js", | ||
@@ -22,3 +22,3 @@ "repository": { | ||
"dependencies": { | ||
"canvas": "^1.2.3" | ||
"canvas": "~1.2.3" | ||
}, | ||
@@ -25,0 +25,0 @@ "devDependencies": { |
@@ -10,3 +10,3 @@ char-to-avatar | ||
- Cairo: http://cairographics.org/ | ||
- Cairo: http://cairographics.org/ (libpng giflib freetype libpixman cairo) | ||
@@ -23,2 +23,34 @@ ## Demo | ||
```js | ||
var CharToAvatar = require('char-to-avatar') | ||
``` | ||
### new CharToAvatar(character[, options]) | ||
- `character`: {String}, a character to render. | ||
- `options.color`: {String}, character color, default to `'#ffffff'`. | ||
- `options.backgroundColor`: {String}, background color, default to `'#383838'`. | ||
- `options.shape`: {String}, `'square'` or `'circle'`,avatar shape, default to `'square'`. | ||
- `options.size`: {Number}, avatar size, default to `100`. | ||
### CharToAvatar.prototype.pngStream() | ||
Return a png image stream. | ||
### CharToAvatar.prototype.jpegStream([quality]) | ||
Return a jpeg stream. | ||
- `quality`: {Number}, `0 - 100`, jpeg quality, default is `85`. | ||
### CharToAvatar.prototype.toBuffer() | ||
Return a thunk function that guarantee a png image buffer. | ||
### CharToAvatar.prototype.toDataURL([mime]) | ||
Return a thunk function that guarantee a image data string. | ||
- `mime`: {String}, mime type, default is `'image/png'`. | ||
[npm-url]: https://npmjs.org/package/char-to-avatar | ||
@@ -25,0 +57,0 @@ [npm-image]: http://img.shields.io/npm/v/char-to-avatar.svg |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8855
10
84
59
+ Addedcanvas@1.2.11(transitive)
- Removedcanvas@1.6.13(transitive)
Updatedcanvas@~1.2.3