简单的HTML画布未显示完整形状

发布于 2025-02-08 14:57:13 字数 2264 浏览 0 评论 0原文

我是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.

It displays like an image below

The canvas should look like an image below.

The canvas should look like an image below.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰雪之触 2025-02-15 14:57:13

您有一种非常独特的方法来绘制圆柱体。当然,您可以通过首先绘制一个圆圈并垂直缩放上下文来完成此操作,以最终将圆扩展到一个椭圆,但让我们做一些不同。

开箱即用CanvasRenderingContext2d具有一种内置方法,可以绘制一个椭圆,很方便地称为ellipse()

因此,您可以首先为顶部画一个椭圆,另一个椭圆形,最后用黑色轮廓将它们连接起来。

像:

var canvas = document.getElementById("cylinder");
var ctx = canvas.getContext("2d");
drawCylinder(canvas, ctx, "lightblue", "black", 7, 100, 0, -100);

function drawCylinder(canvas, ctx, fill, border, lineWidth, r, centX, centY) {
  ctx.translate(canvas.width / 2, canvas.height / 2);
  ctx.lineWidth = lineWidth;
  ctx.beginPath();
  ctx.ellipse(centX, centY, r, r / 5, 0, 0, 2 * Math.PI);
  ctx.fillStyle = 'YellowGreen';
  ctx.strokeStyle = 'GainsBoro';
  ctx.fill();
  ctx.stroke();
  ctx.closePath();

  ctx.beginPath();
  ctx.ellipse(centX, centY + 100, r, r / 5, 0, 0, 2 * Math.PI);
  ctx.fillStyle = 'MediumSlateBlue';
  ctx.strokeStyle = 'GainsBoro';
  ctx.fill();
  ctx.stroke();
  ctx.closePath();

  ctx.lineWidth = 1;
  ctx.strokeStyle = 'Black';
  ctx.beginPath();
  ctx.ellipse(centX, centY, r + lineWidth / 2, r / 5 + lineWidth / 2, 0, 0, 1 * Math.PI);
  ctx.stroke();
  ctx.closePath();
  ctx.beginPath();
  ctx.ellipse(centX, centY + 100, r + lineWidth / 2, r / 5 + lineWidth / 2, 0, 0, 1 * Math.PI);
  ctx.stroke();
  ctx.closePath();
  ctx.beginPath();
  ctx.moveTo(centX - r - lineWidth / 2, centY);
  ctx.lineTo(centX - r - lineWidth / 2, centY + 100);
  ctx.stroke();
  ctx.closePath();
  ctx.beginPath();
  ctx.moveTo(centX + r + lineWidth / 2, centY);
  ctx.lineTo(centX + r + lineWidth / 2, centY + 100);
  ctx.stroke();
  ctx.closePath();
}
<canvas id="cylinder" width="300" height="400"></canvas>

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 called ellipse().

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:

var canvas = document.getElementById("cylinder");
var ctx = canvas.getContext("2d");
drawCylinder(canvas, ctx, "lightblue", "black", 7, 100, 0, -100);

function drawCylinder(canvas, ctx, fill, border, lineWidth, r, centX, centY) {
  ctx.translate(canvas.width / 2, canvas.height / 2);
  ctx.lineWidth = lineWidth;
  ctx.beginPath();
  ctx.ellipse(centX, centY, r, r / 5, 0, 0, 2 * Math.PI);
  ctx.fillStyle = 'YellowGreen';
  ctx.strokeStyle = 'GainsBoro';
  ctx.fill();
  ctx.stroke();
  ctx.closePath();

  ctx.beginPath();
  ctx.ellipse(centX, centY + 100, r, r / 5, 0, 0, 2 * Math.PI);
  ctx.fillStyle = 'MediumSlateBlue';
  ctx.strokeStyle = 'GainsBoro';
  ctx.fill();
  ctx.stroke();
  ctx.closePath();

  ctx.lineWidth = 1;
  ctx.strokeStyle = 'Black';
  ctx.beginPath();
  ctx.ellipse(centX, centY, r + lineWidth / 2, r / 5 + lineWidth / 2, 0, 0, 1 * Math.PI);
  ctx.stroke();
  ctx.closePath();
  ctx.beginPath();
  ctx.ellipse(centX, centY + 100, r + lineWidth / 2, r / 5 + lineWidth / 2, 0, 0, 1 * Math.PI);
  ctx.stroke();
  ctx.closePath();
  ctx.beginPath();
  ctx.moveTo(centX - r - lineWidth / 2, centY);
  ctx.lineTo(centX - r - lineWidth / 2, centY + 100);
  ctx.stroke();
  ctx.closePath();
  ctx.beginPath();
  ctx.moveTo(centX + r + lineWidth / 2, centY);
  ctx.lineTo(centX + r + lineWidth / 2, centY + 100);
  ctx.stroke();
  ctx.closePath();
}
<canvas id="cylinder" width="300" height="400"></canvas>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文