gpt-4o-2024-05-13· OpenAI
WildChat-4.8M (AI2)
"<!DOCTYPE html> <html> <head> <style> canvas { border: 1px solid black; } </style>…
- User
"
- Assistant
"
¡Por supuesto! Vamos a hacer algunos ajustes para mejorar el código y asegurarnos de que solo se acepte un cuadrado válido. Primero, añadiremos validaciones adicionales para asegurarnos de que el usuario esté dibujando un cuadrado de tamaño correcto. A continuación, haremos que el sistema valide la finalización del cuadrado de una manera más estricta, teniendo en cuenta la tolerancia para el punto final.
Aquí tienes el código revisado:
<!DOCTYPE html>
<html>
<head>
<style>
canvas {
border: 1px solid black;
}
</style>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/paper.js/0.12.15/paper-full.min.js"></script>
</head>
<body>
<canvas id="myCanvas" width="400" height="400"></canvas>
<script type="text/javascript">
document.addEventListener("DOMContentLoaded", function() {
// Inicializar el canvas
const canvas = document.getElementById('myCanvas');
paper.setup(canvas);
console.log("Script cargado correctamente");
// Crear un nuevo path para el cuadrado
let square = new paper.Path({
strokeColor: 'black', // Agregar color negro al trazo
strokeWidth: 2 // Agregar grosor al trazo
});
let startPoint = null;
let isDrawing = false;
let currentDirection = 'right'; // Dirección actual del dibujo
let currentLength = 0; // Longitud actual del lado del cuadrado
let lastPoint = null; // Último punto agregado al path
let sides = 0; // Número de lados dibujados
let squareSize = 30; // Tamaño del cuadrado en píxeles
let instructedSideLengths = [0, 0, 0, 0]; // Longitudes reales de cada lado
const tolerance = 5; // Tolerancia en píxeles para validar los lados
canvas.addEventListener('mousedown', (event) => {
startPoint = new paper.Point(event.offsetX, event.offsetY);
isDrawing = true;
square = new paper.Path({
strokeColor: 'black', // Agregar color negro al trazo
strokeWidth: 2 // Agregar grosor al trazo
});
square.add(startPoint);
lastPoint = startPoint;
sides = 0;
currentDirection = 'right';
currentLength = 0;
instructedSideLengths = [0, 0, 0, 0];
const speech = new SpeechSynthesisUtterance('Comienza a dibujar un cuadrado de 3cm de largo hacia la derecha');
window.speechSynthesis.speak(speech);
});
canvas.addEventListener('mousemove', (event) => {
if (isDrawing && startPoint) {
let currentPoint = new paper.Point(event.offsetX, event.offsetY);
let delta = currentPoint.subtract(lastPoint);
currentLength += delta.length;
instructedSideLengths[sides] = currentLength;
square.add(currentPoint);
lastPoint = currentPoint;
if (currentDirection === 'right' && currentLength >= squareSize) {
const speech = new SpeechSynthesisUtterance('Sube 3cm hacia arriba');
window.speechSynthesis.speak(speech);
currentDirection = 'up';
currentLength = 0;
sides++;
} else if (currentDirection === 'up' && currentLength >= squareSize) {
const speech = new SpeechSynthesisUtterance('Ve 3cm hacia la izquierda');
window.speechSynthesis.speak(speech);
currentDirection = 'left';
currentLength = 0;
sides++;
} else if (currentDirection === 'left' && currentLength >= squareSize) {
const speech = new SpeechSynthesisUtterance('Baja 3cm hacia abajo');
window.speechSynthesis.speak(speech);
currentDirection = 'down';
currentLength = 0;
sides++;
} else if (currentDirection === 'down' && currentLength >= squareSize) {
if (sides === 3 && Math.abs(currentPoint.x - startPoint.x) < tolerance && Math.abs(currentPoint.y - startPoint.y) < tolerance) {
let validSides = instructedSideLengths.every(length => Math.abs(length - squareSize) < tolerance);
if(validSides) {
const speech = new SpeechSynthesisUtterance('¡Muy bien! Has dibujado un cuadrado aproximado');
window.speechSynthesis.speak(speech);
} else {
const speech = new SpeechSynthesisUtterance('El cuadrado no es preciso. Inténtalo de nuevo.');
window.speechSynthesis.speak(speech);
}
isDrawing = false; // Finalizar dibujo
} else {
const speech = new SpeechSynthesisUtterance('Debes dibujar un cuadrado de 3cm de largo');
window.speechSynthesis.speak(speech);
isDrawing = false; // Finalizar dibujo ya que el cuadrado no es válido.
}
currentLength = 0;
sides++;
}
}
});
});
</script>
</body>
</html>
instructedSideLengths.Esto debería mejorar la experiencia y asegurar que solo un cuadrado correctamente dibujado sea aceptado como válido.