修改Paperjs画布示例移动多个项目

时间:2013-07-23 16:37:56

标签: html5 canvas paperjs

在Paperjs网站上,有一个在此页面上移动多个项目的示例http://paperjs.org/tutorials/animation/creating-animations/移动项目是从左到右连续浮动的圆圈。我正在尝试修改此示例以使圆圈从底部连续浮动到顶部。到目前为止,我已经设法让它们从底部漂浮到顶部但由于某种原因,它不是连续的。

我尝试对该示例进行的另一个修改是使圆圈具有基于给定颜色数组的随机颜色。到目前为止,每次刷新和加载页面时都会生成随机颜色。

  1. 如何让圆圈从下到上连续浮动?

  2. 如何让动画中的圆圈随机着色,而不仅仅是在页面加载时?

  3. 这是我的修改代码:

    // The amount of circles we want to make:
        var count = 50;
    
        /*random colors for circles*/
        var circleColors = new Array();
        circleColors[0] = "#2ab4e4";//BLUE
        circleColors[1] = "#a2a7a6";//GREY
        circleColors[2] = "#ef7047";//ORANGE
        circleColors[3] = "#ffffff";//WHITE
        /*end random colors for circles*/
    
        // Create a symbol, which we will use to place instances of later:
        var path = new Path.Circle({
            center: [0, 0],
            radius: 35,
            fillColor: circleColors[Math.floor(Math.random() * circleColors.length)]
        });
    
        var symbol = new Symbol(path);
    
        // Place the instances of the symbol:
        for (var i = 0; i < count; i++) {
            // The center position is a random point in the view:
            var center = Point.random() * view.size;
            var placedSymbol = symbol.place(center);
            placedSymbol.scale(i / count);
        }
    
        // The onFrame function is called up to 60 times a second:
        function onFrame(event) {
            // Run through the active layer's children list and change
            // the position of the placed symbols:
            for (var i = 0; i < count; i++) {
                var item = project.activeLayer.children[i];
    
                // Move the item 1/20th of its width to the right. This way
                // larger circles move faster than smaller circles:
                item.position.y -= item.bounds.height / 20;
    
                // If the item has left the view on the right, move it back
                // to the left:
                if (item.bounds.bottom > view.size.height) {
                    item.position.y = -item.bounds.height;
                }
            }
        }
    

    以下是Paperjs的原始代码:

    // The amount of circles we want to make:
        var count = 150;
    
    // Create a symbol, which we will use to place instances of later:
        var path = new Path.Circle({
        center: [0, 0],
        radius: 10,
        fillColor: 'white',
        strokeColor: 'black'
        });
    
        var symbol = new Symbol(path);
    
    // Place the instances of the symbol:
        for (var i = 0; i < count; i++) {
        // The center position is a random point in the view:
        var center = Point.random() * view.size;
        var placedSymbol = symbol.place(center);
        placedSymbol.scale(i / count);
        }
    
    // The onFrame function is called up to 60 times a second:
        function onFrame(event) {
        // Run through the active layer's children list and change
        // the position of the placed symbols:
        for (var i = 0; i < count; i++) {
        var item = project.activeLayer.children[i];
    
        // Move the item 1/20th of its width to the right. This way
        // larger circles move faster than smaller circles:
        item.position.x += item.bounds.width / 20;
    
        // If the item has left the view on the right, move it back
        // to the left:
        if (item.bounds.left > view.size.width) {
            item.position.x = -item.bounds.width;
        }
        }
        }
    

1 个答案:

答案 0 :(得分:0)

paper.js坐标系的原点位于画布的左上角。要将项目放在视口的边界下方,您需要将视图的高度添加到项目的高度。

if (item.bounds.bottom < 0) {
            item.position.y = view.size.height+item.bounds.height;
        }

对于随机颜色,您必须使用pathItems,而不是placementSymbols来改变对象的颜色样式。而不是:

var path = new Path.Circle({
    center: [0, 0],
    radius: 35,
    fillColor: circleColors[Math.floor(Math.random() * circleColors.length)]
});

var symbol = new Symbol(path);

// Place the instances of the symbol:
for (var i = 0; i < count; i++) {
    // The center position is a random point in the view:
    var center = Point.random() * view.size;
    var placedSymbol = symbol.place(center);
    placedSymbol.scale(i / count);
}

你需要更多的东西:

// Place the instances of the symbol:
for (var i = 0; i < count; i++) {
    var path = new Path.Circle({
    center: Point.random() * view.size,
    radius: i / count,
    fillColor: circleColors[Math.floor(Math.random() * circleColors.length)]
});

有关详细信息,请参阅此帖子: Does all Paths in a Swarm need to have the same fillColor?