Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does HTML5 Canvas pixel size depend on the canvas size?

Tags:

html

canvas

I'm drawing simple lines with the HTML5 canvas:

context = $('canvas')[0].getContext('2d');
context.moveTo(150, 20);
context.lineTo(300, 20);
context.stroke();

When my canvas CSS changes from:

canvas {
    width: 500px;
    height: 500px;
}

to

canvas {
    width: 1000px;
    height: 1000px;
}

the stroke width and height also double! What gives?

like image 852
atp Avatar asked May 16 '11 20:05

atp


People also ask

What is the default pixel size of the canvas?

By default, the browser creates canvas elements with a width of 300 pixels and a height of 150 pixels. You can change the size of a canvas element by specifying the width and height attributes.

How do I change the pixel size in canvas?

In the Properties panel, you can input canvas size width and height at specific values. Use the Units property to change the canvas size units between pixels, inches, or centimeters. Use the Resolution setting to set a print resolution in pixels/inch or pixels/centimeter.

How does canvas size work?

Every digital image contains a specific number of pixels, and the size of your canvas is literally just picking how many pixels you want to start with. The bigger your image, the more detail you can add, and the larger you can print.

How does HTML5 canvas work?

The HTML <canvas> element is used to draw graphics, on the fly, via scripting (usually JavaScript). The <canvas> element is only a container for graphics. You must use a script to actually draw the graphics. Canvas has several methods for drawing paths, boxes, circles, text, and adding images.


1 Answers

The CSS only determines its visible size. Change the width and height attributes in the HTML to adjust the actual number of pixels that make it up. If in the HTML it is 100x100, and in the CSS 200x200, it will be visually scaled by 2X.

like image 185
Andrea Avatar answered Oct 21 '22 16:10

Andrea