JSFiddle - React, Tailwind, and code Playground
by IamSleepy32
HTML
<div id="container">
<p id="header">Mortgage Loan Calculator</p>
<hr>
<hr>
<div id="introduction">
<p id="text"> Calculate your loan's monthly payment rate</p>
</div>
<div id="chart">
<canvas id="canvas" width="400" height="250"></canvas>
<p id="caption"> Loan Balance, Cumulative Equity, and Intrest Payments </p>
</div>
<hr id="seg">
<table>
<tbody><tr>
<th> Enter Loan Data:</th>
<td></td>
</tr>
<tr>
<th> Amount of the loan ($): </th>
<td>
<input id="amount">
</td>
</tr>
<tr>
<td>Anual Intrest (%):</td>
<td> <input id="apr"> </td>
</tr>
<tr>
<td>Repayment Period (years)</td>
<td> <input id="years"> </td>
</tr>
<tr>
<th> Approximate Payments: </th>
<td>
<button onclick="calculate();"> Calculate </button>
</td>
</tr>
<tr>
<td>Monthly Payment:</td>
<td> $
<span class="output" id="payment"></span>
</td>
</tr>
<tr>
<td>Total Payment:</td>
<td> $
<span class="output" id="total"></span>
</td>
</tr>
<tr>
<td>Total Intrest:</td>
<td> $
<span class="output" id="totalintrest"></span>
</td>
</tr>
</tbody>
</table>
</div>
CSS
#container {
border: 1px solid black;
height: 62em;
margin: auto;
overflow: hidden;
width: 57%;
}
#header {
font-size: 135%;
font-style: italic;
left: 2%;
margin: 0;
padding: 0;
position: relative;
}
hr {
width: 99%;
}
#introduction {
border: 1px solid black;
height: 15.65em;
left: 22.9%;
position: absolute;
top: 4.55em;
width: 23.5%;
}
#text {
}
#chart {
height: 19em;
left: 47%;
position: absolute;
top: 11.5%;
width: 30.2%;
}
#graph {
height: 15.6em;
left: 2.4%;
position: absolute;
top: 0;
width: 96.2%;
}
#caption {
left: 2%;
position: absolute;
top: 81%;
}
#seg {
height: 0.1em;
left: 26%;
position: absolute;
top: 57%;
width: 47%;
}
table {
border: 2px solid black;
height: 31em;
left: 23%;
position: absolute;
top: 60%;
width: 32%;
}
th {
vertical-align: top;
}
td {
vertical-align: top;
}
.output {
font-weight: bold;
}
#payment {
text-decoration: underline;
}
#graph {
border: 1px solid black;
}
JavaScript
function calculate(){
var amount=document.getElementById("amount");
var apr=document.getElementById("apr");
var years=document.getElementById("years");
var payment=document.getElementById("payment");
var total=document.getElementById("total");
var totalintrest=document.getElementById("totalintrest");
var principal= parseFloat(amount.value);
var intrest= parseFloat(apr.value)/100/12;
var payments= parseFloat(years.value)*12;
var x= Math.pow(1+intrest,payments);
var monthly= (principal*x*intrest)/(x-1);
if(isFinite){
payment.innerHTML=monthly.toFixed(2);
total.innerHTML=(monthly*payments).toFixed(2);
totalintrest.innerHTML=((monthly*payments)-principal).toFixed(2);
}
else{
payment.innerHTML="";
total.innerHTML="";
totalintrest.innerHTML="";
}
}
// This is the beginning of the canvas and the place where im having issues linking up
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var cx = 200;
var cy =100;
var FPS = 30;
function toRadians(deg) {
return deg * Math.PI / 180;
}
var blue={
radius:50,
startAngle:0,
endAngle:90,
draw: function (){
ctx.beginPath();
ctx.moveTo(cx,cy);
ctx.arc(cx,cy,this.radius,toRadians(this.startAngle ),toRadians(this.endAngle));
ctx.lineTo(cx,cy);
ctx.fillStyle = 'lightblue';
ctx.closePath();
ctx.fill();
}
};
var green={
radius: 50,
startAngle:90,
endAngle:180,
draw: function (){
ctx.beginPath();
ctx.moveTo(cx,cy);
ctx.arc(cx,cy,this.radius,toRadians(this.startAngle ),toRadians(this.endAngle));
ctx.lineTo(cx,cy);
ctx.closePath();
ctx.fillStyle = 'darkseagreen';
ctx.fill();
}
};
var red={
radius: 50,
startAngle:180,
endAngle:270,
draw: function (){
ctx.beginPath();
ctx.moveTo(cx,cy);
ctx.arc(cx,cy,this.radius,toRadians(this.startAngle ...