make a gollum in javascript

To create a gollum character in JavaScript, we can use the Canvas API to draw and animate a series of graphic elements, like eyes, mouth, body, and limbs. Here's an example of how to make a simple gollum animation using JavaScript:

index.tsx
// Define the gollum object with properties and methods
const gollum = {
  x: 100,   // starting x position
  y: 100,   // starting y position
  angle: 0, // starting angle
  speed: 2, // speed of movement
  draw: function(context) {
    // draw the gollum body
    context.fillStyle = '#4e4e4e';
    context.beginPath();
    context.arc(this.x, this.y, 30, 0, Math.PI * 2);
    context.fill();

    // draw the gollum eyes
    context.fillStyle = '#fff';
    context.beginPath();
    context.arc(this.x - 10, this.y - 10, 5, 0, Math.PI * 2);
    context.arc(this.x + 10, this.y - 10, 5, 0, Math.PI * 2);
    context.fill();

    // draw the gollum mouth
    context.strokeStyle = '#fff';
    context.beginPath();
    context.arc(this.x, this.y + 10, 15, 0, Math.PI, true);
    context.stroke();

    // draw the gollum arms
    context.beginPath();
    context.moveTo(this.x - 20, this.y);
    context.lineTo(this.x - 40, this.y + 20);
    context.stroke();
    context.beginPath();
    context.moveTo(this.x + 20, this.y);
    context.lineTo(this.x + 40, this.y + 20);
    context.stroke();
  },
  update: function() {
    // move the gollum based on angle and speed
    this.x += Math.cos(this.angle) * this.speed;
    this.y += Math.sin(this.angle) * this.speed;

    // change angle randomly to create a wobbling motion
    this.angle += Math.random() * 0.2 - 0.1;

    // keep the gollum inside the canvas bounds
    if (this.x < 30) {
      this.x = 30;
      this.angle = -this.angle;
    }
    if (this.x > canvas.width - 30) {
      this.x = canvas.width - 30;
      this.angle = -this.angle;
    }
    if (this.y < 30) {
      this.y = 30;
      this.angle = Math.PI - this.angle;
    }
    if (this.y > canvas.height - 30) {
      this.y = canvas.height - 30;
      this.angle = Math.PI - this.angle;
    }
  }
};

// Initialize the canvas and gollum animation loop
const canvas = document.createElement('canvas');
document.body.appendChild(canvas);
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const context = canvas.getContext('2d');
function animate() {
  // clear the canvas and redraw the gollum
  context.clearRect(0, 0, canvas.width, canvas.height);
  gollum.update();
  gollum.draw(context);

  // request a new frame of animation
  requestAnimationFrame(animate);
}
animate();
2323 chars
81 lines

In this example, the gollum object has properties for its position, angle, speed, and methods for drawing and updating its graphics. The draw method uses the Canvas API to draw the different parts of the gollum, like the body, eyes, mouth, and arms. The update method moves the gollum based on its angle and speed, and wobbles its motion to create a more lifelike animation. Finally, the animate function re-draws the gollum on each frame of animation, using requestAnimationFrame to schedule the next frame.

gistlibby LogSnag