Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve fabric.Pattern toSvg #5164

Merged
merged 4 commits into from
Aug 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/pattern.class.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,16 @@
patternImgSrc = '';
if (this.repeat === 'repeat-x' || this.repeat === 'no-repeat') {
patternHeight = 1;
if (patternOffsetY) {
patternHeight += Math.abs(patternOffsetY);
}
}
if (this.repeat === 'repeat-y' || this.repeat === 'no-repeat') {
patternWidth = 1;
if (patternOffsetX) {
patternWidth += Math.abs(patternOffsetX);
}

}
if (patternSource.src) {
patternImgSrc = patternSource.src;
Expand Down
45 changes: 44 additions & 1 deletion test/unit/pattern.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,54 @@
});

QUnit.test('toSVG', function(assert) {
fabric.Object.__uid = 0;
var pattern = createPattern();
var rect = new fabric.Rect({ width: 500, height: 500 });
var expectedSVG = '<pattern id="SVGID_0" x="0" y="0" width="0.3" height="0.248">\n<image x="0" y="0" width="150" height="124" xlink:href="' + img.src + '"></image>\n</pattern>\n';
assert.ok(typeof pattern.toSVG === 'function');
assert.equal(pattern.toSVG(rect), expectedSVG, 'SVG match');
});

QUnit.test('toSVG repeat-y', function(assert) {
fabric.Object.__uid = 0;
var pattern = createPattern();
pattern.repeat = 'repeat-y';
var rect = new fabric.Rect({ width: 500, height: 500 });
var expectedSVG = '<pattern id="SVGID_0" x="0" y="0" width="1" height="0.248">\n<image x="0" y="0" width="150" height="124" xlink:href="' + img.src + '"></image>\n</pattern>\n';
assert.ok(typeof pattern.toSVG === 'function');
assert.equal(pattern.toSVG(rect), expectedSVG, 'SVG match repeat-y');
});

QUnit.test('toSVG repeat-x', function(assert) {
fabric.Object.__uid = 0;
var pattern = createPattern();
pattern.repeat = 'repeat-x';
var rect = new fabric.Rect({ width: 500, height: 500 });
var expectedSVG = '<pattern id="SVGID_0" x="0" y="0" width="0.3" height="1">\n<image x="0" y="0" width="150" height="124" xlink:href="' + img.src + '"></image>\n</pattern>\n';
assert.ok(typeof pattern.toSVG === 'function');
assert.equal(pattern.toSVG(rect), expectedSVG, 'SVG match repeat-x');
});

QUnit.test('toSVG no-repeat', function(assert) {
fabric.Object.__uid = 0;
var pattern = createPattern();
pattern.repeat = 'no-repeat';
var rect = new fabric.Rect({ width: 500, height: 500 });
var expectedSVG = '<pattern id="SVGID_0" x="0" y="0" width="1" height="1">\n<image x="0" y="0" width="150" height="124" xlink:href="' + img.src + '"></image>\n</pattern>\n';
assert.ok(typeof pattern.toSVG === 'function');
assert.equal(pattern.toSVG(rect), expectedSVG, 'SVG match no-repeat');
});

// TODO: test toSVG
QUnit.test('toSVG no-repeat offsetX and offsetY', function(assert) {
fabric.Object.__uid = 0;
var pattern = createPattern();
pattern.repeat = 'no-repeat';
pattern.offsetX = 50;
pattern.offsetY = -50;
var rect = new fabric.Rect({ width: 500, height: 500 });
var expectedSVG = '<pattern id="SVGID_0" x="0.1" y="-0.1" width="1.1" height="1.1">\n<image x="0" y="0" width="150" height="124" xlink:href="' + img.src + '"></image>\n</pattern>\n';
assert.ok(typeof pattern.toSVG === 'function');
assert.equal(pattern.toSVG(rect), expectedSVG, 'SVG match no-repat offsetX and offsetY');
});

QUnit.test('initPattern from object', function(assert) {
Expand Down