با توجه به درخواست شما برای نوشتن فقط "مورد A" و نادیده گرفتن "مورد B"، فرض میکنم که "مورد A" به معنای کد اصلی افزونه است (یعنی فایلهای PHP، JS و CSS که در پاسخ قبلی ارائه دادم). بنابراین، فقط کدهای مربوطه را بدون توضیحات اضافی یا بخشهای توسعه بیشتر ارائه میدهم.
کد افزونه
1. فایل perspective-drawing.php
php Copy code
<?php
/*
Plugin Name: Perspective Drawing Tool
Description: A simple perspective drawing tool for WordPress.
Version: 1.0
Author: Your Name
*/
function pdt_enqueue_scripts() {
wp_enqueue_script('pdt-script', plugins_url('pdt-script.js', __FILE__), array('jquery'), '1.0', true);
wp_enqueue_style('pdt-style', plugins_url('pdt-style.css', __FILE__));
}
add_action('wp_enqueue_scripts', 'pdt_enqueue_scripts');
function pdt_drawing_tool_shortcode() {
ob_start();
?>
<div id="pdt-container">
<canvas id="pdt-canvas" width="800" height="600"></canvas>
<div id="pdt-controls">
<button id="pdt-draw-line">Draw Line</button>
<button id="pdt-clear">Clear</button>
</div>
</div>
<?php
return ob_get_clean();
}
add_shortcode('pdt_drawing_tool', 'pdt_drawing_tool_shortcode');
2. فایل pdt-script.js
javascript Copy code
document.addEventListener('DOMContentLoaded', function() {
const canvas = document.getElementById('pdt-canvas');
const ctx = canvas.getContext('2d');
let drawing = false;
let startX, startY;
document.getElementById('pdt-draw-line').addEventListener('click', function() {
drawing = true;
});
document.getElementById('pdt-clear').addEventListener('click', function() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
});
canvas.addEventListener('mousedown', function(e) {
if (drawing) {
startX = e.offsetX;
startY = e.offsetY;
}
});
canvas.addEventListener('mouseup', function(e) {
if (drawing) {
const endX = e.offsetX;
const endY = e.offsetY;
drawPerspectiveLine(ctx, startX, startY, endX, endY);
drawing = false;
}
});
function drawPerspectiveLine(ctx, startX, startY, endX, endY) {
const vanishingPointX = canvas.width / 2;
const vanishingPointY = canvas.height;
ctx.beginPath();
ctx.moveTo(startX, startY);
ctx.lineTo(vanishingPointX, vanishingPointY);
ctx.strokeStyle = 'black';
ctx.stroke();
}
});
3. فایل pdt-style.css
css Copy code
#pdt-container {
display: flex;
flex-direction: column;
align-items: center;
}
#pdt-canvas {
border: 1px solid #000;
}
#pdt-controls {
margin-top: 10px;
}
#pdt-controls button {
margin: 5px;
}
این کدها کاملاً آماده برای استفاده در افزونه وردپرس هستند. برای فعال کردن، پوشه را در wp-content/plugins قرار دهید و افزونه را فعال کنید. سپس، از شورتکد [pdt_drawing_tool] در صفحات استفاده کنید. اگر منظورتان از "مورد A" چیز دیگری بود، لطفاً دقیقتر توضیح دهید.