1
respuesta

Les comparto mi codigo con posibilidad de cambiar el tamaño

Les comparto mi codigo el cual permite que aunque se cambie el tam;o de canva se amntenga centrado y en proporción el creeper


<canvas height="400" width="600">


</canvas>


<script>

    function centerRec (recArr){
        recArr[2] = screenDim[0]/2-recArr[0]/2; 
        recArr[3] = screenDim[1]/2-recArr[1]/2; 
    }

    var pantalla = document.querySelector('canvas');
    var pincel = pantalla.getContext('2d'); 


    const screenDim = [
        parseInt(pantalla.getAttribute('width')),
        parseInt(pantalla.getAttribute('height'))
    ];

    var faceDim = [350,300];
    centerRec(faceDim);


    var eyesDim = [90,90];
    var noseDim = [70,100];
    var moustacheDim = [40,110]



    pincel.fillStyle = 'white';
    pincel.fillRect(0,0,screenDim[0],screenDim[1]);

    pincel.fillStyle = 'darkgreen';
    pincel.fillRect(faceDim[2],faceDim[3],faceDim[0],faceDim[1]);

    pincel.fillStyle = 'black';
    pincel.fillRect(//moustache 1
        faceDim[2]+faceDim[0]/2-noseDim[0]/2-moustacheDim[0],
        faceDim[3]+faceDim[1]-moustacheDim[1],
        moustacheDim[0],
        moustacheDim[1]
    );
    pincel.fillRect(//moustache 2
        faceDim[2]+faceDim[0]/2+noseDim[0]/2,
        faceDim[3]+faceDim[1]-moustacheDim[1],
        moustacheDim[0],
        moustacheDim[1]
    );
    pincel.fillRect(//nose
        faceDim[2]+faceDim[0]/2-noseDim[0]/2,
        faceDim[3]+faceDim[1]-moustacheDim[1]-noseDim[1]/2,
        noseDim[0],
        noseDim[1]
    );
    pincel.fillRect(//eye 1
        faceDim[2]+faceDim[0]/2-noseDim[0]/2-eyesDim[0],
        faceDim[3]+faceDim[1]-moustacheDim[1]-noseDim[1]/2-eyesDim[1],
        eyesDim[0],
        eyesDim[1]
    );
    pincel.fillRect(//eye 2
        faceDim[2]+faceDim[0]/2-noseDim[0]/2+noseDim[0],
        faceDim[3]+faceDim[1]-moustacheDim[1]-noseDim[1]/2-eyesDim[1],
        eyesDim[0],
        eyesDim[1]
    );




</script>
1 respuesta

¡Hola Brianca!

Gracias por compartir tu código con nosotros. Parece que has creado un dibujo del Creeper de Minecraft utilizando el elemento canvas de HTML y JavaScript.

En cuanto a tu pregunta, si lo que deseas es permitir que el tamaño del canvas se pueda cambiar y que el Creeper se mantenga centrado y en proporción, puedes utilizar JavaScript para hacerlo. Una forma de hacerlo es utilizando el evento "resize" del objeto window para detectar cuando se cambia el tamaño de la ventana y luego ajustar el tamaño del canvas y las dimensiones del Creeper en consecuencia.

Aquí te dejo un ejemplo de cómo podrías hacerlo:

<canvas id="canvas"></canvas>

<script>
  const canvas = document.getElementById('canvas');
  const context = canvas.getContext('2d');

  // Dimensiones iniciales del Creeper
  let faceWidth = 350;
  let faceHeight = 300;
  let eyesWidth = 90;
  let eyesHeight = 90;
  let noseWidth = 70;
  let noseHeight = 100;
  let moustacheWidth = 40;
  let moustacheHeight = 110;

  function drawCreeper() {
    // Limpia el canvas
    context.clearRect(0, 0, canvas.width, canvas.height);

    // Obtiene las dimensiones de la ventana
    const windowWidth = window.innerWidth;
    const windowHeight = window.innerHeight;

    // Calcula las nuevas dimensiones del Creeper
    const aspectRatio = faceWidth / faceHeight;
    const newFaceWidth = Math.min(windowWidth * 0.8, windowHeight * 0.8 * aspectRatio);
    const newFaceHeight = newFaceWidth / aspectRatio;
    const newEyesWidth = newFaceWidth * eyesWidth / faceWidth;
    const newEyesHeight = newFaceHeight * eyesHeight / faceHeight;
    const newNoseWidth = newFaceWidth * noseWidth / faceWidth;
    const newNoseHeight = newFaceHeight * noseHeight / faceHeight;
    const newMoustacheWidth = newFaceWidth * moustacheWidth / faceWidth;
    const newMoustacheHeight = newFaceHeight * moustacheHeight / faceHeight;

    // Dibuja el Creeper con las nuevas dimensiones
    const faceX = (windowWidth - newFaceWidth) / 2;
    const faceY = (windowHeight - newFaceHeight) / 2;
    context.fillStyle = 'darkgreen';
    context.fillRect(faceX, faceY, newFaceWidth, newFaceHeight);
    context.fillStyle = 'black';
    context.fillRect(faceX + newFaceWidth / 2 - newNoseWidth / 2, faceY + newFaceHeight - newMoustacheHeight, newMoustacheWidth, newMoustacheHeight);
    context.fillRect(faceX + newFaceWidth / 2 + newNoseWidth / 2, faceY + newFaceHeight - newMoustacheHeight, newMoustacheWidth, newMoustacheHeight);
    context.fillRect(faceX + newFaceWidth / 2 - newNoseWidth / 2, faceY + newFaceHeight - newMoustacheHeight - newNoseHeight / 2, newNoseWidth, newNoseHeight);
    context.fillRect(faceX + newFaceWidth / 2 - newNoseWidth / 2 - newEyesWidth, faceY + newFaceHeight - newMoustacheHeight - newNoseHeight / 2 - newEyesHeight, newEyesWidth, newEyesHeight);
    context.fillRect(faceX + newFaceWidth / 2 + newNoseWidth / 2, faceY + newFaceHeight - newMoustacheHeight - newNoseHeight / 2 - newEyesHeight, newEyesWidth, newEyesHeight);
  }

  // Dibuja el Creeper inicialmente
  drawCreeper();

  // Redibuja el Creeper cuando se cambia el tamaño de la ventana
  window.addEventListener('resize', drawCreeper);
</script>

Espero que esto te ayude. ¡Buenos estudios!

Si este post te ayudó, por favor, marca como solucionado ✓. Continúa con tus estudios