jak w canva js można zrobic tło żółte: ctx.clearRect(0, 0, canvas.width, canvas.height);…
- User
jak w canva js można zrobic tło żółte: ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.strokeRect(squareX, squareY, 50, 50); ctx.fillText("Text", squareX + 7, squareY + 15);
- Assistant
Aby zmienić tło na żółte w Canva.js, możesz użyć metody fillRect() do narysowania prostokąta o wymiarach równych wielkości canvasa i wypełnieniu go kolorem żółtym. Poniżej znajdziesz przykładowy kod:
ctx.fillStyle = 'yellow'; // Ustaw kolor żółty ctx.fillRect(0, 0, canvas.width, canvas.height); // Narysuj prostokąt (tło)Możesz wstawić ten kod przed innymi operacjami rysowania na canvasie, aby najpierw ustawić tło na żółte.
- User
napraw kod:
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(txt,ctx) { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.strokeStyle = "#552"; ctx.fillStyle = "#ffc"; ctx.fillRect(squareX, squareY, 50, 50); ctx.strokeRect(squareX, squareY, 50, 50);
ctx.fillStyle = "black"; ctx.fillText(txt, squareX + 7, squareY + 15);}
drawSquare("text",a); drawSquare("text2",b);
- Assistant
Oto poprawiony kod:
<!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" style="border:1px solid grey;"></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("Text", ctx); } }); canvas.addEventListener("mouseup", function() { dragging = false; }); function drawSquare(txt, ctx) { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.strokeStyle = "#552"; ctx.fillStyle = "#ffc"; ctx.fillRect(squareX, squareY, 50, 50); ctx.strokeRect(squareX, squareY, 50, 50); ctx.fillStyle = "black"; ctx.fillText(txt, squareX + 7, squareY + 15); } drawSquare("Text", ctx); </script> </body> </html> - User
zamień tak oaby to obsługiwało wiecej niż jeden kwadrat :
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(jakistext); } });
canvas.addEventListener("mouseup", function() { dragging = false; });
function drawSquare(txt) { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.strokeStyle = "#552"; ctx.fillStyle = "#ffc"; ctx.fillRect(squareX, squareY, 50, 50); ctx.strokeRect(squareX, squareY, 50, 50);
ctx.fillStyle = "black"; ctx.fillText(txt, squareX + 7, squareY + 15);}
drawSquare(jakistext);
- Assistant
Aby obsługiwać więcej niż jeden kwadrat w tej animacji, można użyć tablicy obiektów, gdzie każdy obiekt reprezentuje pojedynczy kwadrat. Poniżej znajdziesz zmodyfikowany kod, który pozwoli na przenoszenie wielu kwadratów na canvasie:
<!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" style="border:1px solid grey;"></canvas> <script> const canvas = document.getElementById("myCanvas"); const ctx = canvas.getContext("2d"); const squares = [ { x: 50, y: 50, text: "Square 1" }, { x: 150, y: 150, text: "Square 2" } ]; let dragging = false; let selectedSquare = null; canvas.addEventListener("mousedown", function(event) { const mouseX = event.clientX - canvas.offsetLeft; const mouseY = event.clientY - canvas.offsetTop; selectedSquare = squares.find(square => { return mouseX > square.x && mouseX < square.x + 50 && mouseY > square.y && mouseY < square.y + 50; }); if (selectedSquare) { dragging = true; } }); canvas.addEventListener("mousemove", function(event) { if (dragging && selectedSquare) { selectedSquare.x = event.clientX - canvas.offsetLeft - 25; selectedSquare.y = event.clientY - canvas.offsetTop - 25; drawSquares(); } }); canvas.addEventListener("mouseup", function() { dragging = false; }); function drawSquares() { ctx.clearRect(0, 0, canvas.width, canvas.height); squares.forEach(square => { ctx.strokeStyle = "#552"; ctx.fillStyle = "#ffc"; ctx.fillRect(square.x, square.y, 50, 50); ctx.strokeRect(square.x, square.y, 50, 50); ctx.fillStyle = "black"; ctx.fillText(square.text, square.x + 7, square.y + 15); }); } drawSquares(); </script> </body> </html>