3.3. 和代碼的區別 Coming From Code - Origami Studio教程


See how Origami differs from programming. 瞅瞅和編程的區別。

Using functions and adding logic to your prototypes is straightforward in Origami, but a little different to what you might expect if you come from a programming background. If you haven't touched code before, feel free to skip forward to Adding Logic.

在 Origami 原型中使用函數和添加邏輯很簡單,但如果以編程的角度出發可能會有跟您預期的有一些不一樣。如果以前沒有接觸過代碼,可以跳這篇直接學習如何添加邏輯

Patches and functions 模塊和函數

The basic building blocks in Origami are called patches. They are similar to functions as far as they take data input(s), perform an action on it, and produce a result.

Origami 中的基本構建塊稱為模塊。
它們和函數能類似,獲取數據輸入,對其執行操作,并產生結果。

function name(input1, input2, input3) { 
  code to be outputted}

The format of a function in code. 代碼中函數的格式。

Origami patches work in the same way; taking a single or multiple input(s), performing an action, and producing an output — albeit using a slightly different format.

Origami 模塊的工作方式和這個一樣,接收單個或多個輸入,執行動作,并產生輸出 - 盡管格式稍微不同。

A function in Patch format in Origami Studio. 模塊的格式。

What makes Origami unique is the suite of pre-made patches that allow you to listen for various types of interaction, create natural animations, manipulate layer properties, and more. Just like functions in code, you can also create your own patches.

Origam 使用的是一套預制的模塊,讓您可以監聽各種類型的交互,創建自然的動畫,控制圖層屬性等等。跟函數在程序中一樣,也可以封裝自定義模塊。

Inputs and outputs 輸入和輸出

We are using an example of converting Fahrenheit to Celsius. Fahrenheit being our Input value, and Celsius being our returned value, or Output.

我們以將華氏度轉換為攝氏度為例,華氏度是輸入值,攝氏度時返回值或輸出值。

In programming languages such as JavaScript, this calculation would look something like this:

在編程語言,如JavaScript中,看著是這樣的:

function fahrenheitToCelsius(fahrenheit) { 
       return (5/9) * (fahrenheit-32);
}
var text = fahrenheitToCelsius(86);

A JavaScript function converting Fahrenheit to Celsius. 將華氏轉換為攝氏度的 JavaScript 函數。

Calculations done in patches are hidden from view by default. The patch simply presents the published Input(s) and Output(s).

默認情況下,模塊的計算公式將被隱藏。只簡單地顯示輸入和輸出值。
板栗:模塊庫里沒有這個模塊,不知道怎么弄出來的。

An Origami patch converting Fahrenheit to Celsius. Origami 模塊將華氏度轉為攝氏度。

Entering this custom-made patch through Patch > Enter Patch Group ?↓ reveals the same calculations as in code.

點擊 Patch > Enter Patch Group Cmd 進入這個自定義模塊內部,顯示與代碼中相同的計算。

Inner workings of the Fahrenheit to Celsius patch. 模塊的內部運算。

One important thing to take note of is the order of calculations. Code usually follows the traditional order of mathematical operations; prioritizing some operators (/
,*) over others (+,-). Origami simply calculates from the order in which the patches are connected.

要注意的一個重要事情是計算的順序。代碼通常遵循傳統的數學運算順序;優先計算乘、除(/*)其次計算加、減(+-` )。Origami 根據模塊的連接順序進行計算。

Multiple outputs 多重輸出

Returning multiple outputs is easy in Origami. This means patches can be used beyond what functions are typically used for. As an example, here is our temperature calculating patch going beyond Fahrenheit to Celsius:

多重輸出在 Origami 中也很簡單。這意味著模塊可以超出通常使用的功能。例如,這里是我們的溫度計算模塊超過華氏攝氏度:

Temperature conversion patch outputting multiple outputs. 溫度轉換模塊輸出多個值。

Values calculated inside the patch simply need to be published as outputs:

補丁內計算的值只需要作為輸出發布:

Inner workings of the Fahrenheit to Multiple patch. 內部工作的多個模塊。

Logic and conditionals 邏輯和條件

Logic in code, called conditionals, is a little different from the way Origami handles logic. Code usually runs linearly from top to bottom, and requires calculations to be done before moving to the next line.

邏輯在代碼中稱為條件,和 Origami 處理邏輯的方式有些不同。代碼通常從上到下線性運行的,需要在移動到下一行之前進行計算。

In Origami's visually-focused Patch Editor, information flows from left to right (including logical operations), depending on what is being interacted with and/or triggered. For that reason, you won't find patches analogous to if, else, else if, or while found in code. Instead, information passed through patches (usually from an interaction) will only continue to flow unless the comparison is false.

在 Origami 的可視化模塊編輯器中,信息從左到右(包括邏輯操作)依賴于與什么進行交互和/或觸發。因此,您不會找到類似于代碼中的if, else, else if, 或 while 之類的模塊。相反,信息通過模塊(一般來自交互)后會繼續流動,除非值為false。

Doing math 數學

Math in Origami is largely the same as arithmetic operators in code. Origami has patches for most standard operators.

數學在 Origami 中大部分和代碼里的算數運算符一樣。Origami 有大多數標準運算符模塊。

3+2
3-2
3*2
3/2

Simple arithmetic in code. 代碼中的簡單算數。

Simple arithmetic in Origami. Origami 中的簡單算數。

Comparing items 比較

Origami is able to take values and output a true or false boolean value, similar to comparison operators in code.

Origami 能夠取值并輸出一個真或假的布爾值,和代碼中的比較運算符一樣。

3>2
3<2
3==2

Simple comparisons in code. 代碼里簡單的比較

Simple comparisons in Origami. Origami 中簡單的比較。

Comparing with logic 與邏輯比較

This is where things start to diverge. Origami has built-in patches which allow for common logic to be done quickly and easily. For example:

這是開始有差異的地方。Origami 有內置的模塊,可以快速、輕松地完成通用邏輯。例如:

3>=2

Comparison in code. 代碼中的比較。

Comparing with logic in Origami. Origami 中與邏輯比較

In code, a logical operator is usually paired with a comparison operator (as shown above). Origami makes it easy to make comparisons with dedicated patches for common logic:

在代碼中,邏輯運算符通常與比較運算符配對(如上所示)。折紙可以使用通用邏輯模塊進行比較:

&&
!
||

Comparison operators in code. 代碼中的比較運算符。

Comparison operators in Origami. Origami 中的比較運算符。

These patches are useful for checking conditionals—similar to if or else. Take an example of checking to see if a calculation is true:

這些模塊可用于檢查條件 - 類似于ifelse。舉個例子檢查計算是否成立:

if (3>2 && (2==3||2<3) && 2!=>3) {
   code to be executed if true
}

Origami takes the information flowing left to right (analogous to 3>2 && (2==3||2<3) && 2!=>3 above) and outputs either true or false, represented by the output on the And patch:

Origami 的信息從左向右流動 (類似于上面的 to 3>2 && (2==3||2<3) && 2!=>3),并輸出 true 或 false,由 And 模塊輸出口表示:

Chaining multiple calculations can become complex and difficult to manage in code. This task becomes intuitive and flexible when built in Origami.

鏈接多個計算在代碼中可能變得復雜,難以管理。這個任務在內置 Origami 時變得直觀靈活。


Related Learn Content 相關教程

4. Adding Logic 添加邏輯
給過渡和流程添加邏輯。

Related Examples 案例

16. Fahrenheit to Celsius
通過溫度轉換了解 Origami 中的邏輯和模塊。

Related Patches 相關模塊

Greater Than 大于Less Than 小于Equals Exactly 完全等于Greater Than or Equal 大于或等于And 和Not 翻轉Or 至少一個+ 加? 減× 乘/(÷) 除


NEXT UP
4. Adding Logic 添加邏輯
給過渡和流程添加邏輯。


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 書籍:《狼圖騰》 背景: 初中時候在一家盜版書店看到過《狼圖騰》的封面,當時就只有一個模糊的影像,后來高中在書店里...
    姓錢的一家人閱讀 381評論 0 0
  • 任何年紀,你都必須做些讓你投入精力的事情。
    kanbuting閱讀 163評論 1 0
  • 本文導讀:有過痛經的女性都知道那種痛苦,有時候痛到臉色發白冒冷汗,有的甚至會痛暈。痛經怎么辦才好?痛經的女性要注意...
    養生綠然本閱讀 217評論 0 0