Home >Web Front-end >JS Tutorial >Share the practical 3D development of konva based on canvas in js, taking the rotating five-pointed star as an example

Share the practical 3D development of konva based on canvas in js, taking the rotating five-pointed star as an example

php是最好的语言
php是最好的语言Original
2018-07-23 17:46:552917browse

This article is a development of konva based on canvas. It mainly uses the Tween object in konva. Tween is the core object that controls the animation of Konva objects. The animation in this case is a type of tween animation: rotation animation. A starting angle of 0 and an ending angle of 360 degrees are given. An animation time of 6 seconds is also given. At the same time, in order to achieve a loop effect, after each animation is executed, the animation is reset (that is, returned to 0 degrees) and the animation is executed again.

1. Effect


Share the practical 3D development of konva based on canvas in js, taking the rotating five-pointed star as an example

2. Idea

Mainly use the Tween object in konva . tween, the English meaning is between the two, is actually Tweened Aniamation (the animation between the two, that is, the tween animation). What is tween animation? To put it bluntly, it gives you an initial state and allows you to gradually change to another state. For example, specify the initial (0, 0) coordinates and the end point coordinates (100, 100), and move from the starting point to the end point. How to move? Let the computer calculate it by itself. The movement process will produce displacement animation, which is a kind of tween animation.

  • #Tween is the core object that controls the animation of Konva objects.


  • Tween can control all numerical type attributes for animation, such as: x, y , rotation, width, height, radius, strokeWidth, opacity, scaleX, etc.

## The animation in this case is a type of tween animation: rotation animation. A starting angle of 0 and an ending angle of 360 degrees are given. An animation time of 6 seconds is also given. At the same time, in order to achieve a loop effect, after each animation is executed, the animation is reset (that is, returned to 0 degrees) and the animation is executed again.

3. Code

<!DOCTYPE html>
<html lang="zh-cmn-Hans">
<head>
    <meta charset="UTF-8">
    <title>使用js canvas库konva创建补间动画 楚士岩 2018年7月19日</title>
</head>
<body>
<p id="container"></p>
<script type="text/javascript" src="js/konva/konva2.1.7/konva.js"></script>

<script type="text/javascript">
    var stage = new Konva.Stage({
        container: "container",
        width: window.innerWidth,
        height: window.innerHeight
    });
    var layer = new Konva.Layer();

    var star = new Konva.Star({
        x: stage.getWidth() / 2,
        y: stage.getHeight() / 2,
        numPoints: 5, // 设置为五角形
        innerRadius: 40,  // innerRadius和outerRadius都必须设置
        outerRadius: 100,
        fill: &#39;red&#39;,
    });
    layer.add(star);

    var tween = new Konva.Tween({
        node: star,   // 需要添加补间动画的组件
        duration: 6,  // 持续时间
        draggable: true, // 设置可以拖拽
        easing: Konva.Easings.Linear, // 线性速度,即匀速
        rotation: 360,  // 6秒内旋转360度

        onFinish: function () {  // 动画完成时的回调函数
            this.reset();// 重置动画
            this.play(); // 重新播放动画
        }
        /* 不能使用yoyo:true代替onFinish中的代码
           因为使用yoyo,执行一圈后(6秒360度),会旋转回到原始状态,
           然后再执行下一圈动画。这样就无法实现五角星循环不停的朝一个方向旋转
         */
    });
    tween.play();
    stage.add(layer);
</script>
</body>
</html>

Related recommendations:

canvas and JS to implement dynamic clock animation

JS canvas Out of rotation animation

The above is the detailed content of Share the practical 3D development of konva based on canvas in js, taking the rotating five-pointed star as an example. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn