简单的HTML画布未显示完整形状
我是HTML帆布的新手,我正在尝试使用JavaScript绘制带有不同颜色的圆柱体,我可以看到图纸,但是形状并未完全显示,它应该是带有颜色的完全说明的形状。我试图改变宽度和高度设置,但它伸展了形状本身。
var canvas = document.getElementById("cylinder");
var ctx = canvas.getContext("2d");
drawCylinder(canvas, ctx, "lightblue", "black", 3);
function drawCylinder(canvas, ctx, fill, border, lineWidth, r, centX, centY)
{
//canvas.width = 160;
var centX = 0;
var centY = -100;
var r = 30;
ctx.translate(canvas.width / 2, canvas.height / 2);
ctx.scale(6, 1);
ctx.beginPath();
ctx.arc(centX, centY, r, 0, 2 * Math.PI, false);
ctx.restore();
ctx.fillStyle = 'YellowGreen';
ctx.fill();
ctx.lineWidth = 7;
ctx.strokeStyle = 'GainsBoro';
ctx.stroke();
ctx.save();
ctx.translate(canvas.width / 2, canvas.height / 2);
ctx.scale(6, 1);
ctx.beginPath();
ctx.arc(centX, 100, r, 0, 2 * Math.PI, false);
ctx.restore();
ctx.fillStyle = 'MediumSlateBlue';
ctx.fill();
ctx.lineWidth = 7;
ctx.strokeStyle = 'GainsBoro';
ctx.stroke();
ctx.save();
ctx.lineWidth = 3;
ctx.strokeStyle = 'Black';
ctx.moveTo(105, 150);
ctx.lineTo(105, 352);
ctx.moveTo(474, 150);
ctx.lineTo(474, 352);
ctx.stroke();
ctx.translate(canvas.width / 2, canvas.height / 2);
ctx.scale(6, 1);
ctx.beginPath();
ctx.arc(centX, centY+2, r+0.8, 0, Math.PI, false);
ctx.restore();
ctx.lineWidth = 4;
ctx.strokeStyle = 'Black';
ctx.stroke();
ctx.save();
ctx.translate(canvas.width / 2, canvas.height / 2);
ctx.scale(6, 1);
ctx.beginPath();
ctx.arc(centX, 100+2, r+0.8, 0, Math.PI, false);
ctx.restore();
ctx.lineWidth = 4;
ctx.strokeStyle = 'Black';
}
<div>
<canvas id="cylinder" width="300" height="400"></canvas>
</div>
我不确定为什么会这样显示,您能帮您兄弟吗?
画布应该看起来像下面的图像。
I am new to HTML canvas, I'm trying to draw a cylinder with different colors using JavaScript, I can see the drawing but the shape is not displayed fully, it is supposed to be a fully illustrated shape with colors. I tried to change the width and height settings but it stretched the shape itself.
var canvas = document.getElementById("cylinder");
var ctx = canvas.getContext("2d");
drawCylinder(canvas, ctx, "lightblue", "black", 3);
function drawCylinder(canvas, ctx, fill, border, lineWidth, r, centX, centY)
{
//canvas.width = 160;
var centX = 0;
var centY = -100;
var r = 30;
ctx.translate(canvas.width / 2, canvas.height / 2);
ctx.scale(6, 1);
ctx.beginPath();
ctx.arc(centX, centY, r, 0, 2 * Math.PI, false);
ctx.restore();
ctx.fillStyle = 'YellowGreen';
ctx.fill();
ctx.lineWidth = 7;
ctx.strokeStyle = 'GainsBoro';
ctx.stroke();
ctx.save();
ctx.translate(canvas.width / 2, canvas.height / 2);
ctx.scale(6, 1);
ctx.beginPath();
ctx.arc(centX, 100, r, 0, 2 * Math.PI, false);
ctx.restore();
ctx.fillStyle = 'MediumSlateBlue';
ctx.fill();
ctx.lineWidth = 7;
ctx.strokeStyle = 'GainsBoro';
ctx.stroke();
ctx.save();
ctx.lineWidth = 3;
ctx.strokeStyle = 'Black';
ctx.moveTo(105, 150);
ctx.lineTo(105, 352);
ctx.moveTo(474, 150);
ctx.lineTo(474, 352);
ctx.stroke();
ctx.translate(canvas.width / 2, canvas.height / 2);
ctx.scale(6, 1);
ctx.beginPath();
ctx.arc(centX, centY+2, r+0.8, 0, Math.PI, false);
ctx.restore();
ctx.lineWidth = 4;
ctx.strokeStyle = 'Black';
ctx.stroke();
ctx.save();
ctx.translate(canvas.width / 2, canvas.height / 2);
ctx.scale(6, 1);
ctx.beginPath();
ctx.arc(centX, 100+2, r+0.8, 0, Math.PI, false);
ctx.restore();
ctx.lineWidth = 4;
ctx.strokeStyle = 'Black';
}
<div>
<canvas id="cylinder" width="300" height="400"></canvas>
</div>
I'm not sure why it displays like this, can you please help the brother out.
The canvas should look like an image below.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您有一种非常独特的方法来绘制圆柱体。当然,您可以通过首先绘制一个圆圈并垂直缩放上下文来完成此操作,以最终将圆扩展到一个椭圆,但让我们做一些不同。
开箱即用
CanvasRenderingContext2d
具有一种内置方法,可以绘制一个椭圆,很方便地称为ellipse()
。因此,您可以首先为顶部画一个椭圆,另一个椭圆形,最后用黑色轮廓将它们连接起来。
像:
You have a pretty unique way of drawing a cylinder. Of course you can do it by first drawing a circle and scaling the context vertically afterwards to ultimately stretch the circle to an ellipse but let's do it a little different.
Out of the box the
CanvasRenderingContext2D
has a built-in method to draw an ellipse, conveniently calledellipse()
.So you can start by drawing an ellipse for the top, another ellipse for the bottom and finally connect those with a black outline.
Something like: