Professional Documents
Culture Documents
Canvas-Size - Determine The Maximum Area, Height, Width, and Custom Dimensions of An HTML Canvas Element
Canvas-Size - Determine The Maximum Area, Height, Width, and Custom Dimensions of An HTML Canvas Element
canvas-size
npm v2.0.0 checks passing code quality A coverage 100% License MIT jsDelivr 76k hits/month
Sponsor ❤
Determine the maximum area, height, width, and custom dimensions of an HTML
<canvas> element.
Version 2.x contains new features and breaking changes (see the Changelog
for details). Documentation for version 1.x is available on GitHub.
Why?
The HTML canvas element is widely supported by modern and legacy browsers, but each
browser and platform combination imposes unique size limitations that will render a
canvas unusable when exceeded. Unfortunately, browsers do not provide a way to
determine what their limitations are, nor do they provide any kind of feedback after an
unusable canvas has been created. This makes working with large canvas elements a
challenge, especially for applications that support a variety of browsers and platforms.
This micro-library provides the maximum area, height, and width of an HTML canvas
element supported by the browser as well as the ability to test custom canvas
dimensions. By collecting this information before a new canvas element is created,
applications are able to reliably set canvas dimensions within the size limitations of each
browser/platform.
Demo
https://jhildenbiddle.github.io/canvas-size/#/?id=test-results 1/16
28/2/24, 11:32 canvas-size - Determine the maximum area, height, width, and custom dimensions of an HTML canvas element.
🔴
onError(results) {
log(' ', results);
},
});
Features
Browser Support
Chrome 24+
Edge 12+
Firefox 26+
Safari 8+
Internet Explorer 10+
Installation
NPM
https://jhildenbiddle.github.io/canvas-size/#/?id=test-results 2/16
28/2/24, 11:32 canvas-size - Determine the maximum area, height, width, and custom dimensions of an HTML canvas element.
bash
js
CDN
Available on jsdelivr (below), unpkg, and other CDN services that auto-publish npm
packages.
html
html
Note the @ version lock in the URLs above. This prevents breaking changes
in future releases from affecting your project and is therefore the safest
method of loading dependencies from a CDN. When a new major version is
released, you will need to manually update your CDN URLs by changing the
version after the @ symbol.
Usage
All HTML canvas tests are performed asynchronously. A failed test will return
immediately. Successful test times are dependent upon the canvas dimensions, browser,
and hardware.
https://jhildenbiddle.github.io/canvas-size/#/?id=test-results 3/16
28/2/24, 11:32 canvas-size - Determine the maximum area, height, width, and custom dimensions of an HTML canvas element.
Promises
Each canvasSize() method returns a Promise Object (if supported). The promise
will will resolve after the first successful canvas test or after all tests have been completed
with the following test result data:
js
{
success: boolean, // Status of last test
width: number, // Width of last canvas
height: number, // Height of last canvas
testTime: number, // Time to complete last test
totalTime: number, // Time to complete all tests
}
Test results are provided after the promise has resolved using a then handler:
js
js
https://jhildenbiddle.github.io/canvas-size/#/?id=test-results 4/16
28/2/24, 11:32 canvas-size - Determine the maximum area, height, width, and custom dimensions of an HTML canvas element.
js
if (success) {
console.log(`Created canvas: ${width} x ${height}`);
}
Promises and callbacks can be used together to provide individual test results when
multiple tests are being performed:
js
if (success) {
console.log('Success', width, height, testTime, totalTime);
}
Callbacks
https://jhildenbiddle.github.io/canvas-size/#/?id=test-results 5/16
28/2/24, 11:32 canvas-size - Determine the maximum area, height, width, and custom dimensions of an HTML canvas element.
Callback functions can be used to access canvas test result instead of or in addition to
promises. There are three key differences between using canvasSize promises and
callbacks:
js
// maxArea(), maxHeight(), maxWidth(), or test()
canvasSize.maxArea({
// ...
onError({ width, height, testTime, totalTime }) {
console.log('Error', width, height, testTime, totalTime);
},
onSuccess({ width, height, testTime, totalTime }) {
console.log('Success', width, height, testTime, totalTime);
},
});
js
// maxArea(), maxHeight(), maxWidth(), or test()
canvasSize.maxArea({
// ...
onError: function (results) {
console.log('Error', results);
},
onSuccess: function (results) {
console.log('Success', results);
https://jhildenbiddle.github.io/canvas-size/#/?id=test-results 6/16
28/2/24, 11:32 canvas-size - Determine the maximum area, height, width, and custom dimensions of an HTML canvas element.
},
});
Web Workers
Browsers that support web workers and OffscreenCanvas can have canvas tests
performed on a separate thread by setting useWorker:true . This can prevent the
browser from becoming unresponsive while testing on the browser’s main thread.
Note: Browsers without support for web workers and OffscreenCanvas will ignore this
option and perform tests on the main thread.
js
At the time of this writing, Firefox and Safari are able to perform canvas tests
using a web worker at the same speed or better compared to tests run on the
main thread. Unfortunately, Blink-based browsers like Chrome and Edge
perform significantly slower when using web workers. This is a long-standing
issue and the cause is unknown.
Methods
maxArea
https://jhildenbiddle.github.io/canvas-size/#/?id=test-results 7/16
28/2/24, 11:32 canvas-size - Determine the maximum area, height, width, and custom dimensions of an HTML canvas element.
When options.max is not specified, an optimized test will be performed using known
maximum area/height/width values from previously tested browsers and platforms (see
Test Results for details). This will return the maximum canvas area/height/width in the
shortest amount of time.
When options.max is specified, the value will be used for the initial area/height/width
test, then reduced by the options.step value for each subsequent test until a
successful test occurs. This is useful for determining the maximum area/height/width of a
canvas element for browser/platform combination not listed in the Test Results section.
Note that lower options.step values will provide more accurate results but will
require more time to complete due the increased number of tests that will run.
Options
https://jhildenbiddle.github.io/canvas-size/#/?id=test-results 8/16
28/2/24, 11:32 canvas-size - Determine the maximum area, height, width, and custom dimensions of an HTML canvas element.
Examples
The following examples use maxArea() . Usage for maxHeight() and maxWidth()
is identical.
js
// Optimized tests
const { success, width, height } = await canvasSize.maxArea();
js
// Custom tests with web worker
const { success, width, height } = await canvasSize.maxArea({
max: 16384,
min: 1, // default
step: 1024, // default
useWorker: true,
});
js
// Optimized tests
canvasSize.maxArea({
onSuccess({ width, height, testTime, totalTime }) {
console.log('Success:', width, height, testTime, totalTime);
},
});
https://jhildenbiddle.github.io/canvas-size/#/?id=test-results 9/16
28/2/24, 11:32 canvas-size - Determine the maximum area, height, width, and custom dimensions of an HTML canvas element.
max: 16384,
min: 1, // default
step: 1024, // default
useWorker: true,
onSuccess({ width, height, testTime, totalTime }) {
console.log('Success:', width, height, testTime, totalTime);
},
});
maxHeight
js
canvasSize.maxHeight;
maxWidth
js
canvasSize.maxWidth;
test
Determines if the dimension(s) specified exceed the HTML canvas size limitations of the
browser. Returns a promise or undefined in legacy browsers.
Options
https://jhildenbiddle.github.io/canvas-size/#/?id=test-results 10/16
28/2/24, 11:32 canvas-size - Determine the maximum area, height, width, and custom dimensions of an HTML canvas element.
Returns
boolean when testing a single dimension. Returns true if the dimensions are
within the browser’s size limitations or false when exceeded.
Examples
js
// Single test
const { success } = await canvasSize.test({
height: 16384,
width: 16384,
});
https://jhildenbiddle.github.io/canvas-size/#/?id=test-results 11/16
28/2/24, 11:32 canvas-size - Determine the maximum area, height, width, and custom dimensions of an HTML canvas element.
js
Using callbacks:
js
// Single test
canvasSize.test({
height: 16384,
width: 16384,
onError({ width, height, testTime, totalTime }) {
console.log('Error:', width, height, testTime, totalTime);
},
onSuccess({ width, height, testTime, totalTime }) {
console.log('Success:', width, height, testTime, totalTime);
},
});
js
https://jhildenbiddle.github.io/canvas-size/#/?id=test-results 12/16
28/2/24, 11:32 canvas-size - Determine the maximum area, height, width, and custom dimensions of an HTML canvas element.
Test Results
Some tests were conducted using virtualized devices courtesy of BrowserStack. Results
may vary based on hardware and operating system, most notably on lower-end mobile
devices with less capable hardware.
Desktop
Firefox < 122 (Mac, Win) 32,767 32,767 11,180 x 11,180 (124,992,400)
Mobile
https://jhildenbiddle.github.io/canvas-size/#/?id=test-results 13/16
28/2/24, 11:32 canvas-size - Determine the maximum area, height, width, and custom dimensions of an HTML canvas element.
Max Max
Browser (OS) Max Area (Total)
Width Height
14,188 x 14,188
Chrome 91+ (Android 7) 65,535 65,535
(201,299,344)
16,384 x 16,384
Chrome 91+ (Android 6) 65,535 65,535
(268,435,456)
11,180 x 11,180
Chrome 91+ (Android 5) 65,535 65,535
(124,992,400)
10,836 x 10,836
Chrome 68+ (Android 6) 32,767 32,767
(117,418,896)
11,402 x 11,402
Chrome 68+ (Android 5) 32,767 32,767
(130,005,604)
Known Issues
Tests conducted on virtual machines may produce results that differ from actual
hardware. This is to be expected, as the virtualized hardware used in these environments
can impose its own unique size limitations separate from the browser.
https://jhildenbiddle.github.io/canvas-size/#/?id=test-results 14/16
28/2/24, 11:32 canvas-size - Determine the maximum area, height, width, and custom dimensions of an HTML canvas element.
In some virtualized environments (mostly with older browsers), canvas-size may produce
inconsistent results or fail all tests when calling maxArea() , maxHeight() ,
maxWidth() , and test() using options.sizes . This is a result of the virtual GPU
failing after a test canvas exceeds the browser’s size limitations, causing all subsequent
tests to fail even for canvas dimensions that are actually supported by the browser. The
easiest and most reliable way to address these issues is to use a GPU-optimized virtual
machine. If this isn’t possible and your VM only supports software rendering, avoid
iterating over canvas dimensions that exceed the browser’s size limitations and instead
specify dimensions that are known to be supported by the browser.
Sponsorship
A sponsorship is more than just a way to show appreciation for the open-source authors
and projects we rely on; it can be the spark that ignites the next big idea, the inspiration
to create something new, and the motivation to share so that others may benefit.
If you benefit from this project, please consider lending your support and encouraging
future efforts by becoming a sponsor.
Thank you! 🙏🏻
Contact & Support
License
This project is licensed under the MIT License. See the LICENSE for details.
https://jhildenbiddle.github.io/canvas-size/#/?id=test-results 15/16
28/2/24, 11:32 canvas-size - Determine the maximum area, height, width, and custom dimensions of an HTML canvas element.
https://jhildenbiddle.github.io/canvas-size/#/?id=test-results 16/16