Html5 获取特定图层图像数据
我正在使用 html5 和 jquery 创建一个图像滑块,我想要做的是将 3 个图像添加到一个画布中,然后获取第一个图像的像素数据并删除其中的一些像素,以通过我使用的第一个图像显示第二个图像jCanvas 在 Jquery 中执行此操作的插件目前为止我所得到的是现在
$(document).ready(function(){
function invert() {
$("canvas").setPixels({
x: 150, y: 150,
width: 100, height: 75,
// loop through each pixel
each: function(px) {
px.r = 255 - px.r;
px.g = 255 - px.g;
px.b = 255 - px.b;
px.a = 255 - px.a;
}
});
}
$("canvas")
.addLayer({
method: "drawImage",
source: "images/01.jpg",
x: 100, y: 100,
width: 480, height: 440
}).addLayer({
method: "drawImage",
source: "images/02.jpg",
x: 100, y: 100,
width: 380, height: 340
}).addLayer({
method: "drawImage",
source: "images/01.jpg",
x: 100, y: 100,
width: 280, height: 240,
load: invert
})
// Draw each layer on the canvas
.drawLayers();
});
它的作用是在所有图像中打一个洞意味着擦除所有图像的该部分的所有像素并显示画布的背景是否有可能获得仅特定图像或图层的像素和反转 有可用的 jquery 插件吗?还有其他方法可以做到这一点吗?对此的任何帮助对我来说都非常有用......提前感谢......
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请记住,在画布上绘画就像在纸上绘画一样,它不会记住您之前画过的内容,只会记住您现在在画布上画的内容,因此如果您绘制一个图像然后用另一张图像在其上绘制,则旧图像将被保留。永远失去了。
您应该做的是将所有三个图像保留在三个不同的缓冲区中(只需将三个不同的图像加载到三个不同的图像对象中)。
然后绘制上下文中最上面的图像。
当您希望将第一张图像溶解到第二张图像中时,无需从顶部图像中删除像素(这只会显示背景),只需使用与从第一张图像中删除像素时所用的相同坐标即可获取像素数据从第二个图像(从顶部图像删除像素的坐标可以用作第二个图像的图像数据的索引)并将这些值复制到画布,再次使用相同的坐标,例如:
如果您的算法引导您首先删除像素 x = 100、y = 175,请使用这些坐标从第二个图像的缓冲区中获取数据,并将其复制到画布图像数据中的相同坐标。
这是一些代码:
Keep in mind that drawing on a canvas is like painting on paper, it doesn't remember what you drew before only what you have in the canvas right now so if you draw one image and then draw over it with another, the old picture is lost forever.
What you should do is keep all three images in three different buffers (simply load the three different images into three different image objects).
Then draw the top most image in the context.
When you wish to dissolve the first image into the second, instead of deleting pixels from the top image (which will only show the the background), simply use the same coordinates you would use to remove pixels from the first image to get the pixel data from the second image (the coordinates for deleting pixel from the top image can be used as indexes to the image data for the second image) and copy those values to the canvas, again using the same coordinates, for example:
If you algorithm leads you to first remove pixel x = 100, y = 175, use those coordinates to get the data from the buffer of the second image and copy that to the same coordinates in the canvas' image data.
Here's some code:
canvas 元素不提供像这样使用图层的功能。您可能需要检查 canvas collage 或 CanvasStack
The canvas element does not provide the ability to use layers like that. You may need to check add-ons like canvas collage or CanvasStack