Home >Web Front-end >JS Tutorial >Build a Greeting Card Maker w/ Vue Satori
Written by Dubem Izuorah
For the creative developers out there seeking a festive project that leverages your coding skills, let’s build a digital Holiday Greeting Card that you can customize, add your own photo, and instantly download. We’ll be using some cutting-edge web technologies to make this happen, and along the way, you’ll learn about graphics generation, several cool packages, and some neat Vue 3 tricks.
Screenshot of Holiday Greeting Card Maker interface
By the end of this tutorial, you’ll have:
We’ll use Nuxt 3 as our framework because it provides a robust, batteries-included setup for Vue applications. Open your terminal and let’s create our project by running the commands below:
npx nuxi init christmas-card-maker cd christmas-card-maker yarn install
Why Nuxt? It gives us a solid foundation with built-in routing, easy module integration, and an overall easy setup. Perfect for our greeting card maker!
Now, we’ll add the libraries that will make our card generation magic happen. Run the following commands on your terminal:
yarn add @vue/server-renderer @vueuse/core nuxt satori satori-html yarn add -D @nuxtjs/tailwindcss
Let me break down why we’re choosing each of these:
Let’s update our nuxt.config.ts to set up our development environment:
? nuxt.config.ts
export default defineNuxtConfig({ ssr: false, devtools: { enabled: true }, modules: ["@nuxtjs/tailwindcss"], });
We’re disabling server-side rendering for this client-side app and enabling Tailwind CSS module. This gives us a lightweight, responsive setup perfect for our greeting card maker.
Now that we’re all setup, let’s dive into creating our Holiday Card Maker step-by-step.
Let’s first focus on creating a simple layout for our card maker. We’ll set up a form with fields for the user to add a name, greeting, and an image upload. We’ll also add a preview area where the card will be displayed.
Here’s what the base layout will look like:
? app.vue
npx nuxi init christmas-card-maker cd christmas-card-maker yarn install
Why do we need this setup?
Next, let’s create the card template component we imported earlier. This is where the magic happens.
We’ll use the data from the form to personalize the card. In components/ChristmasCard.vue, we’ll design our card’s visual template. Think of this like designing a canvas where users can personalize their greeting.
? components/ChristmasCard.vue
yarn add @vue/server-renderer @vueuse/core nuxt satori satori-html yarn add -D @nuxtjs/tailwindcss
Breaking it down:
Here’s where we tie everything together. We’ll convert the ChristmasCard component into an SVG.
? app.vue
npx nuxi init christmas-card-maker cd christmas-card-maker yarn install
Let me explain:
Let’s wrap up everything we’ve been working on by adding some essential utility functions for our code and template features.
Adding Image Upload Support
We’re adding functionality to handle image uploads. This will ensure that users can select an image, check if it’s within the size limit (100KB), and display it.
Paste this below the refreshGraphics code:
? app.vue
yarn add @vue/server-renderer @vueuse/core nuxt satori satori-html yarn add -D @nuxtjs/tailwindcss
Why check file size? We’re limiting the file size to ensure smooth performance. Anything larger could slow things down, so 100KB is a safe upper limit.
Next, we’ll render the Vue component as an HTML string. This allows us to use Vue for server-side rendering, email templates, or static site generation. In this case, it’s for generating a greeting card template. Let’s add this as well to our code.
npx nuxi init christmas-card-maker cd christmas-card-maker yarn install
Finally, let’s add code that will enable us to download our card as a JPEG.
? app.vue
yarn add @vue/server-renderer @vueuse/core nuxt satori satori-html yarn add -D @nuxtjs/tailwindcss
Why does this work? By using HTML Canvas API, we can draw the SVG onto the canvas and convert it into a JPEG for easy downloading. It’s a quick and efficient way to generate image files from vector graphics.
To ensure the SVG element is displayed correctly within the container, we need to apply some CSS styling. Since the generated SVG has a fixed size of 1080px by 1080px, but the parent container is smaller, we need to scale the SVG to fit inside the container without distortion.
? app.vue
export default defineNuxtConfig({ ssr: false, devtools: { enabled: true }, modules: ["@nuxtjs/tailwindcss"], });
This CSS rule ensures that the SVG element inside the .banner-here div is resized to fill the available space while maintaining its aspect ratio.
By now, your project should look something like this screenshot. Let’s run it to see the real magic!
To see our app in action, run the command below and open http://localhost:3000 on your browser.
For reference, here is the Github repo for this tutorial.
<template> <div> <p><strong>So what’s happening here?</strong></p> <ol> <li> <strong>Card Preview:</strong> The with v-html="svg" is where our card will appear as an SVG once it’s generated. </li> <li><strong>Form Fields:</strong></li> <ul> <li>Two text fields: name and greeting. These will dynamically update the card when the user types.</li> <li>File input: Allows users to upload an image.</li> </ul> <ol> <li> <strong>File Restrictions:</strong> The small tag below the file input informs users about allowed file size and formats. Large images can slow down rendering, so we’re capping the size at 100KB.</li> <li> <strong>Download Button:</strong> This triggers the downloadSvgAsJpeg function, which saves the card as a JPEG image.</li> </ol> <h3> Step 2. Setting up Dependencies </h3> <p>Now let’s set up the logic and install the packages needed to power our Holiday Card Maker. Let’s import a few packages that will make things easier for us.</p> <p>? <strong>app.vue</strong><br> </p> <pre class="brush:php;toolbar:false">... </template> <script setup lang="ts"> import { createSSRApp } from "vue"; import { renderToString } from "@vue/server-renderer"; import { useLocalStorage, watchDebounced } from "@vueuse/core"; import satori from "satori"; import { html } from "satori-html"; import ChristmasCard from "./components/ChristmasCard.vue"; const form = useLocalStorage("app-form", { name: "", greeting: "Merry Christmas", photo: null, }); const svg = ref(""); const fonts = ref([]); ... </script>
You should see something resembling the interface in the GIF above. You can edit the details, add an image, and download your image. Congrats! You now have your own personal Holiday Card maker.
You’ve just built a personalized greeting card maker! ? But don’t stop here. Experiment with different designs, add more customization options, or even create cards for other occasions.
Some ideas to expand your project:
Want to do more on the client side? Discover how to render 3d objects in the browser by reading this article.
Originally published at https://www.vuemastery.com on December 19, 2024.
The above is the detailed content of Build a Greeting Card Maker w/ Vue Satori. For more information, please follow other related articles on the PHP Chinese website!