Skip to content

Commit 1844844

Browse files
committed
Scrolling a plane will wrap-around
1 parent 640dd5f commit 1844844

File tree

4 files changed

+66
-16
lines changed

4 files changed

+66
-16
lines changed

src/scene.js

+37-16
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ Scene.prototype.render = function(pl) {
371371
let sourcePitch = pl.pitch;
372372
let sourceWidth = pl.width;
373373
let sourceHeight = pl.height;
374+
let targetWidth = pl.width;
375+
let targetHeight = pl.height;
374376
let targetPitch = pl.width*4;
375377
let numPoints = pl.height * pl.width;
376378
let sizeInfo = null;
@@ -404,6 +406,8 @@ Scene.prototype.render = function(pl) {
404406

405407
sourcePitch = this.tiles.tileWidth * pl.width;
406408
targetPitch = sourcePitch*4;
409+
targetWidth = this.tiles.tileWidth * pl.width;
410+
targetHeight = this.tiles.tileHeight * pl.height;
407411
numPoints = numPoints * tileSize;
408412
sizeInfo = {};
409413
sizeInfo.width = this.tiles.tileWidth * pl.width;
@@ -417,25 +421,42 @@ Scene.prototype.render = function(pl) {
417421

418422
let scrollY = Math.floor(this._config.scrollY || 0);
419423
let scrollX = Math.floor(this._config.scrollX || 0);
420-
421-
for (let y = 0; y < sourceHeight; y++) {
422-
for (let x = 0; x < sourceWidth; x++) {
423-
let k = (y+scrollY)*sourcePitch + x+scrollX;
424-
let j = y*targetPitch + x*4;
425-
if (k < 0 || k >= source.length) {
426-
this.rgbBuffer[j+0] = 0;
427-
this.rgbBuffer[j+1] = 0;
428-
this.rgbBuffer[j+2] = 0;
429-
this.rgbBuffer[j+3] = 0xff;
430-
continue;
424+
scrollY = ((scrollY % sourceHeight) + sourceHeight) % sourceHeight;
425+
scrollX = ((scrollX % sourceWidth) + sourceWidth) % sourceWidth;
426+
427+
for (let attempt = 0; attempt < 4; attempt++) {
428+
for (let y = 0; y < sourceHeight; y++) {
429+
for (let x = 0; x < sourceWidth; x++) {
430+
let i, j;
431+
if (attempt == 0) {
432+
i = y - scrollY;
433+
j = x - scrollX;
434+
} else if (attempt == 1) {
435+
i = y - scrollY;
436+
j = x - scrollX + sourceWidth;
437+
} else if (attempt == 2) {
438+
i = y - scrollY + sourceHeight;
439+
j = x - scrollX;
440+
} else if (attempt == 3) {
441+
i = y - scrollY + sourceHeight;
442+
j = x - scrollX + sourceWidth;
443+
} else {
444+
continue;
445+
}
446+
if (i < 0 || i >= targetHeight || j < 0 || j >= targetWidth) {
447+
continue;
448+
}
449+
let s = y*sourcePitch + x;
450+
let t = i*targetPitch + j*4;
451+
let rgb = this._toColor(source[s]);
452+
this.rgbBuffer[t+0] = rgb.r;
453+
this.rgbBuffer[t+1] = rgb.g;
454+
this.rgbBuffer[t+2] = rgb.b;
455+
this.rgbBuffer[t+3] = 0xff;
431456
}
432-
let rgb = this._toColor(source[k]);
433-
this.rgbBuffer[j+0] = rgb.r;
434-
this.rgbBuffer[j+1] = rgb.g;
435-
this.rgbBuffer[j+2] = rgb.b;
436-
this.rgbBuffer[j+3] = 0xff;
437457
}
438458
}
459+
439460
if (sizeInfo) {
440461
this.rgbBuffer.width = sizeInfo.width;
441462
this.rgbBuffer.height = sizeInfo.height;

test/testdata/scroll_simple.png

196 Bytes
Loading

test/use_plane.js

+13
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,19 @@ describe('Use plane', function() {
3333
util.saveTmpCompareTo(ra, 'test/testdata/scroll_square.png');
3434
});
3535

36+
it('scroll same plane', function() {
37+
ra.resetState();
38+
ra.setSize(16);
39+
40+
let img = ra.loadImage('test/testdata/simple.png');
41+
ra.drawImage(img);
42+
43+
ra.setScrollX(4);
44+
ra.setScrollY(2);
45+
46+
util.saveTmpCompareTo(ra, 'test/testdata/scroll_simple.png');
47+
});
48+
3649
it('methods can destructure', function() {
3750
ra.resetState();
3851

test/web/scroll.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
describe('scroll', function () {
2+
it('simple', function(success) {
3+
let require = window['require'];
4+
let ra = require('raster');
5+
ra.resetState();
6+
ra.setSize(16);
7+
let img = ra.loadImage('img/simple.png');
8+
img.then(function() {
9+
ra.drawImage(img, 0, 0);
10+
ra.setScrollX(4);
11+
ra.setScrollY(2);
12+
ra.show(function() {
13+
}, ensureImageMatch('img/scroll_simple.png', success));
14+
});
15+
});
16+
});

0 commit comments

Comments
 (0)