Skip to content

Commit

Permalink
Merge pull request #2703 from asturur/fill-opacity
Browse files Browse the repository at this point in the history
Fill opacity and stroke opacity
  • Loading branch information
kangax committed Dec 24, 2015
2 parents 75db7f5 + 453c7b5 commit 6f0cc79
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,17 @@
function _setStrokeFillOpacity(attributes) {
for (var attr in colorAttributes) {

if (!attributes[attr] || typeof attributes[colorAttributes[attr]] === 'undefined') {
if (typeof attributes[colorAttributes[attr]] === 'undefined') {
continue;
}

if (!attributes[attr]) {
if (!fabric.Object.prototype[attr]) {
continue;
}
attributes[attr] = fabric.Object.prototype[attr];
}

if (attributes[attr].indexOf('url(') === 0) {
continue;
}
Expand Down
46 changes: 46 additions & 0 deletions test/unit/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,52 @@
}
});

test('fill-opacity attribute with fill attribute', function() {
var el = fabric.document.createElement(rect);
var opacityValue = Math.random().toFixed(2);

el.setAttribute('fill-opacity', opacityValue);
el.setAttribute('fill', '#FF0000');
var obj = fabric.Rect.fromElement(el);

equal(obj.fill, 'rgba(255,0,0,' + parseFloat(opacityValue) * 255 + ')',
'opacity should be parsed correctly from "opacity" attribute of ' + tagNames[i] + ' element');
});

test('fill-opacity attribute without fill attribute', function() {
var el = fabric.document.createElement(rect);
var opacityValue = Math.random().toFixed(2);

el.setAttribute('fill-opacity', opacityValue);
var obj = fabric.Rect.fromElement(el);

equal(obj.fill, 'rgba(0,0,0,' + parseFloat(opacityValue) * 255 + ')',
'opacity should be parsed correctly from "opacity" attribute of ' + tagNames[i] + ' element');
});

test('stroke-opacity attribute with fill attribute', function() {
var el = fabric.document.createElement(rect);
var opacityValue = Math.random().toFixed(2);

el.setAttribute('fill-opacity', opacityValue);
el.setAttribute('fill', '#FF0000');
var obj = fabric.Rect.fromElement(el);

equal(obj.fill, 'rgba(255,0,0,' + parseFloat(opacityValue) * 255 + ')',
'opacity should be parsed correctly from "opacity" attribute of ' + tagNames[i] + ' element');
});

test('stroke-opacity attribute without fill attribute', function() {
var el = fabric.document.createElement(rect);
var opacityValue = Math.random().toFixed(2);

el.setAttribute('fill-opacity', opacityValue);
var obj = fabric.Rect.fromElement(el);

equal(obj.fill, 'rgba(0,0,0,' + parseFloat(opacityValue) * 255 + ')',
'opacity should be parsed correctly from "opacity" attribute of ' + tagNames[i] + ' element');
});

test('getCssRule', function() {

ok(fabric.getCSSRules);
Expand Down

0 comments on commit 6f0cc79

Please sign in to comment.