简体   繁体   English

所有画布标签尺寸都以像素为单位?

[英]Are all canvas tag dimensions in pixels?

Are all canvas tag dimensions in pixels? 所有画布标签尺寸都以像素为单位?

I am asking because I understood them to be. 我在问,因为我理解他们是。
But my math is broken or I am just not grasping something here. 但是我的数学被打破了,或者我在这里没有抓到什么。
I have been doing python mostly and just jumped back into Java Scripting. 我一直在做python,只是跳回到Java Scripting。
If I am just doing something stupid let me know. 如果我只是做一些愚蠢的事情让我知道。
For a game I am writing, I wanted to have a blocky gradient. 对于我正在写的游戏,我想要一个块状渐变。
I have the following: 我有以下内容:

HTML

<canvas id="heir"></canvas>

CSS

@media screen {
    body { font-size: 12pt }
    /* Game Rendering Space */
    canvas { 
        width: 640px; 
        height: 480px; 
        border-style: solid; 
        border-width: 1px;
    }
}

JavaScript (Shortened)

function testDraw ( thecontext )
{
    var myblue = 255;
    thecontext.save(); // Save All Settings (Before this Function was called)
    for (var i = 0; i < 480; i = i + 10 ) {
        if (myblue.toString(16).length == 1) 
        {
            thecontext.fillStyle = "#00000" + myblue.toString(16);
        } else {
            thecontext.fillStyle = "#0000" + myblue.toString(16);
        }
        thecontext.fillRect(0, i, 640, 10);
        myblue = myblue - 2;
    };
    thecontext.restore(); // Restore Settings to Save Point (Removing Styles, etc...)
}

function main ()
{
    var targetcontext = document.getElementById(“main”).getContext("2d");
    testDraw(targetcontext);
}

To me this should produce a series of 640w by 10h pixel bars. 对我来说,这应该产生一系列640w×10h的像素条。 In Google Chrome and Fire Fox I get 15 bars. 在Google Chrome和Fire Fox中,我获得了15个酒吧。 To me that means ( 480 / 15 ) is 32 pixel high bars. 对我来说,这意味着(480/15)是32像素高的条形。
So I change the code to: 所以我将代码更改为:

function testDraw ( thecontext )
{
    var myblue = 255;
    thecontext.save(); // Save All Settings (Before this Function was called)
    for (var i = 0; i < 16; i++ ) {
        if (myblue.toString(16).length == 1) 
        {
            thecontext.fillStyle = "#00000" + myblue.toString(16);
        } else {
            thecontext.fillStyle = "#0000" + myblue.toString(16);
        }
        thecontext.fillRect(0, (i * 10), 640, 10);
        myblue = myblue - 10;
    };
    thecontext.restore(); // Restore Settings to Save Point (Removing Styles, etc...)
}

And get a true 32 pixel height result for comparison. 并获得真正的32像素高度结果进行比较。 Other than the fact that the first code snippet has shades of blue rendering in non-visible portions of the canvas they are measuring 32 pixels. 除了第一个代码片段在画布的不可见部分中具有蓝色渲染阴影的事实之外,它们测量的是32个像素。


Now back to the Original Java Code... 现在回到原始Java代码......
If I inspect the canvas tag in Chrome it reports 640 x 480. 如果我检查Chrome中的canvas标签,它会报告640 x 480。
If I inspect it in Fire Fox it reports 640 x 480. 如果我在Fire Fox中检查它,它会报告640 x 480。
BUT! 但! Fire Fox exports the original code to png at 300 x 150 (which is 15 rows of 10). Fire Fox将原始代码导出到png为300 x 150(15行为10行)。 Is it some how being resized to 640 x 480 by the CSS instead of being set to a true 640 x 480? 它是如何通过CSS调整为640 x 480,而不是设置为真正的640 x 480?

Why, how, what? 为什么,如何,什么? O_o I confused... O_o我很困惑......

I believe you just need to set the width and height of your canvas when you create it in the html. 我相信你只需要在html中创建画布时设置画布的宽度和高度。 Those values control the size of the coordinate space in your canvas and the defaults are 300 x 150. 这些值控制画布中坐标空间的大小,默认值为300 x 150。

<canvas id="heir" width="640" height="480"></canvas>

From http://dev.w3.org/html5/spec/Overview.html#canvas 来自http://dev.w3.org/html5/spec/Overview.html#canvas

The canvas element has two attributes to control the size of the coordinate space: width and height. canvas元素有两个属性来控制坐标空间的大小:宽度和高度。 These attributes, when specified, must have values that are valid non-negative integers. 指定时,这些属性必须具有有效的非负整数值。 The rules for parsing non-negative integers must be used to obtain their numeric values. 必须使用解析非负整数的规则来获取它们的数值。 If an attribute is missing, or if parsing its value returns an error, then the default value must be used instead. 如果缺少某个属性,或者解析其值会返回错误,则必须使用默认值。 The width attribute defaults to 300, and the height attribute defaults to 150. width属性默认为300,height属性默认为150。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM