Paper.js代码在Chrome开发人员工具中看起来有所不同

时间:2015-12-30 22:22:49

标签: javascript google-chrome-devtools paperjs

这是游戏的代码,其中圆圈在页面上跳跃,您需要在一定时间内点击它(naomikudren.com/Catch)。

var circleSize = 60;
var circle = new Path.Circle(new Point(50, 50), circleSize);
circle.fillColor = "red";
var counter = 0;
var counter2 = 0;
var speed = 60;
function onFrame(event) {
    counter2++
    if (counter2 % speed === 0) {
        var newPoint = Point.random() * view.size;
        circle.position = new Point(newPoint, newPoint);
    }
    console.log(counter2)
}
circle.onClick = function(event) {
    var newPoint = Point.random() * view.size;
    circle.position = new Point(newPoint, newPoint);
    this.fillColor = {
        hue: Math.random() * 360,
        saturation: 1,
        brightness: 1,
    };
    counter2 = 0;
    counter++;
    circle.scale(0.99);

    text.content = 'Score ' + counter;
}


var text = new PointText(new Point(50, 50));
text.justification = 'center';
text.fillColor = 'black';

这一切都很好,但是当我将Paperscript文件视为Chrome开发者工具中的来源时,它会显示如下:

paper._execute = function(__$__,view,Point,Path,PointText,onFrame) {var circleSize = 60;
var circle = new Path.Circle(new Point(50, 50), circleSize);
circle.fillColor = "red";
var counter = 0;
var counter2 = 0;
var speed = 60;
function onFrame(event) {
    counter2 = __$__(counter2, "+", 1)
    if (__$__(counter2, "%" , speed) === 0) {
        var newPoint = __$__(Point.random(), "*" , view.size);
        circle.position = new Point(newPoint, newPoint);
    }
    console.log(counter2)
}
circle.onClick = function(event) {
    var newPoint = __$__(Point.random(), "*" , view.size);
    circle.position = new Point(newPoint, newPoint);
    this.fillColor = {
        hue: __$__(Math.random(), "*" , 360),
        saturation: 1,
        brightness: 1,
    };
    counter2 = 0;
    counter = __$__(counter, "+", 1);
    circle.scale(0.99);

    text.content = 'Score ' + counter;
}


var text = new PointText(new Point(50, 50));
text.justification = 'center';
text.fillColor = 'black';

//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaHR0cDovL25hb21pa3VkcmVuLmNvbS9DYXRjaC9jYXRjaC5qcyIsIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiaHR0cDovL25hb21pa3VkcmVuLmNvbS9DYXRjaC9jYXRjaC5qcyJdfQ==
return { onFrame: onFrame };
}

我不明白这里发生了什么。 Chrome不应该显示源文件吗?为什么它与源文件不同,这是否意味着从Paperscript到普通JavaScript发生了某种编译?

1 个答案:

答案 0 :(得分:3)

这是因为您使用的是papercript,而不是JavaScript。 paperjs在声明为paperscript时对您的代码进行预处理,而Chrome向您显示的是预处理的结果。如果您考虑一下这是有道理的 - Chrome需要向您展示正在执行的内容以便有效地进行调试。它无法意识到不同类型的脚本预处理可能会产生的所有转换。

这是我总是在JavaScript模式下使用paperjs的原因之一,即使1)运算符不起作用,必须使用函数,2)Path.Circle必须称为paper.Path.Circle }(类似于所有其他纸质对象和变量)。

在某些情况下,使用papercript会导致奇怪的行为。例如,如果在代码中使用view.onFrame = function() {...}但是也声明function onFrame() {...},那么将在代码完成之前调用视图函数,并在结尾返回对象{onFrame: onFrame}。此时onFrame函数将替换view处理程序。

我没有对此进行过测试,但这里是将代码快速转换为原生JavaScript。主要区别是1)我明确地调用paper.setup和2)所有对纸质项目的引用都以paper.为前缀。我还使用view.on处理程序进行帧处理,但还有其他方法可以使其工作。

paper.setup("myCanvas");

var circleSize = 60;
var circle = new paper.Path.Circle(new Point(50, 50), circleSize);
circle.fillColor = "red";
var counter = 0;
var counter2 = 0;
var speed = 60;

paper.view.on('frame', function(event) {
    counter2++
    if (counter2 % speed === 0) {
        var newPoint = paper.Point.random().multiply(paper.view.size);
        circle.position = new paper.Point(newPoint, newPoint);
    }
    console.log(counter2)    
});

circle.onClick = function(event) {
    var newPoint = paper.Point.random().multiply(paper.view.size);
    circle.position = new paper.Point(newPoint, newPoint);
    this.fillColor = {
        hue: Math.random() * 360,
        saturation: 1,
        brightness: 1,
    };
    counter2 = 0;
    counter++;
    circle.scale(0.99);

    text.content = 'Score ' + counter;
}


var text = new paper.PointText(new paper.Point(50, 50));
text.justification = 'center';
text.fillColor = 'black';