canvas绘制坐标轴

效果图如下,

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代码

时间: 2024-10-11 06:16:19

canvas绘制坐标轴的相关文章

用canvas绘制折线图

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>用canvas绘制折线图</title> 6 </head> 7 <body> 8 <canvas id="cv"></canvas> 9 </body> 1

canvas绘制时钟

<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>canvas绘制钟表</title> <style type="text/css"> #canvas{ /*border: 2px solid red;*/ display: block; margin: 0 auto; } </style> </

Android自定义控件 -Canvas绘制折线图(实现动态报表效果)

有时候我们在项目中会遇到使用折线图等图形,Android的开源项目中为我们提供了很多插件,但是很多时候我们需要根据具体项目自定义这些图表,这一篇文章我们一起来看看如何在Android中使用Canvas绘制折线图.先看看绘制的效果: 代码: public class MyView extends View { //坐标轴原点的位置 private int xPoint=60; private int yPoint=260; //刻度长度 private int xScale=8;  //8个单位构

HTML5 Canvas绘制实时时钟

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>try to draw the colock</title> <script src="js/modernizr-1.7.js"></script> <script type="text/javascript"> wind

封装 用canvas绘制直线的函数--面向对象

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>用面向对象的思想 封装 在canvas绘制直线的函数</title> 6 </head> 7 <body> 8 <canvas id="cv"></canvas> 9 &

html5 Canvas绘制图形入门详解

html5,这个应该就不需要多作介绍了,只要是开发人员应该都不会陌生.html5是「新兴」的网页技术标准,目前,除IE8及其以下版本的IE浏览器之外,几乎所有主流浏览器(FireFox.Chrome.Opera.Safari.IE9+)都已经开始支持html5了.除此之外,在移动浏览器市场上,众多的移动浏览器也纷纷展开关于「html5的支持能力以及性能表现」的军备竞赛.html作为革命性的网页技术标准,再加上众多浏览器厂商或组织的鼎力支持,可以想见,html5将会成为未来网页技术的领头羊. ht

用h5中的canvas 绘制八卦图

1 <!doctype html> 2 <html> 3 <head> 4 <meta charset="UTF-8"> 5 <title>canvas绘制八卦图</title> 6 </head> 7 8 <body> 9 <canvas id="canvas" width="600" height="500"><

使用Canvas绘制图形的基本教程

原文地址:http://www.cnblogs.com/picaso/archive/2012/11/26/2789077.html HTML5火的正热,最近有个想法也是要用到HTML的相关功能,所以也要好好学习一把. 好好看了一下Canvas的功能,感觉HTML5在客户端交互的功能性越来越强了,今天看了一下Canvas绘图,下边是几个实例,记下以备后用. 1.使用Canvas绘制直线: <!doctype html> <html> <head> <meta ch

Canvas绘制线条模糊的解决方案

前段时间,做一个跨平台app项目,需要绘制分时图和K线图.找了很多开源的js的图表库,包括echarts.highcharts等等,都不是很满意,原因有2: 1.太臃肿,我实际上只要一个分时和一个K线图表,最多搭配几个线形图 2.不满足需求.主要就是分时图,国内玩的js图表库,几乎都没有分时图.都是用1分钟线的收盘价线来做的,和中国股民的使用习惯完全不搭界. 多年前有人开源了一个js绘制股票图形的库,叫做html54stock,图像表现上很符合中国人的使用习惯,但是也有问题: 1.封装不好,很多