Canvas Grids 的简单实现

下面的代码在HTML Canvas中绘制了坐标轴及坐标格线Grids。

<!DOCTYPE html>

<html>
    <head>
        <title>Canvas Grid</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        
        <script type="text/javascript">
            var GRID_PADDING = 28;
            
            var LONG_MARK_LENGTH = 5;
            var SHORT_MARK_LENGTH = LONG_MARK_LENGTH / 2;
            var LONG_MARK_GAP = 50;
            
            var GRID_LENGTH = LONG_MARK_GAP / 2;
            
            var GRID_STROKE_STYLE = "#999";
            
            var ARROW_ANGLE = 30;
            var ARROW_LENGTH = 10;
            
            function init() {
                var canvas = document.getElementById("canvas");
                var ctx = canvas.getContext("2d");
                
                canvas.width = document.body.clientWidth;
                canvas.height = window.innerHeight - 105;

                ctx.translate(GRID_PADDING, GRID_PADDING);
                
                drawXAxis(ctx);
                drawYAxis(ctx);
                drawGrids(ctx);
                
                doDrawing(ctx);
            }
            
            function doDrawing(ctx) {
                ctx.beginPath();
                
                ctx.rect(50, 50, 100, 100);
                
                ctx.fillStyle = "hsla(160, 80%, 20%, 0.8)";
                ctx.font = "40px serif";
                ctx.textBaseline = "middle";
                ctx.textAlign = "center";
                
                ctx.fillText(&#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值