canvas绘制坐标轴

效果图如下,

image

 

var canvas = document.getElementById("canvas"),
    context = canvas.getContext("2d"),

    AXIS_MARGIN = 40,    //一个常量
    AXIS_ORIGIN = {x:AXIS_MARGIN,y:canvas.height-AXIS_MARGIN},   //原点坐标

    AXIS_TOP = AXIS_MARGIN,        //纵轴端点
    AXIS_RIGHT = canvas.width-AXIS_MARGIN,//横轴端点

    HORIZONTAL_TICK_SPACING = 10,   //横轴间距
    VERTICAL_TICK_SPACING = 10,    //纵轴间距

    AXIS_WIDTH = AXIS_RIGHT-AXIS_ORIGIN.x,    //横轴长度
    AXIS_HEIGHT=AXIS_ORIGIN.y-AXIS_TOP,       //纵轴长度

    NUM_VERTICAL_TICKS = AXIS_HEIGHT/VERTICAL_TICK_SPACING,    //纵轴标尺的数量
    NUM_HORIZONTAL_TICKS = AXIS_WIDTH/HORIZONTAL_TICK_SPACING,  //横轴标尺的数量

    TICK_WIDTH = 10,
    TICKS_LINEWIDTH = 0.5,
    TICKS_COLOR = "navy",

    AXIS_LINEWIDTH = 1.0,
    AXIS_COLOR = "blue";

//一个函数,由于绘制网格
function drawGrid(context,color,stepx,stepy){
    context.strokeStyle = color;
    context.lineWidth = 0.5;

    for(var i = stepx + 0.5; i < context.canvas.width;i += stepx){
        context.beginPath();
        context.moveTo(i,0);
        context.lineTo(i,context.canvas.height);
        context.stroke();
    }

    for(var i = stepy + 0.5;i < context.canvas.height;i +=stepy){
        context.beginPath();
        context.moveTo(0,i);
        context.lineTo(context.canvas.width,i);
        context.stroke();
    }
}

function drawAxes(){
    context.save();
    context.strokeStyle = AXIS_COLOR;
    context.lineWidth = AXIS_LINEWIDTH;

    drawHorizontalAxis();
    drawVerticalAxis();

    context.lineWidth = 0.5;
    context.lineWidth = TICKS_LINEWIDTH;
    context.strokeStyle = TICKS_COLOR;

    drawHorizontalAxisTicks();
    drawVertialAxisTicks();
    drawNumberals();
}

//横坐标
function drawHorizontalAxis(){
    context.beginPath();
    context.moveTo(AXIS_ORIGIN.x,AXIS_ORIGIN.y);
    context.lineTo(AXIS_RIGHT,AXIS_ORIGIN.y);
    context.stroke();
}

//纵坐标
function drawVerticalAxis(){
    context.beginPath();
    context.moveTo(AXIS_ORIGIN.x,AXIS_ORIGIN.y);
    context.lineTo(AXIS_ORIGIN.x,AXIS_TOP);
    context.stroke();
}

//绘制纵坐标标尺及刻度数
function drawHorizontalAxisTicks(){
    var deltaY,num=0;

    for (var i = 1;i<NUM_HORIZONTAL_TICKS;++i){
        context.beginPath();
        if(i%5===0){
            deltaY = TICK_WIDTH;
            text();
            num++;
        }else {
            deltaY = TICK_WIDTH/2;
        }
        context.moveTo(AXIS_ORIGIN.x + i*HORIZONTAL_TICK_SPACING,AXIS_ORIGIN.y - deltaY);
        context.lineTo(AXIS_ORIGIN.x + i*HORIZONTAL_TICK_SPACING,AXIS_ORIGIN.y + deltaY);
        context.stroke();

        function text(){
            context.font = "12pt Helvetica";
            context.fillText(num,AXIS_ORIGIN.x +(i-6)*HORIZONTAL_TICK_SPACING,AXIS_ORIGIN.y + 3*deltaY);
        }
    }
}

//横坐标标尺及刻度
function drawVertialAxisTicks(){
    var deltaX,num=1;

    for (var i=1;i<NUM_VERTICAL_TICKS;++i){
        context.beginPath();
        if(i % 5 === 0){
            deltaX = TICK_WIDTH;
            text();
            num++;
        }else{
            deltaX = TICK_WIDTH/2;
        }
        context.moveTo(AXIS_ORIGIN.x - deltaX,AXIS_ORIGIN.y - i*VERTICAL_TICK_SPACING);
        context.lineTo(AXIS_ORIGIN.x + deltaX,AXIS_ORIGIN.y - i*VERTICAL_TICK_SPACING);
        context.stroke();

        function text(){
            context.font = "12pt Helvetica";
            context.fillText(num,AXIS_ORIGIN.x - 3*deltaX,AXIS_ORIGIN.y - i*VERTICAL_TICK_SPACING);
        }
    }
}

drawGrid(context,"lightgray",10,10);
drawAxes();
JavaScript代码

 

转载于:https://www.cnblogs.com/jasenchen/p/5513730.html

uniApp的Canvas API允许开发者在Webview组件中创建图形,包括绘制坐标轴。要在uniApp中绘制坐标轴,你可以按照以下步骤操作: 1. **准备环境**: 首先,在uniApp项目中引入`<canvas>`元素,并给它一个id,比如`myCanvas`。 ```html <view class="container"> <canvas id="myCanvas" canvas-id="myCanvas" style="display:none;"></canvas> </view> ``` 2. **获取绘图上下文**: 在JavaScript中,通过`uni.createSelectorQuery()`获取Canvas节点并取得2D渲染上下文: ```javascript const canvas = document.getElementById('myCanvas'); const ctx = canvas.getContext('2d'); ``` 3. **设置坐标系**: 设置绘图区域大小,并确定原点位置。例如,如果你想画一个从0到100的x轴和y轴,可以这样做: ```javascript ctx.clearRect(0, 0, canvas.width, canvas.height); // 清除原有内容 const width = canvas.width; const height = canvas.height; const centerX = width / 2; const centerY = height / 2; // 设置坐标轴范围和间隔 const xAxisMin = 0; const yAxisMin = 0; const xAxisMax = 100; const yAxisMax = 100; const xAxisStep = (xAxisMax - xAxisMin) / 10; // 每个刻度的距离 const yAxisStep = (yAxisMax - yAxisMin) / 10; ``` 4. **绘制坐标线**: 使用`moveTo`和`lineTo`方法绘制x轴和y轴线条: ```javascript // x轴 for (let i = xAxisMin; i <= xAxisMax; i += xAxisStep) { ctx.moveTo(centerX, height); ctx.lineTo(centerX, height - Math.round(i * height / xAxisMax)); } // y轴 for (let j = yAxisMin; j <= yAxisMax; j += yAxisStep) { ctx.moveTo(width, centerY); ctx.lineTo(Math.round(j * width / yAxisMax), centerY); } ``` 5. **添加标签**: 可以利用`fillText`方法在每个刻度上添加数字标签: ```javascript // 添加x轴和y轴标签 const fontSize = 14; for (let i = xAxisMin; i <= xAxisMax; i += xAxisStep) { const labelY = height - Math.round(i * height / xAxisMax) - fontSize / 2; ctx.fillText(`${i}`, centerX, labelY); } for (let j = yAxisMin; j <= yAxisMax; j += yAxisStep) { const labelX = Math.round(j * width / yAxisMax) + fontSize / 2; ctx.fillText(`${j}`, labelX, centerY); } ``` 6. **显示结果**: 最后记得将`canvas`元素显示出来: ```javascript canvas.style.display = 'block'; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值