برنامه نویسی

ایجاد یک ژنراتور هنری پیکسل با گزینه صادرات: یک راهنمای گام به گام

Pixel Art یک شکل بی موقع از هنر دیجیتال است که ده ها سال است که سازندگان و گیمرها را به طور یکسان اسیر کرده است. این که آیا شما در حال طراحی Sprites برای یک بازی هستید یا فقط سرگرم کننده هستید ، یک ژنراتور هنری پیکسل می تواند ابزاری خارق العاده برای زنده کردن ایده های شما باشد. در این وبلاگ ، من شما را در طی مراحل ساخت ژنراتور هنری پیکسل با گزینه صادراتی این به شما امکان می دهد هنر پیکسل را بکشید و آن را به عنوان یک فایل PNG صادر کنید. من قبلاً برنامه را ساخته ام ، و می توانید آن را در اینجا امتحان کنید: Pixel Art Generator.

شرح تصویر

در زیر ، ساختار و عملکرد برنامه را با جزئیات توضیح می دهم و صاحبخانه ها را برای کد ترک می کنم. می توانید جای خالی ها را با کد HTML ، CSS و JavaScript ارائه دهید.


ژنراتور هنری پیکسل چه کاری انجام می دهد؟

Pixel Art Generator ابزاری مبتنی بر وب است که به کاربران امکان می دهد:

  1. نقاشی پیکسل در یک شبکه قابل تنظیم (16 16 16 ، 32 32 32 یا 64 64 64).
  2. رنگ ها را انتخاب کنید با استفاده از یک انتخاب کننده رنگ.
  3. استفاده کردن ابزار مانند پر کردن ، پاک کردن و پاک کردن کارهای هنری آنها.
  4. کارهای هنری نهایی را صادر کنید به عنوان یک فایل PNG.

این برنامه با استفاده از HTML ، CSS و JavaScript ساخته شده است و آن را سبک و آسان برای استفاده می کند.


ساختار برنامه

این برنامه از سه مؤلفه اصلی تشکیل شده است:

  1. HTML: ساختار برنامه را تعریف می کند.
  2. CSS: برنامه را برای جذاب کردن آن از نظر بصری جذاب کنید.
  3. جاذب: منطق نقاشی ، پر کردن ، پاک کردن و صادرات را بر عهده دارد.

بیایید هر مؤلفه را تجزیه کنیم.


1. ساختار HTML

پرونده HTML ساختار اصلی برنامه را از جمله بوم ، کنترل پنل و دکمه ها تنظیم می کند. این طرح کلی است:


 lang="en">

     charset="UTF-8">
     name="viewport" content="width=device-width, initial-scale=1.0">
    </span>Pixel Art Studio<span class="nt"/>
    <span class="nt"><link/> <span class="na">rel=</span><span class="s">"stylesheet"</span> <span class="na">href=</span><span class="s">"styles.css"</span><span class="nt">></span>
<span class="nt"/>
<span class="nt"/>
    <span class="nt"/>

    <span class="nt"><script><![CDATA[<span class="na">src=]]></script></span><span class="s">"script.js"</span><span class="nt">></span>
<span class="nt"/>
<span class="nt"/>
</span></span></span></span></code></pre>
<div class="highlight__panel js-actions-panel">
<div class="highlight__panel-action js-fullscreen-code-action">
    <svg xmlns="http://www.w3.org/2000/svg" width="20px" height="20px" viewbox="0 0 24 24" class="highlight-action crayons-icon highlight-action--fullscreen-on"><title>حالت تمام صفحه را وارد کنید
    

از حالت تمام صفحه خارج شوید

توضیح:

  • در section includes meta tags for responsiveness and links to the CSS file.
  • The contains a container with a title, control panel, and canvas wrapper.
  • The control panel includes a dropdown for grid size, a color picker, and buttons for tools like Fill, Erase, Clear, and Export PNG.
  • The canvas is where the pixel art is drawn.

2. CSS Styling

The CSS file styles the app to make it visually appealing and user-friendly. Here’s the outline:

:root {
    --primary: #6c5ce7;
    --secondary: #a29bfe;
    --accent: #00cec9;
    --dark: #2d3436;
    --danger: #e17055;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Poppins', sans-serif;
}

body {
    background: linear-gradient(45deg, #2d3436, #636e72);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
}

.container {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 15px;
    padding: 20px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
    width: 100%;
    max-width: 800px;
}

h1 {
    text-align: center;
    color: var(--dark);
    margin-bottom: 15px;
    font-size: 2em;
    font-weight: 700;
    background: linear-gradient(45deg, var(--primary), var(--accent));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.control-panel {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    padding: 15px;
    background: rgba(223, 230, 233, 0.6);
    border-radius: 10px;
    margin-bottom: 15px;
    justify-content: center;
}

.custom-select {
    padding: 8px 12px;
    border: none;
    border-radius: 8px;
    background: white;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    cursor: pointer;
    transition: all 0.2s ease;
}

.custom-select:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.15);
}

.color-input {
    width: 40px;
    height: 40px;
    border: none;
    padding: 0;
    cursor: pointer;
    border-radius: 50%;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    transition: transform 0.2s ease;
}

.color-input:hover {
    transform: scale(1.1);
}

.tool-button {
    padding: 8px 15px;
    border: none;
    border-radius: 8px;
    background: var(--primary);
    color: white;
    font-size: 0.9em;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    transition: all 0.2s ease;
}

.tool-button:hover {
    transform: translateY(-2px);
    background: var(--secondary);
}

.tool-button.active {
    background: var(--accent);
    transform: scale(0.95);
}

.tool-button.danger {
    background: var(--danger);
}

.tool-button.danger:hover {
    background: #e55039;
}

.export-button {
    padding: 8px 15px;
    border: none;
    border-radius: 8px;
    background: linear-gradient(45deg, var(--primary), var(--secondary));
    color: white;
    font-weight: 600;
    cursor: pointer;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    transition: all 0.2s ease;
}

.export-button:hover {
    transform: translateY(-2px);
    background: linear-gradient(45deg, var(--secondary), var(--primary));
}

.canvas-wrapper {
    padding: 15px;
    background: rgba(223, 230, 233, 0.6);
    border-radius: 10px;
    display: flex;
    justify-content: center;
    max-width: 100%;
    overflow: auto;
}

.pixel-canvas {
    background: white;
    border-radius: 8px;
    box-shadow: 0 5px 20px rgba(0, 0, 0, 0.1);
    max-width: 100%;
}
حالت تمام صفحه را وارد کنید

از حالت تمام صفحه خارج شوید

توضیح:

  • در :root Selector متغیرهای CSS را برای رنگ های سازگار تعریف می کند.
  • در body پیشینه شیب دارد و برنامه را در صفحه متمرکز می کند.
  • در .container محتوای برنامه را نگه می دارد و دارای یک طراحی سایه دار و سایه دار است.
  • دکمه ها و ورودی ها به صورت تعاملی و بصری مجزا هستند.

3. منطق جاوا اسکریپت

پرونده JavaScript عملکرد اصلی برنامه را کنترل می کند. این طرح کلی است:

class PixelArtGenerator {
    constructor() {
        this.canvas = document.getElementById('pixelCanvas');
        this.ctx = this.canvas.getContext('2d');
        this.gridSize = 16;
        this.pixelSize = 20;
        this.color = '#000000';
        this.isDrawing = false;
        this.isFilling = false;
        this.isErasing = false;

        this.init();
        this.bindEvents();
    }

    init() {
        this.adjustCanvasSize();
        this.clearCanvas();
    }

    adjustCanvasSize() {
        const maxWidth = 700; // Reduced for condensed design
        this.pixelSize = Math.floor(maxWidth / this.gridSize);
        this.canvas.width = this.gridSize * this.pixelSize;
        this.canvas.height = this.gridSize * this.pixelSize;
    }

    bindEvents() {
        document.getElementById('gridSize').addEventListener('change', (e) => {
            this.gridSize = parseInt(e.target.value);
            this.adjustCanvasSize();
            this.clearCanvas();
        });

        const colorPicker = document.getElementById('colorPicker');
        colorPicker.addEventListener('change', (e) => {
            this.color = e.target.value;
        });

        document.getElementById('fillTool').addEventListener('click', () => {
            this.isFilling = !this.isFilling;
            this.isErasing = false;
            this.toggleToolState('fillTool', this.isFilling);
        });

        document.getElementById('eraseTool').addEventListener('click', () => {
            this.isErasing = !this.isErasing;
            this.isFilling = false;
            this.toggleToolState('eraseTool', this.isErasing);
        });

        document.getElementById('clearBtn').addEventListener('click', () => {
            this.clearCanvas();
        });

        this.canvas.addEventListener('mousedown', (e) => this.handleMouseDown(e));
        this.canvas.addEventListener('mousemove', (e) => this.handleMouseMove(e));
        this.canvas.addEventListener('mouseup', () => this.isDrawing = false);

        document.getElementById('exportPNG').addEventListener('click', () => this.exportPNG());
    }

    toggleToolState(toolId, isActive) {
        const btn = document.getElementById(toolId);
        btn.classList.toggle('active', isActive);
    }

    getPixelCoordinates(e) {
        const rect = this.canvas.getBoundingClientRect();
        const x = Math.floor((e.clientX - rect.left) / this.pixelSize);
        const y = Math.floor((e.clientY - rect.top) / this.pixelSize);
        return { x, y };
    }

    drawPixel(x, y) {
        this.ctx.fillStyle = this.isErasing ? '#ffffff' : this.color;
        this.ctx.fillRect(x * this.pixelSize, y * this.pixelSize, this.pixelSize, this.pixelSize);
    }

    handleMouseDown(e) {
        this.isDrawing = true;
        const { x, y } = this.getPixelCoordinates(e);

        if (this.isFilling) {
            this.floodFill(x, y, this.ctx.getImageData(x * this.pixelSize, y * this.pixelSize, 1, 1).data);
        } else {
            this.drawPixel(x, y);
        }
    }

    handleMouseMove(e) {
        if (!this.isDrawing || this.isFilling) return;
        const { x, y } = this.getPixelCoordinates(e);
        this.drawPixel(x, y);
    }

    floodFill(x, y, targetColor) {
        const currentColor = this.ctx.getImageData(x * this.pixelSize, y * this.pixelSize, 1, 1).data;
        if (this.colorsMatch(currentColor, targetColor)) {
            this.drawPixel(x, y);
            if (x > 0) this.floodFill(x - 1, y, targetColor);
            if (x < this.gridSize - 1) this.floodFill(x + 1, y, targetColor);
            if (y > 0) this.floodFill(x, y - 1, targetColor);
            if (y < this.gridSize - 1) this.floodFill(x, y + 1, targetColor);
        }
    }

    colorsMatch(color1, color2) {
        return color1[0] === color2[0] && color1[1] === color2[1] && 
               color1[2] === color2[2] && color1[3] === color2[3];
    }

    clearCanvas() {
        this.ctx.fillStyle = '#ffffff';
        this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
    }

    exportPNG() {
        const link = document.createElement('a');
        link.download = 'pixel-art.png';
        link.href = this.canvas.toDataURL('image/png');
        link.click();
    }
}

new PixelArtGenerator();
حالت تمام صفحه را وارد کنید

از حالت تمام صفحه خارج شوید

توضیح:

  • در PixelArtGenerator کلاس تمام عملکرد برنامه را محاصره می کند.
  • در constructor بوم ، زمینه و متغیرها را آغاز می کند.
  • روش هایی مانند adjustCanvasSizeبا drawPixelوت floodFill کارهای خاص را انجام دهید.
  • شنوندگان رویداد به آن اضافه می شوند bindEvents برای پاسخ به اقدامات کاربر.
  • در exportPNG روش به کاربران امکان می دهد کارهای هنری خود را به عنوان یک فایل PNG بارگیری کنند.

چگونه کار می کند

  1. انتخاب اندازه شبکه: کاربران می توانند اندازه شبکه (16 16 16 ، 32 32 32 یا 64 64) را انتخاب کنند. بوم به صورت پویا به اندازه انتخاب شده تنظیم می شود.
  2. نقاشی: کاربران می توانند برای ترسیم پیکسل ها روی بوم کلیک و بکشید.
  3. ابزار:

    • پر کردن: منطقه ای را با رنگ انتخاب شده پر می کند.
    • پاک کردن: پیکسل ها را با ترسیم با سفید پاک می کند.
    • پاک کردن: کل بوم را پاک می کند.
  4. صادر کردن: کاربران می توانند آثار هنری خود را به عنوان یک فایل PNG با یک کلیک صادر کنند.

امتحانش کن

می توانید ژنراتور هنری پیکسل را در اینجا امتحان کنید: ژنراتور هنری پیکسل. احساس راحتی کنید تا با اندازه های مختلف شبکه ، رنگ ها و ابزارهایی برای ایجاد شاهکار هنری پیکسل خود آزمایش کنید!


پایان

ساختن یک ژنراتور هنری پیکسل یک پروژه سرگرم کننده و آموزشی است که برای ایجاد یک برنامه وب تعاملی ، HTML ، CSS و JavaScript را ترکیب می کند. با دنبال کردن این راهنما ، می توانید ساختار و منطق پشت برنامه را درک کرده و حتی آن را متناسب با نیازهای خود سفارشی کنید.

اگر سؤال یا پیشنهادی دارید ، احساس راحتی کنید که در زیر نظر خود را بگذارید. ایجاد هنر پیکسل مبارک! 🎨

نوشته های مشابه

دیدگاهتان را بنویسید

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *

دکمه بازگشت به بالا