JavaScript – die besten Beiträge

Wieso geht der Code nicht?

Hallo

Ich möchte mit js ein Programm bauen, welches meinen Körper erkennt und dort drauf dann ein T-Shirt-Modell legt. Also so eine virtuelle Garderobe quasi. Wieso geht der Code nicht?

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Virtual Try-On Final</title>
<style>
 body { margin: 0; overflow: hidden; }
 #webcam {
  position: absolute;
  transform: scaleX(-1);
  opacity: 0.5;
  z-index: 1;
 }
 #output {
  position: absolute;
  z-index: 2;
 }
</style>
</head>
<body>
<video id="webcam" autoplay playsinline></video>
<canvas id="output"></canvas>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/body-pix"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/loaders/GLTFLoader.js"></script>
<script>
const video = document.getElementById('webcam');
const output = document.getElementById('output');
let net, scene, camera, renderer, shirt;
let bodyPosition = { x: 0, y: 0, width: 0, height: 0 };
async function setupWebcam() {
 try {
  const stream = await navigator.mediaDevices.getUserMedia({
   video: {
    width: { ideal: 640 },
    height: { ideal: 480 },
    facingMode: 'user'
   }
  });
  video.srcObject = stream;
  
  return new Promise(resolve => {
   video.onloadedmetadata = () => {
    output.width = video.videoWidth;
    output.height = video.videoHeight;
    resolve();
   };
  });
 } catch (error) {
  console.error('Webcam error:', error);
  alert('Bitte Webcam-Zugriff erlauben!');
 }
}
function initThreeJS() {
 scene = new THREE.Scene();
 camera = new THREE.PerspectiveCamera(
  60,
  output.width / output.height,
  0.1,
  1000
 );
 camera.position.set(0, 0, 2);
 renderer = new THREE.WebGLRenderer({
  canvas: output,
  alpha: true,
  antialias: true
 });
 renderer.setClearColor(0x000000, 0);
 // Beleuchtung
 const ambientLight = new THREE.AmbientLight(0xffffff, 1.0);
 scene.add(ambientLight);
 const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
 directionalLight.position.set(0, 2, 5);
 scene.add(directionalLight);
 loadShirtModel();
}
async function loadShirtModel() {
 const loader = new THREE.GLTFLoader();
 try {
  const gltf = await loader.loadAsync('tshirt_model.glb');
  shirt = gltf.scene;
  
  // Material-Anpassung
  shirt.traverse(child => {
   if (child.isMesh) {
    child.material = new THREE.MeshPhongMaterial({
     color: 0xffffff,
     transparent: true,
     opacity: 0.9,
     depthWrite: false
    });
   }
  });
  shirt.scale.set(0.3, 0.3, 0.3);
  scene.add(shirt);
  console.log('T-Shirt Modell geladen');
 } catch (error) {
  console.error('Modellfehler:', error);
  addFallbackCube();
 }
}
function addFallbackCube() {
 const geometry = new THREE.BoxGeometry(0.5, 0.5, 0.5);
 const material = new THREE.MeshBasicMaterial({ color: 0xff0000 });
 shirt = new THREE.Mesh(geometry, material);
 scene.add(shirt);
}
function calculateBodyPosition(segmentation) {
 const data = segmentation.data;
 let minX = Infinity, maxX = -Infinity;
 let minY = Infinity, maxY = -Infinity;
 for (let y = 0; y < segmentation.height; y++) {
  for (let x = 0; x < segmentation.width; x++) {
   if (data[y * segmentation.width + x] === 1) {
    minX = Math.min(minX, x);
    maxX = Math.max(maxX, x);
    minY = Math.min(minY, y);
    maxY = Math.max(maxY, y);
   }
  }
 }
 if (minX !== Infinity) {
  bodyPosition = {
   x: (minX + maxX) / 2,
   y: (minY + maxY) / 2,
   width: maxX - minX,
   height: maxY - minY
  };
 }
}
function updateShirtPosition() {
 if (!shirt) return;
 // Koordinatenumrechnung
 const x = (bodyPosition.x / output.width - 0.5) * 2;
 const y = -(bodyPosition.y / output.height - 0.5) * 2;
 
 shirt.position.set(x, y, 0);
 
 // Skalierung basierend auf Körpergröße
 const scaleFactor = bodyPosition.height / output.height * 2;
 shirt.scale.set(scaleFactor, scaleFactor, scaleFactor);
}
async function detectBody() {
 try {
  // Ganzkörper-Segmentierung
  const segmentation = await net.segmentPerson(video, {
   segmentationThreshold: 0.7,
   internalResolution: 'high',
   maxDetections: 1
  });
  calculateBodyPosition(segmentation);
  updateShirtPosition();
  // Rendering
  const ctx = output.getContext('2d');
  ctx.clearRect(0, 0, output.width, output.height);
  renderer.render(scene, camera);
  requestAnimationFrame(detectBody);
 } catch (error) {
  console.error('Detektionsfehler:', error);
 }
}
(async () => {
 await setupWebcam();
 net = await bodyPix.load({
  architecture: 'ResNet50',
  outputStride: 32,
  quantBytes: 2
 });
 initThreeJS();
 detectBody();
})();
</script>
</body>
</html>

Freundliche Grüsse

HTML, Webseite, CSS, JavaScript, HTML5, Code, Datenbank, Programmiersprache, Webentwicklung, Frontend

Snake Game HTML Score?

Ich will in meinem Snake Game: (Hier der Code)

Einen Score einbauen (ein Food = + ein Score) Kann mir jemand helfen?

//board
var blockSize = 25;
var rows = 20;
var cols = 20;
var board;
var context; 

//snake head
var snakeX = blockSize * 5;
var snakeY = blockSize * 5;

var velocityX = 0;
var velocityY = 0;

var snakeBody = [];

//food
var foodX;
var foodY;

var gameOver = false;

window.onload = function() {
    board = document.getElementById("board");
    board.height = rows * blockSize;
    board.width = cols * blockSize;
    context = board.getContext("2d"); //used for drawing on the board

    placeFood();
    document.addEventListener("keyup", changeDirection);
    // update();
    setInterval(update, 1000/10); //100 milliseconds
}

function update() {
    if (gameOver) {
        return;
    }

    context.fillStyle="black";
    context.fillRect(0, 0, board.width, board.height);

    context.fillStyle="red";
    context.fillRect(foodX, foodY, blockSize, blockSize);

    if (snakeX == foodX && snakeY == foodY) {
        snakeBody.push([foodX, foodY]);
        placeFood();
    }

    for (let i = snakeBody.length-1; i > 0; i--) {
        snakeBody[i] = snakeBody[i-1];
    }
    if (snakeBody.length) {
        snakeBody[0] = [snakeX, snakeY];
    }

    context.fillStyle="lime";
    snakeX += velocityX * blockSize;
    snakeY += velocityY * blockSize;
    context.fillRect(snakeX, snakeY, blockSize, blockSize);
    for (let i = 0; i < snakeBody.length; i++) {
        context.fillRect(snakeBody[i][0], snakeBody[i][1], blockSize, blockSize);
    }

    //game over conditions
    if (snakeX < 0 || snakeX > cols*blockSize || snakeY < 0 || snakeY > rows*blockSize) {
        gameOver = true;
        alert("Game Over");
    }

    for (let i = 0; i < snakeBody.length; i++) {
        if (snakeX == snakeBody[i][0] && snakeY == snakeBody[i][1]) {
            gameOver = true;
            alert("Game Over");
        }
    }
}

function changeDirection(e) {
    if (e.code == "ArrowUp" && velocityY != 1) {
        velocityX = 0;
        velocityY = -1;
    }
    else if (e.code == "ArrowDown" && velocityY != -1) {
        velocityX = 0;
        velocityY = 1;
    }
    else if (e.code == "ArrowLeft" && velocityX != 1) {
        velocityX = -1;
        velocityY = 0;
    }
    else if (e.code == "ArrowRight" && velocityX != -1) {
        velocityX = 1;
        velocityY = 0;
    }
}


function placeFood() {
    //(0-1) * cols -> (0-19.9999) -> (0-19) * 25
    foodX = Math.floor(Math.random() * cols) * blockSize;
    foodY = Math.floor(Math.random() * rows) * blockSize;
}
HTML, Webseite, CSS, JavaScript, Code, Programmiersprache, Webentwicklung

Meistgelesene Beiträge zum Thema JavaScript