英文:
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 -->
<svg width="200" height="200" viewBox="0 0 200 200" xmlns="http://www.w3.org/2000/svg">
<g stroke-width="10" fill="none">
<rect x="50" y="50" width="100" height="100" stroke="black" />
<line x1="100" y1="25" x2="100" y2="50" stroke="black" />
<line x1="100" y1="45" x2="100" y2="55" stroke="white" />
<line x1="100" y1="55" x2="100" y2="155" stroke="black" />
<line x1="100" y1="155" x2="100" y2="145" stroke="white" />
<line x1="100" y1="175" x2="100" y2="155" stroke="black" />
</g>
</svg>
<!-- 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
形状元素如<path>
和<polygon>
,以及文本内容元素支持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<path>
and <polygon>
, 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 -->
<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>
<!-- 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 -->
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<svg width="400" height="200" viewBox="0 0 100 50"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink"
onload="init()">
<script>
var mc = {}, wp = {}; // mycross, whitepart
var x = 5, intervalID = 0;
function init() {
mc = document.getElementById("mycross");
wp = document.getElementById("whitepart");
setTimeout(doloop, 1500);
}
function doloop() {
if (intervalID!=0) return;
intervalID = setInterval(loop,30);
}
function loop() {
mc.setAttribute("x",x); x += 0.25;
if(x>75) {
clearInterval(intervalID); intervalID = 0;
if(wp.getAttribute("style") == "display:none") {
x = 5; mc.setAttribute("x",x);
wp.setAttribute("style","display:inline-block");
setTimeout(doloop, 1500);
} else {wp.setAttribute("style","display:none"); }
};
}
</script>
<defs>
<g id="cross" style="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;">>
<g stroke="black">
<circle cx="10" cy="10" r="5"/>
<line x1="0" y1="10" x2= "20" y2="10" />
<line x1="10" y1="0" x2= "10" y2="20" />
</g>
<g id="whitepart" stroke="white" transform="translate(0.1 0.1)" style="display:none">
<circle cx="10" cy="10" r="5"/>
<line x1="0" y1="10" x2= "20" y2="10" />
<line x1="10" y1="0" x2= "10" y2="20" />
</g>
</g>
</defs>
<g fill="none" stroke="black">
<rect x="0" y="0" width="100" height="50" />
<circle cx="50" cy="25" r="2" stroke-width="2.5" />
<circle cx="50" cy="25" r="10" stroke-width="5" />
<circle cx="50" cy="25" r="20" stroke-width="7.5" />
</g>
<use id="mycross" x="5" y="15" xlink:href="#cross" />
</svg>
<!-- 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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论