【Canvas/数学】抛物线y=x^2/256的图像及特征

图像:

代码:

<!DOCTYPE html>
<html lang="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
     <title>抛物线x^2=256y</title>
     <style type="text/css">
     .centerlize{
        margin:0 auto;
        border:0px solid red;
        width:1200px;height:600px;
     }
     </style>
     </head>

     <body οnlοad="draw();">
        <div class="centerlize">
            <canvas id="myCanvas" width="1200px" height="600px" style="border:1px dashed black;">
                您的浏览器尚不支持HTML5 Canvas,请更换浏览器再试.
            </canvas>
        </div>
     </body>
</html>
<script type="text/javascript">
<!--
// 画布宽度
const WIDTH=1200;

// 画布高度
const HEIGHT=600;

// 画布环境
var context=0;   

// 舞台对象
var stage;

// 消逝的时间
var timeElapsed=0;

// 核心勾画函数,由body_onload调用
function draw(){
    // 画图前初始化
    var canvas=document.getElementById('myCanvas');    
    canvas.width=WIDTH;
    canvas.height=HEIGHT; 
    context=canvas.getContext('2d');   
    
    // 进行屏幕坐标系到笛卡尔坐标系的变换
    // 处置完成前,原点在左上角,向右为X正向,向下为Y的正向
    // 处置完毕后,原点移动到画布中央,向右为X正向,向上为Y的正向
    context.translate(WIDTH/2,HEIGHT/2);
    context.rotate(getRad(180));
    context.scale(-1,1);

    // 之后再移动原点和改变横纵比例
    // 进行坐标原点的平移
    //context.translate(0,0);
    // 进行横纵方向的比例转换
    //context.scale(1,1);

    // 初始化舞台
    stage=new Stage();

    // 开始动画
    animate();
};

//-------------------------------
// 画图
//-------------------------------
function animate(){    
    timeElapsed+=1;// 时间每轮增加1

    stage.update(timeElapsed);
    stage.paintBg(context);
    stage.paint(context);

    if(timeElapsed<500){        
        window.requestAnimationFrame(animate);
    }
}

//-------------------------------
// 舞台对象定义处
//-------------------------------
function Stage(){
    var obj=new Object;

    obj.prpty={"x":-300,"y":0,"points1":[]};

    // 随时间更新
    obj.update=function(t){
        obj.prpty.x+=1;// 横坐标递增
        obj.prpty.y=(obj.prpty.x*obj.prpty.x)/256;
        var arr={"x":obj.prpty.x,"y":obj.prpty.y};
        this.prpty.points1.push(arr);
    };

    // 画前景
    obj.paint=function(ctx){  
        
        paintCurve(ctx,"maroon",this.prpty.points1);

        // 绘制焦点到当前点的连线
        ctx.save();
        ctx.lineWidth=0.25;
        ctx.strokeStyle='navy';
        ctx.fillStyle='navy';

        ctx.beginPath();
        ctx.moveTo(this.prpty.x, this.prpty.y);
        ctx.lineTo(0, 64);
        ctx.stroke();
        ctx.closePath();

        ctx.beginPath();
        ctx.moveTo(this.prpty.x, this.prpty.y);
        ctx.lineTo(this.prpty.x, -64);
        ctx.stroke();
        ctx.closePath();

        ctx.restore();

        // 绘制准线
        ctx.save();
        ctx.lineWidth=0.25;
        ctx.strokeStyle='red';
        ctx.fillStyle='red';

        ctx.beginPath();
        ctx.moveTo(-300, -64);
        ctx.lineTo(300, -64);
        ctx.stroke();
        ctx.closePath();

        ctx.restore();
    };

        // 画背景
    obj.paintBg=function(ctx){
        // 清屏
        ctx.clearRect(-600,-300,1200,600);

        // 画X轴
        drawAxisX(ctx,-600,600,50);

        // 画Y轴
        drawAxisY(ctx,-300,300,50);

        // 标注文字
        drawText(ctx,"曲线类型:抛物线",-500,250);
        drawText(ctx,"标准方程:x^2=256y(p=128)",-500,230);
        drawText(ctx,"解析式:y=x^2/256",-500,210);
        drawText(ctx,"顶点:(0,0)",-500,190);
        drawText(ctx,"对称轴:y轴",-500,170);
        drawText(ctx,"焦点:(0,64)",-500,150);
        drawText(ctx,"准线方程:y=-64",-500,130);
        drawText(ctx,"离心率:e=1",-500,110);

        drawText(ctx,"逆火",-500,70);
        drawText(ctx,"2023/9/8",-500,50);

        // 写当前点坐标
        drawText(ctx," 当前 X:"+this.prpty.x.toFixed(3)+"  Y:"+this.prpty.y.toFixed(3),300,-85);

    };
    
    return obj;
}

// 连点成线画曲线
function paintCurve(ctx,color,cds){
    var SU=1;// Scale Unit

    ctx.strokeStyle = color;
    ctx.beginPath();        
    for(var i=0; i<cds.length; i++){
        let y=cds[i].y;
        if(y<300 && y>-300){ // y指超出范围就不画了
            ctx.lineTo(cds[i].x*SU,cds[i].y*SU);
        }
    }         
    ctx.stroke();
    ctx.closePath();
}

// 画横轴
function drawAxisX(ctx,start,end,step){
    ctx.save();
    
    ctx.lineWidth=0.25;
    ctx.strokeStyle='navy';
    ctx.fillStyle='navy';

    // 画轴
    ctx.beginPath();
    ctx.moveTo(start, 0);
    ctx.lineTo(end, 0);
    ctx.stroke();
    ctx.closePath();

    // 画箭头
    ctx.beginPath();
    ctx.moveTo(end-Math.cos(getRad(15))*10, Math.sin(getRad(15))*10);
    ctx.lineTo(end, 0);
    ctx.lineTo(end-Math.cos(getRad(15))*10, -Math.sin(getRad(15))*10);
    ctx.stroke();
    ctx.closePath();
    
    // 画刻度
    var x,y;
    y=5;
    for(x=start;x<end;x+=step){
        ctx.beginPath();
        ctx.moveTo(x, 0);
        ctx.lineTo(x, y);
        
        ctx.stroke();
        ctx.closePath();

        drawText(ctx,x/1+"",x,y-20);
    }

    ctx.restore();
}

// 画纵轴
function drawAxisY(ctx,start,end,step){
    ctx.save();
    
    ctx.lineWidth=0.5;
    ctx.strokeStyle='navy';
    ctx.fillStyle='navy';

    // 画轴
    ctx.beginPath();
    ctx.moveTo(0, start);
    ctx.lineTo(0, end);
    ctx.stroke();
    ctx.closePath();

    // 画箭头
    ctx.beginPath();
    ctx.moveTo(Math.sin(getRad(15))*10, end-Math.cos(getRad(15))*10);
    ctx.lineTo(0, end);
    ctx.lineTo(-Math.sin(getRad(15))*10, end-Math.cos(getRad(15))*10);
    ctx.stroke();
    ctx.closePath();
    
    // 画刻度
    var x,y;
    x=5;
    for(y=start;y<end;y+=step){
        ctx.beginPath();
        ctx.moveTo(x, y);
        ctx.lineTo(0, y);
        
        drawText(ctx,y/1+"",x-15,y);

        ctx.stroke();
        ctx.closePath();
    }
}

//-------------------------------
// 角度得到弧度
//-------------------------------
function getRad(degree){
    return degree/180*Math.PI;
}

//-------------------------------
// 得到颜色
//-------------------------------
function getColor(index){
    var arr=["red","purple","blue","green","skyblue","yellow","#aa0000",
             "orange","maroon","navy",
             "lime","teal","fuchsia",
             "aqua","black"];

    if(index>arr.length){
        index=index % arr.length;
    }

    return arr[index];
}

//-------------------------------------
// 绘制文字,不指定颜色
//-------------------------------------
function drawText(ctx,text,x,y){
    ctx.save();
    ctx.translate(x,y)
    ctx.rotate(getRad(180))
    ctx.scale(-1,1)

    ctx.textBaseline="bottom";
    ctx.textAlign="center";
    ctx.fillText(text,0,0);
    ctx.restore();
}

//-------------------------------------
// 绘制文字,指定颜色
//-------------------------------------
function drawText2(ctx,text,x,y,color){
    ctx.save();
    ctx.translate(x,y)
    ctx.rotate(getRad(180))
    ctx.scale(-1,1)

    ctx.textBaseline="bottom";
    ctx.textAlign="center";
    ctx.fillStyle=color;
    ctx.fillText(text,0,0);
    ctx.restore();
}
//-->
</script>

END

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import QtQuick 2.4 import QtQuick.Controls 2.5 import QtQuick.Window 2.3 ApplicationWindow { visible: true width: 800 height: 600 title: "Drawing Board Example" Item { width: 400 height: 400 property int gridSize: 20 property int scaleFactor: 100 Canvas { id: canvas anchors.fill: parent onPaint: { var ctx = getContext("2d"); var width = canvas.width; var height = canvas.height; // 清除画布 ctx.clearRect(0, 0, width, height); // 绘制网格线 ctx.strokeStyle = "black"; ctx.lineWidth = 0.2; // 将线宽改小 for (var x = -width/2; x <= width/2; x += parent.gridSize) { ctx.beginPath(); ctx.moveTo(x, -height/2); ctx.lineTo(x, height/2); ctx.stroke(); } for (var y = -height/2; y <= height/2; y += parent.gridSize) { ctx.beginPath(); ctx.moveTo(-width/2, y); ctx.lineTo(width/2, y); ctx.stroke(); } } } MouseArea { anchors.fill: parent property int gridSize: parent.gridSize property int scaleFactor: parent.scaleFactor onWheel: { // 根据滚轮事件的delta属性,计算缩放比例 parent.scaleFactor += wheel.angleDelta.y / 120; parent.scaleFactor = Math.max(parent.scaleFactor, 10); // 最小值为10% // 根据缩放比例重新计算gridSize和canvas的宽度和高度 var newGridSize = parent.scaleFactor / 5; var newWidth = width * parent.scaleFactor / 100; var newHeight = height * parent.scaleFactor / 100; var xDiff = (newWidth - width) / 2; // 计算横向偏移量 var yDiff = (newHeight - height) / 2; // 计算纵向偏移量 parent.gridSize = newGridSize; canvas.width = newWidth; canvas.height = newHeight; canvas.x -= xDiff; // 调整canvas的位置 canvas.y -= yDiff; canvas.requestPaint(); } } } }这段代码的画布大小相对而言变得太小了,请找出问题所在,并改进
06-09

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值