How do I duplicate a shape in HTML5#39;s canvas?(如何在 HTML5 的画布中复制形状?)
问题描述
我尝试使用 HTML5 构建一个半复杂且水平对称的形状.当我试图完成它时,我意识到如果我可以复制一半的形状,镜像并移动它以将两个图像连接在一起会更容易.我正在寻找有关如何镜像和移动形状的示例,而不是有关如何复制它的示例.
I have a semi-complex and horizontally symmetrical shape I am trying to build using HTML5. While I was trying to finish it I realized it would be easier if I could just duplicate half of the shape, mirror it and move it to join the two images together. I'm finding examples of how to mirror and move a shape, but not on how to duplicate it.
显然,我希望我不需要两个单独的画布元素.
Obviously, I'm hoping I won't need two separate canvas elements.
这是我的参考代码:
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
)并再次调用:
You could just move your shape into a function, call it once, and then use another state (save
, restore
) to add a mirror effect (using transform
or scale
+translate
) and call it again:
function drawHalfShape(context,width, height,arrowWidth,arrowHeight,edgeCurveWidth,color){
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();
}
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";
}
drawHalfShape(context,width,height,arrowWidth,arrowHeight,edgeCurveWidth,color);
context.save();
context.translate(-width/2,0); // these operations aren't commutative
context.scale(-1,0); // these values could be wrong
drawHalfShape(context,width,height,arrowWidth,arrowHeight,edgeCurveWidth,color);
context.restore();
有关示例,请参阅 MDN:转换.
这篇关于如何在 HTML5 的画布中复制形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 HTML5 的画布中复制形状?
基础教程推荐
- 直接将值设置为滑块 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01