rapha&# 235;l:尝试将半动画文本添加到旋转器图形中

Raphaël: trying to add semi-animated text to Spinner graphic

本文关键字:添加 文本 动画 旋转 图形 rapha      更新时间:2023-09-26

我想添加一串文本到我的旋转器图形。例如,我希望能够将文本放在下面,旁边的,或者内。问题是,我不知道该怎么做。另外,我只想让文本本身半动画化(如果我让它动画化太多,用户可能无法阅读它)。

  • 用Raphael创建的旋转器:
    • http://jsfiddle.net/mnbishop017/vM9uk/
    • http://jsfiddle.net/mnbishop017/5BHhE/
  • HTML 示例模型目标结果:http://jsfiddle.net/mnbishop017/4Enrk/

旋转器代码(JavaScript> Raphaël):

window.onload = function () {
    function spinner(holderid, R1, R2, count, stroke_width, color) {
        var sectorsCount = count || 12,
            color = color || "#fff",
            width = stroke_width || 15,
            r1 = Math.min(R1, R2) || 35,
            r2 = Math.max(R1, R2) || 60,
            cx = r2 + width,
            cy = r2 + width,
            r = Raphael(holderid, r2 * 2 + width * 2, r2 * 2 + width * 2),
            sectors = [],
            opacity = [],
            beta = 2 * Math.PI / sectorsCount,
            pathParams = {
                stroke: color,
                "stroke-width": width,
                "stroke-linecap": "round"
            };
        Raphael.getColor.reset();
        for (var i = 0; i < sectorsCount; i++) {
            var alpha = beta * i - Math.PI / 2,
                cos = Math.cos(alpha),
                sin = Math.sin(alpha);
            opacity[i] = 1 / sectorsCount * i;
            sectors[i] = r.path([
                ["M", cx + r1 * cos, cy + r1 * sin],
                ["L", cx + r2 * cos, cy + r2 * sin]
            ]).attr(pathParams);
            if (color == "rainbow") {
                sectors[i].attr("stroke", Raphael.getColor());
            }
        }
        var tick;
        (function ticker() {
            opacity.unshift(opacity.pop());
            for (var i = 0; i < sectorsCount; i++) {
                sectors[i].attr("opacity", opacity[i]);
            }
            r.safari();
            tick = setTimeout(ticker, 1000 / sectorsCount);
        })();
        return function () {
            clearTimeout(tick);
            r.remove();
        };
    }
    spinner("holder", 0, 120, 17, 2, "#000");
    // spinner("holder", -120, 120, 17, 2, "#005075"); Second spinner inserted above first spinner
};

期望结果的模型(HTML)

<div>
    <!-- .placeholder is where the main Loading graphic (or animation) appears -->
    <div class="placeholder"></div>
    <!-- .text is where the Loading text appears: -->
    <!-- next to the graphic, vertically aligned to the middle -->
    <div class="text">loading</div>
</div>

你在演示中混合了两个库,旋转器是Raphael,加载文本是jQuery。我在这里做了一个两者的组合- http://jsfiddle.net/robschmuecker/vM9uk/13/-它是这个HTML的组合:

<div class="box">
    <!-- .placeholder is where the main Loading graphic (or animation) appears -->
    <div class="placeholder"></div>
    <!-- .text is where the Loading text appears: next to the graphic -->
    <div class="text">loading <span class="dot">.</span>
 <span class="dot">.</span>
 <span class="dot">.</span>
    </div>
</div>

这个JavaScript包含了RaphaelJS和jQuery:

$(document).ready(function () {
    var firstDot = $(".dot:first-of-type");
    var midDot = $(".dot:nth-of-type(2)");
    var lastDot = $(".dot:last-of-type");
    firstDot.hide();
    midDot.hide();
    lastDot.hide();
    $(".placeholder,.text").hide().fadeIn(1000, function () {
        var interv = 0;
        interv = setInterval(function () {
            firstDot.fadeIn(250, function () {
                midDot.fadeIn(250, function () {
                    lastDot.fadeIn(250);
                });
            }).delay(1000).fadeOut(250, function () {
                midDot.fadeOut(250, function () {
                    lastDot.fadeOut(250);
                });
            });
        }, 3000)
    });
});

,然后是一些简单的CSS来操作加载文本和旋转控件的位置。然而,这可能不是理想的,因为您必须同时加载RaphaelJS和jQuery。在纯RaphaelJS中也可以做到这一点;请查看此处的参考:http://raphaeljs.com/reference.html和演示,以了解您想要做什么。作为一个例子,这里我们在旋转器的中间添加了一个简单的文本元素,并在一个连续的循环中淡出它。(演示:http://jsfiddle.net/robschmuecker/vM9uk/14/)

tempText = r.text(R2, R2, "Loading ...");
        function fadeTextOut(){
            tempText.animate({opacity: 0}, 2500, fadeTextIn);
        }
        function fadeTextIn(){
            tempText.animate({opacity: 1}, 2500, fadeTextOut);
        }
        fadeTextOut();