3.1_使用渐变色及图案来填充文本

本文详细探讨了如何在文本中应用渐变色和图案进行填充,包括具体的实现方法和技术要点,旨在帮助读者掌握这一视觉效果的创建技巧。

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

3.1_使用渐变色及图案来填充文本

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>使用渐变色及图案来填充文本</title>
        <style>
            body{
                background: #eee;
            }
            #canvas{
                background: #fff;
                cursor: pointer;
                margin-left: 10px;
                margin-top: 10px;
                box-shadow: 4px 4px 8px rgba(0,0,0,0.5);
                -webkit-box-shadow: 4px 4px 8px rgba(0,0,0,0.5);
                -moz-box-shadow: 4px 4px 8px rgba(0,0,0,0.5);
            }
        </style>
    </head>
    <body>
        <canvas id="canvas" width="600" height="400"></canvas>
    </body>
    <script>
        var canvas = document.getElementById('canvas');
        var context = canvas.getContext('2d');
        var image = new Image();
        var gradient = context.createLinearGradient(0,0,canvas.width,canvas.height);
        var text = 'Canvas';
        var pattern; //英文为图案

        //初始化
        image.src = 'img/1.png';
        context.font = '128px palatino';
        context.strokeStyle = 'cornflowerblue';

        context.shadowColor = 'rgba(0,0,0,0.8)';
        context.shadowOffsetX = 5;
        context.shadowOffsetY = 5;
        context.shadowBlur = 10;

        gradient.addColorStop(0,'blue');
        gradient.addColorStop(0.25,'orange');
        gradient.addColorStop(0.5,'white');
        gradient.addColorStop(0.75,'red');
        gradient.addColorStop(1.0,'yellow');

        drawBackground();
        drawGradientText();
        //事件控制器
        image.onload = function (e){
            pattern = context.createPattern(image,'repeat');
            drawPatternText();
        }
        //绘制背景
        function drawBackground(){
            context.save();
            context.shadowColor = undefined;
            context.shadowOffsetX = 0;
            context.shadowOffsetY = 0;
            context.shadowBlur = 0;

            var step_y = 12;
            var left_margin = step_y*4;
            var top_margin = step_y*3;
            var i = canvas.height;

            context.strokeStyle = 'lightgray';
            context.lineWidth = 0.5;

            while(i>top_margin){
                context.beginPath();
                context.moveTo(0,i);
                context.lineTo(canvas.width,i);
                context.stroke();
                i-=step_y;
            }

            context.strokeStyle = 'rgba(100,0,0,0.3)';
            context.beginPath();
            context.lineWidth = 1;
            context.moveTo(left_margin,0);
            context.lineTo(left_margin,canvas.height);
            context.stroke();
            context.restore();
        }
        //渐变填充文本
        function drawGradientText(){
            context.fillStyle = gradient;
            context.fillText(text,65,200);
            context.strokeText(text,65,200);
        }
        //图案填充文本
        function drawPatternText(){
            context.shadowColor = undefined;
            context.shadowOffsetX = 0;
            context.shadowOffsetY = 0;
            context.shadowBlur = 0;
            context.fillStyle = pattern;
            context.fillText(text,65,350);
            context.strokeText(text,65,350);
        }
    </script>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值