svennergr svennergr - 2 months ago 22
Javascript Question

Problems with adding SVG clip-path attribute via JavaScript

I am currently trying to get something like a conical gradiant on a Google Gauge chart.
You can see my tries in that fiddle: https://jsfiddle.net/fqt8pjuw/

I am adding a gauge-chart and a conical gradiant at a HTML canvas.
Then I am adding a path-element to a SVG-clipPath:

This path-elements gets the path from the red bar in the gauge chart.

Finally I am creating the image-Element and adding all elements to the specifiy element in the svg.

But sadly the clip-path does not seem to work proper. I am not getting the path as expected but the whole image.

There might be some conflicts with using the right namespaces on certain functions. I would be glad if somebody could check.



google.charts.load('current', {'packages':['gauge']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {

var data = google.visualization.arrayToDataTable([
['Label', 'Value'],
['Test1', 80],
['Test2', 80],
]);

var options = {
width: 400, height: 120,
redFrom: 0, redTo: 100,
minorTicks: 5
};

chart = new google.visualization.Gauge(document.getElementById('chart_div'));

chart.draw(data, options);


canvas = document.createElement('canvas'),
d = canvas.width = canvas.height = 200,
ctx = canvas.getContext('2d');

//document.body.appendChild(canvas);

ctx.translate(d/2, d/2);
ctx.rotate(-Math.PI/2-Math.PI/8-Math.PI/16);
ctx.scale(-1,1);
ctx.lineWidth = 2;
ctx.lineCap = 'round';

for(var i=0; i<=360; i++) {
ctx.save();

ctx.rotate(Math.PI * i/180);
ctx.translate(-ctx.lineWidth/2, ctx.lineWidth/2);

ctx.beginPath();
ctx.moveTo(0, 0);
ctx.strokeStyle = 'hsl(' + i + ',100%,50%)';

ctx.lineTo(0, d);
ctx.stroke();
ctx.closePath();

ctx.restore();
}

svgEl=chart.ga.getElementsByTagName("svg");
pathEl=svgEl[0].getElementsByTagName("path")[0];
clipPath=pathEl.getAttribute("d");

xmlns = "http://www.w3.org/2000/svg";

cp=document.createElementNS(xmlns,"clipPath");
cp.setAttribute("id", "cp1");
cp.setAttributeNS(xmlns, "clipPathUnits","userSpaceOnUse");

p=document.createElementNS(xmlns, "path");
p.setAttributeNS(xmlns, "d", clipPath);
//p.setAttribute("transform","translate(40,40)");
//p.setAttribute("style", "opacity:1;fill:none;fill-opacity:1;stroke:#000000;stroke-width:3;stroke-linecap:square;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1");

cp.appendChild(p);

img=document.createElementNS(xmlns, "image");
img.setAttributeNS('http://www.w3.org/1999/xlink', 'href', canvas.toDataURL()+" ");
img.setAttributeNS(xmlns, "clip-path","url(#cp1)");
img.setAttribute("width", 200);
img.setAttribute("height",200);
img.setAttribute("x", 0);
img.setAttribute("y",0);
img.setAttribute("transform","translate(-40,-40)");

svgEl[0].getElementsByTagName("defs")[0].appendChild(cp);


svgEl[0].childNodes[1].insertBefore(img, svgEl[0].childNodes[1].childNodes[3]);
}

<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div" style="width: 400px; height: 120px;"></div>




Answer

There are no attributes in the SVG namespace (http://www.w3.org/2000/svg) in SVG. Only elements are in the SVG namespace.

So for attribute setting in general you want setAttribute except for xlink:href (SVG 1.1) and the rarely used xml:space attribute (also SVG 1.1). SVG 2 will do away with both of these exceptions so that all attributes will be able to be set with setAttribute.