You can get data from image instead of using context.getImageData();
I made two examples with the same image - 6312 particles:
- link (48 FPS on my computer) with context.getImageData
- link (60 FPS on my computer) without context.getImageData, it is just an image array with coordinates which I obtained with this tool.
When I use context.getImageData()
sometimes FPS sags even if I want to get 1px
. More problem if I need to get data from several images, or I use several canvases or use routing.
As far as I understood it very much depends on how much RAM you have.
I was very surprised that the situation changed a lot when I added another 8gb of memory (before was 16gb)
Then I tested a small image (44 particles) on my old very weak laptop with 4gb of memory:
- 38-40 FPS with context.getImageData(); (44 particles)
- 60 FPS just array with coordinates; (44 particles)
class Particle {
constructor({ x, y, r, g, b, a, width, height, color }) {
this.x = x
this.y = y
this.color = color ? color : `rgba(${r}, ${g}, ${b}, ${a})`
this.width = width
this.height = height
}
draw() {
context.beginPath()
context.fillStyle = this.color
context.rect(this.x, this.y, this.width, this.height)
context.fill()
}
}
-
import { data } from './data.js' // your copied data /* set real dimensions of your image */ const image = { width: 100, height: 100, } /* multiply x and y if you want to make big image with visible pixels */ const scale = 1 let i = 0 for (let y = 0; y < image.height; y++) { for (let x = 0; x < image.width; x++) { const [r, g, b, a] = data[i] particles.push( new Particle({ x: x * scale, y: y * scale, r, g, b, /* rgba */ a, /* rgb */ a: 1, /* set width and height to scale, if you scale it*/ width: scale, height: scale, }) ) i++ } }
-
import { data } from './data.js' // your copied data /* set a real width of your image */ const image = { width: 300, } /* multiply x and y if you want to make big image with visible pixels */ const scale = 1 for (let i = 0; i < data.length; i++) { const [r, g, b, a] = data[i] particles.push( new Particle({ x: (i % image.width) * scale, y: Math.floor(i / image.width) * scale, r, g, b, /* rgba */ a, /* rgb */ a: 1, /* set width and height to scale, if you scale it */ width: scale, height: scale, }) ) }
-
import { data } from './data.js' // your copied data /* multiply x and y if you want to make big image with visible pixels */ const scale = 1 for (let i = 0; i < data.length; i++) { const [x, y, r, g, b, a] = data[i] particles.push( new Particle({ x: x * scale, y: y * scale, r, g, b, /* xy rgba */ a, /* xy rgb */ a: 1, /* set width and height to scale, if you scale it*/ width: scale, height: scale, }) ) }
-
import { data } from './data.js' // your copied data /* multiply x and y if you want to make big image with visible pixels */ const scale = 1 for (let i = 0; i < data.length; i++) { const [x, y] = data[i] particles.push( new Particle({ x: x * scale, y: y * scale, color: 'purple', /* set width and height to scale, if you scale it*/ width: scale, height: scale, }) ) }