【问题标题】:Html5 Get Particular Layer image dataHtml5 获取特定图层图像数据
【发布时间】:2012-01-08 18:54:58
【问题描述】:

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

【问题讨论】:

标签: javascript jquery html canvas jcanvas


【解决方案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;

【讨论】:

    【解决方案2】:

    canvas 元素不提供使用这样的层的能力。您可能需要检查诸如 canvas collageCanvasStack 之类的附加组件

    【讨论】:

      猜你喜欢
      • 2021-09-10
      • 2020-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-24
      • 2019-09-07
      • 1970-01-01
      相关资源
      最近更新 更多