使用HTML5 Canvas创建图案

使用HTML5 Canvas创建图案

参考: Create a pattern with HTML5 Canvas

HTML5 Canvas为网页提供了一个强大的绘图接口,允许开发者在网页上绘制图形、制作动画、处理图像等。本文将详细介绍如何使用HTML5 Canvas来创建图案,包括基本图形的绘制、颜色和样式的应用、以及如何将这些元素组合成复杂的图案。我们将通过10-20个示例代码,展示如何使用Canvas API来实现这些功能。

基础图形绘制

示例1:绘制一个简单的矩形

<!DOCTYPE html>
<html>
<head>
    <title>绘制矩形 - how2html.com</title>
</head>
<body>
    <canvas id="myCanvas" width="200" height="100" style="border:1px solid #000;"></canvas>
    <script>
        var canvas = document.getElementById('myCanvas');
        var ctx = canvas.getContext('2d');
        ctx.fillStyle = '#FF0000';
        ctx.fillRect(20, 20, 150, 100);
    </script>
</body>
</html>

Output:

使用HTML5 Canvas创建图案

示例2:绘制一个圆形

<!DOCTYPE html>
<html>
<head>
    <title>绘制圆形 - how2html.com</title>
</head>
<body>
    <canvas id="circleCanvas" width="200" height="200" style="border:1px solid #000;"></canvas>
    <script>
        var canvas = document.getElementById('circleCanvas');
        var ctx = canvas.getContext('2d');
        ctx.beginPath();
        ctx.arc(100, 100, 50, 0, 2 * Math.PI);
        ctx.fillStyle = 'blue';
        ctx.fill();
    </script>
</body>
</html>

Output:

使用HTML5 Canvas创建图案

示例3:绘制一条线

<!DOCTYPE html>
<html>
<head>
    <title>绘制线条 - how2html.com</title>
</head>
<body>
    <canvas id="lineCanvas" width="200" height="100" style="border:1px solid #000;"></canvas>
    <script>
        var canvas = document.getElementById('lineCanvas');
        var ctx = canvas.getContext('2d');
        ctx.moveTo(0, 0);
        ctx.lineTo(200, 100);
        ctx.stroke();
    </script>
</body>
</html>

Output:

使用HTML5 Canvas创建图案

颜色和样式

示例4:设置填充和线条颜色

<!DOCTYPE html>
<html>
<head>
    <title>颜色设置 - how2html.com</title>
</head>
<body>
    <canvas id="colorCanvas" width="200" height="200" style="border:1px solid #000;"></canvas>
    <script>
        var canvas = document.getElementById('colorCanvas');
        var ctx = canvas.getContext('2d');
        // 设置填充颜色
        ctx.fillStyle = 'green';
        ctx.fillRect(10, 10, 100, 100);
        // 设置线条颜色
        ctx.strokeStyle = 'red';
        ctx.strokeRect(10, 10, 100, 100);
    </script>
</body>
</html>

Output:

使用HTML5 Canvas创建图案

示例5:使用渐变

<!DOCTYPE html>
<html>
<head>
    <title>渐变效果 - how2html.com</title>
</head>
<body>
    <canvas id="gradientCanvas" width="200" height="200" style="border:1px solid #000;"></canvas>
    <script>
        var canvas = document.getElementById('gradientCanvas');
        var ctx = canvas.getContext('2d');
        var grd = ctx.createLinearGradient(0, 0, 200, 0);
        grd.addColorStop(0, 'red');
        grd.addColorStop(1, 'white');
        ctx.fillStyle = grd;
        ctx.fillRect(10, 10, 150, 80);
    </script>
</body>
</html>

Output:

使用HTML5 Canvas创建图案

示例6:绘制带有阴影的矩形

<!DOCTYPE html>
<html>
<head>
    <title>阴影效果 - how2html.com</title>
</head>
<body>
    <canvas id="shadowCanvas" width="200" height="200" style="border:1px solid #000;"></canvas>
    <script>
        var canvas = document.getElementById('shadowCanvas');
        var ctx = canvas.getContext('2d');
        ctx.shadowOffsetX = 10;
        ctx.shadowOffsetY = 10;
        ctx.shadowBlur = 5;
        ctx.shadowColor = 'rgba(0, 0, 0, 0.5)';
        ctx.fillStyle = 'green';
        ctx.fillRect(20, 20, 100, 100);
    </script>
</body>
</html>

Output:

使用HTML5 Canvas创建图案

复杂图案绘制

示例7:绘制一个简单的图案

<!DOCTYPE html>
<html>
<head>
    <title>图案绘制 - how2html.com</title>
</head>
<body>
    <canvas id="patternCanvas" width="200" height="200" style="border:1px solid #000;"></canvas>
    <script>
        var canvas = document.getElementById('patternCanvas');
        var ctx = canvas.getContext('2d');
        var img = new Image();
        img.onload = function() {
            var pattern = ctx.createPattern(img, 'repeat');
            ctx.fillStyle = pattern;
            ctx.fillRect(0, 0, canvas.width, canvas.height);
        };
        img.src = 'https://how2html.com/images/pattern.png';
    </script>
</body>
</html>

Output:

使用HTML5 Canvas创建图案

示例8:使用路径绘制复杂图形

<!DOCTYPE html>
<html>
<head>
    <title>路径绘制 - how2html.com</title>
</head>
<body>
    <canvas id="pathCanvas" width="200" height="200" style="border:1px solid #000;"></canvas>
    <script>
        var canvas = document.getElementById('pathCanvas');
        var ctx = canvas.getContext('2d');
        ctx.beginPath();
        ctx.moveTo(50, 50);
        ctx.lineTo(150, 50);
        ctx.lineTo(100, 150);
        ctx.closePath();
        ctx.stroke();
    </script>
</body>
</html>

Output:

使用HTML5 Canvas创建图案

示例9:绘制文本

<!DOCTYPE html>
<html>
<head>
    <title>文本绘制 - how2html.com</title>
</head>
<body>
    <canvas id="textCanvas" width="200" height="200" style="border:1px solid #000;"></canvas>
    <script>
        var canvas = document.getElementById('textCanvas');
        var ctx = canvas.getContext('2d');
        ctx.font = '20px Arial';
        ctx.fillText('how2html.com', 50, 100);
    </script>
</body>
</html>

Output:

使用HTML5 Canvas创建图案

示例10:绘制变换图形

<!DOCTYPE html>
<html>
<head>
    <title>变换绘制 - how2html.com</title>
</head>
<body>
    <canvas id="transformCanvas" width="200" height="200" style="border:1px solid #000;"></canvas>
    <script>
        var canvas = document.getElementById('transformCanvas');
        var ctx = canvas.getContext('2d');
        ctx.translate(100, 100);
        ctx.rotate((Math.PI / 180) * 25);
        ctx.fillStyle = 'red';
        ctx.fillRect(-50, -50, 100, 100);
    </script>
</body>
</html>

Output:

使用HTML5 Canvas创建图案

通过上述示例代码,我们可以看到HTML5 Canvas提供了丰富的API来绘制各种图形和图案。从基本的形状绘制到复杂的图案创建,Canvas都能够满足开发者的需求。### 示例11:使用动画

<!DOCTYPE html>
<html>
<head>
    <title>动画示例 - how2html.com</title>
</head>
<body>
    <canvas id="animationCanvas" width="200" height="200" style="border:1px solid #000;"></canvas>
    <script>
        var canvas = document.getElementById('animationCanvas');
        var ctx = canvas.getContext('2d');
        var posX = 0;
        var posY = 100;

        function draw() {
            ctx.clearRect(0, 0, canvas.width, canvas.height);
            ctx.fillStyle = 'blue';
            ctx.fillRect(posX, posY, 50, 50);
            posX += 2;
            if (posX > canvas.width) {
                posX = -50;
            }
            requestAnimationFrame(draw);
        }

        draw();
    </script>
</body>
</html>

Output:

使用HTML5 Canvas创建图案

示例12:绘制贝塞尔曲线

<!DOCTYPE html>
<html>
<head>
    <title>贝塞尔曲线 - how2html.com</title>
</head>
<body>
    <canvas id="bezierCanvas" width="200" height="200" style="border:1px solid #000;"></canvas>
    <script>
        var canvas = document.getElementById('bezierCanvas');
        var ctx = canvas.getContext('2d');
        ctx.beginPath();
        ctx.moveTo(20, 160);
        ctx.bezierCurveTo(20, 100, 200, 100, 200, 160);
        ctx.stroke();
    </script>
</body>
</html>

Output:

使用HTML5 Canvas创建图案

示例13:绘制二次贝塞尔曲线

<!DOCTYPE html>
<html>
<head>
    <title>二次贝塞尔曲线 - how2html.com</title>
</head>
<body>
    <canvas id="quadraticCanvas" width="200" height="200" style="border:1px solid #000;"></canvas>
    <script>
        var canvas = document.getElementById('quadraticCanvas');
        var ctx = canvas.getContext('2d');
        ctx.beginPath();
        ctx.moveTo(20, 160);
        ctx.quadraticCurveTo(100, 0, 200, 160);
        ctx.stroke();
    </script>
</body>
</html>

Output:

使用HTML5 Canvas创建图案

示例14:使用图像

<!DOCTYPE html>
<html>
<head>
    <title>图像使用 - how2html.com</title>
</head>
<body>
    <canvas id="imageCanvas" width="200" height="200" style="border:1px solid #000;"></canvas>
    <script>
        var canvas = document.getElementById('imageCanvas');
        var ctx = canvas.getContext('2d');
        var img = new Image();
        img.onload = function() {
            ctx.drawImage(img, 0, 0, canvas.width, canvas.height);
        };
        img.src = 'https://how2html.com/images/logo.png';
    </script>
</body>
</html>

Output:

使用HTML5 Canvas创建图案

示例15:绘制多边形

<!DOCTYPE html>
<html>
<head>
    <title>多边形绘制 - how2html.com</title>
</head>
<body>
    <canvas id="polygonCanvas" width="200" height="200" style="border:1px solid #000;"></canvas>
    <script>
        var canvas = document.getElementById('polygonCanvas');
        var ctx = canvas.getContext('2d');
        ctx.beginPath();
        ctx.moveTo(100, 10);
        for (let i = 1; i <= 6; i++) {
            ctx.lineTo(100 + 80 * Math.cos(i * 2 * Math.PI / 6), 100 + 80 * Math.sin(i * 2 * Math.PI / 6));
        }
        ctx.closePath();
        ctx.stroke();
    </script>
</body>
</html>

Output:

使用HTML5 Canvas创建图案

示例16:使用剪切路径

<!DOCTYPE html>
<html>
<head>
    <title>剪切路径 - how2html.com</title>
</head>
<body>
    <canvas id="clipCanvas" width="200" height="200" style="border:1px solid #000;"></canvas>
    <script>
        var canvas = document.getElementById('clipCanvas');
        var ctx = canvas.getContext('2d');
        ctx.rect(50, 50, 100, 100);
        ctx.stroke();
        ctx.clip();
        ctx.fillStyle = 'red';
        ctx.fillRect(0, 0, 150, 150);
    </script>
</body>
</html>

Output:

使用HTML5 Canvas创建图案

示例17:绘制圆弧

<!DOCTYPE html>
<html>
<head>
    <title>圆弧绘制 - how2html.com</title>
</head>
<body>
    <canvas id="arcCanvas" width="200" height="200" style="border:1px solid #000;"></canvas>
    <script>
        var canvas = document.getElementById('arcCanvas');
        var ctx = canvas.getContext('2d');
        ctx.beginPath();
        ctx.arc(100, 100, 50, 0, Math.PI / 2);
        ctx.stroke();
    </script>
</body>
</html>

Output:

使用HTML5 Canvas创建图案

示例18:使用全局透明度

<!DOCTYPE html>
<html>
<head>
    <title>全局透明度 - how2html.com</title>
</head>
<body>
    <canvas id="opacityCanvas" width="200" height="200" style="border:1px solid #000;"></canvas>
    <script>
        var canvas = document.getElementById('opacityCanvas');
        var ctx = canvas.getContext('2d');
        ctx.fillStyle = 'red';
        ctx.fillRect(10, 10, 50, 50);
        ctx.globalAlpha = 0.5;
        ctx.fillStyle = 'blue';
        ctx.fillRect(30, 30, 50, 50);
    </script>
</body>
</html>

Output:

使用HTML5 Canvas创建图案

示例19:绘制点和线

<!DOCTYPE html>
<html>
<head>
    <title>点和线绘制 - how2html.com</title>
</head>
<body>
    <canvas id="pointsLinesCanvas" width="200" height="200" style="border:1px solid #000;"></canvas>
    <script>
        var canvas = document.getElementById('pointsLinesCanvas');
        var ctx = canvas.getContext('2d');
        ctx.beginPath();
        ctx.moveTo(10, 10);
        ctx.lineTo(190, 10);
        ctx.moveTo(10, 20);
        ctx.lineTo(190, 20);
        ctx.stroke();
        ctx.beginPath();
        ctx.arc(100, 100, 5, 0, 2 * Math.PI);
        ctx.fill();
    </script>
</body>
</html>

Output:

使用HTML5 Canvas创建图案

示例20:使用图案填充

<!DOCTYPE html>
<html>
<head>
    <title>图案填充 - how2html.com</title>
</head>
<body>
    <canvas id="patternFillCanvas" width="200" height="200" style="border:1px solid #000;"></canvas>
    <script>
        var canvas = document.getElementById('patternFillCanvas');
        var ctx = canvas.getContext('2d');
        var img = new Image();
        img.onload = function() {
            var pattern = ctx.createPattern(img, 'repeat');
            ctx.fillStyle = pattern;
            ctx.fillRect(0, 0, canvas.width, canvas.height);
        };
        img.src = 'https://how2html.com/images/pattern.png';
    </script>
</body>
</html>

Output:

使用HTML5 Canvas创建图案

以上示例展示了HTML5 Canvas的多样化使用方式,从基本的图形绘制到复杂的图案和动画,Canvas API提供了丰富的功能来满足不同的需求。通过这些示例,你可以学习到如何在你的网页项目中利用Canvas来增加视觉效果和动态交互。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程