繁体   English   中英

如何从Polygon动态更新SVG viewBox值

[英]How can I dynamically update SVG viewBox values from Polygon

我创建了漫画书页面板的SVG地图。

<svg id="svg1413"  class="svg-pg" width="100%" height="100%" version="1.1" viewBox="0 0 178 254" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="transition: .6s ease-out;">
    <image id="image1966" width="178" height="254" clip-path="url('#SvgjsClipPath1413')" xlink:href="https://i.imgur.com/yiZFUK4.jpg" />
    <g id="SvgjsG1413" clip-path="url('#SvgjsClipPath1413')" class="click-panels">
        <polygon fill="transparent" points=" 12,13 88,13 88,49 12,49"></polygon>
        <polygon fill="transparent" points=" 81,44 166,44 166,74 81,74"></polygon>
        <polygon fill="transparent" points=" 12,13 166,13 166,75 12,75"></polygon>
        <polygon fill="transparent" points=" 101,80 166,80 166,118 101,118"></polygon>
        <polygon fill="transparent" points=" 12,80 166,80 166,147 12,147"></polygon>
        <polygon fill="transparent" points=" 12,152 91,152 91,200 12,200"></polygon>
        <polygon fill="transparent" points=" 73,171 155,171 155,209 73,209"></polygon>
        <polygon fill="transparent" points=" 12,198 79,198 79,235 12,235"></polygon>
        <polygon fill="transparent" points=" 12,152 166,152 166,235 12,235"></polygon>
    </g>
    <defs id="SvgjsDefs1413">
        <clipPath id="SvgjsClipPath1413">
            <rect id="SvgjsRect1413" width="100%" height="100%" x="0" y="0">
            </rect>
        </clipPath>
    </defs>     
</svg>
<svg>
    ...svg content
</svg>
<svg>
    ...svg content
</svg>

我想要实现的是:

  • 遍历所有<svg>标签
  • 在<svg>上,循环遍历<svg>中包含的<polygon>标签
  • 每个<polygon>包含4对坐标(例如, points=" x1,y1 x2,y2 x3,y3 x4,y4"
  • 我希望使用循环中当前<polygon>点的值更新父<svg> viewBox值和<rect> X和Y值。

我已经弄清楚如何计算值,但我不知道如何在Javascript中实现这一点。

我的伪代码用于计算<svg> viewBox

if <svg viewBox=" a b c d ">
a = x1, b = y1, c = x2 - x1, d = y3 - y2

更新<rect>值

if <rect x="e" y="f">
e = x1, f = y1

更新:如何通过点击/滑动事件控制过渡而不是自动?

<div id="controls" class="ctl-btn" style="width: 100%; position: absolute; bottom: 0; margin: 0 -8px; background-color: rgba(6,6,6,0.40);">
    <div style="max-width: 800px; text-align: center; margin: 0 auto;">
        <button class="pg-ctl-bk" style="margin: 8px; padding: 8px 10px;">  Back </button>
        <button class="pg-ctl-nxt" style="margin: 8px; padding: 8px 10px;"> Next  </button>
    </div>
</div>

我不知道为什么你在这里使用<polygon>元素,当你需要的只是一个javascript数组/ JSON,或者最终的<rect>元素,它们至少具有viewBox属性所要求的xywidthheight属性,但是很好...让我们处理<polygon> s然后......

要将<polygon>的point属性值转换为viewBox属性,最简单的方法是调用polygonElement.getBBox() ,它将返回带有所需值的SVGRect。

要使viewBox属性设置动画,最简单的可能是使用SMIL动画,以及MS浏览器的polyfill。

您只需定义一个定位viewBox属性的<animate>元素,并在将其from属性更新为当前值后将其to属性更新为目标值。

// animate : <animate attributeName="viewBox" ...>
// rect : {SVGRect} result of polygonElement.getBBox();
function animateViewBox(animate, rect) {
  animate.setAttribute('from', animate.getAttribute('to'));
  animate.setAttribute('to', `${rect.x} ${rect.y} ${rect.width} ${rect.height}`);
  animate.beginElement(); // (re)start the animation
}

一旦我们有了这个,我们只需要设置一个函数来迭代svg中的所有<polygon>元素。

 function animateViewBox(animate, rect) { animate.setAttribute('from', animate.getAttribute('to')); animate.setAttribute('to', `${rect.x} ${rect.y} ${rect.width} ${rect.height}`); animate.beginElement(); // (re)start the animation } // container const svg = document.getElementById('svg1413'); // all the <polygons> coordinates (would be better as JSON...) const polygons = svg.querySelectorAll('polygon'); // <animate> element const animator = svg.querySelector('.viewBoxAnimator'); // our iterator, we could call it on click let i = 0; function iterate() { if (i < polygons.length) { animateViewBox(animator, polygons[i++].getBBox()); return true; } } // but we'll automate it (async() => { while (iterate()) { await wait(1500); } })(); function wait(time) { return new Promise(res => setTimeout(res, time)); } 
 svg { width: 100%; height: 100%; max-width: 100vw; max-height: 100vh; transition: all .6s; } html { background: black; } 
 <!-- SMIL for IE --> <script src="https://cdn.jsdelivr.net/gh/Kaiido/FakeSmile@1e50d675df616a8e784e0e6e931b3f0d595367d4/smil.user.js"></script> <svg id="svg1413" class="svg-pg" width="154" height="83" version="1.1" viewBox="0 0 178 254" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> <animate class="viewBoxAnimator" attributeType="XML" attributeName="viewBox" from="0 0 178 254" to="0 0 178 254" dur="0.6s" fill="freeze"/> <image id="image1966" width="178" height="254" xlink:href="https://i.imgur.com/yiZFUK4.jpg" /> <g id="SvgjsG1413" class="click-panels"> <polygon fill="transparent" points=" 12,13 88,13 88,49 12,49"></polygon> <polygon fill="transparent" points=" 81,44 166,44 166,74 81,74"></polygon> <polygon fill="transparent" points=" 12,13 166,13 166,75 12,75"></polygon> <polygon fill="transparent" points=" 101,80 166,80 166,118 101,118"></polygon> <polygon fill="transparent" points=" 12,80 166,80 166,147 12,147"></polygon> <polygon fill="transparent" points=" 12,152 91,152 91,200 12,200"></polygon> <polygon fill="transparent" points=" 73,171 155,171 155,209 73,209"></polygon> <polygon fill="transparent" points=" 12,198 79,198 79,235 12,235"></polygon> <polygon fill="transparent" points=" 12,152 166,152 166,235 12,235"></polygon> </g> </svg> 

你使用了svg.js标签,所以你得到一个svg.js答案:

 // Reference to svg const canvas = SVG('#svg1413') // List of all polygons const polygons = canvas.find('#SvgjsG1413 polygon') // List of all bboxes const boxes = polygons.bbox() const nextImage = function (index) { // Animate viewbox over 1s to new box canvas.animate(1000).viewbox(boxes[index]) // Next image in 2s setTimeout(() => nextImage(++index), 2000) } nextImage(0) 
 svg { width: 100%; height: 100%; max-width: 100vw; max-height: 100vh; } html { background: black; } 
 <script src="https://cdn.jsdelivr.net/npm/@svgdotjs/svg.js@latest/dist/svg.min.js"></script> <svg id="svg1413" class="svg-pg" width="154" height="254" version="1.1" viewBox="0 0 178 254" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" style="transition: .6s ease-out;"> <image id="image1966" width="178" height="254" clip-path="url('#SvgjsClipPath1413')" xlink:href="https://i.imgur.com/yiZFUK4.jpg" /> <g id="SvgjsG1413" clip-path="url('#SvgjsClipPath1413')" class="click-panels"> <polygon fill="transparent" points=" 12,13 88,13 88,49 12,49"></polygon> <polygon fill="transparent" points=" 81,44 166,44 166,74 81,74"></polygon> <polygon fill="transparent" points=" 12,13 166,13 166,75 12,75"></polygon> <polygon fill="transparent" points=" 101,80 166,80 166,118 101,118"></polygon> <polygon fill="transparent" points=" 12,80 166,80 166,147 12,147"></polygon> <polygon fill="transparent" points=" 12,152 91,152 91,200 12,200"></polygon> <polygon fill="transparent" points=" 73,171 155,171 155,209 73,209"></polygon> <polygon fill="transparent" points=" 12,198 79,198 79,235 12,235"></polygon> <polygon fill="transparent" points=" 12,152 166,152 166,235 12,235"></polygon> </g> </svg> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM