从 paper.js 站点复制粘贴的代码到 codepen 不再工作

问题描述

您好,我正在尝试让 SymbolDeFinitions 在笔中工作。 paper.js 站点上的示例完美运行,但是当我将代码移植到 codepen 时,它不再起作用。代码笔有一些调整可以在窗口中安装纸张paper.install(window) and paper.setup('canvas')

这是代码

paper.install(window);
paper.setup("canvas");
var path = new Path.Star(new Point(100,100),10,5,13);
path.style = {
    fillColor: 'white',strokeColor: 'black'
};

// Create a symbol deFinition from the path:
var deFinition = new SymbolDeFinition(path);

// Place 100 instances of the symbol deFinition:
for (var i = 0; i < 100; i++) {
    // Place an instance of the symbol deFinition in the project:
    var instance = deFinition.place();

    // Move the instance to a random position within the view:
    instance.position = Point.random() * view.size;

    // Rotate the instance by a random amount between
    // 0 and 360 degrees:
    instance.rotate(Math.random() * 360);

    // Scale the instance between 0.25 and 1:
    instance.scale(0.25 + Math.random() * 0.75);
}

提前致谢

解决方法

您可以通过将代码放在 codepen 的 HTML 部分的 <script type="text/paperscript" canvas="canvas"></script/ 标记中来直接使用 PaperScript。
这是一个 demonstration