-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.js
More file actions
90 lines (69 loc) · 2.35 KB
/
profile.js
File metadata and controls
90 lines (69 loc) · 2.35 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { h } from './lib/h.js'
import { apds } from './apds.js'
export const profile = async () => {
const div = h('div')
const avatarImg = await apds.visual(await apds.pubkey())
const existingImage = await apds.get('image')
if (existingImage) { avatarImg.src = await apds.get(existingImage)}
avatarImg.style = 'height: 30px; width: 30px; float: left; margin-right: 5px; object-fit: cover;'
avatarImg.onclick = () => {uploader.click()}
const uploader = h('input', { type: 'file', style: 'display: none;'})
uploader.addEventListener('change', (e) => {
const file = e.target.files[0]
const reader = new FileReader()
reader.onload = (e) => {
const canvas = document.createElement("canvas")
const ctx = canvas.getContext("2d")
const img = new Image()
img.onload = async () => {
const size = 256
if (img.width > size || img.height > size) {
const width = img.width
const height = img.height
let cropWidth
let cropHeight
if (width > height) {
cropWidth = size
cropHeight = cropWidth * (height / width)
} else {
cropHeight = size
cropWidth = cropHeight * (width / height)
}
canvas.width = cropWidth
canvas.height = cropHeight
ctx.drawImage(img, 0, 0, width, height, 0, 0, cropWidth, cropHeight)
const croppedImage = canvas.toDataURL()
avatarImg.src = croppedImage
const hash = await apds.make(croppedImage)
await apds.put('image', hash)
} else {
const croppedImage = canvas.toDataURL()
avatarImg.src = img.src
const hash = await apds.make(img.src)
await apds.put('image', hash)
}
}
img.src = e.target.result
}
reader.readAsDataURL(file)
})
div.appendChild(uploader)
div.appendChild(avatarImg)
div.appendChild(h('div', [await apds.pubkey()]))
const name = await apds.get('name')
const namer = h('input', {
placeholder: name || 'Name yourself'
})
const namerDiv = h('div', [
namer,
h('button', {onclick: async () => {
if (namer.value) {
namer.placeholder = namer.value
await apds.put('name', namer.value)
namer.value = ''
}
}}, ['Save'])
])
div.appendChild(namerDiv)
return div
}