我正在尝试使用HTML5构建一个半复杂且水平对称的形状。在尝试完成它时,我意识到如果我可以复制一半形状并镜像移动它以将两个图像连接起来,那将会更容易。我找到了如何镜像和移动形状的示例,但没有找到如何复制它的示例。
显然,我希望不需要两个单独的画布元素。
以下是我的参考代码:
var canvas = document.getElementById(id),
context = canvas.getContext("2d"),
color,
height = 50;
width = 564;
arrowWidth = 40,
arrowHeight = 15,
arrowStart = height - arrowHeight,
edgeCurveWidth = 50;
if (parseInt(id.substr(-1), 10) % 2) {
color = "#F7E5A5";
} else {
color = "#FFF";
}
context.beginPath();
context.lineWidth = 4;
context.strokeStyle = "#BAAA72";
context.moveTo(0, 0);
context.quadraticCurveTo(-10, arrowStart, edgeCurveWidth, arrowStart);
context.quadraticCurveTo(width/2 - arrowWidth/2 - 15, arrowStart - 15, width/2 - arrowWidth/2, arrowStart);
context.quadraticCurveTo(width/2, height, width/2, height);
context.stroke();
context.lineTo(width/2, 0);
context.closePath();
context.fillStyle = color;
context.fill();
save
,restore
) 来添加镜像效果(使用transform
或者scale
+translate
),再次调用该函数即可。详情请见 https://developer.mozilla.org/zh-CN/docs/Web/API/Canvas_API/Tutorial/Transformations - Zeta