-
Notifications
You must be signed in to change notification settings - Fork 0
/
CanvasPixels.js
52 lines (45 loc) · 1.01 KB
/
CanvasPixels.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
48
49
50
51
52
"use strict";
// access canvas 2d contexts pixel-wise
// exports the function lockContext()
// Martin Lercher 2015
function lockContext(canvas) {
var ctx = canvas.getContext("2d");
var w = canvas.width;
var h = canvas.height;
var id = ctx.getImageData(0, 0, w, h);
var data = id.data;
function index(x, y) {
return 4 * (w * y + x);
}
function putPixel(x, y, red, green, blue, alpha) {
var i = index(x, y);
data[i++] = red;
data[i++] = green;
data[i++] = blue;
data[i++] = alpha || 255;
}
function putWhitePixel(x, y, red, green, blue, alpha) {
var mr = Math.max(green, blue);
var mg = Math.max(red, blue);
var mb = Math.max(red, green);
putPixel(x, y, 255-mr, 255-mg, 255-mb, alpha);
}
function getPixel(x, y) {
var i = index(x, y);
return data.slice(i, i + 4);
}
function show() {
ctx.putImageData(id, 0, 0);
}
var lc = {
w: w,
h: h,
canvas: canvas,
ctx: ctx,
putPixel: putPixel,
putWhitePixel: putWhitePixel,
getPixel: getPixel,
show: show
};
return lc;
}