在展示我的舞台之前,我需要加载大约 50 张图像。我想减少请求的数量并加载一个包含我需要的所有图像的大精灵,然后在任何地方使用它,但要使用正确的偏移量。就像我们在 CSS 中使用 background-position 属性一样。
在文档中没有找到任何关于它的信息 - 这甚至可能吗?
在展示我的舞台之前,我需要加载大约 50 张图像。我想减少请求的数量并加载一个包含我需要的所有图像的大精灵,然后在任何地方使用它,但要使用正确的偏移量。就像我们在 CSS 中使用 background-position 属性一样。
在文档中没有找到任何关于它的信息 - 这甚至可能吗?
对于该用例,您可以使用裁剪属性。
对于每个Konva.Image
实例,您只使用一个源图像元素。
const img = new Image();
img.onload = () => {
const part1 = new Konva.Image({
image: img,
cropX: 0,
cropY: 0,
cropWidth: img.width / 2
cropHeight: img.height
});
layer.add(part1);
const part2 = new Konva.Image({
image: img,
cropX: img.width / 2,
cropY: 0,
cropWidth: img.width / 2
cropHeight: img.height
});
layer.add(part2);
layer.batchDraw();
}
img.src = url;