GVKun编程网logo

javascript – 如何为’Raphael’画布制作动画?(javascript做动画)

14

在本文中,我们将详细介绍javascript–如何为’Raphael’画布制作动画?的各个方面,并为您提供关于javascript做动画的相关解答,同时,我们也将为您带来关于java–如何为陀螺制作动

在本文中,我们将详细介绍javascript – 如何为’Raphael’画布制作动画?的各个方面,并为您提供关于javascript做动画的相关解答,同时,我们也将为您带来关于java – 如何为陀螺制作动画?、JavaScript MVC框架raphaelJS、javascript – gRaphael – 动画线图失败、javascript – gRaphael饼图动态大小的有用知识。

本文目录一览:

javascript – 如何为’Raphael’画布制作动画?(javascript做动画)

javascript – 如何为’Raphael’画布制作动画?(javascript做动画)

我正在使用Raphael库来创建SVG对象.

要初始化SVG形状并为内部的所有内容创建画布,我使用了文档中所述的以下行:

var paper = Raphael(10,50,320,200);

我现在想要对纸张及其内部的所有内容进行动画制作,就像我将动画一个已经附加到纸张上的形状一样:

var firstShape = paper.rect(x,y,width,height);
firstShape.animate({x: 100},500,"<");

当我尝试与纸张类似的东西时,例如:

paper.animate({x: 100},"<");

我得到错误’paper.animate不是函数’.

这里发生了什么,我该如何解决它?

解决方法

您的右括号序列错误

更改

paper.animate({x: 100,"<")};

paper.animate({x: 100},"<");

UPDATE

从文档来看,似乎动画不能直接在纸上调用,而只能在画布上切出的子形状 – 这就是为什么firstshape.animate有效

java – 如何为陀螺制作动画?

java – 如何为陀螺制作动画?

它是光明节,我正在尝试动画陀螺(dreidel):

我可以让它在自己的轴上旋转.这是我的代码:

import static javafx.scene.paint.Color.*;

import javafx.animation.KeyFrame;
import javafx.animation.keyvalue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.geometry.Point3D;
import javafx.scene.Camera;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.scene.paint.phongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.shape.Cylinder;
import javafx.scene.shape.Sphere;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;
import javafx.util.Duration;

public class DreidelAnim extends Application {

    private double bodyBase = 30;
    private double bodyHeight = bodyBase * 3 / 2;
    private double baseRadius = bodyBase / 2;

    @Override
    public void start(Stage stage) throws Exception {
        DoubleProperty spinAngle = new SimpleDoubleproperty();
        Rotate spin = new Rotate(0,Rotate.Z_AXIS);
        spin.angleproperty().bind(spinAngle);

        Timeline spinAnim = new Timeline(new KeyFrame(Duration.seconds(2),new keyvalue(spinAngle,360)));
        spinAnim.setCycleCount(Timeline.INDEFINITE);
        spinAnim.play();

        Group dreidel = createDreidel();
        Translate zTrans = new Translate(0,-(bodyHeight/2 + baseRadius));
        dreidel.getTransforms().addAll(spin,zTrans);

        Scene scene = new Scene(dreidel,200,true,SceneAntialiasing.BALANCED);
        scene.setFill(SKYBLUE);
        scene.setCamera(createCamera());

        stage.setScene(scene);
        stage.show();
    }

    private Group createDreidel() {
        double handleHeight = bodyBase * 3/4;
        Cylinder handle = new Cylinder(bodyBase / 6,handleHeight);
        handle.setTranslateZ(-(bodyHeight + handleHeight) / 2);
        handle.setRotationAxis(Rotate.X_AXIS);
        handle.setRotate(90);
        handle.setMaterial(new phongMaterial(RED));

        Box body = new Box(bodyBase,bodyBase,bodyHeight);
        body.setMaterial(new phongMaterial(BLUE));

        Sphere base = new Sphere(baseRadius);
        base.setTranslateZ(bodyHeight / 2);
        base.setMaterial(new phongMaterial(GREEN));

        return new Group(handle,body,base);
    }

    private Camera createCamera() {
        PerspectiveCamera camera = new PerspectiveCamera(true);
        camera.setFarClip(1000);

        int xy = 150;
        Translate trans = new Translate(-xy,xy,-120);
        Rotate rotXY = new Rotate(70,new Point3D(1,1,0));
        Rotate rotZ = new Rotate(45,new Point3D(0,1));
        camera.getTransforms().addAll(trans,rotXY,rotZ);

        return camera;
    }

    public static void main(String[] args) {
        launch();
    }
}

我创建了一个简单的模型,围绕其轴旋转,并将其翻译为其尖端(0,0).结果如下:

如何才能实现与顶部图片类似的旋转轴旋转?

解决方法

物体旋转的轴的旋转称为 Precession.旋转顶部运动需要2次旋转:

>物体围绕其内轴旋转(与红色手柄平行).
>围绕静态轴旋转其中一个内部轴(在本例中为z).

从表面上看,你需要2个动画实例.但是,两个旋转实际上是相同的.两者的轴心点是(0,0)(在zTrans之后)并且它们都在z轴周围,只有一个以一定角度倾斜.

这是修改后的代码:

import static javafx.scene.paint.Color.*;

import javafx.animation.KeyFrame;
import javafx.animation.keyvalue;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.geometry.Point3D;
import javafx.scene.Camera;
import javafx.scene.Group;
import javafx.scene.PerspectiveCamera;
import javafx.scene.Scene;
import javafx.scene.SceneAntialiasing;
import javafx.scene.paint.phongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.shape.Cylinder;
import javafx.scene.shape.Sphere;
import javafx.scene.transform.Rotate;
import javafx.scene.transform.Translate;
import javafx.stage.Stage;
import javafx.util.Duration;

public class FinalDreidelSpin extends Application {

    private double bodyBase = 30;
    private double bodyHeight = bodyBase * 3 / 2;
    private double baseRadius = bodyBase / 2;

    @Override
    public void start(Stage stage) throws Exception {
        double tiltAngle = 40;
        DoubleProperty spinAngle = new SimpleDoubleproperty();

        Rotate spin = new Rotate(0,Rotate.Z_AXIS);
        Rotate tilt = new Rotate(tiltAngle,Rotate.X_AXIS);

        spin.angleproperty().bind(spinAngle);

        Timeline spinAnim = new Timeline();
        spinAnim.getKeyFrames().add(new KeyFrame(Duration.seconds(2),tilt,spin,zTrans);

        Scene scene = new Scene(new Group(dreidel,createAxes()),-100);
        Rotate rotXY = new Rotate(70,rotZ);

        return camera;
    }

    private Group createAxes() {
        int axisWidth = 1;
        int axisLength = 400;

        Cylinder xAxis = new Cylinder(axisWidth,axisLength);
        xAxis.setMaterial(new phongMaterial(CYAN));

        Cylinder yAxis = new Cylinder(axisWidth,axisLength);
        yAxis.setRotationAxis(Rotate.Z_AXIS);
        yAxis.setRotate(90);
        yAxis.setMaterial(new phongMaterial(magenta));

        Cylinder zAxis = new Cylinder(axisWidth,axisLength);
        zAxis.setRotationAxis(Rotate.X_AXIS);
        zAxis.setRotate(90);
        zAxis.setMaterial(new phongMaterial(YELLOW));

        return new Group(xAxis,yAxis,zAxis);
    }

    public static void main(String[] args) {
        launch();
    }
}

我添加了轴表示以方便查看.请注意,getTransforms()列表不要求其对象是唯一的(与getChildren()不同),这允许我们重用相同的动画.如下所述,动画的顺序也很重要.

倾斜是围绕x或y轴的简单旋转.
如果我们倾斜然后旋转,getTransforms().addAll(tilt,zTrans),我们将得到内部旋转(上面列出的1),只是倾斜:

如果我们旋转然后倾斜,getTransforms().addAll(spin,我们就会得到进动(上面列出的2):

在完整代码中组合2将得到所需的结果:

JavaScript MVC框架raphaelJS

JavaScript MVC框架raphaelJS

我正在尝试制作一个具有客户端MVC架构的应用程序,但除了 HTML模板之外,它还有SVG元素作为View(我使用raphael来管理它).

是否有任何JavaScript MVC框架与raphaelJS一起作为View运行良好?

如果没有,是否有任何建议的框架可以很好地与它一起工作?

解决方法

Backbone.js是一个简单的MVC框架,它不限制使用哪个模板引擎,而是给自己选择.

在骨干渲染功能中,它总是从一些JSON数据生成HTML代码,如下所示:

render: function () {
    // use underscore as template engine
    this.el.innerHTML = _.template(TMPL_STRING).render(JSON_DATA);
}

在这里使用RaphaelJS非常简单:

initialize: function () {
    this.paper = Raphael(this.el,width,height);
},render: function () {
    // use this.paper to render svg here
}

javascript – gRaphael – 动画线图失败

javascript – gRaphael – 动画线图失败

我遇到了gRaphael javascript折线图库的问题.

我正在从CSV文件构建折线图,该文件有五列(分钟数,时间,等待时间,处理时间,关闭时间,位置).

以前我能够绘制没有动画的完整图表.它正确地拥有了所有四条线等.

现在我的代码在动画功能上失败了.这是错误:

Uncaught TypeError: Object # has no method ‘animate’

我假设jQuery在某种程度上搞乱了animate函数,并试图抓住它的缰绳.

        function animateChart(newX, newW, newInT, newC, newInL){
            var chart2 = paper.linechart(
                20, 20, // padding
                newX.length, 400, // dimensions
                newX, [newW, newInT, newC, newInL] // values
            );

            for (i = 0; i < chart.lines.length; i++){
                elem = chart.lines[i][0];
                elem.animate({ path: chart2.lines[i][0].getAttribute("d") }, 200);
            }

            chart2.remove();
        }

完整代码:

http://pastebin.com/YmvkrmQ3

我按顺序加载了以下库:

> raphael-min.js
> g.raphael-min.js
> g.line.min.js
> jquery.js

在此先感谢您的帮助.

更新:
问题出在animate方法中.即使我在路径元素上调用方法,我也会收到错误.我仍然不知道拉斐尔为什么不将路径元素识别为路径元素.

我尝试禁用jQuery(并使用vanilla javascript替换它的ajax函数),但它没有帮助.

解决方法:

您可能有一个SVG路径元素而不是Raphael路径元素.它可能是elem = chart.lines [i] [0];末尾的[0].

javascript – gRaphael饼图动态大小

javascript – gRaphael饼图动态大小

我想在一些事件后放大gRaphael pie chart,点击例如.但是gRaphael饼图是在窗口加载后创建的,具有静态宽度和高度.是否可以实时动态重绘?

我的代码:

var myRadius;

myRadius = 200;

var myChart = Raphael("myChartDiv"), 
    myChartPie = myChart.piechart(
        myRadius, // cx
        myRadius, // cy
        myRadius, //Radius
        [70, 30], //Data    
        {           
        colors: ["#eb5e57", "#ebad50"],         
        stroke: "#f1eeea"       
        }
    );

$('#myButton').click(function(){
     myRadius = 500; 
     myChart.clear(); //Clear current canvas
     // Do something to insert myChart with new myRadius
});

UPD
当然我可以在点击当前图表后删除并创建新的,但这不是一种方式,因为我需要在其他点击后返回原始大小.

解决方法:

你只需要清除&像this fiddle一样重绘你的馅饼.

var myRadius;

myRadius = 200;

var myChart = Raphael("myChartDiv"), 
    myChartPie = myChart.piechart(
        myRadius, // cx
        myRadius, // cy
        myRadius, //Radius
        [70, 30], //Data    
        {           
            colors: ["#eb5e57", "#ebad50"],         
            stroke: "#f1eeea"       
        }
    );

$('#myButton').click(function(){
    if(myRadius == 200)
         myRadius = 500; 
    else
        myRadius = 200;
    myChart.clear();
    myChartPie = myChart.piechart(
        myRadius, // cx
        myRadius, // cy
        myRadius, //Radius
        [70, 30], //Data    
        {           
            colors: ["#eb5e57", "#ebad50"],         
            stroke: "#f1eeea"       
        }
    );
});

当然,数据和选项应该分解并放在全球可用性范围内.

今天关于javascript – 如何为’Raphael’画布制作动画?javascript做动画的讲解已经结束,谢谢您的阅读,如果想了解更多关于java – 如何为陀螺制作动画?、JavaScript MVC框架raphaelJS、javascript – gRaphael – 动画线图失败、javascript – gRaphael饼图动态大小的相关知识,请在本站搜索。

本文标签: