Menu

Build an avatar creator with layered SVG and Stimulus
πŸ“°

Build an avatar creator with layered SVG and Stimulus

DEV Community: cssΒ·Rails DesignerΒ·16 days ago
#YAd8EJem
#devtocss#devto#dev#reset#download
Long article (1344 words) β€” no AI summary yet.
Reading 0:00
15s threshold

A while back I built a custom emoji composer using <canvas> . Fun project. But the canvas did all the work. It was responsible for the drawing surface, the drag logic and the export.

When I rebuilt the same idea as an avatar creator, I went for SVG instead. You pick a head, add some eyes, try a hat, then save the result as an image.

That cute face above is what I want to build today. 😊 A plain <svg> , a Stimulus controller and a folder of parts. No JS libraries except Stimulus.

As always, the code can be found on GitHub .

The HTML

Two elements: a <svg> for the preview and an empty <div> for the controls.

<div data-controller = "avatar image-exporter" class = "grid grid-cols-[300px_1fr] gap-8 max-w-5xl" > <div> <svg data-avatar-target = "preview" viewBox = "0 0 300 300" width = "300" height = "300" class = "bg-white border border-gray-200 rounded-lg" ></svg>

<div class = "grid grid-cols-2 mt-2 gap-x-3 *:w-full *:bg-gray-300 *:rounded-md" > <button data-action = "click->avatar#reset" > Reset…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.