forked from mattdesl/canvas-sketch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanvas-pixel-processing.js
47 lines (39 loc) · 1.12 KB
/
canvas-pixel-processing.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* A Canvas2D example of pixel-perfect RGBA manipualation.
* @author Matt DesLauriers (@mattdesl)
*/
const sketcher = require('canvas-sketch');
const settings = {
// Disable canvas smoothing
pixelated: true,
// A 128x128px output image
dimensions: [ 128, 128 ]
};
const sketch = () => {
return ({ context, width, height }) => {
// Clear canvas
context.clearRect(0, 0, width, height);
// Pure RGBA pixel manipulation
const image = context.getImageData(0, 0, width, height);
const pixels = image.data;
for (let y = 0; y < height; y++) {
for (let x = 0; x < width; x++) {
const i = x + y * width;
// Stepped gradient
const steps = 7;
const xWhole = (x / width) * steps;
const xInt = Math.floor(xWhole);
const px = xInt / steps;
// Red to white gradient
const L = Math.floor(px * 255);
pixels[i * 4 + 0] = 255;
pixels[i * 4 + 1] = L;
pixels[i * 4 + 2] = L;
pixels[i * 4 + 3] = 255;
}
}
// Apply manipulation
context.putImageData(image, 0, 0);
};
};
sketcher(sketch, settings);