This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathhabitica-avatar.js
73 lines (56 loc) · 2.1 KB
/
habitica-avatar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict'
var findS3Src = require('./lib/find-s3-src')
var addLayer = require('./lib/add-layer')
var isHabitica = require('./lib/is-habitica')
var Habitica = require('habitica')
var api = new Habitica()
var CHARACTER_SPRITE_NODES = require('./lib/character-sprites-config')
function habiticaAvatar (options) {
var user = options.user
var container = options.container
var ignore = options.ignore || {}
var appearance = user.preferences
var useClassMode = !options.forceImageMode && Boolean(isHabitica() || options.forceClassMode)
var avatarContainer = document.createElement('div')
var characterSprites = document.createElement('div')
avatarContainer.style.height = '147px'
avatarContainer.style.width = '140px'
avatarContainer.style.position = 'relative'
avatarContainer.style.boxSizing = 'border-box'
avatarContainer.style.imageRendering = 'pixelated'
if (!user.items.currentMount || ignore.mount) {
avatarContainer.style.paddingTop = '24.5px'
}
if (appearance.background && !ignore.background) {
if (useClassMode) {
avatarContainer.classList.add('background_' + appearance.background)
} else {
avatarContainer.style.backgroundImage = 'url("' + findS3Src('background_' + appearance.background) + '")'
}
}
characterSprites.style.margin = '0 auto 0 24px'
characterSprites.style.width = '90px'
characterSprites.style.height = '90px'
CHARACTER_SPRITE_NODES.forEach(addLayer(characterSprites, {
user: options.user,
ignore: options.ignore,
forceEquipment: options.forceEquipment,
forceCostume: options.forceCostume,
useClassMode: useClassMode
}))
avatarContainer.appendChild(characterSprites)
if (typeof container === 'string') {
container = document.querySelector(container)
}
if (container) {
container.appendChild(avatarContainer)
}
return avatarContainer
}
habiticaAvatar.fromUserId = function (userId, options) {
return api.get('/members/' + userId).then(function (response) {
var config = Object.assign({}, options, { user: response.data })
return habiticaAvatar(config)
})
}
module.exports = habiticaAvatar