Last active
October 4, 2019 00:53
-
-
Save kimihito/5e003daad409e4688ab04798acd94373 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
= image_tag '', class: 'avatar width-full height-full', data: { controller: 'avatar', 'avatar-seed': user.name } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as Stimulus from 'stimulus' | |
import Avatars from '@dicebear/avatars' | |
import sprites from '@dicebear/avatars-identicon-sprites' | |
export default class extends Stimulus.Controller { | |
public connect() { | |
if (!(this.element instanceof HTMLImageElement)) return | |
if (this.element.getAttribute('src')) return | |
const seed: string = this.data.get('seed') | |
const avatars = new Avatars(sprites()) | |
const svg = avatars.create(seed) | |
this.element.setAttribute('src', `data:image/svg+xml, ${encodeURIComponent(svg)}`) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment