html5 canvas:重叠路径未正确填充
HTML5 画布的奇怪问题:我试图在另一个形状内绘制一个形状。外部形状是蓝色,内部形状是红色,但最终结果是两个形状都变成红色。如果我单步执行代码,我可以看到蓝色形状正确渲染,但红色形状渲染在蓝色形状之上,即使它较小。可能是 BeginPath/EndPath 的问题,但我似乎尝试了每种组合,但没有运气。在这之后我还有很多形状要画,所以我需要弄清楚如何在继续工作之前正确地开始/结束形状。任何帮助表示赞赏。
<script type="text/javascript">
window.onload = function () {
var drawingCanvas = document.getElementById('canvas1');
// Is element in the DOM and does browser support canvas
if (drawingCanvas && drawingCanvas.getContext) {
// Init drawing context
var InfieldColor = "#BDB76B";
var OutfieldColor = "#F5F5F5";
var iGrassLen = Math.min(drawingCanvas.width, drawingCanvas.height) * 0.7;
var iRad = iGrassLen * 1.475;
var iAng = -60 * Math.PI / 180;
var iptInfBez0x = iRad * Math.cos(iAng);
var iptInfBez0y = -(iRad * Math.sin(iAng));
iAng = -30 * Math.PI / 180;
var iptInfBez1x = iRad * Math.cos(iAng);
var iptInfBez1y = -(iRad * Math.sin(iAng));
var iInfieldLen = (iGrassLen * (88 / 124));
var iBaseLen = iInfieldLen / 12;
//this is the relative offset between Dixon infield and outfield
var iOutfieldLen = iGrassLen * (282 / 124)
//bezier control points for outfield circle
iRad = iOutfieldLen * 1.31;
iAng = -60 * Math.PI / 180;
var iptOutBez0x = iRad * Math.cos(iAng);
var iptOutBez0y = -(iRad * Math.sin(iAng));
iAng = -30 * Math.PI / 180;
var iptOutBez1x = iRad * Math.cos(iAng);
var iptOutBez1y = -(iRad * Math.sin(iAng));
var iHRLen0 = (340 * iInfieldLen / 90) * 1.025; //iInfieldLen = 90 feet. (plus a fudge factor)
var iHRLen1 = (370 * iInfieldLen / 90) * 1.025;
var iHRLen2 = (400 * iInfieldLen / 90) * 1.025;
var iMoundWid = iInfieldLen / 10;
var context = drawingCanvas.getContext('2d');
context.fillStyle = "#FFFF00";
context.fillRect(0, 0, drawingCanvas.width, drawingCanvas.height);
context.beginPath;
context.moveTo(0, 0);
context.lineTo(iGrassLen, 0);
context.bezierCurveTo(iptInfBez1x, iptInfBez1y, iptInfBez0x, iptInfBez0y, 0, iGrassLen); // bezier curve
context.lineTo(0, 0);
context.closePath();
context.fillStyle = "blue";
context.fill();
context.lineWidth = 1;
context.strokeStyle = "black";
context.stroke();
//infield rectangle
context.beginPath;
context.rect(0, 0, iInfieldLen - (iBaseLen / 4), iInfieldLen - (iBaseLen / 4));
context.closePath;
context.fillStyle = "red";
context.fill();
context.lineWidth = 1;
context.strokeStyle = "black";
context.stroke();
}
}
</script>
strange issue with HTML5 canvas: I am trying to draw one shape inside another. The outer shape is blue and the inner one red, but the end result is that both shapes end up red. If I step through the code, I can see the blue shape rendered correctly, but then the red shape renders over the blue one, even though it's smaller. Probably a problem with BeginPath/EndPath stuff, but I've seemingly tried every combination with no luck. I have lots more shapes to draw after this one, so I need to figure out how to correctly begin/end a shape before I resume work. Any help is appreciated.
<script type="text/javascript">
window.onload = function () {
var drawingCanvas = document.getElementById('canvas1');
// Is element in the DOM and does browser support canvas
if (drawingCanvas && drawingCanvas.getContext) {
// Init drawing context
var InfieldColor = "#BDB76B";
var OutfieldColor = "#F5F5F5";
var iGrassLen = Math.min(drawingCanvas.width, drawingCanvas.height) * 0.7;
var iRad = iGrassLen * 1.475;
var iAng = -60 * Math.PI / 180;
var iptInfBez0x = iRad * Math.cos(iAng);
var iptInfBez0y = -(iRad * Math.sin(iAng));
iAng = -30 * Math.PI / 180;
var iptInfBez1x = iRad * Math.cos(iAng);
var iptInfBez1y = -(iRad * Math.sin(iAng));
var iInfieldLen = (iGrassLen * (88 / 124));
var iBaseLen = iInfieldLen / 12;
//this is the relative offset between Dixon infield and outfield
var iOutfieldLen = iGrassLen * (282 / 124)
//bezier control points for outfield circle
iRad = iOutfieldLen * 1.31;
iAng = -60 * Math.PI / 180;
var iptOutBez0x = iRad * Math.cos(iAng);
var iptOutBez0y = -(iRad * Math.sin(iAng));
iAng = -30 * Math.PI / 180;
var iptOutBez1x = iRad * Math.cos(iAng);
var iptOutBez1y = -(iRad * Math.sin(iAng));
var iHRLen0 = (340 * iInfieldLen / 90) * 1.025; //iInfieldLen = 90 feet. (plus a fudge factor)
var iHRLen1 = (370 * iInfieldLen / 90) * 1.025;
var iHRLen2 = (400 * iInfieldLen / 90) * 1.025;
var iMoundWid = iInfieldLen / 10;
var context = drawingCanvas.getContext('2d');
context.fillStyle = "#FFFF00";
context.fillRect(0, 0, drawingCanvas.width, drawingCanvas.height);
context.beginPath;
context.moveTo(0, 0);
context.lineTo(iGrassLen, 0);
context.bezierCurveTo(iptInfBez1x, iptInfBez1y, iptInfBez0x, iptInfBez0y, 0, iGrassLen); // bezier curve
context.lineTo(0, 0);
context.closePath();
context.fillStyle = "blue";
context.fill();
context.lineWidth = 1;
context.strokeStyle = "black";
context.stroke();
//infield rectangle
context.beginPath;
context.rect(0, 0, iInfieldLen - (iBaseLen / 4), iInfieldLen - (iBaseLen / 4));
context.closePath;
context.fillStyle = "red";
context.fill();
context.lineWidth = 1;
context.strokeStyle = "black";
context.stroke();
}
}
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您忘记了
()
。如果没有它,这些只是被丢弃的对函数的引用,而不是对它的调用。You forgot
()
. Without that, these are just discarded references to a function, not calls to it.