<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>canvas 面绘制</title>
<style>
#canvas {
background-color: #f9f9f9;
}
</style>
</head>
<body>
<canvas id="canvas">浏览器不支持 canvas </canvas>
</body>
<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
canvas.height = 500;
canvas.width = 500;
// 矩形:红色填充、绿色描边
function drawRect(x, y, width, height) {
ctx.fillStyle = "red";
ctx.strokeStyle = "green";
ctx.fillRect(x, y, width, height);
ctx.strokeRect(x, y, width, height);
}
// 三角形:黄色填充、粉色描边
const points = [
{ x: 300, y: 100 },
{ x: 200, y: 200 },
{ x: 400, y: 200 },
];
function drawTria() {
ctx.fillStyle = "yellow";
ctx.strokeStyle = "pink";
ctx.beginPath();
ctx.moveTo(points[0].x, points[0].y);
ctx.lineTo(points[1].x, points[1].y);
ctx.lineTo(points[2].x, points[2].y);
ctx.closePath();
ctx.fill();
ctx.stroke();
}
// 圆形:绿填充、红色描边
function drawCircle(
x,
y,
radius,
startAngle = 0,
endAngle = Math.PI * 2,
anticlockwise = false
) {
ctx.fillStyle = "green";
ctx.strokeStyle = "red";
ctx.beginPath();
// ctx.arc(圆心-x, 圆心-y, 半径-radius, 开始角度-startAngle, 结束角度-endAngle , anticlockwise 逆时针);
ctx.arc(x, y, radius, startAngle, endAngle, anticlockwise);
ctx.fill();
ctx.stroke();
}
drawRect(50, 50, 100, 50);
drawTria(points);
drawCircle(100, 200, 50);
</script>
</html>
canvas 面 绘制
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 解决方案:galaxy s4的webview显示canvas的时候,有两个条件必须满足:1,canvas外层的di...
- 原生打印香吗?当然香啊,window.print() 页头页尾页码统统有,还能用 @media print 设置打...
- 说在前面 Canvas中绘制组件: 参数 scrollregion指定 Canvas 可以被滚动的范围,该选项的值...
- 1、定创建canvas画布 2、绘制直线canvas绘图是基于状态的,然后在调用方法来绘图 效果图: 3、绘制三角...