简体   繁体   English

Paper.js互操作性

[英]Paper.js Interoperability

I would like to call paper.js functions from HTML buttons in my page but I believe the paper.js functions exist in their own scope. 我想从我的页面中的HTML按钮调用paper.js函数,但我相信paper.js函数存在于它们自己的范围内。 The paper.js docs mention interoperability which sounds like the right direct by then take me to a page that says "coming soon": paper.js文档提到互操作性,这听起来像是正确的直接然后带我到一个“即将推出”的页面:

http://paperjs.org/tutorials/getting-started/paperscript-interoperability/ http://paperjs.org/tutorials/getting-started/paperscript-interoperability/

Does anyone know how I can call a function created within a paper.js script from my HTML page? 有谁知道如何从我的HTML页面调用在paper.js脚本中创建的函数?

Apologies for that missing tutorial. 为那个缺少的教程道歉。 I shall really invest some time into writing it finally. 我真的会投入一些时间来写它。

I've answered this question on the mailing list last year: https://groups.google.com/d/msg/paperjs/C6F0XFlplqM/_67AMqCR_nAJ 我去年在邮件列表上回答了这个问题: https//groups.google.com/d/msg/paperjs/C6F0XFlplqM/_67AMqCR_nAJ

Scoped PaperScript run inside the global scope, and have access to all elements of the global scope. Scoped PaperScript在全局范围内运行,并且可以访问全局范围的所有元素。 The normal JavaScripts running in the global scope (= window) will not see these PaperScopes, and won't have access to their variables. 在全局范围(=窗口)中运行的普通JavaScripts将不会看到这些PaperScope,也无法访问其变量。

There is a simple solution to exchange information between the two: Simply declare a global structure that you use to exchange tings back and forth, eg 在两者之间交换信息有一个简单的解决方案:简单地声明一个用于来回交换的全局结构,例如

window.globals = {
    someValue: 10,
    someFunction: function() { alert(globals.someValue); }
};

In your PaperScript, you can then access this simply through 'globals', since it's in the window scope: 在PaperScript中,您可以通过“全局”来访问它,因为它位于窗口范围内:

globals.someValue = 20;
globals.someFunction();

And in the same way, you can use this structure from normal JavaScript. 同样,您可以使用普通JavaScript中的此结构。

I just wanted to add some clarification for anyone who runs into this. 我只想为遇到这种情况的人添加一些说明。

For PaperScript-to-JavaScript interoperability, do the following... 对于PaperScript到JavaScript的互操作性,请执行以下操作...

In HTML file: 在HTML文件中:

<button type="button" id="btn">Click Me!</button>

In JavaScript file: 在JavaScript文件中:

$(document).ready(init);

function init(jQuery) {
  $("#btn").click(window.globals.paperClicked);
}

// PaperScript Interop
window.globals = {
  paperClicked: function() {}
}

In PaperScript file: 在PaperScript文件中:

// JavaScript Interop
globals.paperClicked = internalClicked;

function internalClicked() {
  alert('clicked!');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM