Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

237
Visualizações
Render an array of rgb pixels on a canvas

I have this problem, I have a socket that sends me packets each one containing a row of a frame. Using python I reconstruct the full-frame as an array of RBG pixels. How can I render that array of pixels on a canvas, using WebGL or something similar?

With Python SDL2 was pretty easy, now I need to do the same on a webpage.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I found an interesting way to render ONE pixel here: What's the best way to set a single pixel in an HTML5 canvas?

I don't think there is a way in vanilla JavaScript to render a single pixel at a time, but the library p5.js (that I use a lot) does have this functionality.

A few other ways would be to 1) Just have a 1x1 square render (acting as a pixel). 2) Use 1x1 images like in the stackoverflow link above.

EDIT:

I found anouther stackoverflow about this and apparently I was wrong about vanilla JavaScript: Drawing a dot on HTML5 canvas

about 4 years ago · Juan Pablo Isaza Relatório

0

Here is my new and improved answer:

The problem is you cant load the pixels very fast, rendering them is the easy part. So why not reuse already loaded pixels? What I've created is an automated way to load an array of pixels and save them to be rendered later. Here's the not-to-optimized code:

let render;
let pixelArray;

function setup() {
  
  createCanvas(640, 480);
  
  render = new renderPixels();
  
  pixelArray = Array(width*height).fill(0);
  
  pixelArray = pixelArray.map( r => {return color(random(255), random(255), random(255)) });
  
  render.newImage("random", pixelArray, height, width);
  
  pixelArray = pixelArray.map( r => {return color(random(255), random(255), random(255)) });
  
  render.newImage("random2", pixelArray, height, width);
  
  pixelArray = pixelArray.map( r => {return color(random(255), random(255), random(255)) });
  
  render.newImage("random3", pixelArray, height, width);
  
  frameRate(5) //<-- Decrease to see full potential
  
}

function draw() {
  
  background(220);
  
  if(frameCount % 3 == 0){
  
    render.loadImage(0, 0, "random");
  
  } else if(frameCount % 3 == 1){
    
    render.loadImage(0, 0, "random2");
    
  } else {
    
    render.loadImage(0, 0, "random3");
    
  }
    
}

class renderPixels {
  
  constructor(){
    
    this.loadedImages = {}
    
  }
  
  newImage(name, arr, height, width){
  
    let img = createImage(height, width);

    img.loadPixels();

    for(let p = 0; p < (4 * height * width); p+=4){

      const l = arr[p/4].levels;
      
      img.pixels[p] = l[0];
      img.pixels[p+1] = l[1];
      img.pixels[p+2] = l[2];
      img.pixels[p+3] = l[3];

    }
    
    img.updatePixels();
    
    this.loadedImages[name] = img;
    
  }
  
  loadImage(x, y, name){
    
    image(this.loadedImages[name], x, y);
    
  }
  
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.5.8/p5.min.js"></script>
I could make this into vanilla JavaScript if you like, but I need to know the format of array of RGB pixels you talk about. Currently I'm using P5.js's color() function (which isn't very optimized) and it's working well enough.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda