Skip to content

Commit efb6a96

Browse files
committed
Fix faulty position regression in Item#rasterize()
Closes #1905
1 parent cd0b87e commit efb6a96

File tree

3 files changed

+45
-10
lines changed

3 files changed

+45
-10
lines changed

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
13
1+
14

src/item/Item.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1796,21 +1796,23 @@ new function() { // Injection scope for various item event handlers
17961796
resolution = arg0;
17971797
insert = arg1;
17981798
}
1799-
if (!raster) {
1799+
if (raster) {
1800+
raster.matrix.reset(true);
1801+
} else {
18001802
raster = new Raster(Item.NO_INSERT);
18011803
}
1802-
// TODO: Switch to options object for more descriptive call signature.
18031804
var bounds = this.getStrokeBounds(),
18041805
scale = (resolution || this.getView().getResolution()) / 72,
18051806
// Floor top-left corner and ceil bottom-right corner, to never
18061807
// blur or cut pixels.
18071808
topLeft = bounds.getTopLeft().floor(),
18081809
bottomRight = bounds.getBottomRight().ceil(),
1809-
size = new Size(bottomRight.subtract(topLeft)).multiply(scale);
1810+
boundsSize = new Size(bottomRight.subtract(topLeft)),
1811+
rasterSize = boundsSize.multiply(scale);
18101812
// Pass `true` for clear, so reused rasters don't draw over old pixels.
1811-
raster.setSize(size, true);
1813+
raster.setSize(rasterSize, true);
18121814

1813-
if (!size.isZero()) {
1815+
if (!rasterSize.isZero()) {
18141816
var ctx = raster.getContext(true),
18151817
matrix = new Matrix().scale(scale).translate(topLeft.negate());
18161818
ctx.save();
@@ -1819,11 +1821,15 @@ new function() { // Injection scope for various item event handlers
18191821
this.draw(ctx, new Base({ matrices: [matrix] }));
18201822
ctx.restore();
18211823
}
1822-
raster.transform(new Matrix().translate(topLeft.add(size.divide(2)))
1824+
raster.transform(
1825+
new Matrix()
1826+
.translate(topLeft.add(boundsSize.divide(2)))
18231827
// Take resolution into account and scale back to original size.
1824-
.scale(1 / scale));
1825-
if (insert === undefined || insert)
1828+
.scale(1 / scale)
1829+
);
1830+
if (insert === undefined || insert) {
18261831
raster.insertAbove(this);
1832+
}
18271833
return raster;
18281834
},
18291835

test/tests/Item.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1025,10 +1025,39 @@ test('Children global matrices are cleared after parent transformation', functio
10251025

10261026
test('Item#rasterize() with empty bounds', function() {
10271027
new Path.Line([0, 0], [100, 0]).rasterize();
1028-
view.update();
1028+
view.update(); // This should not throw
10291029
expect(0);
10301030
});
10311031

1032+
test('Item#rasterize() bounds', function() {
1033+
var circle = new Path.Circle({
1034+
center: [50, 50],
1035+
radius: 5,
1036+
fillColor: 'red'
1037+
});
1038+
equals(function() {
1039+
return circle.bounds;
1040+
}, new Rectangle({ x: 45, y: 45, width: 10, height: 10 }));
1041+
equals(function() {
1042+
return circle.rasterize({ resolution: 72 }).bounds;
1043+
}, new Rectangle({ x: 45, y: 45, width: 10, height: 10 }));
1044+
equals(function() {
1045+
return circle.rasterize({ resolution: 144 }).bounds;
1046+
}, new Rectangle({ x: 45, y: 45, width: 10, height: 10 }));
1047+
equals(function() {
1048+
return circle.rasterize({ resolution: 200 }).bounds;
1049+
}, new Rectangle({ x: 45.14, y: 45.14, width: 9.72, height: 9.72 }));
1050+
equals(function() {
1051+
return circle.rasterize({ resolution: 400 }).bounds;
1052+
}, new Rectangle({ x: 45.05, y: 45.05, width: 9.9, height: 9.9 }));
1053+
equals(function() {
1054+
return circle.rasterize({ resolution: 600 }).bounds;
1055+
}, new Rectangle({ x: 45.02, y: 45.02, width: 9.96, height: 9.96 }));
1056+
equals(function() {
1057+
return circle.rasterize({ resolution: 1000 }).bounds;
1058+
}, new Rectangle({ x: 45.032, y: 45.032, width: 9.936, height: 9.936 }));
1059+
});
1060+
10321061
test('Item#draw() with CompoundPath as clip item', function() {
10331062
function createdClippedGroup(invertedOrder) {
10341063
var compound = new CompoundPath({

0 commit comments

Comments
 (0)