怎样用Javascript画个实心圆和计算器10
发布网友
发布时间:2023-10-09 09:19
我来回答
共3个回答
热心网友
时间:2024-11-26 22:05
1、JS画圆
<script type="text/javascript">
//功能:画实心圆
//参数:圆心坐标,半径,精确度,背景颜色
//主要用到了中学时的圆的一些特征公式呵呵
function SolidCircle(centreX, centreY, radius, precision, color){
var cx = Math.abs(parseInt(centreX));
var cy = Math.abs(parseInt(centreY));
var r = parseInt(radius<2 ? 60 : radius);
var p = parseInt(precision<1 ? 1 : precision);
var c = color;
var y;
for(var x=cx-r; x<=cx+r; x+=p){
y = cy - Math.sqrt(Math.pow(r, 2) - Math.pow(cx - x, 2));
document.write('<img style="background:'+c+'; border:1 solid '+c+'; width:'+p+'; height:'+parseInt(2*Math.sqrt(Math.pow(r, 2) - Math.pow(cx - x, 2)))+'; position:absolute; top:'+parseInt(y)+'; left:'+parseInt(x)+';">');
}
//alert("一共有 " + document.all.length + " 个 <img>");
}
SolidCircle(450, 100, 100, 1, "green");
</script>
2、计算器的连加原理其实和连续计算的原理是一样的,所以,在JS里面一定要定义一个全局变量,不论是加减乘除都调用这个变量即可。下面是一段完整的计算器代码,你可以参考一下,也可以解决你的问题。
<FORM name="Keypad" action="">
<TABLE>
<B>
<TABLE border=2 width=50 height=60 cellpadding=1 cellspacing=5>
<TR>
<TD colspan=3 align=middle>
<input name="ReadOut" type="Text" size=24 value="0" width=100%>
</TD>
<TD
</TD>
<TD>
<input name="btnClear" type="Button" value=" C " onclick="Clear()">
</TD>
<TD><input name="btnClearEntry" type="Button" value=" CE " onclick="ClearEntry()">
</TD>
</TR>
<TR>
<TD>
<input name="btnSeven" type="Button" value=" 7 " onclick="NumPressed(7)">
</TD>
<TD>
<input name="btnEight" type="Button" value=" 8 " onclick="NumPressed(8)">
</TD>
<TD>
<input name="btnNine" type="Button" value=" 9 " onclick="NumPressed(9)">
</TD>
<TD>
</TD>
<TD>
<input name="btnNeg" type="Button" value=" +/- " onclick="Neg()">
</TD>
<TD>
<input name="btnPercent" type="Button" value=" % " onclick="Percent()">
</TD>
</TR>
<TR>
<TD>
<input name="btnFour" type="Button" value=" 4 " onclick="NumPressed(4)">
</TD>
<TD>
<input name="btnFive" type="Button" value=" 5 " onclick="NumPressed(5)">
</TD>
<TD>
<input name="btnSix" type="Button" value=" 6 " onclick="NumPressed(6)">
</TD>
<TD>
</TD>
<TD align=middle><input name="btnPlus" type="Button" value=" + " onclick="Operation('+')">
</TD>
<TD align=middle><input name="btnMinus" type="Button" value=" - " onclick="Operation('-')">
</TD>
</TR>
<TR>
<TD>
<input name="btnOne" type="Button" value=" 1 " onclick="NumPressed(1)">
</TD>
<TD>
<input name="btnTwo" type="Button" value=" 2 " onclick="NumPressed(2)">
</TD>
<TD>
<input name="btnThree" type="Button" value=" 3 " onclick="NumPressed(3)">
</TD>
<TD>
</TD>
<TD align=middle><input name="btnMultiply" type="Button" value=" * " onclick="Operation('*')">
</TD>
<TD align=middle><input name="btnDivide" type="Button" value=" / " onclick="Operation('/')">
</TD>
</TR>
<TR>
<TD>
<input name="btnZero" type="Button" value=" 0 " onclick="NumPressed(0)">
</TD>
<TD>
<input name="btnDecimal" type="Button" value=" . " onclick="Decimal()">
</TD>
<TD colspan=3>
</TD>
<TD>
<input name="btnEquals" type="Button" value=" = " onclick="Operation('=')">
</TD>
</TR>
</TABLE>
</TABLE>
</B>
</FORM>
</CENTER>
<font face="Verdana, Arial, Helvetica" size=2>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
var FKeyPad = document.Keypad;
var Accum = 0;
var FlagNewNum = false;
var PendingOp = "";
function NumPressed (Num) {
if (FlagNewNum) {
FKeyPad.ReadOut.value = Num;
FlagNewNum = false;
}
else {
if (FKeyPad.ReadOut.value == "0")
FKeyPad.ReadOut.value = Num;
else
FKeyPad.ReadOut.value += Num;
}
}
function Operation (Op) {
var Readout = FKeyPad.ReadOut.value;
if (FlagNewNum && PendingOp != "=");
else
{
FlagNewNum = true;
if ( '+' == PendingOp )
Accum += parseFloat(Readout);
else if ( '-' == PendingOp )
Accum -= parseFloat(Readout);
else if ( '/' == PendingOp )
Accum /= parseFloat(Readout);
else if ( '*' == PendingOp )
Accum *= parseFloat(Readout);
else
Accum = parseFloat(Readout);
FKeyPad.ReadOut.value = Accum;
PendingOp = Op;
}
}
function Decimal () {
var curReadOut = FKeyPad.ReadOut.value;
if (FlagNewNum) {
curReadOut = "0.";
FlagNewNum = false;
}
else
{
if (curReadOut.indexOf(".") == -1)
curReadOut += ".";
}
FKeyPad.ReadOut.value = curReadOut;
}
function ClearEntry () {
FKeyPad.ReadOut.value = "0";
FlagNewNum = true;
}
function Clear () {
Accum = 0;
PendingOp = "";
ClearEntry();
}
function Neg () {
FKeyPad.ReadOut.value = parseFloat(FKeyPad.ReadOut.value) * -1;
}
function Percent () {
FKeyPad.ReadOut.value = (parseFloat(FKeyPad.ReadOut.value) / 100) * parseFloat(Accum);
}
// End -->
</SCRIPT>
补充哦,怕在百度hi上给你的留言你看不到呵呵
----------------------------------------
那个圆也是没有问题的,因为给学生上课讲过这个算法,它有些类似于微积分中圆的面积求法,其实是将圆沿X轴进行细分,细分的精度由precision决定,所以,当你在这段代码中将精确降低的话,圆的边界就很粗糙,把细分后的每一块把它看成一个图形,这样累加起来,不就组成一个实习圆了嘛(截止到这儿,讲的是算法问题)。然后呢,代码的精髓在于那个For循环,(var x=cx-r; x<=cx+r; x+=p)表示变量是从圆的最左边开始的[因为圆心cx-半径r得到最左边,下同],到最右边结束[cx+r],而每次增加多少呢,是由精度控制的[x+=p],然后循环体内的代码的精髓是图片的宽度和高度那块儿,宽度当然就等于精度了,高度的那个公式y = sqrt(r^2 - x^2)是画圆的必备公式,你应该明白吧。好了,到此为止,这段代码的原理和实现我都说到这儿了,希望你能明白。
热心网友
时间:2024-11-26 22:05
计算器参考如下:
运算符号选项形式,可以避免输入错误:<html>
<body>
第一个数<input type="text" value="" width="100" id="num1"><br>
运算符号<select id="fh">
<option value="+">加</option>
<option value="-">减</option>
<option value="*">乘</option>
<option value="/">除</option>
</select><br>
第二个数<input type="text" value="" width="100" id="num2"><br>
运算结果<input type="text" value="" width="100" id="jg" ><br><br>
<input type="button" value=" 计算 " onclick="js()">
<script>
function js(){
var num1=document.getElementById("num1").value
var num2=document.getElementById("num2").value
var fh=document.getElementById("fh").value
var jg=document.getElementById("jg")
if(!isNaN(num1) && !isNaN(num2)){
if(fh=="+")jg.value=Number(num1)+Number(num2)
if(fh=="-")jg.value=Number(num1)-Number(num2)
if(fh=="*")jg.value=Number(num1)*Number(num2)
if(fh=="/")jg.value=Number(num1)/Number(num2)
}
}
</script>
</body>
</html>
JS画实心圆参考如下:
<script type="text/javascript">
//功能:画实心圆
//参数:圆心坐标,半径,精确度,背景颜色
function SolidCircle(centreX, centreY, radius, precision, color){
var cx = Math.abs(parseInt(centreX));
var cy = Math.abs(parseInt(centreY));
var r = parseInt(radius<2 ? 60 : radius);
var p = parseInt(precision<1 ? 1 : precision);
var c = color;
var y;
for(var x=cx-r; x<=cx+r; x+=p){
y = cy - Math.sqrt(Math.pow(r, 2) - Math.pow(cx - x, 2));
document.write('<img style="background:'+c+'; border:1 solid '+c+'; width:'+p+'; height:'+parseInt(2*Math.sqrt(Math.pow(r, 2) - Math.pow(cx - x, 2)))+'; position:absolute; top:'+parseInt(y)+'; left:'+parseInt(x)+';">');
}
//alert("一共有 " + document.all.length + " 个 <img>");
}
SolidCircle(450, 100, 100, 1, "green");
</script>
热心网友
时间:2024-11-26 22:06
一楼真是位高手