canvas笔记-文字渲染

字体以及填充文字

程序运行截图如下:

源码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<canvas id="canvas" style="border: 1px solid #aaa; display: block; margin: 50px auto;">
    当前浏览器不支持canvas
</canvas>

<script>

    window.onload = function(){

        let canvas = document.getElementById("canvas");
        canvas.width = 800;
        canvas.height = 800;
        let context = canvas.getContext("2d");

        context.font = "bold 40px Arial";
        context.fillStyle = "#058";
        context.fillText("中文ABC1234!@#¥%", 40, 100);
    }


</script>

</body>
</html>

其中:

context.font = "bold 40px Arial";
context.fillStyle = "#058";
context.fillText("中文ABC1234!@#¥%", 40, 100);

其中:font指定了加粗bold与字体大小为40px及Arial。

使用fillStyle指定了颜色为#058,使用fillText设置了文字内容,以及再40,100处进行画文字。

 

如果使用描边进行绘画

源码如下:

<script>

    window.onload = function(){

        let canvas = document.getElementById("canvas");
        canvas.width = 800;
        canvas.height = 800;
        let context = canvas.getContext("2d");

        context.font = "bold 40px Arial";
        context.fillStyle = "#058";
        context.fillText("中文ABC1234!@#¥%", 40, 100);

        context.strokeText("中文ABC1234!@#¥%", 40, 200);
    }


</script>

程序运行截图如下:

再fillText与strokeText包含第四个参数,这个参数的意义是限制其宽度:

如下代码:

<script>

    window.onload = function(){

        let canvas = document.getElementById("canvas");
        canvas.width = 800;
        canvas.height = 800;
        let context = canvas.getContext("2d");

        context.font = "bold 40px Arial";
        context.fillStyle = "#058";
        context.fillText("中文ABC1234!@#¥%", 40, 100);
        context.strokeText("中文ABC1234!@#¥%", 40, 200);

        context.fillText("中文ABC1234!@#¥%", 40, 300, 100);
        context.strokeText("中文ABC1234!@#¥%", 40, 400, 100);
    }


</script>

程序运行截图如下:

下面是使用线性梯度进行渲染下:

程序运行截图如下:

源码如下:

window.onload = function(){

    let canvas = document.getElementById("canvas");
    canvas.width = 800;
    canvas.height = 800;
    let context = canvas.getContext("2d");

    context.font = "bold 40px Arial";
    context.fillStyle = "#058";
    context.fillText("中文ABC1234!@#¥%", 40, 100);
    context.strokeText("中文ABC1234!@#¥%", 40, 200);

    context.fillText("中文ABC1234!@#¥%", 40, 300, 100);
    context.strokeText("中文ABC1234!@#¥%", 40, 400, 100);

    let linearGrad = context.createLinearGradient(0, 0, 800, 0);
    linearGrad.addColorStop(0.0, "red");
    linearGrad.addColorStop(0.1, "black");
    linearGrad.addColorStop(0.2, "green");
    linearGrad.addColorStop(0.3, "yellow");
    linearGrad.addColorStop(0.4, "orange");
    linearGrad.addColorStop(0.5, "purple");
    linearGrad.addColorStop(0.6, "teal");
    linearGrad.addColorStop(0.7, "pink");
    linearGrad.addColorStop(1.0, "brown");
    context.fillStyle = linearGrad;
    context.fillText("中文ABC1234!@#¥%", 40, 500);
}

 

下面是使用图片纹理的例子:

程序运行截图如下:

源码如下:

<script>

    window.onload = function(){

        let canvas = document.getElementById("canvas");
        canvas.width = 800;
        canvas.height = 800;
        let context = canvas.getContext("2d");

        context.font = "bold 40px Arial";
        context.fillStyle = "#058";
        context.fillText("中文ABC1234!@#¥%", 40, 100);
        context.strokeText("中文ABC1234!@#¥%", 40, 200);

        context.fillText("中文ABC1234!@#¥%", 40, 300, 100);
        context.strokeText("中文ABC1234!@#¥%", 40, 400, 100);

        let linearGrad = context.createLinearGradient(0, 0, 800, 0);
        linearGrad.addColorStop(0.0, "red");
        linearGrad.addColorStop(0.1, "black");
        linearGrad.addColorStop(0.2, "green");
        linearGrad.addColorStop(0.3, "yellow");
        linearGrad.addColorStop(0.4, "orange");
        linearGrad.addColorStop(0.5, "purple");
        linearGrad.addColorStop(0.6, "teal");
        linearGrad.addColorStop(0.7, "pink");
        linearGrad.addColorStop(1.0, "brown");
        context.fillStyle = linearGrad;
        context.fillText("中文ABC1234!@#¥%", 40, 500);

        let bakgroundImage = new Image();
        bakgroundImage.src = "img/bg.jpg";
        bakgroundImage.onload = function(){

            let pattern = context.createPattern(bakgroundImage, "repeat");
            context.fillStyle = pattern;
            context.font = "bold 60px Arial";
            context.fillText("中文ABC1234!@#¥%", 40, 600);
        }
    }


</script>

 

展开阅读全文
©️2020 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值