<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-R">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Cabbage Water Intake Animation</title>
<style>
body { sans-serif; display: flex; flex-direction: column; align-items: center; 20px; background-color: #f0f0f0; }
#chartContainer { position: relative; width: 800px; height: 500px; background-color: #fff; border: 1px solid #ccc; box-shadow: 2px 2px 5px rgba(0,0,0,0.1); }
canvas { display: block; }
.controls { 20px; padding: 15px; background-color: #e9e9e9; border-radius: 8px; box-shadow: 1px 1px 3px rgba(0,0,0,0.1); }
.controls fieldset { border: 1px solid #ccc; 10px; padding: 10px; }
.controls legend { font-weight: bold; }
.controls button, .controls select, .controls input { 5px; padding: 8px 12px; border-radius: 4px; border: 1px solid #aaa; }
.controls button { background-color: #4CAF50; color: white; cursor: pointer; }
.controls button:hover { background-color: #45a049; }
.controls button.stop { background-color: #f44336; }
.controls button.stop:hover { background-color: #da190b; }
.controls button.reset { background-color: #008CBA; }
.controls button.reset:hover { background-color: #007ba7; }
#functionDisplay { position: absolute; top: 10px; right: 10px; background-color: rgba(255,255,255,0.8); padding: 5px; border: 1px solid #ddd; font-size: 0.9em; }
#valueDisplay { 10px; font-weight: bold; }
</style>
</head>
<body>
<h1>Cabbage Cumulative Water Intake</h1>
<div id="chartContainer">
<canvas id="cabbageChart"></canvas>
<div id="functionDisplay">
Curve: <i>f(day) = 35 / (1 + e<sup>-0.1(day - 45)</sup>)</i> Liters
</div>
</div>
<div class="controls">
<fieldset>
<legend>Differentiation (Rate of Intake)</legend>
<button id="runDiff">Today intake faster?</button>
<button id="stopDiff" class="stop">Stop</button>
<button id="resumeDiff">Resume</button>
<button id="resetDiff" class="reset">Reset</button>
<label for="speedDiff">Speed:</label>
<select id="speedDiff">
<option value="0.25">0.25x</option>
<option value="0.5">0.5x</option>
<option value="0.75">0.75x</option>
<option value="1" selected>1x</option>
</select>
<div id="diffValueDisplay" class="valueDisplay"></div>
</fieldset>
<fieldset>
<legend>Integration (Area Under Curve)</legend>
<label for="dayStart">Start Day:</label>
<input type="number" id="dayStart" value="10" min="0" max="99">
<label for="dayEnd">End Day:</label>
<input type="number" id="dayEnd" value="70" min="1" max="100">
<button id="runInteg">Total water these duration</button>
<button id="stopInteg" class="stop">Stop</button>
<button id="resumeInteg">Resume</button>
<button id="resetInteg" class="reset">Reset</button>
<label for="speedInteg">Speed:</label>
<select id="speedInteg">
<option value="0.25">0.25x</option>
<option value="0.5">0.5x</option>
<option value="0.75">0.75x</option>
<option value="1" selected>1x</option>
</select>
<div id="integValueDisplay" class="valueDisplay"></div>
</fieldset>
</div>
<script>
const canvas = document.getElementById('cabbageChart');
const ctx = canvas.getContext('2d');
const chartContainer = document.getElementById('chartContainer');
const diffValueDisplay = document.getElementById('diffValueDisplay');
const integValueDisplay = document.getElementById('integValueDisplay');
// --- Configuration ---
const config = {
{ top: 40, right: 50, bottom: 60, left: 60 }, // Increased right margin for 35kg label
width: 800,
height: 500,
maxX: 100, // Max days
maxY: 40, // Max Liters (cumulative)
animationBaseSpeed: 0.2, // days per frame at 1x speed
tangentLineLength: 50 // pixels on each side of the point
};
canvas.width = config.width;
canvas.height = config.height;
const graphWidth = config.width - config.margin.left - config.margin.right;
const graphHeight = config.height - config.margin.top - config.margin.bottom;
// --- Mathematical Functions ---
function cumulativeWaterIntake(day) {
if (day <= 0) return 0;
return 35 / (1 + Math.exp(-0.1 * (day - 45)));
}
function rateOfWaterIntake(day) { // Derivative
if (day <= 0) return 0;
const expTerm = Math.exp(-0.1 * (day - 45));
return (3.5 * expTerm) / Math.pow(1 + expTerm, 2);
}
function getDayForMilestone(litersTarget) {
if (litersTarget <= 0) return 0.01; // Return a tiny positive day for 0 or less
if (litersTarget >= 35) { // Max capacity of the logistic function
return config.maxX; // Place this milestone at the end of the graph
}
// Formula: day = day_inflection - (1/k) * ln((L / Y_target) - 1)
let day = 45 - (1/0.1) * Math.log((35 / litersTarget) - 1);
if (day < 0 && litersTarget > 0) day = 0.01; // Ensure day is positive if liters > 0
if (day > config.maxX) day = config.maxX; // Cap at maxX
return day;
}
// Numerical integration using Trapezoidal rule
function N_integrate(func, a, b, N_steps = 100) {
if (a >=b) return 0;
const h = (b - a) / N_steps;
let sum = 0.5 * (func(a) + func(b));
for (let i = 1; i < N_steps; i++) {
sum += func(a + i * h);
}
return sum * h;
}
// --- Coordinate Transformation ---
function T_x(day) {
return config.margin.left + (day / config.maxX) * graphWidth;
}
function T_y(liters) {
return config.margin.top + graphHeight - (liters / config.maxY) * graphHeight;
}
function T_inv_x(canvasX) {
return ((canvasX - config.margin.left) / graphWidth) * config.maxX;
}
// --- Drawing Functions ---
function drawAxes() {
ctx.beginPath();
ctx.strokeStyle = '#333';
ctx.lineWidth = 1;
ctx.moveTo(config.margin.left, config.margin.top);
ctx.lineTo(config.margin.left, config.height - config.margin.bottom);
ctx.moveTo(config.margin.left, config.height - config.margin.bottom);
ctx.lineTo(config.width - config.margin.right, config.height - config.margin.bottom);
ctx.stroke();
ctx.fillStyle = '#333';
ctx.textAlign = 'center';
ctx.fillText("Day", config.width / 2, config.height - config.margin.bottom / 3);
ctx.save();
ctx.translate(config.margin.left / 2.5, config.height / 2);
ctx.rotate(-Math.PI / 2);
ctx.fillText("Cumulative Water Intake (Liters)", 0, 0);
ctx.restore();
ctx.textAlign = 'center';
ctx.textBaseline = 'top';
for (let i = 0; i <= config.maxX; i += 10) {
const x = T_x(i);
ctx.moveTo(x, config.height - config.margin.bottom);
ctx.lineTo(x, config.height - config.margin.bottom + 5);
ctx.fillText(i, x, config.height - config.margin.bottom + 10);
}
ctx.textAlign = 'right';
ctx.textBaseline = 'middle';
for (let i = 0; i <= config.maxY; i += 5) {
const y = T_y(i);
ctx.moveTo(config.margin.left, y);
ctx.lineTo(config.margin.left - 5, y);
ctx.fillText(i, config.margin.left - 10, y);
}
ctx.stroke();
}
function drawMainCurve() {
ctx.beginPath();
ctx.strokeStyle = 'black';
ctx.lineWidth = 3;
const startDay = 0.01;
ctx.moveTo(T_x(startDay), T_y(cumulativeWaterIntake(startDay)));
for (let day = startDay; day <= config.maxX; day += 0.5) {
ctx.lineTo(T_x(day), T_y(cumulativeWaterIntake(day)));
}
ctx.stroke();
}
function drawMilestones() {
const milestoneWeights = [1, 5, 10, 15, 20, 25, 30, 35];
ctx.fillStyle = '#007bff'; // Dot color
ctx.font = 'bold 12px Arial'; // Milestone text font
milestoneWeights.forEach(weight => {
// Assume 1kg weight corresponds to 'weight' Liters of cumulative water intake
const litersForMilestone = weight;
let dayAtMilestone = getDayForMilestone(litersForMilestone);
// The Y-value for the dot should be the actual curve's value at 'dayAtMilestone'
// This ensures the dot is ON the curve.
let yValueOnCurve = cumulativeWaterIntake(dayAtMilestone);
// If dayAtMilestone was capped (e.g., to config.maxX for 35kg)
// yValueOnCurve will be the curve's value at config.maxX.
// If 35kg is desired to be exactly at 35L mark and day is config.maxX:
if (weight === 35 && dayAtMilestone === config.maxX) {
// Ensure the Y value for the 35kg dot corresponds to 35L if the curve reaches it or is asymptotic to it.
// If curve doesn't reach 35L by config.maxX, this will be slightly below.
// For our function, cumulativeWaterIntake(config.maxX) is the correct value.
}
const x = T_x(dayAtMilestone);
const y = T_y(yValueOnCurve);
ctx.beginPath();
ctx.arc(x, y, 4, 0, 2 * Math.PI);
ctx.fill();
ctx.fillStyle = '#333'; // Text color
const labelText = `${weight}kg`;
const textMetrics = ctx.measureText(labelText);
let textX = x;
let textY = y - 8; // Default: centered above the dot
ctx.textAlign = 'center';
ctx.textBaseline = 'bottom';
// Adjust text position for milestones near edges
if (textY - 12 < config.margin.top) { // If text goes above top margin
textY = y + 8 + 12; // Place below the dot
ctx.textBaseline = 'top';
}
if (x - textMetrics.width / 2 < config.margin.left + 5) { // Too close to left edge
ctx.textAlign = 'left';
textX = x + 8; // Place to the right of the dot
} else if (x + textMetrics.width / 2 > config.width - config.margin.right - 5) { // Too close to right edge
ctx.textAlign = 'right';
textX = x - 8; // Place to the left of the dot
}
// Special precise placement for 35kg label if at far right
if (weight === 35 && dayAtMilestone >= config.maxX * 0.98) { // If it's at the very end
ctx.textAlign = 'right';
textX = x - 6; // To the left of the dot
ctx.textBaseline = 'middle';
textY = y; // Vertically centered with the dot
}
ctx.fillText(labelText, textX, textY);
ctx.fillStyle = '#007bff'; // Reset dot color
});
}
function clearCanvas() {
ctx.clearRect(0, 0, config.width, config.height);
}
function redrawBaseGraph() {
clearCanvas();
drawAxes();
drawMainCurve();
drawMilestones();
}
// --- Differentiation Animation State & Functions ---
let diffState = {
running: false,
currentDay: 0.1,
speedMultiplier: 1,
animationFrameId: null
};
function drawTangent(day) {
const y_val = cumulativeWaterIntake(day);
const gradient = rateOfWaterIntake(day);
const canvasX = T_x(day);
const canvasY = T_y(y_val);
ctx.beginPath();
ctx.fillStyle = 'red';
ctx.arc(canvasX, canvasY, 5, 0, 2 * Math.PI);
ctx.fill();
const m_canvas = gradient * (-graphHeight / config.maxY) * (config.maxX / graphWidth);
const dx_canvas = config.tangentLineLength;
const dy_canvas = m_canvas * dx_canvas;
ctx.beginPath();
ctx.strokeStyle = 'red';
ctx.lineWidth = 2;
ctx.moveTo(canvasX - dx_canvas, canvasY - dy_canvas);
ctx.lineTo(canvasX + dx_canvas, canvasY + dy_canvas);
ctx.stroke();
ctx.fillStyle = 'red';
ctx.font = '12px Arial';
ctx.textAlign = 'center';
ctx.fillText(`${gradient.toFixed(3)} L/day`, canvasX, canvasY - 15);
diffValueDisplay.textContent = `Day: ${day.toFixed(1)}, Rate: ${gradient.toFixed(3)} L/day`;
}
function updateDifferentiation() {
if (!diffState.running) return;
diffState.currentDay += config.animationBaseSpeed * diffState.speedMultiplier;
if (diffState.currentDay > config.maxX) {
diffState.currentDay = config.maxX;
diffState.running = false;
document.getElementById('runDiff').disabled = false;
document.getElementById('stopDiff').disabled = true;
document.getElementById('resumeDiff').disabled = true;
}
redrawBaseGraph();
drawTangent(diffState.currentDay);
if (diffState.running) {
diffState.animationFrameId = requestAnimationFrame(updateDifferentiation);
}
}
document.getElementById('runDiff').addEventListener('click', () => {
if (diffState.currentDay >= config.maxX) diffState.currentDay = 0.1;
diffState.running = true;
diffState.speedMultiplier = parseFloat(document.getElementById('speedDiff').value);
if (diffState.animationFrameId) cancelAnimationFrame(diffState.animationFrameId);
updateDifferentiation();
document.getElementById('runDiff').disabled = true;
document.getElementById('stopDiff').disabled = false;
document.getElementById('resumeDiff').disabled = true;
});
document.getElementById('stopDiff').addEventListener('click', () => {
diffState.running = false;
if (diffState.animationFrameId) cancelAnimationFrame(diffState.animationFrameId);
document.getElementById('runDiff').disabled = true;
document.getElementById('stopDiff').disabled = true;
document.getElementById('resumeDiff').disabled = false;
});
document.getElementById('resumeDiff').addEventListener('click', () => {
if (diffState.currentDay < config.maxX) {
diffState.running = true;
diffState.speedMultiplier = parseFloat(document.getElementById('speedDiff').value);
if (diffState.animationFrameId) cancelAnimationFrame(diffState.animationFrameId);
updateDifferentiation();
document.getElementById('runDiff').disabled = true;
document.getElementById('stopDiff').disabled = false;
document.getElementById('resumeDiff').disabled = true;
}
});
document.getElementById('resetDiff').addEventListener('click', () => {
diffState.running = false;
if (diffState.animationFrameId) cancelAnimationFrame(diffState.animationFrameId);
diffState.currentDay = 0.1;
redrawBaseGraph();
diffValueDisplay.textContent = '';
document.getElementById('runDiff').disabled = false;
document.getElementById('stopDiff').disabled = true;
document.getElementById('resumeDiff').disabled = true;
});
document.getElementById('speedDiff').addEventListener('change', (e) => {
diffState.speedMultiplier = parseFloat(e.target.value);
});
// --- Integration Animation State & Functions ---
let integState = {
running: false,
startDay: 10,
endDay: 70,
currentFillDay: 10,
speedMultiplier: 1,
animationFrameId: null,
totalArea: 0
};
function shadeArea(day1, day2_current_fill) {
if (day1 >= day2_current_fill) return;
ctx.beginPath();
ctx.fillStyle = 'rgba(0, 255, 0, 0.3)';
const startX = T_x(day1);
const startY_bottom = T_y(0);
ctx.moveTo(startX, startY_bottom);
ctx.lineTo(startX, T_y(cumulativeWaterIntake(day1)));
const steps = Math.max(10, Math.floor((day2_current_fill - day1) * 2));
for (let i = 0; i <= steps; i++) {
const day = day1 + (day2_current_fill - day1) * (i / steps);
ctx.lineTo(T_x(day), T_y(cumulativeWaterIntake(day)));
}
ctx.lineTo(T_x(day2_current_fill), startY_bottom);
ctx.closePath();
ctx.fill();
const currentArea = N_integrate(cumulativeWaterIntake, day1, day2_current_fill, 100);
const midDay = (day1 + day2_current_fill) / 2;
const midYValueOnCurve = cumulativeWaterIntake(midDay);
const midYCanvas = T_y(midYValueOnCurve / 2); // Approx middle height of shaded area
ctx.fillStyle = 'darkgreen';
ctx.font = 'bold 12px Arial';
ctx.textAlign = 'center';
// Ensure text is visible within shaded area or graph bounds
let areaTextY = midYCanvas;
if (areaTextY > config.height - config.margin.bottom - 15) areaTextY = config.height - config.margin.bottom - 15;
if (areaTextY < config.margin.top + 15) areaTextY = config.margin.top + 15;
ctx.fillText(`${currentArea.toFixed(2)} Liter-days`, T_x(midDay), areaTextY);
integValueDisplay.textContent = `Area from Day ${day1.toFixed(1)} to ${day2_current_fill.toFixed(1)}: ${currentArea.toFixed(2)} Liter-days`;
if (day2_current_fill >= integState.endDay) {
integValueDisplay.textContent += ` (Total: ${integState.totalArea.toFixed(2)} Liter-days)`;
}
}
function updateIntegration() {
if (!integState.running) return;
integState.currentFillDay += config.animationBaseSpeed * integState.speedMultiplier;
if (integState.currentFillDay > integState.endDay) {
integState.currentFillDay = integState.endDay;
integState.running = false;
document.getElementById('runInteg').disabled = false;
document.getElementById('stopInteg').disabled = true;
document.getElementById('resumeInteg').disabled = true;
}
redrawBaseGraph();
shadeArea(integState.startDay, integState.currentFillDay);
if (integState.running) {
integState.animationFrameId = requestAnimationFrame(updateIntegration);
}
}
document.getElementById('runInteg').addEventListener('click', () => {
integState.startDay = parseFloat(document.getElementById('dayStart').value);
integState.endDay = parseFloat(document.getElementById('dayEnd').value);
if (integState.startDay >= integState.endDay || integState.startDay < 0 || integState.endDay > config.maxX) {
alert("Invalid day range for integration. Ensure Start Day < End Day, and within 0-" + config.maxX + " days.");
return;
}
integState.currentFillDay = integState.startDay;
integState.totalArea = N_integrate(cumulativeWaterIntake, integState.startDay, integState.endDay, 200);
integState.running = true;
integState.speedMultiplier = parseFloat(document.getElementById('speedInteg').value);
if (integState.animationFrameId) cancelAnimationFrame(integState.animationFrameId);
updateIntegration();
document.getElementById('runInteg').disabled = true;
document.getElementById('stopInteg').disabled = false;
document.getElementById('resumeInteg').disabled = true;
});
document.getElementById('stopInteg').addEventListener('click', () => {
integState.running = false;
if (integState.animationFrameId) cancelAnimationFrame(integState.animationFrameId);
document.getElementById('runInteg').disabled = true;
document.getElementById('stopInteg').disabled = true;
document.getElementById('resumeInteg').disabled = false;
});
document.getElementById('resumeInteg').addEventListener('click', () => {
if (integState.currentFillDay < integState.endDay) {
integState.running = true;
integState.speedMultiplier = parseFloat(document.getElementById('speedInteg').value);
if (integState.animationFrameId) cancelAnimationFrame(integState.animationFrameId);
updateIntegration();
document.getElementById('runInteg').disabled = true;
document.getElementById('stopInteg').disabled = false;
document.getElementById('resumeInteg').disabled = true;
}
});
document.getElementById('resetInteg').addEventListener('click', () => {
integState.running = false;
if (integState.animationFrameId) cancelAnimationFrame(integState.animationFrameId);
integState.startDay = parseFloat(document.getElementById('dayStart').value);
integState.endDay = parseFloat(document.getElementById('dayEnd').value);
integState.currentFillDay = integState.startDay;
redrawBaseGraph();
integValueDisplay.textContent = '';
document.getElementById('runInteg').disabled = false;
document.getElementById('stopInteg').disabled = true;
document.getElementById('resumeInteg').disabled = true;
});
document.getElementById('speedInteg').addEventListener('change', (e) => {
integState.speedMultiplier = parseFloat(e.target.value);
});
// --- Initial Draw ---
redrawBaseGraph();
</script>
</body>
</html>