Advertisement
EdmundC

ai-character

Sep 22nd, 2024 (edited)
12
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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):</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.  
  47.         // Draw a line as the mouse is moved while dragging
  48.         canvas.addEventListener('mousemove', function(event) {
  49.             if (!drawing) return;
  50.  
  51.             const rect = canvas.getBoundingClientRect();
  52.             const endX = event.clientX - rect.left;
  53.             const endY = event.clientY - rect.top;
  54.  
  55.             // Clear the canvas and redraw everything
  56.             ctx.clearRect(0, 0, canvas.width, canvas.height);
  57.             drawCharacter();  // Redraw existing character structure
  58.  
  59.             // Draw the line being dragged
  60.             ctx.beginPath();
  61.             ctx.moveTo(startX, startY);
  62.             ctx.lineTo(endX, endY);
  63.             ctx.stroke();
  64.         });
  65.  
  66.         // Finish the line and add a bone when mouse is released
  67.         canvas.addEventListener('mouseup', function(event) {
  68.             if (!drawing) return;
  69.  
  70.             const rect = canvas.getBoundingClientRect();
  71.             const endX = event.clientX - rect.left;
  72.             const endY = event.clientY - rect.top;
  73.             drawing = false;
  74.  
  75.             // Add the joint if it doesn't exist yet
  76.             if (joints.length === 0 || (startX !== joints[joints.length - 1].x || startY !== joints[joints.length - 1].y)) {
  77.                 joints.push({ x: startX, y: startY });
  78.             }
  79.  
  80.             // Add the new joint (end of the line)
  81.             joints.push({ x: endX, y: endY });
  82.  
  83.             // Add a bone (line) between the two joints
  84.             bones.push({
  85.                 start: { x: startX, y: startY },
  86.                 end: { x: endX, y: endY },
  87.                 length: Math.hypot(endX - startX, endY - startY)
  88.             });
  89.  
  90.             drawCharacter();  // Redraw everything including the new joint and bone
  91.         });
  92.  
  93.         // Draw the character with joints and bones
  94.         function drawCharacter() {
  95.             // Draw bones (lines)
  96.             ctx.strokeStyle = 'black';
  97.             bones.forEach(bone => {
  98.                 ctx.beginPath();
  99.                 ctx.moveTo(bone.start.x, bone.start.y);
  100.                 ctx.lineTo(bone.end.x, bone.end.y);
  101.                 ctx.stroke();
  102.             });
  103.  
  104.             // Draw joints as circles with defined radius
  105.             ctx.fillStyle = 'black';
  106.             joints.forEach(joint => {
  107.                 ctx.beginPath();
  108.                 ctx.arc(joint.x, joint.y, jointRadius, 0, Math.PI * 2);
  109.                 ctx.fill();
  110.             });
  111.         }
  112.     </script>
  113. </body>
  114. </html>
  115.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement