Skip to content

Commit c2aa5ba

Browse files
author
Lauren McCarthy
committed
Merge pull request #1169 from gtoast/issue#1167
Fix for Issue#1167
2 parents 12b98c8 + 8e959f6 commit c2aa5ba

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/core/p5.Renderer2D.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ p5.Renderer2D.prototype.get = function(x, y, w, h) {
237237

238238
this.loadPixels.call(ctx);
239239

240+
// round down to get integer numbers
241+
x = Math.floor(x);
242+
y = Math.floor(y);
243+
240244
if (w === 1 && h === 1){
241245

242246
return [

test/unit/core/renderer.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,42 @@ suite('Renderer', function() {
7070
});
7171
});
7272

73+
suite('p5.prototype.get', function() {
74+
var img;
75+
76+
setup(function() {
77+
//create a 50 x 50 half red half green image
78+
img = myp5.createImage(50, 50);
79+
img.loadPixels();
80+
81+
for (var i = 0; i < img.width; i++) {
82+
for (var j = 0; j < img.height; j++) {
83+
var col;
84+
85+
if (j <= 25) {
86+
col = myp5.color(255, 0, 0);
87+
} else {
88+
col = myp5.color(0, 0, 255);
89+
}
90+
91+
img.set(i, j, col);
92+
}
93+
}
94+
95+
img.updatePixels();
96+
});
97+
98+
99+
100+
test('works with integers', function() {
101+
assert.deepEqual(img.get(25, 25), [255, 0, 0, 255]);
102+
assert.deepEqual(img.get(25, 26), [0, 0, 255, 255]);
103+
});
104+
105+
test('rounds down when given decimal numbers',
106+
function() {
107+
assert.deepEqual(img.get(25, 25.999), img.get(25, 25));
108+
});
109+
});
110+
73111
});

0 commit comments

Comments
 (0)