您可以将一个特定区域从一个画布复制到另一个画布吗?

发布于 2025-02-07 11:02:01 字数 169 浏览 0 评论 0 原文

截至目前,我在屏幕外画布上渲染一张地图,然后将其复制到主帆布中,因此我不必在地图上重新渲染每个框架上的每个对象,wich真的变得很懒惰,我的远场帆布需要相对较大以适合整个地图,整个画布都会被复制到普通的癌症上,每个框架也不是那样的表现,所以我的问题是:有什么方法可以将画布的一部分复制到另一个画布上,所以我不必复制整个东西?

As of right now Im rendering a map once to an offscreen canvas, wich then gets copied to the main canvas every frame, so I dont have to re-render every object on my map every frame, wich gets really laggy, my offscreen canvas needs to be relativly large to fit the whole map tho and the whole canvas gets copied onto the normal canas every frame wich also isn't that performant, so my question is; is there any way to only copy a section of a canvas to another canvas, so I dont have to copy the whole thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧伤慢歌 2025-02-14 11:02:01

您可以

const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
const offscreen = Object.assign(
  document.createElement("canvas"),
  { width: 2500, height: 2500 }
);
makeNoise(offscreen.getContext("2d"));

canvas.onmousemove = evt => {
  const x = evt.clientX - canvas.offsetLeft;
  const y = evt.clientY - canvas.offsetTop;
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.drawImage(offscreen, x - offscreen.width / 2, y - offscreen.height / 2);
}

// fills a context with noise,
// we use it only to draw somehing on the offscreen canvas
function makeNoise(ctx) {
  const img = new ImageData(ctx.canvas.width, ctx.canvas.height);
  const data = new Uint32Array(img.data.buffer);
  for (let i = 0; i<data.length; i++) {
    data[i] = Math.random() * 0xFFFFFF + 0xFF000000;
  }
  ctx.putImageData(img, 0, 0);
}
Use your mouse to move the offscreen canvas<br>
<canvas><canvas>

You can drawImage your canvas:

const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
const offscreen = Object.assign(
  document.createElement("canvas"),
  { width: 2500, height: 2500 }
);
makeNoise(offscreen.getContext("2d"));

canvas.onmousemove = evt => {
  const x = evt.clientX - canvas.offsetLeft;
  const y = evt.clientY - canvas.offsetTop;
  ctx.clearRect(0, 0, canvas.width, canvas.height);
  ctx.drawImage(offscreen, x - offscreen.width / 2, y - offscreen.height / 2);
}

// fills a context with noise,
// we use it only to draw somehing on the offscreen canvas
function makeNoise(ctx) {
  const img = new ImageData(ctx.canvas.width, ctx.canvas.height);
  const data = new Uint32Array(img.data.buffer);
  for (let i = 0; i<data.length; i++) {
    data[i] = Math.random() * 0xFFFFFF + 0xFF000000;
  }
  ctx.putImageData(img, 0, 0);
}
Use your mouse to move the offscreen canvas<br>
<canvas><canvas>

美羊羊 2025-02-14 11:02:01

/code>

允许您根据X/y坐标提取图像数据和宽度&amp;身高,

允许您将其放在X/Y坐标处。

或使用

drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight)

getImageData(sx, sy, sw, sh)

allows you to extract image data based on a X/Y coordinate and a width & height, and

putImageData(imageData, dx, dy)

allows you to place it at a X/Y coordinate.

Or use drawImage, that allows you to use the existing canvas as "input" directly, and you can specify X/Y and width/height for both the origin and the target:

drawImage(image, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文