【问题标题】:Error in JavaScript on Canvas(HTML5)- can't figure it outCanvas 上的 JavaScript 错误(HTML5)- 无法弄清楚
【发布时间】:2014-08-14 15:17:59
【问题描述】:

我的网站中有 JavaScript 代码。
该代码使用 2 张相同的照片 - 相同的尺寸、相同的分辨率 - 只有不同的颜色。 第一张照片是黑白照片——这就是画布所呈现的。 第二张照片与原始颜色相同。
我有一个触发 JS 代码的按钮——它通常从黑白中删除一个像素——并在画布上绘制彩色像素。起初我使用 Math.random 作为像素位置。
然后我决定按订单使用它。无论它从哪里开始或乞求..只要它会去 按此顺序(x,y)..(x+1,y).. 直到最大 x.. 和 (x,y+1).. 直到最大 x.. 依此类推,直到所有黑白照片“转换”为彩色照片.. 出于某种原因,我无法做到这一点..我尝试了很多技术.. 这是全局理解的演示: 演示工作很抱歉 - 他们停用了我的免费主机:\希望你仍然理解..

这是原始代码-我只是更改了最后一个功能:**removeDrawRandomPixel** ..它只是在那里播放功能,它应该被修复..

///////////////////////global variables///////////////////
var gray_url="bwcat.jpg"; //black and white image URI
var regular_url="cat.jpg"; //regular image URI
var n=100; //number of pixels changed per click
/////////////////////////////////////

document.addEventListener("DOMContentLoaded", function(){
    var c=new EditableCanvas(document.getElementById('cnvs'));
    grayScaleImage=new Image();
    grayScaleImage.src=gray_url;
    grayScaleImage.onload=function()
    {
        c.drawImage(this);
    }
    regularImage=new Image();
    regularImage.src=regular_url;
    regularImage.onload=function()
    {
        var p=getPixelArray(this);
        btn.onclick=function(){
                for(var i=1;i<=n&&p.length>0;i++){
                removeDrawRandomPixel(p,c);
                }
            }
    }
},false);   

//create a Pixel object
function ImagePixel(x,y,r,g,b,a)
{
    this.x=x;
    this.y=y;
    this.r=r;
    this.g=g;
    this.b=b;
    this.a=a;
}

//object that allows custom methods
function EditableCanvas(canvas)
{
    this.canvas=canvas;
    this.context=canvas.getContext('2d');
    this.width=canvas.width;
    this.height=canvas.height;
    this.pixelImage=this.context.createImageData(1,1);
    //draw an entire picture and adjust the canvas for it
    this.drawImage=function(image)
    {
        this.width=image.width;
        this.height=image.height;
        this.canvas.height=image.height;
        this.canvas.width=image.width;
        this.context.drawImage(image,0,0);
    }
    //draw a single pixel, ImagePixel pixel
    this.drawPixel=function(pixel)
    {
        this.pixelImage.data[0]=pixel.r;
        this.pixelImage.data[1]=pixel.g;
        this.pixelImage.data[2]=pixel.b;
        this.pixelImage.data[3]=pixel.a;
        this.context.putImageData(this.pixelImage,pixel.x,pixel.y);//,pixel.x,pixel.y);
    }
}
//the function returns an ordered array of Pixel pixels of the image at `src`.
function getPixelArray(img)
{
    var pixelArray=[];
    var cnvs=document.createElement('canvas');
    cnvs.width=img.width;
    cnvs.height=img.width;
    var context=cnvs.getContext('2d');
    context.drawImage(img,0,0);
    var originalData = context.getImageData(0,0,img.width,img.height).data;
    for(var i=0,pixelId=0,px;i<originalData.length;i+=4)
    {
        px=new ImagePixel(pixelId%img.width,Math.floor(pixelId/img.width),originalData[i],originalData[i+1],originalData[i+2],originalData[i+3]);
        pixelArray.push(px);
        pixelId++;
    }
    return pixelArray;
}

//the function remove a random pixel from pixelArray and draws it on editableCnvs
function removeDrawRandomPixel(pixelArray,editableCnvs)
{
    var place=Math.floor(Math.random()*pixelArray.length);
    var px=pixelArray.splice(place,1)[0];
    editableCnvs.drawPixel(px);
}

html:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>canvas rules</title>
        <script src="pixel.js"></script>    
    </head>
    <body>
        <canvas id="cnvs">
        oh goddmamit no support
        </canvas>
        <button id="btn">click to convert</button>
    </body>
</html>

我尝试播放最后一个函数..因为我知道答案在函数中,如何选择像素..

【问题讨论】:

    标签: javascript html image canvas pixel


    【解决方案1】:

    这是未经测试的,但这就是我将removeDrawRandomPixel 函数更改为的内容。您当前正在抓取数组中的一个随机点并传递它,因此它可能从一个像素的蓝色分量开始,到另一个像素的 g 分量结束。

    //the function remove a random pixel from pixelArray and draws it on editableCnvs
    function removeDrawRandomPixel(pixelArray,editableCnvs)
    {
        // width and height need to be the width and height of the canvas.
    
        var width = canvas.width,
            height = canvas.height,
            x = Math.floor(Math.random()*width),
            y = Math.floo(Math.random()*height);
    
        var px = (y * width + x) * 4;
        editableCnvs.drawPixel(px);
    }
    

    【讨论】:

    • 按照你描述的方式,我可能会在同一个点上再次绘制。使用数组和拼接命令是最有效的方法。
    • 我需要一种从 (0,0) 点绘制到最后一点的方法。应该很简单,该函数已经为您提供了所有 img 的像素数组,我无法做到明白为什么它不起作用..
    • @user3770900 我解释了为什么它不起作用..您没有抓取组件,而只是从数组中获取一个索引。
    • 仍然不明白,数学随机返回一个数字,所以如果我只放置另一个不是随机的数字。它会工作吗?我的意思是:var place=Math.floor((x) *pixelArray.length);而不仅仅是 x+=1.. 在方法的末尾?
    猜你喜欢
    • 1970-01-01
    • 2015-10-26
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多