7. In the JavaScript file, instantiate the library and call createGenerator(). The generator should
appear on the page.
const avatar = new AvatarMaker();
avatar.createGenerator("#generator-container");
API
.createGenerator(domElement)
Creates a generator in the domElement
specified.
.hide(featureName, isShowing)
Hides feature specified by featureName in the
featureName specified. isShowing is true
by default. If set to false, the feature will not be shown in the generator.
Acceptable arguments for featureName:
"base"
"hair"
"eyes"
"nose"
"mouth"
"facialHair"
"clothes"
"accessories"
"glasses"
.onSave(function)
The developer can specify what they would like to do when the user clicks save
after creating their avatar. The function argument must be a function. In the
example
below, onAvatarSave()
will be called when
the user saves an avatar.
const avatarCreator = new AvatarMaker();
avatarCreator.createGenerator("generator-container")
function onAvatarSave() {
console.log("Avatar saved!")
// The avatar object will return an object containing an id and the svgs that make up
the avatar. This can be saved externally (for ex. to a database) for storage.
This method can be used to display an avatar by passing in the array of SVGs.
This method is useful if you are getting your avatar from an external source and rendering it on the page. The
svgsArray is an array containing the svgs of all the features. The domElement is the id of the element in which you want to render the avatar
in. See example
below:
const avatar = new AvatarMaker()
// SVG code not put here to prevent example from taking up too much space on the page. See actual code
here.
const svgs = [`baseSVG`, `hairSVG`, `eyesSVG`, `noseSVG`, `mouthSVG`, `facialHairSVG`, `clothesSVG`,
`accessoriesSVG`, `glassesSVG`]
avatar.displayAvatarGivenSVGs(svgs, "#displaying-example")
Code above will output:
The following API methods will use the generator below as an
example.
.getAvatars()
This will return an array of all the avatar objects that are saved. Important:
If page is refreshed, previously saved avatars will be lost.
To see this function in action, follow these steps:
Create an avatar in the generator above.
Click save.
Click the "Get Avatars" button below. In the console, you will see an array with all the saved avatar
objects.
Use this method to display all the saved avatars in the specified domElement.
Important:
If page is refreshed, previously saved avatars will be lost.
To see this function in action, follow these steps:
Create an avatar in the generator above.
Click save.
Click the "Display Avatars" button below. If there any saved avatars, they will appear below.
Repeat steps 1-3 to create more avatars and display them.
Given the avatarID, this method will return
the avatar object which contains an id and an array of all the feature SVGs. Important:
If page is refreshed, previously saved avatars will be lost.
To see this function in action, follow these steps:
Create an avatar in the generator above.
Click save.
Click the "Get Avatars" button above. In the console, use the output to get an id of an avatar.
Enter the id in the input below.
Click "Get Avatar" button below to get the corresponding avatar for that id. The avatar object will be
returned in the console.
Given the avatarID, this method will display
the avatar in the specified domElement. Important:
If page is refreshed, previously saved avatars will be lost.
To see this function in action, follow these steps:
Create an avatar in the generator above.
Click save.
Click the "Get Avatars" button above. In the console, use the output to get an id of an avatar.
Enter the id in the input below.
Click "Display Avatar" button below to see the avatar below.