SVG 寻找一种反向绘图风格(异或笔)

huangapple go评论58阅读模式
英文:

SVG looking for an inverse drawing style (xor pen)

问题

SVG中是否实现了'反向绘图风格'?我在代码段中模拟了我的意思,但当事情变得更加复杂时,这不是一个合适的做法。例如,当您想要绘制一个'橡皮筋框'(或选择框)或者想要制作校准十字线时。

在这个示例中,应该是反向模式中的1条线,而不是模拟它的5条线。

这种反向绘图风格在许多编程环境中都可以找到。如果SVG中没有实现它,我会感到惊讶。但到目前为止,我还没有找到它。我尝试使用JavaScript模拟更复杂的绘图,但即使如此,也不容易,因为据我所知,没有办法探测特定点的颜色。

英文:

Is there an 'inverse drawing style' implemented in SVG? I simulate what I mean in the snippet, but that's not a proper way of doing it when things get more complicated. For instance when you want to draw a 'rubber box' (or selection box) or you want to make calibration crosses.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;svg width=&quot;200&quot; height=&quot;200&quot; viewBox=&quot;0 0 200 200&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;
&lt;g stroke-width=&quot;10&quot; fill=&quot;none&quot;&gt;
   &lt;rect x=&quot;50&quot; y=&quot;50&quot; width=&quot;100&quot; height=&quot;100&quot; stroke=&quot;black&quot; /&gt; 
   &lt;line x1=&quot;100&quot; y1=&quot;25&quot; x2=&quot;100&quot; y2=&quot;50&quot; stroke=&quot;black&quot; /&gt; 
   &lt;line x1=&quot;100&quot; y1=&quot;45&quot; x2=&quot;100&quot; y2=&quot;55&quot; stroke=&quot;white&quot; /&gt; 
   &lt;line x1=&quot;100&quot; y1=&quot;55&quot; x2=&quot;100&quot; y2=&quot;155&quot; stroke=&quot;black&quot; /&gt; 
   &lt;line x1=&quot;100&quot; y1=&quot;155&quot; x2=&quot;100&quot; y2=&quot;145&quot; stroke=&quot;white&quot; /&gt; 
   &lt;line x1=&quot;100&quot; y1=&quot;175&quot; x2=&quot;100&quot; y2=&quot;155&quot; stroke=&quot;black&quot; /&gt; 
&lt;/g&gt;
&lt;/svg&gt;

<!-- end snippet -->

In this example it should have been 1 line in invert modus in stead of 5 lines simulating it.

This kind of invert drawing you find in many programming environments. It would surprise me if it wasn't implemented in SVG. But so far, I didn't find it. I tried to simulate more complicated ones using javascript, but even then it is not easy, since there is no way of probing the color at a certain point, as far as I know.

答案1

得分: 2

形状元素如&lt;path&gt;&lt;polygon&gt;,以及文本内容元素支持fill-rule演示属性,以两种不同的方式计算单个路径内部和外部的内容。这使得可以像这样定义示例:

<svg viewBox="0 0 30 40" width="40%">
  <path fill-rule="nonzero" d="M 4,9 H 26 V 31 H 4 Z M 24,29 H 6 V 11 H 24 Z M 14,5 H 16 V 35 H 14 Z" />
</svg>
<svg viewBox="0 0 30 40" width="40%">
  <path fill-rule="evenodd" d="M 4,9 H 26 V 31 H 4 Z M 24,29 H 6 V 11 H 24 Z M 14,5 H 16 V 35 H 14 Z" />
</svg>

对于nonzero值,结果将取决于子路径是顺时针还是逆时针绘制的。

请注意,您无法将此属性应用于描述多个元素的交集;它总是适用于单个元素或字形。同样,它描述填充,对描边的渲染没有影响。

英文:

Shape elements like&lt;path&gt; and &lt;polygon&gt;, and text content elements support a fill-rule presentation attribute that computes what is inside and outside of a single path in two different ways. That makes it possible to define your example like this:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;svg viewBox=&quot;0 0 30 40&quot; width=&quot;40%&quot;&gt;
  &lt;path fill-rule=&quot;nonzero&quot; d=&quot;M 4,9 H 26 V 31 H 4 Z M 24,29 H 6 V 11 H 24 Z M 14,5 H 16 V 35 H 14 Z&quot; /&gt;
&lt;/svg&gt;
&lt;svg viewBox=&quot;0 0 30 40&quot; width=&quot;40%&quot;&gt;
  &lt;path fill-rule=&quot;evenodd&quot; d=&quot;M 4,9 H 26 V 31 H 4 Z M 24,29 H 6 V 11 H 24 Z M 14,5 H 16 V 35 H 14 Z&quot; /&gt;
&lt;/svg&gt;

<!-- end snippet -->

Results for the nonzero value will differ depending on subpaths being drawn in clockwise or anti-clockwise direction.

Note you cannot apply this attribute to describe the intersection of multiple elements; they always apply to only one element or glyph. Similarily, it describes fills and has no effect on the rendering of strokes.

答案2

得分: 0

以下是您要翻译的内容:

I want to give some more explanation for what I am looking for, what my question is about, and show you a possible go-around or circumvention.
By the way: invert mode is also known as xor pen.

In this example above I suppose the purpose of an invert modus is clearer.

The first time the 'cross' passes, it is not 'inverted' (nor simulated). So where the 'cross' passes over a black surface, you cannot see it anymore. If you had a real invert mode or xor pen: on white it would be black, on black it would be white. Where ever the cross is, then you can see it.

The second time the cross passes, I added a 'layer' with the same cross in white, translated or shifted 0.1 points. This is a circumvention, and like they all do, they ask more work or programming and might get you in trouble in some cases. A simple xor pen would make it easier to handle.

(most recent adaptation makes it work in Safari too - besides FireFox, Chrome, MS Edge)

英文:

I want to give some more explanation for what I am looking for, what my question is about, and show you a possible go-around or circumvention.
By the way: invert mode is also known as xor pen.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?&gt;
&lt;svg  width=&quot;400&quot; height=&quot;200&quot; viewBox=&quot;0 0 100 50&quot; 
   xmlns=&quot;http://www.w3.org/2000/svg&quot; 
   xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;
   onload=&quot;init()&quot;&gt;
   &lt;script&gt;
   var mc = {}, wp = {}; // mycross, whitepart
   var x = 5, intervalID = 0;
   function init() {
      mc = document.getElementById(&quot;mycross&quot;);
      wp = document.getElementById(&quot;whitepart&quot;);
      setTimeout(doloop, 1500);   
   }
   function doloop() {
      if (intervalID!=0) return;
      intervalID = setInterval(loop,30);    
   }
   function loop() {
      mc.setAttribute(&quot;x&quot;,x); x += 0.25;
      if(x&gt;75) { 
         clearInterval(intervalID); intervalID = 0; 
         if(wp.getAttribute(&quot;style&quot;) == &quot;display:none&quot;) {
            x = 5; mc.setAttribute(&quot;x&quot;,x);
            wp.setAttribute(&quot;style&quot;,&quot;display:inline-block&quot;);
            setTimeout(doloop, 1500); 
         } else {wp.setAttribute(&quot;style&quot;,&quot;display:none&quot;); }
      };
   }
   &lt;/script&gt;
   &lt;defs&gt;
      &lt;g id=&quot;cross&quot; style=&quot;fill:none;fill-opacity:1;stroke-width:0.26px;stroke-linecap:butt;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:0.264583, 0.264583;stroke-dashoffset:0;&quot;&gt;&gt;
         &lt;g stroke=&quot;black&quot;&gt;
            &lt;circle cx=&quot;10&quot; cy=&quot;10&quot; r=&quot;5&quot;/&gt;
            &lt;line x1=&quot;0&quot; y1=&quot;10&quot; x2= &quot;20&quot; y2=&quot;10&quot; /&gt;
            &lt;line x1=&quot;10&quot; y1=&quot;0&quot; x2= &quot;10&quot; y2=&quot;20&quot; /&gt;
         &lt;/g&gt;
         &lt;g id=&quot;whitepart&quot; stroke=&quot;white&quot; transform=&quot;translate(0.1 0.1)&quot; style=&quot;display:none&quot;&gt;
            &lt;circle cx=&quot;10&quot; cy=&quot;10&quot; r=&quot;5&quot;/&gt;
            &lt;line x1=&quot;0&quot; y1=&quot;10&quot; x2= &quot;20&quot; y2=&quot;10&quot; /&gt;
            &lt;line x1=&quot;10&quot; y1=&quot;0&quot; x2= &quot;10&quot; y2=&quot;20&quot; /&gt;
         &lt;/g&gt;
      &lt;/g&gt;
   &lt;/defs&gt;
   
   &lt;g fill=&quot;none&quot; stroke=&quot;black&quot;&gt;
      &lt;rect x=&quot;0&quot; y=&quot;0&quot; width=&quot;100&quot; height=&quot;50&quot; /&gt;
      &lt;circle cx=&quot;50&quot; cy=&quot;25&quot; r=&quot;2&quot; stroke-width=&quot;2.5&quot; /&gt;
      &lt;circle cx=&quot;50&quot; cy=&quot;25&quot; r=&quot;10&quot; stroke-width=&quot;5&quot; /&gt;
      &lt;circle cx=&quot;50&quot; cy=&quot;25&quot; r=&quot;20&quot; stroke-width=&quot;7.5&quot; /&gt;
   &lt;/g&gt;
   
   &lt;use id=&quot;mycross&quot; x=&quot;5&quot; y=&quot;15&quot; xlink:href=&quot;#cross&quot; /&gt;
&lt;/svg&gt;

<!-- end snippet -->

In this example above I suppose the purpose of an invert modus is clearer.

The first time the 'cross' passes, it is not 'inverted' (nor simulated). So where the 'cross' passes over a black surface, you cannot see it anymore. If you had a real invert mode or xor pen: on white it would be black, on black it would be white. Where ever the cross is, then you can see it.

The second time the cross passes, I added a 'layer' with the same cross in white, translated or shifted 0.1 points.
This is a circumvention, and like they all do, they ask more work or programming and might get you in trouble in some cases. A simple xor pen would make it easier to handle.

(most recent adaptation makes it work in Safari too - besides FireFox, Chrome, MS Edge)

huangapple
  • 本文由 发表于 2023年2月27日 15:41:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577846.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定