织梦CMS - 轻松建站从此开始!

abg欧博官网|登陆|游戏|

Canvas width and height in HTML5欧博官网

时间:2025-09-19 15:51来源: 作者:admin 点击: 3 次
A canvas has 2 sizes, the dimension of the pixels in the canvas (it's backingstore or drawingBuffer) and the display size. The number of pixels is set

A canvas has 2 sizes, the dimension of the pixels in the canvas (it's backingstore or drawingBuffer) and the display size. The number of pixels is set using the the canvas attributes. In HTML

<canvas></canvas>

Or in JavaScript

someCanvasElement.width = 400; someCanvasElement.height = 300;

Separate from that are the canvas's CSS style width and height

In CSS

canvas { /* or some other selector */ width: 500px; height: 400px; }

Or in JavaScript

canvas.style.width = "500px"; canvas.style.height = "400px";

The arguably best way to make a canvas 1x1 pixels is to ALWAYS USE CSS to choose the size then write a tiny bit of JavaScript to make the number of pixels match that size.

function resizeCanvasToDisplaySize(canvas) { // look up the size the canvas is being displayed const width = canvas.clientWidth; const height = canvas.clientHeight; // If it's resolution does not match change it if (canvas.width !== width || canvas.height !== height) { canvas.width = width; canvas.height = height; return true; } return false; }

Why is this the best way? Because it works in most cases without having to change any code.

Here's a full window canvas:

const ctx = document.querySelector("#c").getContext("2d"); function render(time) { time *= 0.001; resizeCanvasToDisplaySize(ctx.canvas); ctx.fillStyle = "#DDE"; ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height); ctx.save(); const spacing = 64; const size = 48; const across = ctx.canvas.width / spacing + 1; const down = ctx.canvas.height / spacing + 1; const s = Math.sin(time); const c = Math.cos(time); for (let y = 0; y < down; ++y) { for (let x = 0; x < across; ++x) { ctx.setTransform(c, -s, s, c, x * spacing, y * spacing); ctx.strokeRect(-size / 2, -size / 2, size, size); } } ctx.restore(); requestAnimationFrame(render); } requestAnimationFrame(render); function resizeCanvasToDisplaySize(canvas) { // look up the size the canvas is being displayed const width = canvas.clientWidth; const height = canvas.clientHeight; // If it's resolution does not match change it if (canvas.width !== width || canvas.height !== height) { canvas.width = width; canvas.height = height; return true; } return false; } body { margin: 0; } canvas { display: block; width: 100vw; height: 100vh; } <canvas></canvas>

And Here's a canvas as a float in a paragraph

const ctx = document.querySelector("#c").getContext("2d"); function render(time) { time *= 0.001; resizeCanvasToDisplaySize(ctx.canvas); ctx.fillStyle = "#DDE"; ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height); ctx.save(); const spacing = 64; const size = 48; const across = ctx.canvas.width / spacing + 1; const down = ctx.canvas.height / spacing + 1; const s = Math.sin(time); const c = Math.cos(time); for (let y = 0; y <= down; ++y) { for (let x = 0; x <= across; ++x) { ctx.setTransform(c, -s, s, c, x * spacing, y * spacing); ctx.strokeRect(-size / 2, -size / 2, size, size); } } ctx.restore(); requestAnimationFrame(render); } requestAnimationFrame(render); function resizeCanvasToDisplaySize(canvas) { // look up the size the canvas is being displayed const width = canvas.clientWidth; const height = canvas.clientHeight; // If it's resolution does not match change it if (canvas.width !== width || canvas.height !== height) { canvas.width = width; canvas.height = height; return true; } return false; } span { width: 250px; height: 100px; float: left; padding: 1em 1em 1em 0; display: inline-block; } canvas { width: 100%; height: 100%; } <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent cursus venenatis metus. Mauris ac nibh at odio scelerisque scelerisque. Donec ut enim <span><canvas></canvas></span> vel urna gravida imperdiet id ac odio. Aenean congue hendrerit eros id facilisis. In vitae leo ullamcorper, aliquet leo a, vehicula magna. Proin sollicitudin vestibulum aliquet. Sed et varius justo. <br/><br/> Quisque tempor metus in porttitor placerat. Nulla vehicula sem nec ipsum commodo, at tincidunt orci porttitor. Duis porttitor egestas dui eu viverra. Sed et ipsum eget odio pharetra semper. Integer tempor orci quam, eget aliquet velit consectetur sit amet. Maecenas maximus placerat arcu in varius. Morbi semper, quam a ullamcorper interdum, augue nisl sagittis urna, sed pharetra lectus ex nec elit. Nullam viverra lacinia tellus, bibendum maximus nisl dictum id. Phasellus mauris quam, rutrum ut congue non, hendrerit sollicitudin urna. </p>

Here's a canvas in a sizable control panel

const ctx = document.querySelector("#c").getContext("2d"); function render(time) { time *= 0.001; resizeCanvasToDisplaySize(ctx.canvas); ctx.fillStyle = "#DDE"; ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height); ctx.save(); const spacing = 64; const size = 48; const across = ctx.canvas.width / spacing + 1; const down = ctx.canvas.height / spacing + 1; const s = Math.sin(time); const c = Math.cos(time); for (let y = 0; y < down; ++y) { for (let x = 0; x < across; ++x) { ctx.setTransform(c, -s, s, c, x * spacing, y * spacing); ctx.strokeRect(-size / 2, -size / 2, size, size); } } ctx.restore(); requestAnimationFrame(render); } requestAnimationFrame(render); function resizeCanvasToDisplaySize(canvas) { // look up the size the canvas is being displayed const width = canvas.clientWidth; const height = canvas.clientHeight; // If it's resolution does not match change it if (canvas.width !== width || canvas.height !== height) { canvas.width = width; canvas.height = height; return true; } return false; } // ----- the code above related to the canvas does not change ---- // ---- the code below is related to the slider ---- const $ = document.querySelector.bind(document); const left = $(".left"); const slider = $(".slider"); let dragging; let lastX; let startWidth; slider.addEventListener('mousedown', e => { lastX = e.pageX; dragging = true; }); window.addEventListener('mouseup', e => { dragging = false; }); window.addEventListener('mousemove', e => { if (dragging) { const deltaX = e.pageX - lastX; left.style.width = left.clientWidth + deltaX + "px"; lastX = e.pageX; } }); body { margin: 0; } .frame { display: flex; align-items: space-between; height: 100vh; } .left { width: 70%; left: 0; top: 0; right: 0; bottom: 0; } canvas { width: 100%; height: 100%; } pre { padding: 1em; } .slider { width: 10px; background: #000; } .right { flex 1 1 auto; } <div> <div> <canvas></canvas> </div> <div> </div> <div> <pre> * controls * go * here &lt;- drag this </pre> </div> </div>

here's a canvas as a background

const ctx = document.querySelector("#c").getContext("2d"); function render(time) { time *= 0.001; resizeCanvasToDisplaySize(ctx.canvas); ctx.fillStyle = "#DDE"; ctx.fillRect(0, 0, ctx.canvas.width, ctx.canvas.height); ctx.save(); const spacing = 64; const size = 48; const across = ctx.canvas.width / spacing + 1; const down = ctx.canvas.height / spacing + 1; const s = Math.sin(time); const c = Math.cos(time); for (let y = 0; y < down; ++y) { for (let x = 0; x < across; ++x) { ctx.setTransform(c, -s, s, c, x * spacing, y * spacing); ctx.strokeRect(-size / 2, -size / 2, size, size); } } ctx.restore(); requestAnimationFrame(render); } requestAnimationFrame(render); function resizeCanvasToDisplaySize(canvas) { // look up the size the canvas is being displayed const width = canvas.clientWidth; const height = canvas.clientHeight; // If it's resolution does not match change it if (canvas.width !== width || canvas.height !== height) { canvas.width = width; canvas.height = height; return true; } return false; } body { margin: 0; } canvas { display: block; width: 100vw; height: 100vh; position: fixed; } #content { position: absolute; margin: 0 1em; font-size: xx-large; font-family: sans-serif; font-weight: bold; text-shadow: 2px 2px 0 #FFF, -2px -2px 0 #FFF, -2px 2px 0 #FFF, 2px -2px 0 #FFF; } <canvas></canvas> <div> <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent cursus venenatis metus. Mauris ac nibh at odio scelerisque scelerisque. Donec ut enim vel urna gravida imperdiet id ac odio. Aenean congue hendrerit eros id facilisis. In vitae leo ullamcorper, aliquet leo a, vehicula magna. Proin sollicitudin vestibulum aliquet. Sed et varius justo. </p> <p> Quisque tempor metus in porttitor placerat. Nulla vehicula sem nec ipsum commodo, at tincidunt orci porttitor. Duis porttitor egestas dui eu viverra. Sed et ipsum eget odio pharetra semper. Integer tempor orci quam, eget aliquet velit consectetur sit amet. Maecenas maximus placerat arcu in varius. Morbi semper, quam a ullamcorper interdum, augue nisl sagittis urna, sed pharetra lectus ex nec elit. Nullam viverra lacinia tellus, bibendum maximus nisl dictum id. Phasellus mauris quam, rutrum ut congue non, hendrerit sollicitudin urna. </p> </div>

Because I didn't set the attributes the only thing that changed in each sample is the CSS (as far as the canvas is concerned)

Notes:

Don't put borders or padding on a canvas element. Computing the size to subtract from the number of dimensions of the element is troublesome

(责任编辑:)
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:
发布者资料
查看详细资料 发送留言 加为好友 用户等级: 注册时间:2025-09-20 16:09 最后登录:2025-09-20 16:09
栏目列表
推荐内容