Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>JPG Generator</title>
- <style>
- body {
- font-family: sans-serif;
- background-color: #f0f0f0;
- display: flex;
- justify-content: center;
- align-items: center;
- flex-direction: column;
- height: 100vh;
- margin: 0;
- }
- h1 {
- color: #333;
- }
- p {
- color: #555;
- margin-bottom: 20px;
- }
- img {
- border: 2px solid #ccc;
- box-shadow: 0 4px 8px rgba(0,0,0,0.1);
- }
- /* We hide the canvas because the user only needs to see the final image */
- canvas {
- display: none;
- }
- </style>
- </head>
- <body>
- <h1>Your Image is Ready!</h1>
- <p>The image below was generated by your browser.</p>
- <!-- The final JPG image will be displayed here -->
- <img id="generatedImage" alt="Generated Image">
- <!-- This is the canvas element we use for drawing. It's hidden from view. -->
- <canvas id="imageCanvas"></canvas>
- <script>
- // Wait for the window to load to ensure all elements are ready
- window.onload = function() {
- // --- 1. Get the Canvas and its 2D drawing context ---
- const canvas = document.getElementById('imageCanvas');
- const ctx = canvas.getContext('2d');
- // --- 2. Define Image Properties ---
- const width = 800;
- const height = 400;
- const backgroundColor = '#28a745'; // A nice green color
- const textColor = 'white';
- const textLine1 = "Hello World.";
- const textLine2 = "Feel free to right click this green image";
- const textLine3 = "and then save as image";
- // Set the canvas dimensions
- canvas.width = width;
- canvas.height = height;
- // --- 3. Draw the Image ---
- // Draw the green background
- ctx.fillStyle = backgroundColor;
- ctx.fillRect(0, 0, width, height);
- // Set text properties and draw the text
- ctx.fillStyle = textColor;
- ctx.textAlign = 'center'; // Center the text horizontally
- ctx.textBaseline = 'middle'; // Align text vertically to the center
- // Draw the first line (larger font)
- ctx.font = 'bold 52px Arial';
- ctx.fillText(textLine1, width / 2, height / 2 - 60);
- // Draw the next two lines (smaller font)
- ctx.font = '28px Arial';
- ctx.fillText(textLine2, width / 2, height / 2 + 20);
- ctx.fillText(textLine3, width / 2, height / 2 + 60);
- // --- 4. Convert Canvas to a JPG Image ---
- // The toDataURL method creates a base64 encoded string of the image
- // 'image/jpeg' specifies the format. 0.9 is the quality (from 0 to 1).
- const dataUrl = canvas.toDataURL('image/jpeg', 0.9);
- // --- 5. Display the JPG in the <img> tag ---
- const imgElement = document.getElementById('generatedImage');
- imgElement.src = dataUrl;
- imgElement.alt = textLine1 + " " + textLine2 + " " + textLine3;
- };
- </script>
- </body>
- </html>
Add Comment
Please, Sign In to add comment