文字定位源码

简介: 文字定位源码

源码:

<!DOCTYPE html>
<!--
    Author:苏一恒
    Date:2019/10/9 14:10
    Motto:
        The best time to plant trees is ten years ago, followed by now. 
        种树最好的时间是十年前,其次是现在。
 -->
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>文字定位</title>
    <style>
        body {
            background: #eeeeee;
        }
 
        #canvas {
            background: #ffffff;
            margin-top: 5px;
            margin-left: 10px;
            -webkit-box-shadow: 4px 4px 8px rgba(0, 0, 0, 0.5);
            -moz-box-shadow: 4px 4px 8px rgba(0, 0, 0, 0.5);
            box-shadow: 4px 4px 8px rgba(0, 0, 0, 0.5);
            border: 1px solid rgba(0, 0, 0, 0.2);
        }
    </style>
</head>
<body>
<canvas id='canvas' width='780' height='440'>canvas not supports</canvas>
 
<script>
    'use strict';
 
    let canvas = document.getElementById('canvas'),
        context = canvas.getContext('2d'),
        fontHeight = 24,
        alignValues = ['start','center','end'],
        baseLineValue = ['top','middle','bottom','alphabetic', 'ideographic', 'hanging'],
        x,y;
 
    //Function……
 
    let drawGrid=(color, stepx, stepy)=> {
        context.save();
 
        context.strokeStyle = color;
        context.fillStyle = '#ffffff';
        context.lineWidth = 0.5;
        context.fillRect(0, 0, context.canvas.width, context.canvas.height);
 
        for (let 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 (let i = stepy + 0.5; i < context.canvas.height; i += stepy) {
            context.beginPath();
            context.moveTo(0, i);
            context.lineTo(context.canvas.width, i);
            context.stroke();
        }
 
        context.restore();
    };
 
    /**
     * 绘制(x,y)的标记点
     */
    let drawTextMarker=()=>{
      context.fillStyle='yellow';
      context.fillRect(x,y,7,7);
      context.strokeRect(x,y,7,7);
    };
 
    /**
     * 绘制文本
     * @param text
     * @param textAlign
     * @param textBaseLine
     */
    let drawText=(text,textAlign,textBaseLine)=>{
        if (textAlign)context.textAlign = textAlign;
        if (textBaseLine)context.textBaseline=textBaseLine;
 
        context.fillStyle='cornflowerblue';
        context.fillText(text,x,y);
    };
 
    /**
     * 绘制文本的线
     */
    let drawTextLine=()=>{
        context.strokeStyle='gray';
 
        context.beginPath();
        context.moveTo(x,y);
        context.lineTo(x+738,y);
        context.stroke();
    };
 
 
    //Init……
    context.font='oblique normal bold 24px palatino';
 
    drawGrid('lightgray',10,10);
 
    for (let align = 0;align<alignValues.length;++align){
        for (let baseline = 0;baseline<baseLineValue.length;baseline++){
            x= 20+align*fontHeight*15;
            y=20+baseline*fontHeight*3;
 
            drawText(alignValues[align]+'/'+baseLineValue[baseline],alignValues[align],baseLineValue[baseline]);
            drawTextMarker();
            drawTextLine();
        }
    }
 
</script>
</body>
</html>
相关文章
搜索和替换PPT里面指定字体文字的(某些字体无法随演示文稿一起保存)解决方案
搜索和替换PPT里面指定字体文字的(某些字体无法随演示文稿一起保存)解决方案
147 0
|
JavaScript
文字、图片左右无缝滚动效果--支持拖拽js效果demo效果示例(整理)
文字、图片左右无缝滚动效果--支持拖拽js效果demo效果示例(整理)
|
前端开发 JavaScript
前端——把一大段文字显示在前端并且有文字下面有虚线
前端——把一大段文字显示在前端并且有文字下面有虚线
|
Web App开发 文字识别 JavaScript
强大的实况文本功能,直接复制图片上的文字(macOS)
用OCR软件,是很多人的选择。我曾经也给大家推荐过“天若OCR”软件,还有朋友在用吗?
339 0
|
定位技术
百度标注地图markers图片icon不正常显示的样式冲突解决方案
百度标注地图markers图片icon不正常显示的样式冲突解决方案
156 0
|
搜索推荐 JavaScript 定位技术
百度地图绘制地区的棱柱效果-定位-自定义点-文本标记-信息弹窗
百度地图绘制地区的棱柱效果-定位-自定义点-文本标记-信息弹窗
211 0
|
Java
全网首发:JDK绘制文字:六、字符对应的字体图像加载流程
全网首发:JDK绘制文字:六、字符对应的字体图像加载流程
91 0
|
开发者
图片与文字的修改| 学习笔记
快速学习图片与文字的修改。
104 0
图片与文字的修改| 学习笔记
|
移动开发 前端开发 小程序
【笔记】纯css实现列表水平滑动(图片或文字内容不限)
纯css实现列表水平滑动(图片或文字内容不限)
291 0