简体   繁体   English

在htmll5画布中缓慢加载所有颜色

[英]loading very slowly all colors in htmll5 canvas

i am trying to make a color tools.so I i tried to show the colors in a canvas.But the problem is,it is loading very slowly.here is my code 我正在尝试制作颜色工具。所以我试图在画布上显示颜色。但是问题是,加载速度非常慢。这是我的代码

  var l=0

  var ctx = document.getElementById('color').getContext('2d');
  for (var i=0;i<125;i++){
    for (var j=0;j<124;j++){
        for(var k=0;k<125;k++){
      ctx.fillStyle = 'rgb(' +i*2+ ',' +j*2 + ','+k*2+')';
      l=i+k; 
      ctx.fillRect(l*1,j*1,1,1);
    }
    }   
  }

i have tested it firefox and crome.Both shows the same behaviour.Please help me.thanks in advance. 我已经测试过Firefox和crome。两者都表现出相同的行为。请提前帮助我。

First of all, your algorithm is basically wrong. 首先,您的算法基本上是错误的。 You are calculating the colors in a 3d space, (ijk) and maping that on a 2d space (l, j). 您正在计算3d空间(ijk)中的颜色,并将其映射到2d空间(l,j)中。

Every value for the l variable is calculated several times and only the last one is visible (i=0 k=100 -> l=100 ; 1=1 k=99 > l=100; i=2 k=98 > l=100 ... l变量的每个值都会计算几次,只有最后一个可见(i = 0 k = 100-> l = 100; 1 = 1 k = 99> l = 100; i = 2 k = 98> l = 100 ...

You should decide beforehand what colors go where, and have only 2 loops and not 3 (Probably the most usual decision here is going to a hsl space, omitting l and doing the loops on h and s. 您应该事先决定什么颜色去哪里,并且只有2个循环而不是3个(这里最常见的决定可能是去hsl空间,省略l并在h和s上进行循环。

The other big improvement would be to set pixels directly and do not use fillrect. 另一个重大改进是直接设置像素,而不使用fillrect。 Something in the line of: 符合:

var imgData=ctx.createImageData (125,125);

accesing pixels

ctx.putImageData(imgData,0,0);

You should have picture already precalculated (as one of the comments suggest) and then only read the position from this picture. 您应该已经预先计算了图片(如注释之一所示),然后仅从该图片中读取位置。 From that you could reverse engineer your color in RGB. 由此,您可以对RGB颜色进行反向工程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM