Here's a basic implementation of a full calculator with a graphing (draw) feature using HTML, CSS, and JavaScript. This example allows users to perform arithmetic operations and plot simple mathematical functions (e.g., x^2, sin(x), sqrt(x)).
✅ Features:
- Basic Calculator: Addition, subtraction, multiplication, division.
- Function Graphing: Plot equations like
x^2, sin(x), log(x), etc.
- Interactive Canvas: Draw graphs in real-time.
🧾 Code Example
1. HTML (index.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Calculator with Graph</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div class="calculator">
<h1>Calculator + Graph</h1>
<input type="text" id="display" disabled />
<div class="buttons">
<button onclick="appendValue('7')">7</button>
<button onclick="appendValue('8')">8</button>
<button onclick="appendValue('9')">9</button>
<button onclick="appendValue('/')">÷</button>
<button onclick="appendValue('4')">4</button>
<button onclick="appendValue('5')">5</button>
<button onclick="appendValue('6')">6</button>
<button onclick="appendValue('*')">×</button>
<button onclick="appendValue('1')">1</button>
<button onclick="appendValue('2')">2</button>
<button onclick="appendValue('3')">3</button>
<button onclick="appendValue('-')">−</button>
<button onclick="appendValue('0')">0</button>
<button onclick="appendValue('.')">.</button>
<button onclick="calculate()">=</button>
<button onclick="appendValue('+')">+</button>
<button onclick="clearDisplay()">C</button>
</div>
<div class="graph-section">
<label for="functionInput">Enter Function (e.g., x^2, sin(x)):</label>
<input type="text" id="functionInput" placeholder="x^2" />
<button onclick="drawGraph()">Draw</button>
<canvas id="graphCanvas" width="600" height="400"></canvas>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
2. CSS (styles.css)
body {
font-family: Arial, sans-serif;
background: #f0f0f0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.calculator {
background: #fff;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0,0,0,0.1);
text-align: center;
}
#display {
width: 100%;
height: 50px;
font-size: 24px;
text-align: right;
padding: 10px;
margin-bottom: 10px;
border: 1px solid #ccc;
border-radius: 5px;
}
.buttons {
display: grid;
grid-template-columns: repeat(4, 1fr);
gap: 10px;
}
button {
padding: 15px;
font-size: 18px;
border: none;
border-radius: 5px;
background: #e0e0e0;
cursor: pointer;
}
button:hover {
background: #d0d0d0;
}
.graph-section {
margin-top: 20px;
}
canvas {
border: 1px solid #000;
margin-top: 10px;
}
3. JavaScript (script.js)
// Calculator Logic
const display = document.getElementById("display");
function appendValue(value) {
display.value += value;
}
function clearDisplay() {
display.value = "";
}
function calculate() {
try {
display.value = eval(display.value);
} catch (e) {
display.value = "Error";
}
}
// Graphing Logic
const canvas = document.getElementById("graphCanvas");
const ctx = canvas.getContext("2d");
const functionInput = document.getElementById("functionInput");
function drawGraph() {
const func = functionInput.value.trim();
if (!func) return;
const width = canvas.width;
const height = canvas.height;
// Clear canvas
ctx.clearRect(0, 0, width, height);
// Draw axes
ctx.beginPath();
ctx.moveTo(0, height / 2);
ctx.lineTo(width, height / 2);
ctx.moveTo(width / 2, 0);
ctx.lineTo(width / 2, height);
ctx.strokeStyle = "#000";
ctx.stroke();
// Plot function
try {
const xScale = width / 10; // 10 units per pixel
const yScale = height / 10;
ctx.beginPath();
ctx.strokeStyle = "blue";
let prevX = -5;
let prevY = evaluateFunction(prevX, func);
for (let x = -5; x <= 5; x += 0.01) {
const y = evaluateFunction(x, func);
const px = (x + 5) * xScale;
const py = height / 2 - y * yScale;
if (prevY !== null) {
ctx.moveTo(prevX * xScale + width / 2, height / 2 - prevY * yScale);
ctx.lineTo(px, py);
}
prevX = x;
prevY = y;
}
ctx.stroke();
} catch (e) {
alert("Invalid function! Example: x^2, sin(x), log(x)");
}
}
function evaluateFunction(x, func) {
try {
const expr = func
.replace(/x/g, `(${x})`)
.replace(/\^/g, "**")
.replace(/sin/g, "Math.sin")
.replace(/cos/g, "Math.cos")
.replace(/tan/g, "Math.tan")
.replace(/log/g, "Math.log10")
.replace(/sqrt/g, "Math.sqrt");
return eval(expr);
} catch (e) {
return null;
}
}
📌 How to Use
- Calculator: Use the buttons to perform arithmetic operations.
- Graph: Enter a function (e.g.,
x^2, sin(x), sqrt(x)) and click Draw.
- The graph is plotted on a coordinate plane with axes.
🧠 Notes
- Supported Functions:
x^2, sin(x), cos(x), tan(x), log(x), sqrt(x).
- Extensibility: Add more functions or improve scaling/zooming for advanced use.
- Error Handling: Basic validation for invalid inputs.
Let me know if you'd like a version with more advanced features (e.g., zoom, multiple functions, or 3D graphs)! 🚀