How can I implement eraser function in SVG? - javascript

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

Related

<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>

How to change fill color SVG object for specific path id via AngularJS

svg:
<path id="svg_1" stroke-width="0.5" stroke="black" fill="none" d="m50.610001,63.470001l12.869999,0m0,0l0,464.709991m0,0l-12.869999,0m0,0l0,-464.709991"/>
<path id="svg_2" stroke-width="0.5" stroke="black" fill="none" d="m66.529999,260.670013l12.870003,0m0,0l-0.029999,267.519989m0,0l-12.870003,0m0,0l0.029999,-267.519989"/>
<path id="svg_3" stroke-width="0.5" stroke="black" fill="none" d="m66.519997,275.440002l-3.039997,0"/>
html:
<div ng-include="'example.svg'"></div>
I would like to change fill color for id="svg_2" for example on button click, How to do it?
The problem are your paths. After every line you are using a move to command (m) where you are moving to the last point. I've rewritten the first 2 paths bu removing the move to (m0,0) Now the paths can be filled. The third path is just a line.
Now you can fill the paths using css or bu resetting the value of the attribute fill
path{fill:red}
<svg viewBox="0 0 130 600" width="100">
<path id="svg_1" stroke-width="0.5" stroke="black" fill="none" d="M50.610001,63.470001l12.869999,0
l0,464.709991
l-12.869999,0
l0,-464.709991"/>
<path id="svg_2" stroke-width="0.5" stroke="black" fill="none" d="M66.529999,260.670013l12.870003,0
l-0.029999,267.519989
l-12.870003,0
l0.029999,-267.519989"/>
<path id="svg_3" stroke-width="0.5" stroke="black" fill="none" d="m66.519997,275.440002l-3.039997,0"/>
</svg>

How i can style SVG <use> elements on hover?

I'm beginner in SVG. I'm trying to change style of multiple <use> elements on hover at a specific <use> element with css, but I can't, because <use> elements using Shadow DOM.
I have the following <defs>:
<defs>
<filter id="Sjax0b81q1" filterUnits="userSpaceOnUse">...</filter>
<circle cx="0" cy="0" r="40" id="action-circle" style="cursor: move; fill: #fff;" filter="url('#Sjax0b81q1')" class="el action-el"></circle>
<g id="condition-rhombus" style="cursor: move; fill: #fff;" class="el condition-el" transform="matrix(1,0,0,1,0,0)">
<circle cx="0" cy="0" r="40" fill="none"></circle>
<path d="M -35 0, L 0 -35, L 35 0, L 0 35 L -35 0" style="stroke-linecap: round; stroke: white;" filter="url('#Sjax0b81q1')" class="condition-rhombus"></path>
</g>
<g id="svg-plus-button">
<circle cx="0" cy="40" r="10" id="svg-plus-circle" fill="none" style="fill-opacity: 1;" class="svg-plus-circle"></circle>
<path d="M956.8,408.....408.5z" id="svg-plus-sign" fill="none" transform="matrix(0.008,0,0,0.008,-4,36)" style="pointer-events: none;" class="svg-plus-sign"></path>
</g>
<rect x="-20" y="-20" width="40" height="40" id="rect-active-layer" fill="none" style="pointer-events: visible;" class="rect-active-layer"></rect>
<path d="M252.967.....2v1Z" id="api-svg" class="cls-1"></path>
</defs>
And I have a group of elements that contains several <use> elements:
<g id="action-group-2" class="external action-group" transform="matrix(1,0,0,1,420,180)">
<g class="internal action-group">
<rect x="-40" y="-40" width="80" height="80" fill="none"></rect>
</g>
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#action-circle"></use>
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#svg-plus-button" id="useSjax0b81q1k"></use>
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#rect-active-layer"></use>
<use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#api-svg"></use>
</g>
For example, I need to change the <path> fill in element with id #api-svg, when I hover on the #action-circle.
How can I do this? Maybe there is another way to render and styling reusable elements on hover.
Define the path to have fill="inherit", then you should be able to set fill="whatever" on the <use> element's styles and it will work.
use:hover {
fill: red;
}
<svg>
<defs>
<circle id="test" fill="inherit" cy="10" r="10" />
</defs>
<use xlink:href="#test" transform="translate(10,0)" />
<use xlink:href="#test" transform="translate(30,0)" />
<use xlink:href="#test" transform="translate(50,0)" />
<text y="40">Hover over the circles!</text>
</svg>
There's nothing overly complex here. Just change the 'use' element, rather than anything in the defs area (unless you want it to affect everything that references it).
You can either style the use element via normal css, via a css selector, e.g it's id is possibly simplest.
Or you can set the fill svg attribute on the svg 'use' element you are handling.
You shouldn't need fill inherit or anything unless I'm missing something.

encircle the object depending on the set percentages

I have such task: a circle without borders and we get some percentages (from user or just enter by ourself - doesn't mater), after it the border of circle should be filled depending on this number.
I'm trying to use SVG. But it's not clear for me. It doesn't work with persentages and I don't know how to get right number for all length
<svg width="270" height="270">
<circle
r="115" cx="135" cy="135"
fill="none";
stroke="blue"
stroke-width="15"
stroke-dasharray="1000"
stroke-dashoffset="0%">
<animate
attributeName="stroke-dashoffset"
attributeType="CSS"
from="1000" to="00"
begin="0s" dur="2s"/>
</svg>
Image to see how it shoul be
One observation : the value of stroke-dasharray if you want it to be full should be the circumference of the circle in pixels. So 2*Pi*r.
The stroke-dashoffset is the amount of pixels that are offset from the stroke, so for example :
A circle of 115px radius will have a circumference of 2*Pi*115=722.56 723px.
If we want to fill it at 75%, we must offset the stroke by 723*(1-0.75)=180.75 181px.
Examples below :
<svg id="svg" width="270" height="270">
<circle id="circle"
r="115" cx="135" cy="135"
fill="none";
stroke="blue"
stroke-dasharray="723"
stroke-dashoffset="361"
stroke-width="15">
<animate id="animation"
attributeName="stroke-dashoffset"
attributeType="CSS"
begin="0s"
to="361"
from="723"
dur="2s"/>
</circle>
<text x="50%" y="50%" text-anchor="middle" stroke="#000" stroke-width="2px" dy=".3em">50%</text>
</svg>
<svg id="svg" width="270" height="270">
<circle id="circle"
r="115" cx="135" cy="135"
fill="none";
stroke="blue"
stroke-dasharray="723"
stroke-dashoffset="181"
stroke-width="15">
<animate id="animation"
attributeName="stroke-dashoffset"
attributeType="CSS"
begin="0s"
to="181"
from="723"
dur="2s"/>
</circle>
<text x="50%" y="50%" text-anchor="middle" stroke="#000" stroke-width="2px" dy=".3em">75%</text>
</svg>

SVG element not visible on screen, can see element added in console

I have the following SVG code. The SVG with id "nestedsvg" is being appended in the HTML, I can view it on the console. But it's not visible on the screen. I tried assigning it a z-index of 99 but still it's invisible. Where am I going wrong?
<svg data="BusinessRoleFigure" x="144" y="95"
width="128" height="66" id="outer" style="position: relative;">
<rect x="0" y="0" width="100%" height="100%"
stroke="rgb(178,178,126)" stroke-width="1" fill="rgb(255,255,181)"
style="position: relative;"></rect>
<svg id="nestedsvg" x="100%" height="100" width="50">
<rect x="-50" rx="5" ry="5" width="20" height="10" stroke="black"
stroke-width="1" fill="black" z-index="99"></rect>
</svg>
<circle cx="118" cy="13" r="5" fill="none"
stroke-linejoin="round" stroke="black"
z-index="1" stroke-width="1"></circle>
</svg>
Jsfiddle: http://jsfiddle.net/MxHPq/145/
This is because the rectangle you are drawing is outside of the nested SVG viewport.
That SVG has a width and height of 100x50, and you are drawing a 20x10 rectangle at (-50,0). Meaning the rectangle covers the area from (-50,0) to (-30,10). So it is not visible. By default, objects outside a nested SVG viewport are not visible.
There are two ways to fix this:
Make objects outside the viewport visible. You can do this by setting overflow="visible" on the nested SVG.
<svg data="BusinessRoleFigure" x="144" y="95" width="128" height="66" id="outer">
<rect x="0" y="0" width="100%" height="100%" stroke="rgb(178,178,126)" stroke-width="1" fill="rgb(255,255,181)"></rect>
<svg id="nestedsvg" x="100%" height="100" width="50" overflow="visible">
<rect x="-50" rx="5" ry="5" width="20" height="10" stroke="black" stroke-width="1" fill="black"></rect>
</svg>
<circle cx="118" cy="13" r="5" fill="none" stroke-linejoin="round" stroke="black" stroke-width="1"></circle>
</svg>
Move the rectangle inside the SVG viewport and reposition the SVG so that the rectangle ends up in the same place.
I don't know why you wanted the nested SVG to be at x="100%", but you would need to change that if you go with this solution.
<svg data="BusinessRoleFigure" width="128" height="66" id="outer">
<rect x="0" y="0" width="100%" height="100%" stroke="rgb(178,178,126)" stroke-width="1" fill="rgb(255,255,181)"></rect>
<svg id="nestedsvg" x="78" height="100" width="50">
<rect x="0" rx="5" ry="5" width="20" height="10" stroke="black" stroke-width="1" fill="black"></rect>
</svg>
<circle cx="118" cy="13" r="5" fill="none" stroke-linejoin="round" stroke="black" stroke-width="1"></circle>
</svg>
A few other notes about your original SVG:
x and y coordinates on the root <svg> element have no effect.
z-index currently has no meaning in SVGs. Although this may change for the upcoming SVG2 standard.
position: relative has no meaning in SVGs.
I've removed these things from my modified examples.

Categories

Resources