Advertisement
EdmundC

alan.becker

Sep 22nd, 2024 (edited)
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 4.54 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>AI Character with Joints and Physics</title>
  7.     <style>
  8.         canvas {
  9.             border: 1px solid black;
  10.         }
  11.     </style>
  12. </head>
  13. <body>
  14.     <h2>Draw your character (click to set joints or drag to draw bones):</h2>
  15.     <canvas id="characterCanvas" width="500" height="500"></canvas>
  16.     <br />
  17.     <label>Gravity Strength: </label><input type="range" id="gravity" min="0" max="1" step="0.1" value="0.5">
  18.     <button id="startMovement">Start Movement</button>
  19.     <br />
  20.     <input type="file" id="itemUpload" accept="image/*">
  21.    
  22.     <script>
  23.         const canvas = document.getElementById('characterCanvas');
  24.         const ctx = canvas.getContext('2d');
  25.  
  26.         let joints = [];  // Store joint positions
  27.         let bones = [];   // Store bones (lines between joints)
  28.         let gravity = 0.5;  // Gravity strength
  29.         let drawing = false;  // Track whether a line is being drawn
  30.         let startX, startY;  // Starting point of the line
  31.  
  32.         // Joint properties
  33.         const jointRadius = 5;  // Size of the joints (visible dots)
  34.  
  35.         document.getElementById('gravity').addEventListener('input', function(event) {
  36.             gravity = parseFloat(event.target.value);
  37.         });
  38.  
  39.         // Start drawing a line when mouse is pressed
  40.         canvas.addEventListener('mousedown', function(event) {
  41.             const rect = canvas.getBoundingClientRect();
  42.             startX = event.clientX - rect.left;
  43.             startY = event.clientY - rect.top;
  44.             drawing = true;
  45.  
  46.             // Log start position to check if coordinates are correct
  47.             console.log(`Starting position: (${startX}, ${startY})`);
  48.         });
  49.  
  50.         // Draw a line as the mouse is moved while dragging
  51.         canvas.addEventListener('mousemove', function(event) {
  52.             if (!drawing) return;
  53.  
  54.             const rect = canvas.getBoundingClientRect();
  55.             const endX = event.clientX - rect.left;
  56.             const endY = event.clientY - rect.top;
  57.  
  58.             // Log current position while moving
  59.             console.log(`Current position: (${endX}, ${endY})`);
  60.  
  61.             // Clear the canvas and redraw everything
  62.             ctx.clearRect(0, 0, canvas.width, canvas.height);
  63.             drawCharacter();  // Redraw existing character structure
  64.  
  65.             // Draw the line being dragged
  66.             ctx.beginPath();
  67.             ctx.moveTo(startX, startY);
  68.             ctx.lineTo(endX, endY);
  69.             ctx.stroke();
  70.         });
  71.  
  72.         // Finish the line and add a bone when mouse is released
  73.         canvas.addEventListener('mouseup', function(event) {
  74.             if (!drawing) return;
  75.  
  76.             const rect = canvas.getBoundingClientRect();
  77.             const endX = event.clientX - rect.left;
  78.             const endY = event.clientY - rect.top;
  79.             drawing = false;
  80.  
  81.             // Log end position to ensure correct coordinates
  82.             console.log(`Ending position: (${endX}, ${endY})`);
  83.  
  84.             // Add the joint if it doesn't exist yet
  85.             if (joints.length === 0 || (startX !== joints[joints.length - 1].x || startY !== joints[joints.length - 1].y)) {
  86.                 joints.push({ x: startX, y: startY });
  87.             }
  88.  
  89.             // Add the new joint (end of the line)
  90.             joints.push({ x: endX, y: endY });
  91.  
  92.             // Add a bone (line) between the two joints
  93.             bones.push({
  94.                 start: { x: startX, y: startY },
  95.                 end: { x: endX, y: endY },
  96.                 length: Math.hypot(endX - startX, endY - startY)
  97.             });
  98.  
  99.             // Redraw everything, including the new joint and bone
  100.             drawCharacter();
  101.         });
  102.  
  103.         // Draw the character with joints and bones
  104.         function drawCharacter() {
  105.             // Draw bones (lines)
  106.             ctx.strokeStyle = 'black';
  107.             bones.forEach(bone => {
  108.                 ctx.beginPath();
  109.                 ctx.moveTo(bone.start.x, bone.start.y);
  110.                 ctx.lineTo(bone.end.x, bone.end.y);
  111.                 ctx.stroke();
  112.             });
  113.  
  114.             // Draw joints as circles with defined radius
  115.             ctx.fillStyle = 'black';
  116.             joints.forEach(joint => {
  117.                 ctx.beginPath();
  118.                 ctx.arc(joint.x, joint.y, jointRadius, 0, Math.PI * 2);
  119.                 ctx.fill();
  120.             });
  121.         }
  122.     </script>
  123. </body>
  124. </html>
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement