Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

canvas default size

Chrome seems to set canvas tags a default width of 300px.

This is a bit of a nuisance when I want a canvas to default to the size of it's containing div, which has an unspecified size.

Using the example here, I'd like the canvas to default to the width of the heading with added padding.

Hope that makes sense.

Is this possible without javascript?

like image 538
Finnnn Avatar asked Oct 17 '11 11:10

Finnnn


People also ask

How will you set a canvas size?

With the Select and Move Tool, click the canvas size label or border to select the canvas. Click the canvas border handles to resize the canvas dynamically. When you drag the border handles without using any keyboard modifiers, the canvas resizes non-uniformly. Hold Shift to constrain the resize proportions.

What is canvas width?

Description. pixels. Specifies the width of the canvas, in pixels (e.g. "100"). Default value is 300.

What is canvas aspect ratio?

2:3 is the most popular aspect ratio for printing. Print sizes that are a 2:3 aspect ratio and are popular with poster, canvas, and decal printing are 24 x 36 inches and 40 x 60 inches. 3:4 Aspect Ratio. Some popular print sizes for the 3:4 ratio includes 30 x 40 inches and 18 x 24 inches.

What is maximum canvas size?

All web browsers limit the canvas element's width, height, and area. For Google Chrome, the maximum allowable width and height are 32,767 pixels and the maximum allowable area is 268,435,456 pixels.


2 Answers

Finnnn, you are right.

From html5 specification, http://www.w3.org/TR/2012/WD-html5-author-20120329/the-canvas-element.html#the-canvas-element

The canvas element has two attributes to control the size of the coordinate space: width and height.

These attributes, when specified, must have values that are valid non-negative integers.

The width attribute defaults to 300, and the height attribute defaults to 150.

like image 155
Robert Avatar answered Nov 03 '22 07:11

Robert


Arrange your layout such that there is a div and that div's only job is to dictate and be the size you want the canvas to be.

var canvas = document.createElement('canvas');
canvas.width = div.clientWidth;
canvas.height = div.clientHeight;

Then add the canvas to the div.

like image 35
Simon Sarris Avatar answered Nov 03 '22 07:11

Simon Sarris