为什么这个不创建一个正方形?

huangapple go评论83阅读模式
英文:

Can anyone tell me why this isn't creating a square?

问题

我正在按照教程学习Canvas,并且在早期遇到了困难。

以下是代码部分:

<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-js -->

var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
    
context.lineWidth = 3;
context.strokeStyle = "blue";
context.lineJoin = "square";
context.strokeRect(10,10,200,200);

<!-- 语言: lang-css -->

#canvas{
    border: 1px solid black;
    display: block;
    width: 900px;
    height: 600px;
    margin: 0 auto;
}

<!-- 语言: lang-html -->

<!DOCTYPE html>

<html>
    <head>
        <meta charset="UTF-8">
        <title>HTML Canvas</title>
        <link rel="stylesheet" href="stylesheet.css" type="text/css">
    </head>
    
    <body>

        <canvas id="canvas"></canvas>
        
        <script src="script.js" type="text/javascript"></script>
        
    </body>
</html>

&lt;!-- 结束代码片段 --&gt;

看起来应该很简单,但它并没有按照我的期望工作。有人可以提供建议吗?

英文:

I'm following a tutorial to learn about canvas, and I'm unstuck pretty early on.

Here is the code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var canvas = document.getElementById(&quot;canvas&quot;);
var context = canvas.getContext(&quot;2d&quot;);
	
context.lineWidth = 3;
context.strokeStyle = &quot;blue&quot;;
context.lineJoin = &quot;square&quot;;
context.strokeRect(10,10,200,200);

<!-- language: lang-css -->

#canvas{
	border: 1px solid black;
	display: block;
	width: 900px;
	height: 600px;
	margin: 0 auto;
}

<!-- language: lang-html -->

&lt;!DOCTYPE html&gt;

&lt;html&gt;
	&lt;head&gt;
		&lt;meta charset=&quot;UTF-8&quot;&gt;
		&lt;title&gt;HTML Canvas&lt;/title&gt;
		&lt;link rel=&quot;stylesheet&quot; href=&quot;stylesheet.css&quot; type=&quot;text/css&quot;&gt;
	&lt;/head&gt;
	
	&lt;body&gt;

		&lt;canvas id=&quot;canvas&quot;&gt;&lt;/canvas&gt;
		
		&lt;script src=&quot;script.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
		
	&lt;/body&gt;
&lt;/html&gt;

<!-- end snippet -->

It seems like it should be pretty straight-forward, but it's not doing what I expect. Can anyone advise?

答案1

得分: 7

<canvas>元素的heightwidth应该最初在HTML中或通过DOM属性设置,而不是通过CSS来避免重新调整大小。

MDN中的引用:

实际上,<canvas>元素只有两个属性,widthheight。这两个属性都是可选的,也可以使用DOM属性来设置。当没有指定widthheight属性时,画布最初的宽度为300像素,高度为150像素。可以通过CSS任意调整元素的大小,但在渲染时,图像会被缩放以适应其布局大小:如果CSS大小不 res 不尊重初始画布的比例,它会显得扭曲。

因此,你的正方形也被调整为与画布相同的大小,无法完全适应其中。

<canvas id="canvas" height="600" width="900"></canvas>
英文:

The height and width of the canvas should initially be set in HTML or via DOM properties, not CSS to avoid resizing.

From MDN:

> Indeed, the <canvas> element has only two attributes, width and
> height. These are both optional and can also be set using DOM
> properties. When no width and height attributes are specified, the
> canvas will initially be 300 pixels wide and 150 pixels high. The
> element can be sized arbitrarily by CSS, but during rendering the
> image is scaled to fit its layout size: if the CSS sizing doesn't
> respect the ratio of the initial canvas, it will appear distorted.

Because of this, your square was also being resized to the same size of the canvas and could no longer fit completely within it.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var canvas = document.getElementById(&quot;canvas&quot;);
var context = canvas.getContext(&quot;2d&quot;);
	
context.lineWidth = 3;
context.strokeStyle = &quot;blue&quot;;
context.lineJoin = &quot;square&quot;;
context.strokeRect(10,10,200,200);

<!-- language: lang-css -->

#canvas{
	border: 1px solid black;
	display: block;
	margin: 0 auto;
}

<!-- language: lang-html -->

&lt;canvas id=&quot;canvas&quot; height=&quot;600&quot; width=&quot;900&quot;&gt;&lt;/canvas&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月4日 01:03:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582490.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定