官方教程地址鏈接
http://paperjs.org/tutorials/getting-started/using-javascript-directly/
Scope
每個Scope都有自己的view和project
The global paper object through which the library is exposed is also
such a PaperScope object.
通過庫暴露出來的全局paper object也是一個PaperScope object
所以在使用方面
PaperScope object 等價 paper object
Setting Up a Scope
直接使用JavaScript時,在大多數情況下,要保證有一個Scope(範圍)。
在這個範圍內,仍然可以通過使用 new Project()和new View(canvas)構造函數創建它們來處理多個項目或視圖。
// Get a reference to the canvas object
var canvas = document.getElementById('myCanvas');
// Create an empty project and a view for the canvas:
paper.setup(canvas);
簡單來說,在使用paper.js以前,你需要用到上面兩條語句。
Making the Scope Global
It might not seem so practical to access all classes and objects
through the paper object, so here are two strategies for circumventing
that.
程序員總是很懶,多打一個paper都不願意,所以這裏,有兩個簡單方法。
paper.install(window);
個人傾向於下面這種。如果有需要查看具體的例子,可以通過最上方的鏈接傳送。
with (paper) {
}
Installing Event Handlers
view.onFrame = function(event) {
// On each frame, rotate the path by 3 degrees:
path.rotate(3);
}
we have to do is install these handlers on the existing view object:
view is automatically created for us if we use the
paperScope.setup(canvas) function
在view 上設置Event Handlers
並且,view 已經通過 paper.setup()函數創建過。
Working with Tools
Tool對象是指用戶可以通過鼠標和鍵盤進行交互的腳本。
onMouseDown, onMouseUp, onMouseDrag, onMouseMove, 之類的都通過這個對象控制。
var tool = new Tool();
tool.onMouseDown = function(event) {
}
tool.onMouseDrag = function(event) {
}
Multiple Tools
使用 多個Tool可以在不同的情況下,Tool可以做出不同的反應。
代碼部分較長。具體代碼請看 官網。