Canvas画文字、渐变文字以及调整文字位置

本文介绍了如何使用Canvas在JavaScript中绘制文字,包括创建渐变文字以及调整文字的位置,通过示例代码展示了具体实现步骤和最终效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一.画文字

在这里插入图片描述

代码如下:

<style>
    canvas {
        margin: 0 auto;
        border: 2px solid #aaa;
        display: block; /*画布居中*/
    }
</style>
<body>
    <canvas id="cont" width="500px" height="500px">Hello Canvas</canvas>   
    <script>
        var canvas = document.querySelector("#cont");
        //获取画布上下文
        var ctx = canvas.getContext('2d');
        //设置字体大小样式
        ctx.font = '100px 宋体';
        //设置字体颜色
        ctx.fillStyle = 'gold';
        //文字内容和位置
        ctx.fillText("hello", 250, 250);

        //绘制空心文字
        ctx.strokeStyle = 'purple';
        ctx.strokeText('你好', 0, 250);

        ctx.fillStyle = 'pink';
        //当前区域最大宽度为300px
        ctx.fillText("hello canvas!!", 100, 100, 300);
    </script>
</body>

结果如下:
在这里插入图片描述

二.画渐变文字

在这里插入图片描述
在这里插入图片描述

代码如下:

<style>
    canvas {
        margin: 0 auto;
        border: 2px solid #aaa;
        display: block; /*画布居中*/
    }
</style>
<body>
    <canvas id="cont" width="500px" height="500px">Hello Canvas</canvas>   
    <script>
        var canvas = document.querySelector("#cont");
        //获取画布上下文
        var ctx = canvas.getContext('2d');
        //设置字体样式
        ctx.font = '100px 宋体';
        
        //设置线性渐变
        var gradient = ctx.createLinearGradient(0, 0, canvas.width, 0);
        //从0到整个区域的一半由黄渐变到蓝
        //从中间到最后由蓝渐变到红
        gradient.addColorStop('0', 'yellow');
        gradient.addColorStop('0.5', 'blue');
        gradient.addColorStop('1.0', 'red');
        //应用渐变,空心文字
        ctx.strokeStyle = gradient;
        ctx.strokeText('Hello Canvas!!!', 0, 300, 500);
        //实心文字
        ctx.fillStyle = gradient;
        ctx.fillText('Hello World!!!', 0, 100, 500);
    </script>
</body>

结果如下:
在这里插入图片描述

三.调整文字位置

在这里插入图片描述
代码如下:

<style>
    canvas {
        margin: 0 auto;
        border: 2px solid #aaa;
        display: block; /*画布居中*/
    }
</style>
<body>
    <canvas id="cont" width="500px" height="500px">Hello Canvas</canvas>   
    <script>
        var canvas = document.querySelector("#cont");
        //获取画布上下文
        var ctx = canvas.getContext('2d');

        //画直线
        ctx.beginPath();
        ctx.moveTo(250, 0);
        ctx.lineTo(250, 500);
        ctx.stroke();
        ctx.closePath();

        ctx.beginPath();
        ctx.moveTo(0, 250);
        ctx.lineTo(500, 250);
        ctx.stroke();
        ctx.closePath();
        //设置字体样式
        ctx.font = '100px 宋体';
        //设置水平文字的位置
        //ctx.textAlign = 'end';  默认为start
        ctx.textAlign = 'center';
        //设置垂直文字的位置
        //ctx.textBaseline = 'top';
        ctx.textBaseline = 'middle';
        ctx.fillText("Hello", 250, 250);

    </script>
</body>

结果如下:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值