"ellipse" element created through JS doesn't appear in html - javascript

I'm new to svg. What i'm trying to do is to create an ellipse element with JS and append it to the SVG tag. The HTML code is
<svg width="640" height="480">
<ellipse cx="200" cy="100" rx="90" ry="60" stroke-width="10" stroke="orange" fill="none" opacity="0.6"/>
<ellipse cx="200" cy="100" rx="70" ry="40" stroke-width="10" stroke="green" fill="none" opacity="0.6"/>
</svg>
Below is the JS code
<script type="text/javascript">
var el=document.createElement('ellipse');
$(el).attr("cx",300);
$(el).attr("cy",200);
$(el).attr("stroke","red");
$(el).attr("stroke-width","10");
$(el).attr("fill","green");
$(el).attr("rx",120);
$(el).attr("ry",80);
$("svg").append(el);
</script>
But the ellipse didn't appear on the viewport ,but when i inspect HTML i found the ellipse element that i created is appended to SVG. what makes the difference when created this way & what is the correct approach to dynamically add elements to SVG
FYI see the image below

What you have is a custom HTML element instead of an SVG element. Use createElementNS to create the SVG element:
$(function(){
var el = document.createElementNS("http://www.w3.org/2000/svg", 'ellipse');
el.setAttribute('cx', 300);
el.setAttribute('cy', 200);
el.setAttribute('stroke', "red");
el.setAttribute('stroke-width', 10);
el.setAttribute('fill', "green");
el.setAttribute('rx', 120);
el.setAttribute('ry', 80);
$("svg").append(el);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<svg width="640" height="480">
<ellipse cx="200" cy="100" rx="90" ry="60" stroke-width="10" stroke="orange" fill="none" opacity="0.6"/>
<ellipse cx="200" cy="100" rx="70" ry="40" stroke-width="10" stroke="green" fill="none" opacity="0.6"/>
</svg>

Related

Render a single SVG element to canvas or image

How can I render a single SVG element (as opposed to the whole SVG document or parts of it) to an image or a canvas?
I have an SVG document with a lot of nodes. If I render the whole SVG or a part of it, the resulting image will contain other pieces of graphics I'm not interested in. I am interested in one specific SVG element and would like to render all of it without anything else.
Example:
<!DOCTYPE html>
<html>
<body>
<svg height="150" width="150">
<circle cx="50" cy="50" r="25" stroke="green" stroke-width="3" fill="gray" />
<circle id="IWantToRenderThis" cx="75" cy="75" r="25" stroke="red" stroke-width="3" fill="white" />
<circle cx="100" cy="100" r="25" stroke="blue" stroke-width="3" fill="black" />
</svg>
<script>
const target = document.getElementById("IWantToRenderThis");
// how can I render *target* into a texture?
</script>
</body>
</html>
How can I render target alone? I would like to obtain an image which has no traces of the other two circles, a transparent background, and the right size to fit target perfectly.
You could remove all elements but the one you want to render.
To fit the SVG to the remaining element, you need to calculate a new position and size. For your example the code could look like this:
<!DOCTYPE html>
<html>
<body>
<svg height="150" width="150" id="svg">
<circle cx="50" cy="50" r="25" stroke="green" stroke-width="3" fill="gray" />
<circle id="IWantToRenderThis" cx="75" cy="75" r="25" stroke="red" stroke-width="3" fill="white" />
<circle cx="100" cy="100" r="25" stroke="blue" stroke-width="3" fill="black" />
</svg>
<script>
const svg = document.getElementById("svg");
const target = document.getElementById("IWantToRenderThis");
const children = svg.children;
// Remove all child elements but the target
for(let index = 0; index < children.length; index++) {
const child = children[index];
if(child.id !== 'IWantToRenderThis') {
child.remove()
}
}
// Recalculate element position and svg size
const targetSize = parseInt(target.getAttribute('r'))
const targetStroke = parseInt(target.getAttribute('stroke-width'))
target.setAttribute('cx', targetSize + (targetStroke/2))
target.setAttribute('cy', targetSize + (targetStroke/2))
svg.setAttribute('width', targetSize*2 + targetStroke)
svg.setAttribute('height', targetSize*2 + targetStroke)
</script>
</body>
</html>
Note that you have to include the stroke width of your element to properly calculate its new position and the SVG's new size.
Here the target element is just copied using outerHTML into a new data URL representing the new SVG, loaded into an image object, drawn in a canvas and exported as a PNG image.
let img = document.getElementById('img');
let target = document.getElementById("IWantToRenderThis");
let svg = target.closest('svg');
let width = svg.attributes['width'].value;
let height = svg.attributes['height'].value;
let image = new Image();
let canvas = document.getElementById('canvas');
canvas.height = height;
canvas.width = width;
let ctx = canvas.getContext('2d');
image.addEventListener('load', e => {
ctx.drawImage(e.target, 0, 0, e.target.width, e.target.height);
img.src = canvas.toDataURL("image/png");
});
image.src = `data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg"
width="${width}" height="${height}">${target.outerHTML}</svg>`;
<p>Original SVG:</p>
<svg id="svg" height="150" width="150">
<circle cx="50" cy="50" r="25" stroke="green" stroke-width="3" fill="gray" />
<circle id="IWantToRenderThis" cx="75" cy="75" r="25" stroke="red" stroke-width="3" fill="white" />
<circle cx="100" cy="100" r="25" stroke="blue" stroke-width="3" fill="black" />
</svg>
<p>SVG rendered in canvas:</p>
<canvas id="canvas"></canvas>
<p>PNG image based on canvas:</p>
<img id="img" />

<use> - SVG: Stroke of Circle overriding stroke of use element

Stroke of both <use> element ignored here. The stroke color of <circle> is set blue which is appearing on both <use> element too. Why?
I want different strock color of all of 3 these element. but it is not working.
<svg width="300" class="svg-elem" viewBox="0 0 30 10" xmlns="http://www.w3.org/2000/svg">  
<circle id="myCircle" cx="5" cy="5" r="4" stroke="blue"/>
 
<use class="circle1" href="#myCircle" x="10" stroke="grey" fill="blue"/>
 
<use href="#myCircle" x="20" fill="white" stroke="red"/>
</svg>
Because circle still overriding the <use>.
You can consider using CSS variable to control stroke color like below.
<svg width="300" class="svg-elem" viewBox="0 0 30 10" xmlns="http://www.w3.org/2000/svg">
<circle id="myCircle" cx="5" cy="5" r="4" style="stroke:var(--stroke, blue)"/>
<use class="circle1" href="#myCircle" x="10" style="--stroke:gray;" fill="blue"/>
<use href="#myCircle" x="20" fill="white" style="--stroke:red;"/>
</svg>

Dynamic Animation for SVG Elements is not working in IE

I'm trying to do SVG element's animation while dynamically adding DOM elements with jquery. If I add those elements inside <body> as below its working.Working Sample for this is
http://jsfiddle.net/bZdfH/2/
<svg>
<script type="text/ecmascript" xlink:href="http://leunen.me/fakesmile/smil.user.js"/>
<circle cx="60" cy="60" r="20" style="fill: pink; stroke: red;" >
<animate attributeName="r" dur="4s" values="20; 0; 20" repeatCount="indefinite"/>
</circle>
</svg>
When I add it dynamically, animation will not start in IE, however it works with Chrome and FireFox.Here is what I have.
<svg>
<script type="text/ecmascript" xlink:href="http://leunen.me/fakesmile/smil.user.js"/>
<circle cx="60" cy="60" r="20" style="fill: pink; stroke: red;" onmouseover="changeImage(this)" >
</circle>
</svg>
<script>
function changeImage(circle) {
while (circle.firstChild) {
circle.removeChild(circle.firstChild);
}
circle.setAttributeNS(null, "fill", "blue");
var animate = document.createElementNS("http://www.w3.org/2000/svg", "animate");
animate.setAttributeNS(null, "attributeName", "r");
animate.setAttributeNS(null, "values", "20;0;20");
animate.setAttributeNS(null, "dur", "6s");
animate.setAttributeNS(null, "repeatCount", "indefinite");
circle.appendChild(animate);
}
</script>
Here is jsfiddle for the Working Sample.Can anyone please help me??
IE doesn't support SMIL animation.
Source : http://caniuse.com/#search=svg

How to get all elements at mouse position?

I have many elements on the same position and I want to listen for hover event on every element behind even if they are behind other elements, is there a way I can do this?
(They are not hierarchically related and sometimes they are circles, polygons, etc, so checking for bounding rect is not ok)
http://jsfiddle.net/4NdNS/4/
$circles.on("mouseover",function(){console.log(this);});
this is the solution:
FIDDLE
html:
<div id=response></div>
<svg id="mycircle Area">
<circle id="C1" fill="none" r="20" stroke="black" stroke-width="4" cx="100" cy="100"></circle>
<circle fill="none" r="20" stroke="black" stroke-width="4" cx="100" cy="100"></circle>
<circle fill="none" r="20" stroke="black" stroke-width="4" cx="100" cy="100"></circle>
</svg>
jq:
$('circle').on("mousedown",function(e){
$("#response").append($(e).attr('id')+' ');
e.preventDefault();
});
css:
circle{
pointer-events: all;
}
this is your edited fiddle

How can I implement eraser function in SVG?

I think SVG is better than HTML5 canvas for some features, but I can't imagine an easy way to make eraser function.
I there any way or any example?
This is a really janky way of doing it, but you could simply mimic your standard pen tool with a white stroke.
After looking at many examples (including Geert Bellemans answer here), I finally came up with this code that works. To use the Eraser, you draw new circles and append them inside the node. To draw new lines/shapes, you append them inside the node. Here it is:
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" xmlns:xlink="http://www.w3.org/1999/xlink" width="500px" height="638px">
<mask id="mask_1">
<rect width="100%" height="100%" fill="#fff"></rect>
<circle cx="223" cy="122" r="8" stroke="#000" stroke-width="0px" fill="#000"></circle>
<circle cx="222" cy="124" r="8" stroke="#000" stroke-width="0px" fill="#000"></circle>
<circle cx="221" cy="125" r="8" stroke="#000" stroke-width="0px" fill="#000"></circle>
<circle cx="220" cy="126" r="8" stroke="#000" stroke-width="0px" fill="#000"></circle>
<circle cx="220" cy="127" r="8" stroke="#000" stroke-width="0px" fill="#000"></circle>
</mask>
<g mask="url(#mask_1)">
<polyline fill="none" points=" 210,149 212,148 213,144 215,142 216,139 219,135 220,133 224,128 226,126 229,121 233,117 237,112 243,107 248,103 253,99 258,96 263,92 268,90 271,88 272,87 273,87 274,87 275,86 275,85 276,85 277,85 278,85 279,85 280,84 " stroke="#ff0000" stroke-width="4"></polyline>
</g>
</svg>
You could use the clipPath element. Put your eraser drawing inside a clipPath element and clip the drawing you want to overlay with the eraser. This way the background stays visible.
<clipPath id="test">
<polyline points="298,351 302,350 307,347 313,343 320,339 329,336 343,332 357,328 374,325 389,321 402,320 416,319 428,317 439,316 447,314 455,313 462,312 467,312 471,312 478,310 482,310 485,310 486,310 487,309 490,309 491,309 493,309 494,308 495,308 497,308 497,306 499,304 501,301 502,298 504,294 505,290 506,287 508,283 509,281 512,275 513,273 513,270 514,266 516,263 517,259 517,255 518,251 520,245 521,241 521,239 521,236 521,235 521,232 521,229 521,228 520,228 516,228 513,228 508,228 502,232 494,237 487,241 478,248 466,256 452,267 435,278 421,289 408,300 394,310 386,320 375,329 368,339 362,346 356,350 352,356 347,362 344,363 341,367 341,369 340,371 340,377 339,382 337,390 335,400 330,409 326,419 322,428 318,436 314,444 312,450 310,455 307,458 307,459 307,462 309,461 312,458 314,455 317,452 321,450 324,446 326,444 332,440 337,436 349,428 364,420 376,412 390,405 405,397 421,390 436,383 452,378 470,373 483,369 501,366 513,363 527,362 536,360 541,359 544,359 548,362 554,363 563,367 577,370 587,375 602,379 617,383 633,390 655,396 674,402 697,409 714,415 733,420 748,425 759,429 767,432 770,432 771,432 773,432 777,431 783,424 792,417 802,410 813,400 827,389 843,377 866,358 884,343 898,333 909,327 920,320 927,317 932,313 935,310 938,309 940,308"
fill="none" stroke="#ffffff" stroke-width="20"></polyline>
</clipPath>
<path d="M600,329 600,325 600,321 595,317 591,310 590,306 586,304 585,301 582,298 577,296 574,294 571,293 567,290 563,290 556,287 556,287 551,286 544,285 539,285 539,285 532,285 524,285 516,283 506,283 498,283 490,283 482,285 474,287 467,289 459,291 455,293 451,293 448,296 447,296 445,297 445,298 445,300 445,301 445,302 445,304 445,305 445,306 445,309 445,312 445,316 445,320 445,325 447,329 448,332 449,335 449,337 452,340 454,344 456,346 459,348 460,352 464,355 467,356 468,359 472,360 474,362 475,363 478,363 481,363 487,366 490,367 493,367 497,367 498,367 502,367 505,367 508,367 512,367 516,367 520,367 525,367 531,367 539,367 545,367 555,367 560,367 564,367 567,367 568,367 570,367 570,366 571,364 571,363 571,362 573,362"
fill="none" stroke="#000000" stroke-width="3" clip-path="url(#test)"></polyline>
You could redraw objects with display attribute set to "none"
See here or here

Categories

Resources