canvas的研究

一、Canvas 上下文获取与基础

1. 获取绘图上下文

javascript

ini 复制代码
const canvas = document.getElementById('myCanvas');
const ctx = canvas.getContext('2d');  // 获取2D上下文
// const ctx3d = canvas.getContext('webgl');  // 获取3D上下文(WebGL)

2. Canvas 元素属性

javascript

arduino 复制代码
canvas.width = 800;      // 设置Canvas宽度(像素)
canvas.height = 600;     // 设置Canvas高度(像素)
const width = canvas.width;   // 读取宽度
const height = canvas.height; // 读取高度

🎨 二、基本图形绘制API

1. 矩形绘制

javascript

arduino 复制代码
// 填充矩形
ctx.fillRect(x, y, width, height);

// 描边矩形
ctx.strokeRect(x, y, width, height);

// 清除矩形区域
ctx.clearRect(x, y, width, height);

2. 路径绘制

javascript

scss 复制代码
// 开始新路径
ctx.beginPath();

// 移动笔触
ctx.moveTo(x, y);

// 绘制直线
ctx.lineTo(x, y);

// 绘制圆弧
ctx.arc(x, y, radius, startAngle, endAngle, anticlockwise);
// anticlockwise: true逆时针,false顺时针

// 绘制圆弧(通过控制点)
ctx.arcTo(x1, y1, x2, y2, radius);

// 绘制二次贝塞尔曲线
ctx.quadraticCurveTo(cp1x, cp1y, x, y);

// 绘制三次贝塞尔曲线
ctx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, x, y);

// 绘制矩形路径
ctx.rect(x, y, width, height);

// 闭合路径
ctx.closePath();

// 填充路径
ctx.fill();

// 描边路径
ctx.stroke();

// 判断点是否在路径内
const isInPath = ctx.isPointInPath(x, y);

3. 复杂路径

arduino 复制代码
// 椭圆
ctx.ellipse(x, y, radiusX, radiusY, rotation, startAngle, endAngle, anticlockwise);

// 圆角矩形(需要自定义函数或较新浏览器)
ctx.roundRect(x, y, width, height, [radius]);

三、样式与颜色API

1. 填充与描边样式

javascript

ini 复制代码
// 填充颜色
ctx.fillStyle = 'color';  // 颜色值、渐变或图案
ctx.fillStyle = '#FF0000';
ctx.fillStyle = 'rgba(255, 0, 0, 0.5)';
ctx.fillStyle = 'red';

// 描边颜色
ctx.strokeStyle = 'color';

// 透明度(全局)
ctx.globalAlpha = 0.5;

2. 渐变

javascript

ini 复制代码
// 线性渐变
const linearGrad = ctx.createLinearGradient(x1, y1, x2, y2);
linearGrad.addColorStop(0, 'red');
linearGrad.addColorStop(1, 'blue');
ctx.fillStyle = linearGrad;

// 径向渐变
const radialGrad = ctx.createRadialGradient(x1, y1, r1, x2, y2, r2);
radialGrad.addColorStop(0, 'white');
radialGrad.addColorStop(1, 'black');

3. 图案

javascript

ini 复制代码
const img = new Image();
img.src = 'pattern.png';
img.onload = function() {
    const pattern = ctx.createPattern(img, 'repeat');
    ctx.fillStyle = pattern;
};
// repeat模式: 'repeat', 'repeat-x', 'repeat-y', 'no-repeat'

4. 线条样式

javascript

ini 复制代码
ctx.lineWidth = 5;           // 线条宽度(像素)
ctx.lineCap = 'butt';        // 端点样式: 'butt', 'round', 'square'
ctx.lineJoin = 'miter';      // 交点样式: 'miter', 'round', 'bevel'
ctx.miterLimit = 10;         // 斜接限制
ctx.setLineDash([5, 15]);    // 设置虚线样式
ctx.getLineDash();           // 获取虚线样式
ctx.lineDashOffset = 5;      // 虚线偏移量

四、文本绘制API

1. 文本绘制

javascript

arduino 复制代码
// 填充文本
ctx.fillText(text, x, y [, maxWidth]);

// 描边文本
ctx.strokeText(text, x, y [, maxWidth]);

// 测量文本
const metrics = ctx.measureText(text);
metrics.width;      // 文本宽度
metrics.actualBoundingBoxLeft;   // 左边界距离
metrics.actualBoundingBoxRight;  // 右边界距离

2. 文本样式

javascript

perl 复制代码
ctx.font = 'italic bold 20px Arial';  // 字体样式
ctx.textAlign = 'center';     // 对齐: 'left', 'center', 'right', 'start', 'end'
ctx.textBaseline = 'middle';  // 基线: 'top', 'hanging', 'middle', 'alphabetic', 'ideographic', 'bottom'
ctx.direction = 'ltr';        // 方向: 'ltr', 'rtl', 'inherit'

五、图像操作API

1. 绘制图像

javascript

scss 复制代码
const img = new Image();
img.src = 'image.jpg';

// 基本绘制
ctx.drawImage(img, dx, dy);

// 缩放绘制
ctx.drawImage(img, dx, dy, dWidth, dHeight);

// 切片绘制
ctx.drawImage(img, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight);
// sx,sy: 源图像起始坐标
// sWidth,sHeight: 源图像裁切尺寸
// dx,dy: 目标位置
// dWidth,dHeight: 目标尺寸

2. 图像数据操作

javascript

ini 复制代码
// 获取图像数据
const imageData = ctx.getImageData(sx, sy, sw, sh);
// imageData.data: Uint8ClampedArray [R,G,B,A,R,G,B,A,...]

// 放置图像数据
ctx.putImageData(imageData, dx, dy);

// 创建空白图像数据
const newImageData = ctx.createImageData(width, height);
const newImageData2 = ctx.createImageData(imageData);  // 相同尺寸

六、变形与合成API

1. 坐标变换

javascript

scss 复制代码
// 平移
ctx.translate(x, y);

// 旋转(弧度)
ctx.rotate(angle);

// 缩放
ctx.scale(x, y);

// 变换矩阵
ctx.transform(a, b, c, d, e, f);
// a: 水平缩放  b: 水平倾斜
// c: 垂直倾斜  d: 垂直缩放
// e: 水平移动  f: 垂直移动

// 设置变换矩阵
ctx.setTransform(a, b, c, d, e, f);

// 重置变换
ctx.resetTransform();

2. 状态保存与恢复

javascript

scss 复制代码
// 保存当前状态(样式、变形等)
ctx.save();

// 恢复上次保存的状态
ctx.restore();

3. 合成与裁剪

javascript

perl 复制代码
// 全局合成操作
ctx.globalCompositeOperation = 'source-over';
// 常用值: 'source-over', 'source-in', 'source-out', 
// 'destination-over', 'destination-in', 'destination-out',
// 'lighter', 'copy', 'xor', 'multiply', 'screen', 'overlay',
// 'darken', 'lighten', 'color-dodge', 'color-burn', 
// 'hard-light', 'soft-light', 'difference', 'exclusion',
// 'hue', 'saturation', 'color', 'luminosity'

// 创建裁剪区域
ctx.clip();  // 基于当前路径创建裁剪区域

// 判断点是否在裁剪区域内
const isInClip = ctx.isPointInStroke(x, y);
相关推荐
掘了5 分钟前
「2025 年终总结」在所有失去的人中,我最怀念我自己
前端·后端·年终总结
崔庆才丨静觅9 分钟前
实用免费的 Short URL 短链接 API 对接说明
前端
崔庆才丨静觅30 分钟前
5分钟快速搭建 AI 平台并用它赚钱!
前端
崔庆才丨静觅1 小时前
比官方便宜一半以上!Midjourney API 申请及使用
前端
Moment1 小时前
富文本编辑器在 AI 时代为什么这么受欢迎
前端·javascript·后端
崔庆才丨静觅1 小时前
刷屏全网的“nano-banana”API接入指南!0.1元/张量产高清创意图,开发者必藏
前端
剪刀石头布啊1 小时前
jwt介绍
前端
爱敲代码的小鱼1 小时前
AJAX(异步交互的技术来实现从服务端中获取数据):
前端·javascript·ajax
Cobyte2 小时前
AI全栈实战:使用 Python+LangChain+Vue3 构建一个 LLM 聊天应用
前端·后端·aigc
NEXT062 小时前
前端算法:从 O(n²) 到 O(n),列表转树的极致优化
前端·数据结构·算法