html przenoszenie obiektów jak na pulpicie windows
- User
html przenoszenie obiektów jak na pulpicie windows
- Assistant
Aby umożliwić przenoszenie obiektów na stronie internetowej tak, jak na pulpicie systemu Windows, można skorzystać z techniki przeciągnij i upuść (drag and drop) oraz odpowiednich zdarzeń JavaScript.
Poniżej przedstawiam prosty przykład, jak można zaimplementować przenoszenie obiektów na stronie internetowej:
- Stwórz na stronie dwa elementy, które będą mogły być przenoszone:
<div id="obiekt1" draggable="true">Obiekt 1</div> <div id="obiekt2" draggable="true">Obiekt 2</div>- Dodaj obsługę zdarzeń dla tych elementów w JavaScript:
var przenoszonyObiekt; document.addEventListener("drag", function(event) { // Zapisz przeciągany obiekt przenoszonyObiekt = event.target; }); document.addEventListener("dragover", function(event) { event.preventDefault(); }); document.addEventListener("drop", function(event) { event.preventDefault(); // Sprawdź czy obiekt przenoszony jest poprawnie if (przenoszonyObiekt) { event.target.appendChild(przenoszonyObiekt); przenoszonyObiekt = null; } }); - User
napisz mi cały potrzebny kod do przenoszenia obiegtów svg pływających po dostepnym polu (ekranie) na stronie internetowe, czyli biore obiekt "a" i przesówam go o "x" i "y" i puszczam i on tam zostaje
- Assistant
Oczywiście. Poniżej znajdziesz przykładowy kod HTML, CSS i JavaScript, który umożliwia przenoszenie obiektów SVG po dostępnym polu na stronie internetowej:
HTML:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Przenoszenie obiektów SVG</title> <link rel="stylesheet" href="styles.css"> </head> <body> <svg id="canvas" width="800" height="600"> <circle id="obiekt" cx="50" cy="50" r="20" fill="blue" draggable="true"></circle> </svg> <script src="script.js"></script> </body> </html>CSS (styles.css):
#canvas { border: 1px solid black; }JavaScript (script.js):
let przesowanie = false; let startX, startY; document.getElementById("obiekt").addEventListener("mousedown", function(event) { przesowanie = true; startX = event.clientX - this.getAttribute("cx"); startY = event.clientY - this.getAttribute("cy"); }); document.addEventListener("mousemove", function(event) { if (przesowanie) { let obiekt = document.getElementById("obiekt"); obiekt.setAttribute("cx", event.clientX - startX); obiekt.setAttribute("cy", event.clientY - startY); } }); document.addEventListener("mouseup", function() { przesowanie = false; }); - User
"karteczka" w svg która ma na sobie napisane "text"
- Assistant
Poniżej znajdziesz kod HTML zawierający element SVG reprezentujący "karteczkę" z tekstem "text":
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>"Karteczka" z tekstem SVG</title> </head> <body> <svg width="200" height="100" xmlns="http://www.w3.org/2000/svg"> <rect x="10" y="10" width="180" height="80" fill="yellow" stroke="black" stroke-width="2"></rect> <text x="100" y="50" text-anchor="middle" alignment-baseline="middle" fill="black" font-size="16">Tekst</text> </svg> </body> </html>W powyższym kodzie,
<rect>reprezentuje prostokątną "karteczkę" (karteczkę na post-it) w kolorze żółtym z czarnym obramowaniem. Następnie,<text>zawiera tekst "Tekst" wyśrodkowany w karteczce. Możesz dostosować właściwości elementów SVG, takie jak kolory, rozmiary, czcionki itp., aby dopasować je do swoich wymagań. Oba elementy SVG znajdują się wewnątrz tagu<svg>, który określa obszar roboczy, na którym elementy SVG są wyświetlane. - User
w js canvas zrób takie coś: <svg width=“200” height=“100” xmlns=“http://www.w3.org/2000/svg”> Tekst
- Assistant
Oto odpowiednik "karteczki" z tekstem w elemencie canvas za pomocą JavaScript:
HTML:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>"Karteczka" z tekstem w Canvas</title> </head> <body> <canvas id="myCanvas" width="200" height="100"></canvas> <script src="script.js"></script> </body> </html>JavaScript (script.js):
const canvas = document.getElementById("myCanvas"); const ctx = canvas.getContext("2d"); // Rysowanie prostokąta ctx.fillStyle = "yellow"; ctx.strokeStyle = "black"; ctx.lineWidth = 2; ctx.fillRect(10, 10, 180, 80); ctx.strokeRect(10, 10, 180, 80); // Rysowanie tekstu ctx.fillStyle = "black"; ctx.font = "16px Arial"; ctx.textAlign = "center"; ctx.textBaseline = "middle"; ctx.fillText("Tekst", 100, 50);W powyższym kodzie JavaScript tworzymy element canvas o wymiarach 200x100 i otrzymujemy jego kontekst renderowania 2D. Następnie rysujemy prostokąt (karteczkę) oraz tekst wewnątrz tego prostokąta, używając zdefiniowanych stylów i właściwości. Upewnij się, że kod jest zgodny z zaleceniami i standardami HTML5 i JavaScript. Ostatecznie, na stronie zostanie wyświetlona "karteczka" z tekstem, podobna do wersji SVG przedstawionej we wcześniejszej odpowiedzi.
- User
w canva js, przenoszenie kwadratu za pomocą złapania myszką i przesunięcia i puszczenia
- Assistant
Poniżej znajdziesz kod JavaScript, który umożliwia przenoszenie kwadratu za pomocą myszki poprzez złapanie i przesunięcie, a następnie puszczenie myszki:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Przenoszenie kwadratu w Canvas</title> </head> <body> <canvas id="myCanvas" width="400" height="400"></canvas> <script> const canvas = document.getElementById("myCanvas"); const ctx = canvas.getContext("2d"); let dragging = false; let squareX = 50; let squareY = 50; canvas.addEventListener("mousedown", function(event) { const mouseX = event.clientX - canvas.offsetLeft; const mouseY = event.clientY - canvas.offsetTop; // Sprawdź czy myszka jest nad kwadratem if (mouseX > squareX && mouseX < squareX + 50 && mouseY > squareY && mouseY < squareY + 50) { dragging = true; } }); canvas.addEventListener("mousemove", function(event) { if (dragging) { squareX = event.clientX - canvas.offsetLeft - 25; squareY = event.clientY - canvas.offsetTop - 25; drawSquare(); } }); canvas.addEventListener("mouseup", function() { dragging = false; }); function drawSquare() { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.fillStyle = "blue"; ctx.fillRect(squareX, squareY, 50, 50); } drawSquare(); </script> </body> </html> - User
w funkcji drawSquare zamień na kwadrat z obramówką z textem "text"
- Assistant
Oto zmieniony kod JavaScript, który rysuje kwadrat z obramówką i zawiera tekst "text" w środku:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Przenoszenie kwadratu z tekst w Canvas</title> </head> <body> <canvas id="myCanvas" width="400" height="400"></canvas> <script> const canvas = document.getElementById("myCanvas"); const ctx = canvas.getContext("2d"); let dragging = false; let squareX = 50; let squareY = 50; canvas.addEventListener("mousedown", function(event) { const mouseX = event.clientX - canvas.offsetLeft; const mouseY = event.clientY - canvas.offsetTop; // Sprawdź czy myszka jest nad kwadratem if (mouseX > squareX && mouseX < squareX + 50 && mouseY > squareY && mouseY < squareY + 50) { dragging = true; } }); canvas.addEventListener("mousemove", function(event) { if (dragging) { squareX = event.clientX - canvas.offsetLeft - 25; squareY = event.clientY - canvas.offsetTop - 25; drawSquare(); } }); canvas.addEventListener("mouseup", function() { dragging = false; }); function drawSquare() { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.strokeStyle = "blue"; ctx.strokeRect(squareX, squareY, 50, 50); ctx.fillStyle = "black"; ctx.fillText("Text", squareX + 25, squareY + 25); } drawSquare(); </script> </body> </html> - User
żeby tło było żółte function drawSquare() { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.strokeStyle = "#552"; ctx.strokeRect(squareX, squareY, 50, 50); ctx.fillStyle = "black"; ctx.fillText("Text", squareX + 25, squareY + 25); }
- Assistant
Oto zmodyfikowany kod JavaScript, aby ustawić tło na kolor żółty:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Przenoszenie kwadratu z tekstem w Canvas</title> </head> <body> <canvas id="myCanvas" width="400" height="400" style="background-color: yellow;"></canvas> <script> const canvas = document.getElementById("myCanvas"); const ctx = canvas.getContext("2d"); let dragging = false; let squareX = 50; let squareY = 50; canvas.addEventListener("mousedown", function(event) { const mouseX = event.clientX - canvas.offsetLeft; const mouseY = event.clientY - canvas.offsetTop; // Sprawdź czy myszka jest nad kwadratem if (mouseX > squareX && mouseX < squareX + 50 && mouseY > squareY && mouseY < squareY + 50) { dragging = true; } }); canvas.addEventListener("mousemove", function(event) { if (dragging) { squareX = event.clientX - canvas.offsetLeft - 25; squareY = event.clientY - canvas.offsetTop - 25; drawSquare(); } }); canvas.addEventListener("mouseup", function() { dragging = false; }); function drawSquare() { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.strokeStyle = "#552"; ctx.strokeRect(squareX, squareY, 50, 50); ctx.fillStyle = "black"; ctx.fillText("Text", squareX + 25, squareY + 25); } drawSquare(); </script> </body> </html>