0

我正在使用 html5 和 jquery 创建一个图像滑块我想要做的是在一个画布中添加 3 个图像,然后获取第一个图像的像素数据并删除它的一些像素以显示我使用的第一个图像在 Jquery 中执行此操作的jCanvas 插件到目前为止我所做的是

 $(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 插件在可用?还有其他方法吗?对此的任何帮助都将对我非常有用.... 提前谢谢....

4

2 回答 2

1

请记住,在画布上绘画就像在纸上绘画,它不记得你之前画了什么,只记得你现在画布上的东西,所以如果你画一个图像,然后用另一个画在它上面,旧的图片是永远失去。

您应该做的是将所有三个图像保存在三个不同的缓冲区中(只需将三个不同的图像加载到三个不同的图像对象中)。然后在上下文中绘制最上面的图像。当您希望将第一张图像分解为第二张图像时,而不是从顶部图像中删除像素(只会显示背景),只需使用与从第一张图像中删除像素相同的坐标来获取像素数据从第二张图像(从顶部图像中删除像素的坐标可用作第二张图像的图像数据的索引)并将这些值复制到画布,再次使用相同的坐标,例如:如果您的算法引导您首先移除像素 x = 100, y = 175,

这是一些代码:

var width  = 300;
var height = 300;

var img1 = new Image();
img1.src = "image1.png";
var img2 = new Image();
img2.src = "image2.png";

function go()
{
    // Wait for the images to load

    if ( !img1.complete || !img2.complete )
    {
        setTimeout( go, 100 );
        return;
    }

    // Create a temporary canvas to draw the other images in the background

    var tc = document.createElement( "canvas" );
    tc.width = width;
    tc.height = height;
    var c2 = tc.getContext( "2d" );

    // Draw the first image in the real canvas (change the ID to your canvas ID)

    var c = document.getElementById( "myCanvas" ).getContext( "2d" );
    c.drawImage( img1, 0, 0 );
    var data1 = c.getImageData( 0, 0, width, height );  // Get the data for the first image

    // Draw the second image in the temporary canvas (which is hidden) and get its data

    c2.drawImage( img2, 0, 0 );
    var data2 = c2.getImageData( 0, 0, width, height );

    // Copy the data from the hidden image to the visible one
    // This is where your magic comes into play, the following
    // is just a very very simple example

    var pix1 = data1.data;
    var pix2 = data2.data;

    for ( var x = 0; x < width; x++ )
    {
        for ( var y = 0; y < height; y++ )
        {
            var pos = ( ( y * width ) + x ) * 4;
            pix1[ pos ] = pix2[ pos++ ];
            pix1[ pos ] = pix2[ pos++ ];
            pix1[ pos ] = pix2[ pos++ ];
            pix1[ pos ] = pix2[ pos ];
        }
    }

    // Redraw the visible canvas with the new data

    c.putImageData( data1, 0, 0 );
}

window.onload = go;
于 2012-01-08T10:54:58.217 回答
0

画布元素不提供使用这样的图层的能力。您可能需要检查诸如画布拼贴CanvasStack 之类的附加组件

于 2012-01-08T08:35:27.793 回答