JSFiddle - React, Tailwind, and code Playground
by Vloxxity
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style>
canvas {
border: 1px solid #000;
}
</style>
<title>Koi Carp Movement</title>
</head>
<body>
<canvas id="myCanvas" width="800" height="600"></canvas>
<script>
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
const fishes = [];
function getRandomColor() {
const colors = ["#FF5E00", "#FFD800", "#FF1300", "#00274D", "#29AB87", "#FF5E00", "#FFD800"];
return colors[Math.floor(Math.random() * colors.length)];
}
function getRandomOffset(limit) {
// Generate a random offset within the given limit
return Math.random() * limit;
}
function createRandomDot(size, dotSize, dotColor, offsetLimit) {
const offset = getRandomOffset(offsetLimit);
return {
size: dotSize,
color: dotColor,
offset: offset
};
}
function Fish(x, y, size, speed, color) {
this.x = x;
this.y = y;
this.size = size;
this.speed = speed;
this.direction = Math.random() * 2 * Math.PI; // Random initial direction
this.color = color;
this.changeDirectionCounter = 0;
// Draw multiple randomly placed dots on the fish body
this.dots = [];
const numDots = Math.floor(Math.random() * 6) + 5; // Random number of dots between 5 and 10
for (let i = 0; i < numDots; i++) {
const dotSize = Math.random() * 10 + 5; // Random dot size between 5 and 15
const dotColor = getRandomColor();
const dotOffsetLimit = this.size / 2 - dotSize / 2; // Limit dot offset to stay within the body
...