views:

96

answers:

3

For some reason I can't get my SVG filters to work in Firefox. They work fine in Opera, however. The element whose property I set to the filter on just disappears. It's very odd.

Here's my javascript code:

defsElement = SVGDoc.createElement("defs");
var filterElement = SVGDoc.createElement("filter");
filterElement.setAttribute( "id", "cm-mat");
filterElement.setAttribute( "filterUnits", "objectBoundingBox");

var fecolormatrixElement = SVGDoc.createElement("feColorMatrix");
fecolormatrixElement.setAttribute("type", "matrix");
fecolormatrixElement.setAttribute("in", "SourceGraphic");
fecolormatrixElement.setAttributeNS(null, "values", "1 1 1 1 1  2 2 2 2 1  1 1 1 1 1  1 1 1 1 1");
filterElement.appendChild(fecolormatrixElement);
defsElement.appendChild(filterElement);
SVGDoc.documentElement.insertBefore(defsElement, SVGDoc.documentElement.childNodes.item(1));

partRef = getElementFromID(SVGDoc.documentElement, part);
if(partRef != null)
{
    partRef.style.setProperty('filter', 'url(#cm-mat)', null);
}

Any Thoughts? Thanks

A: 

Paul Irish made a demo applying SVG Filters to HTML 5 video.

The source code for the live demo shows how to switch between filters. In that case, all the SVG pieces are written directly into the page as tags, not inserted dynamically via JavaScript.

It might help to try and get it working using straight up tags, then switch over to JavaScript once it's working. There may be some strange oddity of the implementation (bug) which only expresses itself when created dynamically (/speculation).

Also, it may depend no what version of Firefox you're using. I'm not sure which version started supporting SVG filters, but Paul's post seems to suggest it may require a nightly build.

Good luck!

jimbojw
+1  A: 

That color matrix looks to me like it should to turn every component of every color to fully on, making the element completely white.

(It also might be easier for other people to figure out if you posted the URL of a complete example showing the problem rather than just a javascript snippet; that would allow other people to test theories as to why it's going wrong.)

David Baron
Yes, the result should be fully white if the filter is applied. I'd be interested in hearing more about what Opera is doing in your original example (my guess would be elements in the wrong namespace, since you use createElement instead of createElementNS). A non-scripted equivalent of the above snippet shows Opera and Firefox behaving the same (white output).
Erik Dahlström
Yea sorry, I was screwing around with the values and forgot to reset them. I've tried a few drastically different values with no change. I just gave up on dynamically adding the svg elements with javascript and added them manually. It works now, thanks.
Nick
A: 

your page needs to be served as xml.

Henry