我创建了漫画页面面板的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>
我想要实现的是:
points=" x1,y1 x2,y2 x3,y3 x4,y4"
)viewBox
值和我已经弄清楚如何计算值,但是我不知道如何用Javascript实现。
我的用于计算viewBox
值的伪代码
if <svg viewBox=" a b c d ">
a = x1, b = y1, c = x2 - x1, d = y3 - y2
更新
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>
答案 0 :(得分:4)
我不确定为什么您需要在其中使用JavaScript Array / JSON或最终x
,y
,{ width
属性所请求的{1}}和height
属性,但是...让我们处理
要将您的viewBox
属性值转换为point
属性,最简单的方法是调用viewBox
,它将返回带有所需值的SVGRect。
要使polygonElement.getBBox()
属性具有动画效果,最简单的方法可能是使用SMIL动画和用于MS浏览器的polyfill。
您只需定义一个viewBox
属性为目标的<animate>元素,并将其viewBox
属性更新为目标值,即可将其to
属性更新为目标值当前值。
from
有了这个,我们只需要设置一个函数即可迭代svg中的所有
// 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
}
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;
}
答案 1 :(得分:4)
您使用了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>